1、子类化教程和范例1. 何谓子类化(subclassing)众所周知,Windows是一个基于消息的系统,消息在Windows的对象之间进行着传递。子类化和Windows的钩子机制存在于消息系统之中,我们可以利用这些机制来操纵、修改甚至丢弃那些在操作系统或是进程中传递的消息,以求改变系统的一些行为。子类化技术用来截取窗口或控件之间的消息,当然是消息在到达目的窗口之前完成的操作。这些被截获的消息既可以保留也可以修改它们的状态,之后就继续发送到目的地。子类化技术实现了一些正常情况下无法实现的功能,试想鼠标右键单击TextBox,系统默认弹出Undo、Cut、Copy、Paste等菜单,我们就可以利用
2、子类化技术来改变这个系统菜单。简单的说,子类化就是创建一个新的窗口消息处理过程,并将其插入到原先的默认窗口消息处理过程之前。子类化分为三类:实例子类化(instance subclassing)从窗口或控件的单一实例截获消息,这种子类化技术最普遍;全局子类化(global subclassing)能够截获从相同的窗口类创建出来的多个窗口或控件的消息;超类化(superclassing)和全局子类化很类似,区别在于可以应用在新的窗口类上面。2. Visual Basic 6子类化的实现在Visual Basic 6子类化的实现中我们将通过一段代码的实例来介绍这一技术在VB6中的应用。现在很多开发
3、社区中经常谈到的一个话题就是界面开发如何Skin,这种技术有很多解决方式,如使用可以贴图的控件或使用第三方开发的换肤ActiveX控件。其实Skin技术需要处理的是WM_DRAWITEM、WM_MEASUREITEM、WM_NCPAINT消息,这些消息的主要用途就是可以重画控件和窗口的非客户区。想要对这些VB6无法处理的消息进行编程就必须用到子类化,这些消息都会被发送到能够自绘的控件的窗口上,因此下面的例子就是利用窗口子类化来重画Button控件。创建工程启动Visual Basic 6同时创建一个标准EXE工程。窗口布局在FORM1上放置3个Button控件,并将前两个Button的Styl
4、e属性设置为1-Graphical,因为只有Style属性设置为Graphical的Button才可以Owner-drawn。在窗体中录入代码Private Sub Command3_Click() 通过Enabled属性的控制,来显示重画控件在Unenabled状态时的效果 If Command1.Enabled Then Command1.Enabled = False Else Command1.Enabled = True End IfEnd SubPrivate Sub Form_Load() 安装子类化入口 Call Init(Me.hWnd)End SubPrivate Sub
5、Form_Unload(Cancel As Integer) 卸载子类化 Call Terminate(Me.hWnd)End Sub加入一个模块并录入代码Option Explicit - 引用Win32Api 得到默认的窗口消息处理过程的地址需要的APIPrivate Declare Function GetWindowLong Lib user32 Alias GetWindowLongA (ByVal hWnd As Long, ByVal nIndex As Long) As Long设置一个新的窗口消息处理过程的地址需要的APIPrivate Declare Function Se
6、tWindowLong Lib user32 Alias SetWindowLongA (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long给指定的窗口消息处理过程传递消息需要的APIPrivate Declare Function CallWindowProc Lib user32 Alias CallWindowProcA (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As L
7、ong, ByVal lParam As Long) As Long内存拷贝Private Declare Sub CopyMemory Lib kernel32 Alias RtlMoveMemory (Destination As Any, Source As Any, ByVal Length As Long)Const GWL_WNDPROC = (-4&)Dim PrevWndProc&Private Const WM_DESTROY = &H2Private Const WM_DRAWITEM = &H2BPrivate Type RECT Left As Long Top As
8、Long Right As Long Bottom As LongEnd TypeWM_DRAWITEM需要处理的结构体Private Type DRAWITEMSTRUCT CtlType As Long CtlID As Long itemID As Long itemAction As Long itemState As Long hwndItem As Long hdc As Long rcItem As RECT itemData As LongEnd Type Owner draw 常量Private Const ODT_BUTTON = 4 Owner draw 动作Privat
9、e Const ODA_DRAWENTIRE = &H1Private Const ODA_SELECT = &H2Private Const ODA_FOCUS = &H4 Owner draw 状态Private Const ODS_SELECTED = &H1Private Const ODS_GRAYED = &H2Private Const ODS_DISABLED = &H4Private Const ODS_CHECKED = &H8Private Const ODS_FOCUS = &H10得到指定窗口的文本Private Declare Function GetWindowT
10、ext Lib user32 Alias GetWindowTextA (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As LongGDI相关API函数,重画Button时使用Private Declare Function SelectObject Lib gdi32 (ByVal hdc As Long, ByVal hObject As Long) As LongPrivate Declare Function CreateSolidBrush Lib gdi32 (ByVal crColor As L
11、ong) As LongPrivate Declare Function DeleteObject Lib gdi32 (ByVal hObject As Long) As LongPrivate Declare Function GetSysColor Lib user32 (ByVal nIndex As Long) As Long色彩常量Const COLOR_SCROLLBAR = 0Const COLOR_BACKGROUND = 1Const COLOR_ACTIVECAPTION = 2Const COLOR_INACTIVECAPTION = 3Const COLOR_MENU
12、 = 4Const COLOR_WINDOW = 5Const COLOR_WINDOWFRAME = 6Const COLOR_MENUTEXT = 7Const COLOR_WINDOWTEXT = 8Const COLOR_CAPTIONTEXT = 9Const COLOR_ACTIVEBORDER = 10Const COLOR_INACTIVEBORDER = 11Const COLOR_APPWORKSPACE = 12Const COLOR_HIGHLIGHT = 13Const COLOR_HIGHLIGHTTEXT = 14Const COLOR_BTNFACE = 15C
13、onst COLOR_BTNSHADOW = 16Const COLOR_GRAYTEXT = 17Const COLOR_BTNTEXT = 18Const COLOR_INACTIVECAPTIONTEXT = 19Const COLOR_BTNHIGHLIGHT = 20Private Declare Function FillRect Lib user32 (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As LongPrivate Declare Function FrameRect Lib user32 (ByVa
14、l hdc As Long, lpRect As RECT, ByVal hBrush As Long) As LongPrivate Declare Function CreatePen Lib gdi32 (ByVal nPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long) As Long画笔格式Const PS_SOLID = 0Const PS_DASH = 1 -Const PS_DOT = 2 .Const PS_DASHDOT = 3 _._._._Const PS_DASHDOTDOT = 4 _._._C
15、onst PS_NULL = 5Const PS_INSIDEFRAME = 6Const PS_USERSTYLE = 7Const PS_ALTERNATE = 8Const PS_STYLE_MASK = &HFPrivate Declare Function MoveToEx Lib gdi32 (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, lpPoint As POINTAPI) As LongPrivate Declare Function LineTo Lib gdi32 (ByVal hdc As Long, ByV
16、al x As Long, ByVal y As Long) As LongPrivate Type POINTAPI x As Long y As LongEnd TypePrivate Declare Function DrawText Lib user32 Alias DrawTextA (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As LongPrivate Const DT_SINGLELINE = &H20Private
17、 Const DT_CENTER = &H1Private Const DT_VCENTER = &H4Private Declare Function SetTextColor Lib gdi32 (ByVal hdc As Long, ByVal crColor As Long) As LongPrivate Declare Function SetBkMode Lib gdi32 (ByVal hdc As Long, ByVal nBkMode As Long) As LongPrivate Const TRANSPARENT = 1 声明结束 -Private Sub DrawBut
18、ton(ByVal hWnd As Long, ByVal hdc As Long, rct As RECT, ByVal nState As Long) Dim P As POINTAPI Dim s As String Dim hbr As Long Dim hpen As Long hbr = CreateSolidBrush(GetSysColor(COLOR_BTNFACE) 获得按钮的背景颜色RGB(231, 231, 231) SelectObject hdc, hbr 选择使用刷子 FillRect hdc, rct, hbr DeleteObject hbr 画文字时背景为透
19、明状 SetBkMode hdc, TRANSPARENT 得到Button的Caption s = String$(255, ) GetWindowText hWnd, s, 255 s = Trim$(s) 根据Button的Enabled状态进行重画 If (nState And ODS_DISABLED) = ODS_DISABLED Then 画外围灰框 hbr = CreateSolidBrush(RGB(132, 130, 132) SelectObject hdc, hbr FrameRect hdc, rct, hbr DeleteObject hbr 画内侧3D效果-亮色
20、hpen = CreatePen(PS_SOLID, 1, RGB(255, 255, 255) SelectObject hdc, hpen MoveToEx hdc, rct.Left + 1, rct.Top + 1, P LineTo hdc, rct.Right - 1, rct.Top + 1 MoveToEx hdc, rct.Left + 1, rct.Top + 1, P LineTo hdc, rct.Left + 1, rct.Bottom - 1 DeleteObject hpen 画内侧3D效果-暗色 hpen = CreatePen(PS_SOLID, 1, RGB
21、(189, 190, 189) SelectObject hdc, hpen MoveToEx hdc, rct.Left + 1, rct.Bottom - 2, P LineTo hdc, rct.Right - 1, rct.Bottom - 2 MoveToEx hdc, rct.Right - 2, rct.Top + 1, P LineTo hdc, rct.Right - 2, rct.Bottom - 1 DeleteObject hpen 画阴影文字 rct.Left = rct.Left + 1 rct.Right = rct.Right + 1 rct.Bottom =
22、rct.Bottom + 1 rct.Top = rct.Top + 1 SetTextColor hdc, GetSysColor(COLOR_BTNHIGHLIGHT) DrawText hdc, s, LenB(StrConv(s, vbFromUnicode), rct, DT_CENTER Or DT_SINGLELINE Or DT_VCENTER rct.Left = rct.Left - 1 rct.Right = rct.Right - 1 rct.Bottom = rct.Bottom - 1 rct.Top = rct.Top - 1 SetTextColor hdc,
23、GetSysColor(COLOR_GRAYTEXT) DrawText hdc, s, LenB(StrConv(s, vbFromUnicode), rct, DT_CENTER Or DT_SINGLELINE Or DT_VCENTER Exit Sub End If 按下Button时重画 If (nState And ODS_SELECTED) = ODS_SELECTED Then 画内部区域颜色 hbr = CreateSolidBrush(RGB(156, 186, 222) SelectObject hdc, hbr FillRect hdc, rct, hbr Delet
24、eObject hbr 画外围灰框 hbr = CreateSolidBrush(RGB(99, 125, 165) SelectObject hdc, hbr FrameRect hdc, rct, hbr DeleteObject hbr 画内侧3D效果-亮色 hpen = CreatePen(PS_SOLID, 1, RGB(123, 158, 206) SelectObject hdc, hpen MoveToEx hdc, rct.Left + 1, rct.Top + 1, P LineTo hdc, rct.Right - 1, rct.Top + 1 MoveToEx hdc,
25、 rct.Left + 1, rct.Top + 1, P LineTo hdc, rct.Left + 1, rct.Bottom - 1 DeleteObject hpen 画内侧3D效果-暗色 hpen = CreatePen(PS_SOLID, 1, RGB(181, 203, 231) SelectObject hdc, hpen MoveToEx hdc, rct.Left + 1, rct.Bottom - 2, P LineTo hdc, rct.Right - 1, rct.Bottom - 2 MoveToEx hdc, rct.Right - 2, rct.Top + 1
26、, P LineTo hdc, rct.Right - 2, rct.Bottom - 1 DeleteObject hpen rct.Left = rct.Left + 1 rct.Right = rct.Right + 1 rct.Bottom = rct.Bottom + 1 rct.Top = rct.Top + 1 SetTextColor hdc, GetSysColor(COLOR_BTNTEXT) DrawText hdc, s, LenB(StrConv(s, vbFromUnicode), rct, DT_CENTER Or DT_SINGLELINE Or DT_VCEN
27、TER Exit Sub End If Button得到焦点时重画 If (nState And ODS_FOCUS) = ODS_FOCUS Then 画内部区域颜色 hbr = CreateSolidBrush(RGB(173, 203, 239) SelectObject hdc, hbr FillRect hdc, rct, hbr DeleteObject hbr 画外围灰框 hbr = CreateSolidBrush(RGB(107, 138, 181) SelectObject hdc, hbr FrameRect hdc, rct, hbr DeleteObject hbr
28、画内侧3D效果-亮色 hpen = CreatePen(PS_SOLID, 1, RGB(198, 223, 247) SelectObject hdc, hpen MoveToEx hdc, rct.Left + 1, rct.Top + 1, P LineTo hdc, rct.Right - 1, rct.Top + 1 MoveToEx hdc, rct.Left + 1, rct.Top + 1, P LineTo hdc, rct.Left + 1, rct.Bottom - 1 DeleteObject hpen 画内侧3D效果-暗色 hpen = CreatePen(PS_SO
29、LID, 1, RGB(132, 174, 222) SelectObject hdc, hpen MoveToEx hdc, rct.Left + 1, rct.Bottom - 2, P LineTo hdc, rct.Right - 1, rct.Bottom - 2 MoveToEx hdc, rct.Right - 2, rct.Top + 1, P LineTo hdc, rct.Right - 2, rct.Bottom - 1 DeleteObject hpen SetTextColor hdc, GetSysColor(COLOR_BTNTEXT) DrawText hdc,
30、 s, LenB(StrConv(s, vbFromUnicode), rct, DT_CENTER Or DT_SINGLELINE Or DT_VCENTER Else 画外围灰框 hbr = CreateSolidBrush(RGB(132, 130, 132) SelectObject hdc, hbr FrameRect hdc, rct, hbr DeleteObject hbr 画内侧3D效果-亮色 hpen = CreatePen(PS_SOLID, 1, RGB(255, 255, 255) SelectObject hdc, hpen MoveToEx hdc, rct.L
31、eft + 1, rct.Top + 1, P LineTo hdc, rct.Right - 1, rct.Top + 1 MoveToEx hdc, rct.Left + 1, rct.Top + 1, P LineTo hdc, rct.Left + 1, rct.Bottom - 1 DeleteObject hpen 画内侧3D效果-暗色 hpen = CreatePen(PS_SOLID, 1, RGB(189, 190, 189) SelectObject hdc, hpen MoveToEx hdc, rct.Left + 1, rct.Bottom - 2, P LineTo hdc, rct.Right -
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1