真正懂windows消息的进来
先说下,我不是很懂,最近做项目才研究了一下,我现在遇到的困难是,我要给一个EXCEL的默认sheet发送消息,就发一个tab 或者esc的消息,我用sendkeys发送成功,但是sendkeys需要激活窗体,这样会影响用户体验,所以希望可以用SendMessage来做,可是我用SendMessage发送过去总是没反应,我用spy++观察了一下消息的日志,发现以下不同:
用SendKeys方式发的,如果我用手动去按一个TAB,那么消息也是这样的
//用SendKeys方式发的,如果我用手动去按一个TAB,那么消息也是这样的//SPY++观察到的消息//<00211> 00040700 P WM_KEYDOWN nVirtKey:VK_TAB cRepeat:1 ScanCode:0F fExtended:0 fAltDown:0 fRepeat:0 fUp:0//<00212> 00040700 P WM_CHAR chCharCode:'0009' (9) cRepeat:1 ScanCode:0F fExtended:0 fAltDown:0 fRepeat:0 fUp:0//<00213> 00040700 S WM_CTLCOLORSCROLLBAR hdcSB:0B01178F hwndSB:00030764//<00214> 00040700 R WM_CTLCOLORSCROLLBAR hBrush:00000000//<00215> 00040700 P WM_KEYUP nVirtKey:VK_TAB cRepeat:1 ScanCode:0F fExtended:0 fAltDown:0 fRepeat:1 fUp:1IntPtr hwnd = FindWindow("XLMAIN", null);IntPtr p = FindWindowEx(hwnd, System.IntPtr.Zero, "XLDESK", null); SetForegroundWindow(hwnd);SendKeys.SendWait("{TAB}");
//SPY++观察到的消息//<00069> 00040700 S WM_KEYDOWN nVirtKey:VK_TAB cRepeat:58236 ScanCode:FD fExtended:1 fAltDown:0 fRepeat:0 fUp:0//<00070> 00040700 R WM_KEYDOWN//<00071> 00040700 S WM_CHAR chCharCode:'0009' (9) cRepeat:58236 ScanCode:FD fExtended:1 fAltDown:0 fRepeat:0 fUp:0//<00072> 00040700 R WM_CHAR//<00073> 00040700 S WM_KEYUP nVirtKey:VK_TAB cRepeat:58236 ScanCode:FD fExtended:1 fAltDown:0 fRepeat:0 fUp:0//<00074> 00040700 R WM_KEYUP IntPtr hwnd = FindWindow("XLMAIN", null); IntPtr p = FindWindowEx(hwnd, System.IntPtr.Zero, "XLDESK", null); IntPtr p1 = FindWindowEx(p, System.IntPtr.Zero, "EXCEL7", null); SendMessage(p1, 0x0100, (IntPtr)0x00000009, ""); SendMessage(p1, 0x0102, (IntPtr)0x00000009, ""); SendMessage(p1, 0x0101, (IntPtr)0x00000009, "");