分享

什么是VB进行子类化及简单介绍

 hdzgx 2017-10-22

目录

  • 何谓子类化(subclassing)
  • Visual Basic 6子类化的实现
  • Visual Basic .NET子类化的实现
  • 小结

1. 何谓子类化(subclassing) 

  众所周知,Windows是一个基于消息的系统,消息在Windows的对象之间进行着传递。子类化和Windows的钩子机制存在于消息系统之中,我们可以利用这些机制来操纵、修改甚至丢弃那些在操作系统或是进程中传递的消息,以求改变系统的一些行为。子类化技术用来截取窗口或控件之间的消息,当然是消息在到达目的窗口之前完成的操作。这些被截获的消息既可以保留也可以修改它们的状态,之后就继续发送到目的地。子类化技术实现了一些正常情况下无法实现的功能,试想鼠标右键单击TextBox,系统默认弹出Undo、Cut、Copy、Paste等菜单,我们就可以利用子类化技术来改变这个系统菜单。 

  简单的说,子类化就是创建一个新的窗口消息处理过程,并将其插入到原先的默认窗口消息处理过程之前。 

  子类化分为三类:实例子类化(instance subclassing)—从窗口或控件的单一实例截获消息,这种子类化技术最普遍;全局子类化(global subclassing)—能够截获从相同的窗口类创建出来的多个窗口或控件的消息;超类化(superclassing)—和全局子类化很类似,区别在于可以应用在新的窗口类上面。 

2. Visual Basic 6子类化的实现 

  在Visual Basic 6子类化的实现中我将通过一段代码的实例来介绍这一技术在VB6中的应用。现在很多开发社区中经常谈到的一个话题就是界面开发如何Skin,这种技术有很多解决方式,如使用可以贴图的控件或使用第三方开发的换肤ActiveX控件。其实Skin技术需要处理的是WM_DRAWITEM、WM_MEASUREITEM、WM_NCPAINT消息,这些消息的主要用途就是可以重画控件和窗口的非客户区。想要对这些VB6无法处理的消息进行编程就必须用到子类化,这些消息都会被发送到能够自绘的控件的窗口上,因此下面的例子就是利用窗口子类化来重画Button控件。 

①创建工程 

  启动Visual Basic 6同时创建一个标准EXE工程。 

②窗口布局 

  在FORM1上放置3个Button控件,并将前两个Button的Style属性设置为1-Graphical,因为只有Style属性设置为Graphical的Button才可以Owner-drawn。 


