努力了一天,还有很多问题: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 Const WM_CLOSE = &H10 Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long 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 LongPrivate Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long Private Const SW_SHOWNORMAL = 1 Private Const SWP_SHOWWINDOW = &H40 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 Const BM_CLICK = &HF5 Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As LongPrivate Function GetHwnd(buttonHwnd As Long, MenuHwnd As Long) As Boolean Dim LhWnd As Long Dim StartMenuHwnd As Long LhWnd = FindWindow("Shell_TrayWnd", vbNullString) buttonHwnd = FindWindowEx(LhWnd, 0, "Button", vbNullString) StartMenuHwnd = FindWindow("DV2ControlHost", vbNullString) If StartMenuHwnd = 0 Then StartMenuHwnd = FindWindow("BaseBar", vbNullString) End If MenuHwnd = StartMenuHwnd End FunctionPrivate Sub Command1_Click() Dim hwnd As Long, bHwnd As Long Dim myRect As RECT GetHwnd bHwnd, hwnd SendMessage bHwnd, BM_CLICK, 0&, 0& '开始 按钮 GetWindowRect hwnd, myRect '理论上是开始菜单,可见开始菜单其实是个“窗体” 'ShowWindow hwnd, SW_SHOWNORMAL Print myRect.Top, myRect.Right'这个地方好象不加个操作就会有问题。在IDE下尤其明显 'SetWindowPos hwnd, 0, 200, 200, 200, 350, SWP_SHOWWINDOW MoveWindow hwnd, 200, 200, 220, 300, True '无法获得其宽度和高度 End Sub除了注释的问题,还有就是显示后必须有个操作才能关闭菜单……
mnu1 为窗体上设计的菜单名PopupMenu mnu1 '直接在当前鼠标位置弹出菜单 PopupMenu mnu1, ,x, y '在坐标(x,y)处弹出菜单
以下是我的代码,解决了如上2个问题,开始菜单的位置在(200,200)根据需要自己设置吧 虽然代码很简单,但是应该能拿满50分吧Private Type RECT Left As Long Bottom As Long Right As Long Top As Long End Type Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) 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 FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long Private hWndSMenu As Long, rSMenu As RECTPrivate Sub Command1_Click() hWndSMenu = FindWindow("DV2ControlHost", "「开始」菜单") GetWindowRect hWndSMenu, rSMenu MoveWindow hWndSMenu, 200, 200, rSMenu.Right - rSMenu.Left, rSMenu.Top - rSMenu.Bottom, True ShowWindow hWndSMenu, 1End Sub
麻烦2楼的测试一下?
谢谢
Private Const WM_CLOSE = &H10
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
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 LongPrivate Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Const SW_SHOWNORMAL = 1
Private Const SWP_SHOWWINDOW = &H40
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 Const BM_CLICK = &HF5
Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As LongPrivate Function GetHwnd(buttonHwnd As Long, MenuHwnd As Long) As Boolean
Dim LhWnd As Long
Dim StartMenuHwnd As Long
LhWnd = FindWindow("Shell_TrayWnd", vbNullString)
buttonHwnd = FindWindowEx(LhWnd, 0, "Button", vbNullString)
StartMenuHwnd = FindWindow("DV2ControlHost", vbNullString)
If StartMenuHwnd = 0 Then
StartMenuHwnd = FindWindow("BaseBar", vbNullString)
End If
MenuHwnd = StartMenuHwnd
End FunctionPrivate Sub Command1_Click()
Dim hwnd As Long, bHwnd As Long
Dim myRect As RECT
GetHwnd bHwnd, hwnd
SendMessage bHwnd, BM_CLICK, 0&, 0& '开始 按钮
GetWindowRect hwnd, myRect '理论上是开始菜单,可见开始菜单其实是个“窗体”
'ShowWindow hwnd, SW_SHOWNORMAL
Print myRect.Top, myRect.Right'这个地方好象不加个操作就会有问题。在IDE下尤其明显
'SetWindowPos hwnd, 0, 200, 200, 200, 350, SWP_SHOWWINDOW
MoveWindow hwnd, 200, 200, 220, 300, True '无法获得其宽度和高度
End Sub除了注释的问题,还有就是显示后必须有个操作才能关闭菜单……
PopupMenu mnu1, ,x, y '在坐标(x,y)处弹出菜单
每当我们按下win键或者点击开始菜单时,explorer。exe都会为开始菜单重置位置,因此你的代码虽然模拟了开始按钮按下的操作,但是由于你代码中重置位置的代码与上一行,点击开始按钮的操作间隔十分的小,2行代码的延时可以十分轻松的超过,点击开始按钮至显示出开始菜单的时间,所以开始菜单的位置虽然被你改变了,但是随后又被explorer所重置,所以看上去好像,位置没有改变一样
虽然代码很简单,但是应该能拿满50分吧Private Type RECT
Left As Long
Bottom As Long
Right As Long
Top As Long
End Type
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) 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 FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private hWndSMenu As Long, rSMenu As RECTPrivate Sub Command1_Click()
hWndSMenu = FindWindow("DV2ControlHost", "「开始」菜单")
GetWindowRect hWndSMenu, rSMenu
MoveWindow hWndSMenu, 200, 200, rSMenu.Right - rSMenu.Left, rSMenu.Top - rSMenu.Bottom, True
ShowWindow hWndSMenu, 1End Sub
鼠标在显示的开始菜单上移动的时候,菜单没有选中的的效果
切换为经典的时候无效
原因不仅仅因为hWndSMenu = FindWindow("DV2ControlHost", "「开始」菜单")之外还可能是hWndSMenu = FindWindow("BaseBar", vbNullString)
问题的关键在于,经典模式下的菜单GetWindowRect hWndSMenu, rSMenu无法取得正确的值。
请继续关注,谢谢。