怎么在VB中判斷Windows9x的運(yùn)行模式
發(fā)表時(shí)間:2023-08-07 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]在Windows下編程,經(jīng)常發(fā)現(xiàn)有不少功能Windows系統(tǒng)已經(jīng)做了,如果能夠直接調(diào)用,就可省去不少程序的編寫,并能提高程序的運(yùn)行效率。在很多情況下,我們都可以用“Ctrl + X”、“Ctrl +...
在Windows下編程,經(jīng)常發(fā)現(xiàn)有不少功能Windows系統(tǒng)已經(jīng)做了,如果能夠直接調(diào)用,就可省去不少程序的編寫,并能提高程序的運(yùn)行效率。在很多情況下,我們都可以用“Ctrl + X”、“Ctrl + C”、 “Ctrl + V”和“Ctrl + Z”分別進(jìn)行“剪切”、“復(fù)制”、“粘貼”和“撤消”操作,由此想到,如果我們能夠在程序中調(diào)用系統(tǒng)的這些功能,就無(wú)需為如何實(shí)現(xiàn)這些操作而操心了。經(jīng)過(guò)不斷的探索,終于發(fā)現(xiàn)SendMessage和PostMessage能夠擔(dān)此重任,真是如獲至寶,于是迫不及待地把它們介紹給各位朋友。
用VB5的“API瀏覽器”可以很容易地找到這兩個(gè)API 函數(shù):
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
這兩個(gè)函數(shù)的功能幾乎是一樣的,只是SendMessage是直接調(diào)用Windows函數(shù)來(lái)發(fā)送消息,只有這個(gè)消息完全被處理后此函數(shù)才返回,而PostMessage則給窗體的消息隊(duì)列增加一個(gè)消息,這個(gè)消息將在未來(lái)某個(gè)時(shí)候進(jìn)行正常事件處理時(shí)得到處理。以下僅以SendMessage為例。
函數(shù)中雖然有四個(gè)參數(shù),但關(guān)鍵的是前兩個(gè):hwnd 和wMsg。Hwnd是句柄,Microsoft Windows應(yīng)用程序中的每個(gè)窗體和控件都擁有一個(gè)句柄,通過(guò)句柄可以指明函數(shù)的操作對(duì)象;wMsg是一個(gè)十六進(jìn)制數(shù),代表了函數(shù)要發(fā)送的具體消息。
下面以具體例子說(shuō)明如何用SendMessage實(shí)現(xiàn)“剪切”、“復(fù)制”、“粘貼”、“撤消” 和“刪除”功能:
在窗體中放置一個(gè)文本框Text1和五個(gè)按鈕,分別執(zhí)行以上五種功能,編寫以下程序。
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 還可以對(duì)RitchTextBox和ComboBox等進(jìn)行操作,只要相應(yīng)改變hwnd參數(shù)即可。