setforegroundwindow只能啟用桌面級的父窗體,即使後面跟的hwnd是子窗體的hwnd也不行!
啟用子窗體,可以先用setforegroundwindow把父窗體帶到前台,然後用以下訊息啟用子窗體:
'private const wm_mouseactivate=&h21
sendmessage thwnd, wm_mouseactivate, 0, 0
'private const wm_lbuttondown = &h201
sendmessage thwnd, wm_lbuttondown, 0, 0
或者直接用sendmessage thwnd, 33, 0, 0來啟用子窗體。
在form1下面新建3個command按鈕,然後開啟excel,新建book1,book2,把焦點切換到book1,然後測試以下**,會把焦點切換到book2:
'form1的**:
private declare function findwindow lib "user32" alias "findwindowa" (byval lpclassname as string, byval lpwindowname as string) as long
private declare function findwindowex lib "user32" alias "findwindowexa" (byval hwnd1 as long, byval hwnd2 as long, byval lpsz1 as string, byval lpsz2 as string) as long
private declare function setforegroundwindow lib "user32" (byval hwnd as long) as long
private 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
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
private declare sub sleep lib "kernel32" (byval dwmilliseconds as long)
private const wm_mouseactivate = &h21
private const wm_lbuttondown = &h201
private sub command1_click()
' call setnumlock(false) '關閉numlock鍵
' call mysendkey (vbkeycapital)
' msgbox getasynckeystate(vbkeycapital)
if getkeystate(vbkeycapital) = 1 then '如果capslock被按下
sendkeys "", false
' call mysendkey(vbkeycapital)
' scancode = mapvirtualkey(vbkeycapital, 0)
' call keybd_event(vbkeycapital, scancode, 0, 0)
' sleep 1000
' call keybd_event(vbkeycapital, scancode, keyeventf_keyup, 0)
' sleep 1000
end if
dim dhwnd as long
dim thwnd as long
dhwnd = findwindow("xlmain", vbnullstring)
if dhwnd > 0 then
thwnd = findwindowex(dhwnd, byval 0&, "xldesk", vbnullstring)
if thwnd > 0 then
thwnd = findwindowex(thwnd, byval 0&, vbnullstring, "book2")
end if
end if
if thwnd = 0 then
msgbox "請先新建兩個空白excel,名字叫做book1,book2"
exit sub
end if
setforegroundwindow dhwnd '啟用父窗體
sleep 1000 '等待視窗切換
'private const wm_mouseactivate = &h21
' sendmessage thwnd, wm_mouseactivate, 0, 0
' private const wm_lbuttondown = &h201
sendmessage thwnd, wm_lbuttondown, 0, 0
' private const wm_parentnotify = &h210
' private const wm_activate = &h6
' sendmessage hwnd4, wm_activate, 0, 0
' private const wm_lbuttondown = &h201
' sendmessage hwnd4, wm_lbuttondown, 0, 0
sleep 1000 '等待視窗切換
' sendkeys "", false
sleep 1000
sendkeys ""
sendkeys "aaa", false
sendkeys "bbb", false
sendkeys "ccc", false
sendkeys "ddd", false
sendkeys "eee", false
end sub
private sub command2_click()
endend sub
private sub command3_click()
dhwnd = findwindow("notepad", vbnullstring)
if dhwnd > 0 then
thwnd = findwindowex(dhwnd, byval 0&, "edit", vbnullstring)
end if
setforegroundwindow dhwnd '啟用父窗體
mysendkey vbkeya
end sub
用Windows API分解路徑
在很多 中看到,由於需要獲取乙個路徑字串中的檔名或者副檔名,很多程式設計師都用了c庫中的字串查詢函式來獲取。這樣子寫得 很多,也麻煩。其實在vc中,可以用更好的方法來獲取。沒有什麼特別多說明,如下 lptstr szfilename new tchar max path getmodulefilen...
Delphi下用WindowsAPI建立窗體
delphi 下呼叫windows api 建立窗體.模板 by hottey 2004 4 13 0 18 作者 視窗訊息處理函式.function mywinproc hwnd thandle umsg uint wparam,lparam cardinal cardinal exp ort s...
在VB中呼叫Windows API的注意事項
visual basic vb 作為一種高效程式設計環境,它封裝了部分windows api函式,但也犧牲了一些api的功能。呼叫api時稍有不慎就可能導致api程式設計錯誤,出現難於捕獲或間歇性錯誤,甚至出現程式崩潰。要減少api程式設計錯誤,提高vb呼叫api時的安全性,應重點注意下列八個問題 ...