③在窗体中录入代码

  1. 代码  
  2.   
  3. Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->Private Sub Command3_Click()   
  4.   
  5. '通过Enabled属性的控制,来显示重画控件在Unenabled状态时的效果   
  6. If Command1.Enabled Then   
  7.   
  8. Command1.Enabled = False   
  9.   
  10. Else   
  11.   
  12. Command1.Enabled = True   
  13.   
  14. End If   
  15.   
  16. End Sub   
  17.   
  18. Private Sub Form_Load()   
  19.   
  20. '安装子类化入口   
  21. Call Init(Me.hWnd)   
  22.   
  23. End Sub   
  24.   
  25. Private Sub Form_Unload(Cancel As Integer)   
  26.   
  27. '卸载子类化   
  28. Call Terminate(Me.hWnd)   
  29.   
  30. End Sub  
  31. ④加入一个模块并录入代码   
  32. Option Explicit  
  33.   
  34. ' -- 引用Win32Api –  
  35. '得到默认的窗口消息处理过程的地址需要的API  
  36.   
  37. Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long  
  38.   
  39. '设置一个新的窗口消息处理过程的地址需要的API  
  40.   
  41. Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long  
  42.   
  43. '给指定的窗口消息处理过程传递消息需要的API  
  44.   
  45. Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long  
  46.   
  47. '内存拷贝  
  48.   
  49. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)  
  50.   
  51. Const GWL_WNDPROC = (-4&)  
  52.   
  53. Dim PrevWndProc&  
  54.   
  55. Private Const WM_DESTROY = &H2  
  56.   
  57. Private Const WM_DRAWITEM = &H2B  
  58.   
  59. Private Type RECT  
  60.   
  61.  Left As Long  
  62.    
  63.  Top As Long  
  64.    
  65.  Right As Long  
  66.    
  67.  Bottom As Long  
  68.   
  69. End Type  
  70.   
  71. 'WM_DRAWITEM需要处理的结构体  
  72.   
  73. Private Type DRAWITEMSTRUCT  
  74.   
  75. CtlType As Long  
  76.   
  77. CtlID As Long  
  78.   
  79. itemID As Long  
  80.   
  81. itemAction As Long  
  82.   
  83. itemState As Long  
  84.   
  85. hwndItem As Long  
  86.   
  87. hdc As Long  
  88.   
  89. rcItem As RECT  
  90.   
  91. itemData As Long  
  92.   
  93. End Type  
  94.   
  95. ' Owner draw 常量  
  96.   
  97. Private Const ODT_BUTTON = 4  
  98.   
  99. ' Owner draw 动作  
  100.   
  101. Private Const ODA_DRAWENTIRE = &H1  
  102.   
  103. Private Const ODA_SELECT = &H2  
  104.   
  105. Private Const ODA_FOCUS = &H4  
  106.   
  107. ' Owner draw 状态  
  108.   
  109. Private Const ODS_SELECTED = &H1  
  110.   
  111. Private Const ODS_GRAYED = &H2  
  112.   
  113. Private Const ODS_DISABLED = &H4  
  114.   
  115. Private Const ODS_CHECKED = &H8  
  116.   
  117. Private Const ODS_FOCUS = &H10  
  118.   
  119. '得到指定窗口的文本  
  120.   
  121. Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long  
  122.   
  123. 'GDI相关API函数,重画Button时使用  
  124.   
  125. Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long  
  126.   
  127. Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long  
  128.   
  129. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long  
  130.   
  131. Private Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long  
  132.   
  133. '色彩常量  
  134.   
  135. Const COLOR_SCROLLBAR = 0  
  136.   
  137. Const COLOR_BACKGROUND = 1  
  138.   
  139. Const COLOR_ACTIVECAPTION = 2  
  140.   
  141. Const COLOR_INACTIVECAPTION = 3  
  142.   
  143. Const COLOR_MENU = 4  
  144.   
  145. Const COLOR_WINDOW = 5  
  146.   
  147. Const COLOR_WINDOWFRAME = 6  
  148.   
  149. Const COLOR_MENUTEXT = 7  
  150.   
  151. Const COLOR_WINDOWTEXT = 8  
  152.   
  153. Const COLOR_CAPTIONTEXT = 9  
  154.   
  155. Const COLOR_ACTIVEBORDER = 10  
  156.   
  157. Const COLOR_INACTIVEBORDER = 11  
  158.   
  159. Const COLOR_APPWORKSPACE = 12  
  160.   
  161. Const COLOR_HIGHLIGHT = 13  
  162.   
  163. Const COLOR_HIGHLIGHTTEXT = 14  
  164.   
  165. Const COLOR_BTNFACE = 15  
  166.   
  167. Const COLOR_BTNSHADOW = 16  
  168.   
  169. Const COLOR_GRAYTEXT = 17  
  170.   
  171. Const COLOR_BTNTEXT = 18  
  172.   
  173. Const COLOR_INACTIVECAPTIONTEXT = 19  
  174.   
  175. Const COLOR_BTNHIGHLIGHT = 20  
  176.   
  177. Private Declare Function FillRect Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As Long  
  178.   
  179. Private Declare Function FrameRect Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As Long  
  180.   
  181. Private Declare Function CreatePen Lib "gdi32" (ByVal nPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long) As Long  
  182.   
  183. '画笔格式  
  184.   
  185. Const PS_SOLID = 0  
  186.   
  187. Const PS_DASH = 1 ' -------  
  188.   
  189. Const PS_DOT = 2 ' .......  
  190.   
  191. Const PS_DASHDOT = 3 ' _._._._  
  192.   
  193. Const PS_DASHDOTDOT = 4 ' _.._.._  
  194.   
  195. Const PS_NULL = 5  
  196.   
  197. Const PS_INSIDEFRAME = 6  
  198.   
  199. Const PS_USERSTYLE = 7  
  200.   
  201. Const PS_ALTERNATE = 8  
  202.   
  203. Const PS_STYLE_MASK = &HF  
  204.   
  205. Private Declare Function MoveToEx Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, lpPoint As POINTAPI) As Long  
  206.   
  207. Private Declare Function LineTo Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long  
  208.   
  209. Private Type POINTAPI  
  210.   
  211.  x As Long  
  212.    
  213.  y As Long  
  214.   
  215. End Type  
  216.   
  217. Private 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 Long  
  218.   
  219. Private Const DT_SINGLELINE = &H20  
  220.   
  221. Private Const DT_CENTER = &H1  
  222.   
  223. Private Const DT_VCENTER = &H4  
  224.   
  225. Private Declare Function SetTextColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long  
  226.   
  227. Private Declare Function SetBkMode Lib "gdi32" (ByVal hdc As Long, ByVal nBkMode As Long) As Long  
  228.   
  229. Private Const TRANSPARENT = 1  
  230.   
  231. ' – 声明结束 --  
  232.   
  233. Private Sub DrawButton(ByVal hWnd As Long, ByVal hdc As Long, rct As RECT, ByVal nState As Long)  
  234.   
  235.  Dim P As POINTAPI  
  236.    
  237.  Dim s As String  
  238.    
  239.  Dim hbr As Long  
  240.    
  241.  Dim hpen As Long  
  242.    
  243.    
  244.    
  245.  hbr = CreateSolidBrush(GetSysColor(COLOR_BTNFACE)) 'RGB(231, 231, 231)  
  246.    
  247.  SelectObject hdc, hbr  
  248.    
  249.  FillRect hdc, rct, hbr  
  250.    
  251.  DeleteObject hbr  
  252.    
  253.    
  254.    
  255.  '画文字时背景为透明状  
  256.    
  257.  SetBkMode hdc, TRANSPARENT  
  258.    
  259.  '得到Button的Caption  
  260.    
  261.  s = String$(255, " ")  
  262.    
  263.  GetWindowText hWnd, s, 255  
  264.    
  265.  s = Trim$(s)  
  266.    
  267.  '根据Button的Enabled状态进行重画  
  268.    
  269.  If (nState And ODS_DISABLED) = ODS_DISABLED Then  
  270.    
  271.  '画外围灰框  
  272.    
  273.  hbr = CreateSolidBrush(RGB(132, 130, 132))  
  274.    
  275.  SelectObject hdc, hbr  
  276.    
  277.  FrameRect hdc, rct, hbr  
  278.    
  279.  DeleteObject hbr  
  280.    
  281.  '画内侧3D效果->亮色  
  282.    
  283.  hpen = CreatePen(PS_SOLID, 1, RGB(255, 255, 255))  
  284.    
  285.  SelectObject hdc, hpen  
  286.    
  287.  MoveToEx hdc, rct.Left + 1, rct.Top + 1, P  
  288.    
  289.  LineTo hdc, rct.Right - 1, rct.Top + 1  
  290.    
  291.  MoveToEx hdc, rct.Left + 1, rct.Top + 1, P  
  292.    
  293.  LineTo hdc, rct.Left + 1, rct.Bottom - 1  
  294.    
  295.  DeleteObject hpen  
  296.    
  297.  '画内侧3D效果->暗色  
  298.    
  299.  hpen = CreatePen(PS_SOLID, 1, RGB(189, 190, 189))  
  300.    
  301.  SelectObject hdc, hpen  
  302.    
  303.  MoveToEx hdc, rct.Left + 1, rct.Bottom - 2, P  
  304.    
  305.  LineTo hdc, rct.Right - 1, rct.Bottom - 2  
  306.    
  307.  MoveToEx hdc, rct.Right - 2, rct.Top + 1, P  
  308.    
  309.  LineTo hdc, rct.Right - 2, rct.Bottom - 1  
  310.    
  311.  DeleteObject hpen  
  312.    
  313.  '画阴影文字  
  314.    
  315.  rct.Left = rct.Left + 1  
  316.    
  317.  rct.Right = rct.Right + 1  
  318.    
  319.  rct.Bottom = rct.Bottom + 1  
  320.    
  321.  rct.Top = rct.Top + 1  
  322.    
  323.  SetTextColor hdc, GetSysColor(COLOR_BTNHIGHLIGHT)  
  324.    
  325.  DrawText hdc, s, LenB(StrConv(s, vbFromUnicode)), rct, DT_CENTER Or DT_SINGLELINE Or DT_VCENTER  
  326.    
  327.  rct.Left = rct.Left - 1  
  328.    
  329.  rct.Right = rct.Right - 1  
  330.    
  331.  rct.Bottom = rct.Bottom - 1  
  332.    
  333.  rct.Top = rct.Top - 1  
  334.    
  335.  SetTextColor hdc, GetSysColor(COLOR_GRAYTEXT)  
  336.    
  337.  DrawText hdc, s, LenB(StrConv(s, vbFromUnicode)), rct, DT_CENTER Or DT_SINGLELINE Or DT_VCENTER  
  338.    
  339.  Exit Sub  
  340.    
  341.  End If  
  342.    
  343.  '按下Button时重画  
  344.    
  345.  If (nState And ODS_SELECTED) = ODS_SELECTED Then  
  346.    
  347.  '画内部区域颜色  
  348.    
  349.  hbr = CreateSolidBrush(RGB(156, 186, 222))  
  350.    
  351.  SelectObject hdc, hbr  
  352.    
  353.  FillRect hdc, rct, hbr  
  354.    
  355.  DeleteObject hbr  
  356.    
  357.  '画外围灰框  
  358.    
  359.  hbr = CreateSolidBrush(RGB(99, 125, 165))  
  360.    
  361.  SelectObject hdc, hbr  
  362.    
  363.  FrameRect hdc, rct, hbr  
  364.    
  365.  DeleteObject hbr  
  366.    
  367.  '画内侧3D效果->亮色  
  368.    
  369.  hpen = CreatePen(PS_SOLID, 1, RGB(123, 158, 206))  
  370.    
  371.  SelectObject hdc, hpen  
  372.    
  373.  MoveToEx hdc, rct.Left + 1, rct.Top + 1, P  
  374.    
  375.  LineTo hdc, rct.Right - 1, rct.Top + 1  
  376.    
  377.  MoveToEx hdc, rct.Left + 1, rct.Top + 1, P  
  378.    
  379.  LineTo hdc, rct.Left + 1, rct.Bottom - 1  
  380.    
  381.  DeleteObject hpen  
  382.    
  383.  '画内侧3D效果->暗色  
  384.    
  385.  hpen = CreatePen(PS_SOLID, 1, RGB(181, 203, 231))  
  386.    
  387.  SelectObject hdc, hpen  
  388.    
  389.  MoveToEx hdc, rct.Left + 1, rct.Bottom - 2, P  
  390.    
  391.  LineTo hdc, rct.Right - 1, rct.Bottom - 2  
  392.    
  393.  MoveToEx hdc, rct.Right - 2, rct.Top + 1, P  
  394.    
  395.  LineTo hdc, rct.Right - 2, rct.Bottom - 1  
  396.    
  397.  DeleteObject hpen  
  398.    
  399.    
  400.    
  401.  rct.Left = rct.Left + 1  
  402.    
  403.  rct.Right = rct.Right + 1  
  404.    
  405.  rct.Bottom = rct.Bottom + 1  
  406.    
  407.  rct.Top = rct.Top + 1  
  408.    
  409.  SetTextColor hdc, GetSysColor(COLOR_BTNTEXT)  
  410.    
  411.  DrawText hdc, s, LenB(StrConv(s, vbFromUnicode)), rct, DT_CENTER Or DT_SINGLELINE Or DT_VCENTER  
  412.    
  413.  Exit Sub  
  414.    
  415.  End If  
  416.    
  417.  'Button得到焦点时重画  
  418.    
  419.  If (nState And ODS_FOCUS) = ODS_FOCUS Then  
  420.    
  421.  '画内部区域颜色  
  422.    
  423.  hbr = CreateSolidBrush(RGB(173, 203, 239))  
  424.    
  425.  SelectObject hdc, hbr  
  426.    
  427.  FillRect hdc, rct, hbr  
  428.    
  429.  DeleteObject hbr  
  430.    
  431.  '画外围灰框  
  432.    
  433.  hbr = CreateSolidBrush(RGB(107, 138, 181))  
  434.    
  435.  SelectObject hdc, hbr  
  436.    
  437.  FrameRect hdc, rct, hbr  
  438.    
  439.  DeleteObject hbr  
  440.    
  441.  '画内侧3D效果->亮色  
  442.    
  443.  hpen = CreatePen(PS_SOLID, 1, RGB(198, 223, 247))  
  444.    
  445.  SelectObject hdc, hpen  
  446.    
  447.  MoveToEx hdc, rct.Left + 1, rct.Top + 1, P  
  448.    
  449.  LineTo hdc, rct.Right - 1, rct.Top + 1  
  450.    
  451.  MoveToEx hdc, rct.Left + 1, rct.Top + 1, P  
  452.    
  453.  LineTo hdc, rct.Left + 1, rct.Bottom - 1  
  454.    
  455.  DeleteObject hpen  
  456.    
  457.  '画内侧3D效果->暗色  
  458.    
  459.  hpen = CreatePen(PS_SOLID, 1, RGB(132, 174, 222))  
  460.    
  461.  SelectObject hdc, hpen  
  462.    
  463.  MoveToEx hdc, rct.Left + 1, rct.Bottom - 2, P  
  464.    
  465.  LineTo hdc, rct.Right - 1, rct.Bottom - 2  
  466.    
  467.  MoveToEx hdc, rct.Right - 2, rct.Top + 1, P  
  468.    
  469.  LineTo hdc, rct.Right - 2, rct.Bottom - 1  
  470.    
  471.  DeleteObject hpen  
  472.    
  473.    
  474.    
  475.  SetTextColor hdc, GetSysColor(COLOR_BTNTEXT)  
  476.    
  477.  DrawText hdc, s, LenB(StrConv(s, vbFromUnicode)), rct, DT_CENTER Or DT_SINGLELINE Or DT_VCENTER  
  478.    
  479.  Else  
  480.    
  481.  '画外围灰框  
  482.    
  483.  hbr = CreateSolidBrush(RGB(132, 130, 132))  
  484.    
  485.  SelectObject hdc, hbr  
  486.    
  487.  FrameRect hdc, rct, hbr  
  488.    
  489.  DeleteObject hbr  
  490.    
  491.  '画内侧3D效果->亮色  
  492.    
  493.  hpen = CreatePen(PS_SOLID, 1, RGB(255, 255, 255))  
  494.    
  495.  SelectObject hdc, hpen  
  496.    
  497.  MoveToEx hdc, rct.Left + 1, rct.Top + 1, P  
  498.    
  499.  LineTo hdc, rct.Right - 1, rct.Top + 1  
  500.    
  501.  MoveToEx hdc, rct.Left + 1, rct.Top + 1, P  
  502.    
  503.  LineTo hdc, rct.Left + 1, rct.Bottom - 1  
  504.    
  505.  DeleteObject hpen  
  506.    
  507.  '画内侧3D效果->暗色  
  508.    
  509.  hpen = CreatePen(PS_SOLID, 1, RGB(189, 190, 189))  
  510.    
  511.  SelectObject hdc, hpen  
  512.    
  513.  MoveToEx hdc, rct.Left + 1, rct.Bottom - 2, P  
  514.    
  515.  LineTo hdc, rct.Right - 1, rct.Bottom - 2  
  516.    
  517.  MoveToEx hdc, rct.Right - 2, rct.Top + 1, P  
  518.    
  519.  LineTo hdc, rct.Right - 2, rct.Bottom - 1  
  520.    
  521.  DeleteObject hpen  
  522.    
  523.  '画文字  
  524.    
  525.  SetTextColor hdc, GetSysColor(COLOR_BTNTEXT)  
  526.    
  527.  DrawText hdc, s, LenB(StrConv(s, vbFromUnicode)), rct, DT_CENTER Or DT_SINGLELINE Or DT_VCENTER  
  528.    
  529.  End If  
  530.   
  531. End Sub  
  532.   
  533. '新的窗口消息处理过程,将被插入到默认处理过程之前  
  534.   
  535. Private Function SubWndProc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long  
  536.   
  537.  Dim di As DRAWITEMSTRUCT  
  538.    
  539.  If Msg = WM_DESTROY Then Terminate (hWnd)  
  540.    
  541.  '处理自画消息  
  542.    
  543.  If Msg = WM_DRAWITEM Then  
  544.    
  545.  CopyMemory di, ByVal lParam, Len(di)  
  546.    
  547.  '判断是自画Button  
  548.    
  549.  If di.CtlType = ODT_BUTTON Then  
  550.    
  551.  DrawButton di.hwndItem, di.hdc, di.rcItem, di.itemState  
  552.    
  553.  '不返回VB的默认Button绘制过程  
  554.    
  555.  SubWndProc = 1  
  556.    
  557.  Exit Function  
  558.    
  559.  End If  
  560.    
  561.  End If  
  562.    
  563.  '调用默认的窗口处理过程  
  564.    
  565.  SubWndProc = CallWindowProc(PrevWndProc, hWnd, Msg, wParam, lParam)  
  566.   
  567. End Function  
  568.   
  569. '子类化入口  
  570.   
  571. Public Sub Init(hWnd As Long)  
  572.   
  573.  PrevWndProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf SubWndProc)  
  574.   
  575. End Sub  
  576.   
  577. '子类化出口  
  578.   
  579. Public Sub Terminate(hWnd As Long)  
  580.   
  581.  Call SetWindowLong(hWnd, GWL_WNDPROC, PrevWndProc)  
  582.   
  583. End Sub  
  584.   
  585. ' -- 模块结束 -- '  

