在 .net 中单击按钮切换到另一个应用程序(以前处于活动状态)

发布于 2024-12-19 23:10:27 字数 290 浏览 2 评论 0原文

嗨,我想做一些类似屏幕键盘的事情。我希望用户单击非活动应用程序上的按钮,然后按键将被发送到活动应用程序,同时保持活动应用程序处于活动状态。我为非活动应用程序中按钮的悬停事件编写了代码,并且它正在工作。但我想要的是在点击事件中做到这一点。它不起作用,因为不活动的应用程序变为活动的。悬停事件的代码如下。谢谢。


private void button1_MouseHover(object sender, EventArgs e)
{
    SendKeys.Send("{TAB}");
}

Hi I want to do something like on screen keyboard. I want user to click a button on inactive application and then key press will be sent to active application while keeping active application active. I wrote the code for hover event of the button in inactive application and it is working. But what I want is to do it in click event. It is not working because inactive application becomes active. The code is below for hover event. Thank you.


private void button1_MouseHover(object sender, EventArgs e)
{
    SendKeys.Send("{TAB}");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那一片橙海, 2024-12-26 23:10:27

最后我能找到一种方法来做到这一点。
请参考下面的代码。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Timers;
using tmr = System.Timers;
using System.Threading;

namespace KeyPressTest
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll")]
        static extern int GetForegroundWindow();

        [DllImport("user32")]
        private static extern UInt32 GetWindowThreadProcessId(Int32 hWnd, out Int32 lpdwProcessId);

        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

        tmr.Timer tm = new tmr.Timer();
        Int32 hwnd = 0;

        private Int32 GetWindowProcessID(Int32 hwnd)
        {
            Int32 pid = 1;
            GetWindowThreadProcessId(hwnd, out pid);
            return pid;
        }     

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            tm.Elapsed += Timer_Elapsed;
            tm.Interval = 100;
            tm.Start();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SetForegroundWindow((IntPtr)hwnd);
            Thread.Sleep(40);
            SendKeys.Send("{TAB}");
        }       

        private void Timer_Elapsed(object sender, System.EventArgs args)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(delegate
                {
                    if (this.Handle.ToInt32() != GetForegroundWindow())
                    {
                        hwnd = GetForegroundWindow();
                    }
                }));
            }
            else
            {
                if (this.Handle.ToInt32() != GetForegroundWindow())
                {
                    hwnd = GetForegroundWindow();
                }
            }
            if (hwnd == 0)
            {
                return;
            }
            string appProcessName = "";
            string appExePath = "";
            string appExeName = "";
            try
            {
                appProcessName = Process.GetProcessById(GetWindowProcessID(hwnd)).ProcessName;
                appExePath = Process.GetProcessById(GetWindowProcessID(hwnd)).MainModule.FileName;
                appExeName = appExePath.Substring(appExePath.LastIndexOf(@"\") + 1);
            }
            catch (Exception ex)
            {
            }
            if (textBox1.InvokeRequired)
            {
                textBox1.Invoke(new MethodInvoker(delegate
                {
                    textBox1.Text = appProcessName + " | " + appExePath + " | " + appExeName;
                }));
            }
            else
            {
                textBox1.Text = appProcessName + " | " + appExePath + " | " + appExeName;
            }
        }
    }
}

Finally I could figure out a way to do it.
Refer the code below.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Timers;
using tmr = System.Timers;
using System.Threading;

