C# SetForegroundWindow()问题

mirbnbhdt01 2014-01-08 04:22:59
[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位下激活指定的窗体呢
...全文
2216 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
xipho518 2014-03-02
  • 打赏
  • 举报
回复
我遇到的是同样的问题呀,请问你解决了吗?共享一下方法可以吗?我是想把窗体置前并模拟鼠标按键,在32位系统没问题,64位的就不行了。我也是用setforegroundwindow,现在都没解决。
「已注销」 2014-01-08
  • 打赏
  • 举报
回复
那是你句柄没找对 播放器是有窗体的 窗体里面才是播放器
mirbnbhdt01 2014-01-08
  • 打赏
  • 举报
回复
引用 8 楼 wawd74520 的回复:
ClickCharKey2(MainHwnd, 32);这个就是发送空格 MainHwnd这个是那个窗体的句柄
哥们儿 我这样写了 窗体没有反应 能加个QQ帮我调试下否
「已注销」 2014-01-08
  • 打赏
  • 举报
回复
ClickCharKey2(MainHwnd, 32);这个就是发送空格 MainHwnd这个是那个窗体的句柄
mirbnbhdt01 2014-01-08
  • 打赏
  • 举报
回复
引用 6 楼 wawd74520 的回复:
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 static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
private void ClickCharKey2(IntPtr hwnd, char key)这个方法的char key参数传的是空格的键值吗?
「已注销」 2014-01-08
  • 打赏
  • 举报
回复
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 static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
mirbnbhdt01 2014-01-08
  • 打赏
  • 举报
回复
引用 4 楼 wawd74520 的回复:
找到文本框没? 找到了 就直接设置文本 如果是窗体接收空格指令 用sendmessage即可
是这样的 我这打开了一个视频播放器 然后想在另外一个面板设置2个按钮 一个播放 一个暂停 来对这个播放器进行操作 暂停我就打算模拟键盘的空格键 在32位系统下 我这程序是没问题的 但是64位下面SetForegroundWindow()这个方法就不能对播放器窗口激活 导致SendKeys.SendWait(" ");没办法执行
「已注销」 2014-01-08
  • 打赏
  • 举报
回复
找到文本框没? 找到了 就直接设置文本 如果是窗体接收空格指令 用sendmessage即可
mirbnbhdt01 2014-01-08
  • 打赏
  • 举报
回复
引用 2 楼 wawd74520 的回复:
IsWindowVisible 先看是不是隐藏了 然后设置 SetWindowPos(句柄)
private void continuePlay(){ string fileName = filePath.Substring(filePath.LastIndexOf('\\') + 1); Console.WriteLine("fileName:"+fileName); string titleName = fileName+" - The KMPlayer"; //取得句柄 IntPtr ParenthWnd = FindWindow(null, titleName); if (ParenthWnd != IntPtr.Zero) { //选中当前的句柄窗口 //SetForegroundWindow(ParenthWnd); SetWindowPos(ParenthWnd); Console.WriteLine("暂停"); Thread.Sleep(1000); SendKeys.SendWait(" "); } } 我调用这个方法向那个窗口发送一个空格 我这应该怎么写呢 能帮我修改下这个方法吗
「已注销」 2014-01-08
  • 打赏
  • 举报
回复
IsWindowVisible 先看是不是隐藏了 然后设置 SetWindowPos(句柄)
「已注销」 2014-01-08
  • 打赏
  • 举报
回复
[System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern bool SetWindowPos(IntPtr hWnd,
        int hWndInsertAfter,
        int X,
        int Y,
        int cx,
        int cy,
        int uFlags
        );

        public static void SetWindowPos(IntPtr hWnd)
        {
            SetWindowPos(hWnd, -1, 0, 0, 0, 0, 3);
            Thread.Sleep(500);
            SetWindowPos(hWnd, -2, 0, 0, 0, 0, 3);
        }
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern bool IsWindowVisible(IntPtr hWnd);

111,131

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