要使用這一技巧,你就需要聲明用于剪切、復(fù)制和粘貼的常數(shù):
Private Const WM_COPY = &H301
Private Const WM_CUT = &H300
Private Const WM_PASTE = &H302
然后,聲明對SendMessage API的調(diào)用:
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" (ByValhWnd As Long, _
ByValwMsg As Long, ByValwParam As Long, lParam As Any) As Long
HWnd自變量能夠接受消息發(fā)送的目的控件的句柄,而wMsg自變量會接受一個表明要被發(fā)送的是哪個對象的常數(shù)。WParam和lParam自變量被用來把其他信息同消息一起傳遞,但是不對WM_CUT、WM_COPY或者WM_PASTE使用。
下面是從菜單點(diǎn)擊事件調(diào)用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
這個技巧能夠用于任何實(shí)現(xiàn)hWnd屬性,并能夠響應(yīng)WM_CUT、WM_COPY和WM_PASTE消息的控件。還應(yīng)該實(shí)現(xiàn)錯誤處理,以處理不帶hWnd屬性的控件。