namespace KeyPressTest
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll")]
        static extern int GetForegroundWindow();

        [DllImport("user32")]
        private static extern UInt32 GetWindowThreadProcessId(Int32 hWnd, out Int32 lpdwProcessId);

        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

        tmr.Timer tm = new tmr.Timer();
        Int32 hwnd = 0;

        private Int32 GetWindowProcessID(Int32 hwnd)
        {
            Int32 pid = 1;
            GetWindowThreadProcessId(hwnd, out pid);
            return pid;
        }     

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            tm.Elapsed += Timer_Elapsed;
            tm.Interval = 100;
            tm.Start();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SetForegroundWindow((IntPtr)hwnd);
            Thread.Sleep(40);
            SendKeys.Send("{TAB}");
        }       

        private void Timer_Elapsed(object sender, System.EventArgs args)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(delegate
                {
                    if (this.Handle.ToInt32() != GetForegroundWindow())
                    {
                        hwnd = GetForegroundWindow();
                    }
                }));
            }
            else
            {
                if (this.Handle.ToInt32() != GetForegroundWindow())
                {
                    hwnd = GetForegroundWindow();
                }
            }
            if (hwnd == 0)
            {
                return;
            }
            string appProcessName = "";
            string appExePath = "";
            string appExeName = "";
            try
            {
                appProcessName = Process.GetProcessById(GetWindowProcessID(hwnd)).ProcessName;
                appExePath = Process.GetProcessById(GetWindowProcessID(hwnd)).MainModule.FileName;
                appExeName = appExePath.Substring(appExePath.LastIndexOf(@"\") + 1);
            }
            catch (Exception ex)
            {
            }
            if (textBox1.InvokeRequired)
            {
                textBox1.Invoke(new MethodInvoker(delegate
                {
                    textBox1.Text = appProcessName + " | " + appExePath + " | " + appExeName;
                }));
            }
            else
            {
                textBox1.Text = appProcessName + " | " + appExePath + " | " + appExeName;
            }
        }
    }
}
请远离我 2024-12-26 23:10:27

我从未做过类似的事情,但这是我在 forum

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace WindowsApplication1
{
    public partial class Form1 : Form
{

[DllImport("user32.dll")]
static extern int GetForegroundWindow();

[DllImport("user32")]
private static extern UInt32 GetWindowThreadProcessId(Int32 hWnd, out Int32 lpdwProcessId);

private int teller = 0;

public Form1()
{
   InitializeComponent();
}

private void timer1_Tick(object sender, EventArgs e)
{
   if (teller == 1)
   {
      setTextje();
   }
   teller++;
}

private Int32 GetWindowProcessID(Int32 hwnd)
{
    Int32 pid = 1;
    GetWindowThreadProcessId(hwnd, out pid);
    return pid;
}

private void setTextje()
{
   Int32 hwnd = 0;
   hwnd = GetForegroundWindow();
   string appProcessName = Process.GetProcessById(GetWindowProcessID(hwnd)).ProcessName;
   string appExePath = Process.GetProcessById(GetWindowProcessID(hwnd)).MainModule.FileName;
   string appExeName = appExePath.Substring(appExePath.LastIndexOf(@"\") + 1);
   textBox1.Text = appProcessName + " | " + appExePath + " | " + appExeName;
}
}
}

它并不能完全回答你的问题,但会给你一个提示。您需要DllImport“User32.dll”。之后你就可以获得前台窗口的 id 并使用它。
还有一篇非常有趣的关于应用程序切换跟踪的文章 C#

I have never done something similar, but here is what I found in this forum:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace WindowsApplication1
{
    public partial class Form1 : Form
{

[DllImport("user32.dll")]
static extern int GetForegroundWindow();

[DllImport("user32")]
private static extern UInt32 GetWindowThreadProcessId(Int32 hWnd, out Int32 lpdwProcessId);

private int teller = 0;

public Form1()
{
   InitializeComponent();
}

private void timer1_Tick(object sender, EventArgs e)
{
   if (teller == 1)
   {
      setTextje();
   }
   teller++;
}

private Int32 GetWindowProcessID(Int32 hwnd)
{
    Int32 pid = 1;
    GetWindowThreadProcessId(hwnd, out pid);
    return pid;
}

private void setTextje()
{
   Int32 hwnd = 0;
   hwnd = GetForegroundWindow();
   string appProcessName = Process.GetProcessById(GetWindowProcessID(hwnd)).ProcessName;
   string appExePath = Process.GetProcessById(GetWindowProcessID(hwnd)).MainModule.FileName;
   string appExeName = appExePath.Substring(appExePath.LastIndexOf(@"\") + 1);
   textBox1.Text = appProcessName + " | " + appExePath + " | " + appExeName;
}
}
}

It doesn't answer exactly to your question but it will give you a hint. You need to DllImport "User32.dll". After that you can get the id of the foreground window and play with that.
There is also a very interesting article about application switch tracking written in C#

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文