设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

12345下一页
返回列表 发新帖
查看: 15929|回复: 48
打印 上一主题 下一主题

用位图文件装饰窗体背景及文本框背景

[复制链接]
跳转到指定楼层
1#
发表于 2008-1-8 10:16:47 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
声明:本贴非原创,转自http://www.vbaccelerator.com/home/vb/code/libraries/subclassing/tile_a_bitmap_into_a_textbox/article.asp著作权及版权均归原创作者所有说明:这是一篇E文资料,本人E文不甚匮乏,没有翻译成中文还望各位看客谅解。不过写得比较简洁,一般水平还是看得懂的
原文如下:
Tile a Bitmap Into a TextBox Background
An interesting hack which gives you a picture in the background of any multi-line text box.

This sample presents a small class that allows you to tile a bitmap into the background of a TextBox. Note that the technique only works on multi-line text boxes, as the drawing of single-line TextBoxes is done in a different way and cannot be easily overridden in code.
Drawing A TextBox BackgroundThe TextBox control in VB is basically a Windows TextBox control with a thin VB wrapper around it. Although the Windows TextBox control hasn't been designed to allow a user to modify it's background, you can still do it by overriding some of the draw messages sent to the control.
The basic principle of overriding the drawing is to subclass the TextBox for the WM_PAINT message which is sent whenever a portion of the control needs to be repainted. Unfortunately, the TextBox also draws portions of itself outside WM_PAINT messages so some other messages also need to be worked on as well, which I will describe later.
When a WM_PAINT message is received, you can determine the portion of the control which needs to be updated using the GetUpdateRect API call. You can then take control of the drawing by drawing into the update area after you've called the standard processing for this message. To actually draw a rendition of a multi-line TextBox control would be difficult if it wasn't for the fact that all Windows controls support a method of drawing themselves into a DC to enable you to print a copy of the control surface. This message is the WM_PRINT message, and takes the DC to draw into as the wParam parameter of the SendMessage call and flags controlling which parts of the control to draw in the lParam parameter. Here is the code to cause the control to print itself into a DC:

' declares: Private Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" ( _   ByVal hwnd As Long, ByVal wMsg As Long, _   ByVal wParam As Long, ByVal lParam As Long) As LongPrivate Const PRF_CHECKVISIBLE = &H1&Private Const PRF_NONCLIENT = &H2&Private Const PRF_CLIENT = &H4&Private Const PRF_ERASEBKGND = &H8&Private Const PRF_CHILDREN = &H10&Private Const PRF_OWNED = &H20&  
SendMessageLong m_hWnd, WM_PRINT, m_cWorkDC.hDC, PRF_CLIENT Or PRF_CHECKVISIBLE
Once you know this, it is fairly simple to create a version of the control with a different background: draw the background, then ask the control to draw itself (with a transparent background) onto it and then draw it in place.
Unfortunately as mentioned before, the control draws itself outside of the WM_PAINT message as well. To work around this, you also have to respond to WM_CTLCOLOREDIT messages by setting the background to transparent and then repainting and to WM_ERASEBKGND messages (which you eat). Finally, during scrolling the control also needs to be updated. This requires a bit of hacking as you will see if you look at the code, where I create a scroll state machine that ensures painting at the right time. Once this is done, though, you have a control which works exactly as before except with a bitmap in the background!
The cTextBoxBackground classTo make this easier to use, I've wrapped the code up in a simple class. This class has the following methods:

  • Attach(ByVal hWndA As Long)
    Attaches the class to the multi-line text box with the specified hWnd. Don't call this method until you've set up the background picture first.
  • Detach()
    Detaches from the class so the text box painting goes back to normal. Called automatically when the TextBox is destroyed.
  • SetBackdrop(pic As IPicture)
    Sets the background bitmap from a standard VB picture object (for example, a StdPicture returned from LoadPicture or the picture returned by a PictureBox's Picture property.
  • TileArea(ByVal hdcTo As Long, ByVal x As Long, ByVal y As Long, ByVal Width As Long, ByVal Height As Long)
    The bitmap tiling function is exposed to allow you to tile the bitmap onto any other objects with a hDC property. This is used in the second sample form in the demo to tile the background of the form.
  • TileOffsetX() As Long
    Gets or Sets the initial X offset in the bitmap to start tiling from.
  • TileOffsetY() As Long
    Gets or Sets the initial Y offset in the bitmap to start tiling from.
To use it, first add a TextBox to a form, set it to multi-line mode and then create a form level instance of the cTextBoxBackground class (here I'm assuming it's called m_cLargeTextBoxBack). Then set the background picture and attach it to the text box as follows:
   Set m_cLargeTextBoxBack = New cTextBoxBackground   m_cLargeTextBoxBack.SetBackdrop LoadPicture(App.Path & "\back.bmp")   m_cLargeTextBoxBack.Attach txtTest.hwnd

Custom Bitmap Dialogs

Just for fun, I tried using the class to create a dialog with a completely customised appearance, so all of the UI elements are drawn using a bitmap. The result is shown below:

抱歉,回复之后更精彩!
游客,如果您要查看本帖隐藏内容请回复


[ 本帖最后由 fannky 于 2008-1-8 10:23 编辑 ]

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
发表于 2008-1-8 11:37:15 | 只看该作者
看看。
3#
发表于 2008-1-8 11:44:23 | 只看该作者
ACCESS能实现这样的效果吗
vbaccelerator是学习控件的好地方 控件都带有源码 值得一看
4#
发表于 2008-1-8 11:46:07 | 只看该作者
大哥,能做个例子上来吗/?
5#
发表于 2008-1-8 12:39:05 | 只看该作者
謝謝分享!坐下來慢慢看!
6#
发表于 2008-1-8 13:22:06 | 只看该作者
谢谢分享....努力学习中.
7#
发表于 2008-1-9 14:59:40 | 只看该作者
8#
发表于 2008-1-9 21:52:01 | 只看该作者
ACCESS能实现这样的效果吗

点击这里给我发消息

9#
发表于 2008-1-9 23:16:41 | 只看该作者
http://www.vbaccelerator.com/  是个非常好的网站, 一直跟随它一起成长
10#
发表于 2008-1-10 08:01:46 | 只看该作者
看看................
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-10 18:26 , Processed in 0.092631 second(s), 35 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表