3. Visual Basic .NET子类化的实现 

  .NET中使用子类化技术要比VB6中简单,因为微软在.NET中已经提供了接口,不需要我们再自己SetWindowLong了,我们做的是Overrides(覆盖) WndProc过程即可。 
  Overrides Protected Sub WndProc( ByRef m As Message )参数m实现了Windows的消息类型。 

  下面的例子将演示如何将About加入窗口的系统菜单。 

①创建工程 

  创建一个VB.NET的Windows Application工程。 

②录入代码

  1. 代码  
  2.   
  3. Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->Public Class Form1   
  4.   
  5. Inherits System.Windows.Forms.Form   
  6.   
  7. '中间隐去了.NET自动生成的代码   
  8. ' – 引用Win32Api   
  9. Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As IntPtr, ByVal bRevert As Int32) As Int32   
  10. Private Declare Function InsertMenu Lib "user32" Alias "InsertMenuA" (ByVal hMenu As Int32, ByVal nPosition As Int32, ByVal wFlags As Int32, ByVal wIDNewItem As Int32, ByVal lpNewItem As String) As Int32   
  11.   
  12. Private Const MF_BYCOMMAND = &H0&   
  13. Private Const MF_BYPOSITION = &H400&   
  14. Private Const MF_STRING = &H0&   
  15. Private Const MF_SEPARATOR = &H800&   
  16. Private Const WM_SYSCOMMAND = &H112   
  17.   
  18. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load   
  19.   
  20. InsertMenu(GetSystemMenu(Me.Handle, False), 0, MF_BYPOSITION Or MF_SEPARATOR, 2001, "") '加入一条分割线   
  21.   
  22. 'GetSystemMenu(Me.Handle, False)是得到系统菜单的句柄,第二个参数为True的话不能改变系统菜单,所以要设为False   
  23. InsertMenu(GetSystemMenu(Me.Handle, False), 0, MF_BYPOSITION Or MF_STRING, 2002, "About Me(&A)") '加入About me菜单在系统菜单中   
  24.   
  25. End Sub   
  26.   
  27. Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)   
  28. '类化窗口--覆盖WndProc过程   
  29.   
  30. If m.Msg = WM_SYSCOMMAND Then   
  31.   
  32. If m.WParam.ToInt32 = 2002 Then   
  33.   
  34. MsgBox("About Context", vbInformation, "About...")   
  35.   
  36. End If   
  37.   
  38. End If   
  39.   
  40. '调用窗口默认的处理过程   
  41. MyBase.WndProc(m)   
  42.   
  43. End Sub   
  44.   
  45. End Class  

4. 小结 

  子类化技术可以让我们实现一些使用VB在正常条件下无法完成的任务,而且通过这些技术可以更为深入的学习Windows编程,成为VB开发人员中的高手。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约