日期:2014-05-17  浏览次数:21154 次

C# SetForegroundWindow()问题
本帖最后由 mirbnbhdt01 于 2014-01-08 16:24:27 编辑
        [DllImport("user32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("user32.dll")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        
        IntPtr ParenthWnd = FindWindow(null, "窗体名");
            if (ParenthWnd != IntPtr.Zero)
            {
                //选中当前的句柄窗口
                SetForegroundWindow(ParenthWnd);
                Console.WriteLine("暂停");
                Thread.Sleep(1000);
                SendKeys.SendWait(" ");
            }

我通过FindWindow 取得窗体的句柄 通过SetForegroundWindow()激活当前窗体  然后对该窗体发送一个空格事件  在32位系统下面能正常 但是64位系统里面之下 所选的窗体就不能激活 我应该怎么做才能在64位下激活指定的窗体呢

------解决方案--------------------
ClickCharKey2(MainHwnd, 32);
private void ClickCharKey2(IntPtr hwnd, char key)
        {
            SystemApi.SendMessage(hwnd, SystemApi.WM_KEYDOWN, key, 0);
            Thread.Sleep(500);
            SystemApi.SendMessage(hwnd, SystemApi.WM_KEYUP, key, 0);
        }
public const int WM_KEYDOWN = 0x0100;
        public const int WM_KEYUP = 0x0101;
        public const int WM_CHAR = 0x0102;
        public const int WM_IME_KEYDOWN = 0x0290;//适用于tab键
DllImport("user32.dll", EntryPoint = "SendMessage")]
        public&n