用SendMessage实现剪贴板操作

发表于:2007-06-21来源:作者:点击数: 标签:
要使用这一技巧,你就需要声明用于剪切、复制和粘贴的常数: Private Const WM_COPY = H301 Private Const WM_CUT = H300 Private Const WM_PASTE = H302 然后,声明对SendMessage API的调用: Private Declare Function SendMessage Lib "user32" _ Alias "

   
  要使用这一技巧,你就需要声明用于剪切、复制和粘贴的常数:

Private Const WM_COPY = &H301
Private Const WM_CUT = &H300
Private Const WM_PASTE = &H302

然后,声明对SendMessage API的调用:

Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" (ByValhWnd As Long, _
ByValwMsg As Long, ByValwParam As Long, lParam As Any) As Long

   HWnd自变量能够接受消息发送的目的控件的句柄,而wMsg自变量会接受一个表明要被发送的是哪个对象的常数。WParam和lParam自变量被用来把其他信息同消息一起传递,但是不对WM_CUT、WM_COPY或者WM_PASTE使用。

   下面是从菜单点击事件调用SendMessage API的代码:

Private Sub mnuCopy_Click()
Call SendMessage(Me.ActiveControl.hwnd, WM_COPY, 0, 0)
End Sub

Private Sub mnuCut_Click()
Call SendMessage(Me.ActiveControl.hwnd, WM_CUT, 0, 0)
End Sub

Private Sub mnuPaste_Click()
Call SendMessage(Me.ActiveControl.hwnd, WM_PASTE, 0, 0)
End Sub

   这个技巧能够用于任何实现hWnd属性,并能够响应WM_CUT、WM_COPY和WM_PASTE消息的控件。还应该实现错误处理,以处理不带hWnd属性的控件。

原文转自:http://www.ltesting.net