在VisualBasic中判断Windows9x的运行模式
用VB5的“API浏览器”可以很容易地找到这两个API 函数:
Declare Function SendMessage Lib “user32” Alias “SendMessageA” _(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _lParam As Any) As Long
Declare Function PostMessage Lib “user32” Alias “PostMessageA” _(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _ByVal lParam As Long) As Long
这两个函数的功能几乎是一样的,只是SendMessage是直接调用Windows函数来发送消息,只有这个消息完全被处理后此函数才返回,而PostMessage则给窗体的消息队列增加一个消息,这个消息将在未来某个时候进行正常事件处理时得到处理。以下仅以SendMessage为例。
函数中虽然有四个参数,但关键的是前两个:hwnd 和wMsg。Hwnd是句柄,Microsoft Windows应用程序中的每个窗体和控件都拥有一个句柄,通过句柄可以指明函数的操作对象;wMsg是一个十六进制数,代表了函数要发送的具体消息。
下面以具体例子说明如何用SendMessage实现“剪切”、“复制”、“粘贴”、“撤消” 和“删除”功能:
在窗体中放置一个文本框Text1和五个按钮,分别执行以上五种功能,编写以下程序。
Option Explicit
Private Declare Function SendMessage Lib “user32” Alias “SendMessageA” _(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const WM_CUT = &H300
Const WM_COPY = &H301
Const WM_PAST = &H302
Const WM_CLEAR = &H303
Const WM_UNDO = &H304
Dim fb As Long
Private Sub cmdClear_Click()
fb = PostMessage(Text1.hwnd, WM_CLEAR, 0, 0)
End Sub
Private Sub cmdCopy_Click()
fb = SendMessage(Text1.hwnd, WM_COPY, 0, 0)
End Sub
Private Sub cmdCut_Click()
fb = SendMessage(Text1.hwnd, WM_CUT, 0, 0)
End Sub
Private Sub cmdPast_Click()
fb=SendMessage(Text1.hwnd, WM_PAST, 0, 0)
End Sub
Private Sub cmdUndo_Click()
fb=SendMessage(Text1.hwnd, WM_UNDO, 0, 0)
End Sub
除了TextBox外SendMessage 还可以对RitchTextBox和ComboBox等进行操作,只要相应改变hwnd参数即可。