保持对表单的关注

发布于 2024-10-20 07:05:58 字数 348 浏览 0 评论 0原文

我正在使用 C# .NET 为盲人开发一个软件。
该软件仅适用于键盘和语音转语音。
当计算机启动时,该程序位于启动菜单中,但由于某种原因,该程序被激活时不在焦点中,因此除非将焦点重新转移到该程序,否则该程序无法正常工作。

我找到了一种方法,即使软件没有聚焦,也可以挂钩键盘按键,但我不认为这是一个解决方案。

我想要一种方法来执行以下一项或多项操作:

  1. 确保程序在启动时加载并处于焦点位置。
  2. 保持对程序的关注(该计算机将仅使用该程序运行)。
  3. 找到一个键盘快捷键,最好只有一个键(不是 Alt + Tab)将焦点返回到程序。

I am developing a software for a blind individual in C# .NET.
The software works only with the keyboard and voice to speech.
When the computer starts the program is in the start up menu, but for some reason the program is activated not in focus therefore it does not work properly unless the focus is re transferred to it.

I found a way to hook keyboard keys even when the software is not in focus but I don't see that as a solution.

I want a way to do one or more of the following:

  1. Make sure the program loads on start up and is in focus.
  2. Maintain focus on the program (this computer will be run only using this program).
  3. Find a keyboard shortcut, preferably one key only (not Alt + Tab) to return focus to the program.

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

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

发布评论

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

评论(1

似最初 2024-10-27 07:05:58

有很多方法可以解决这个问题,即您可以在启动控制台应用程序上运行,该应用程序将运行并集中您的程序:

    [STAThread]
    static void Main(string[] args)
    {
        System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
        myProcess.StartInfo.FileName = "calc";
        myProcess.Start();
        IntPtr hWnd = myProcess.Handle;
        SetFocus(new HandleRef(null, hWnd));
    }

    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern IntPtr SetFocus(HandleRef hWnd);

您可以托管 Windows 服务应用程序并使用计时器检查您的应用程序是否处于活动状态并集中,或者您可以使用热键来启动它回到焦点:http://www.codeproject.com/KB/miscctrl/ashsimplehotkeys。 aspx

编辑

这是控制台应用程序,这将使您的应用程序保持活力并集中(经过测试)。我需要找到 Windows 服务的解决方案,因为自从 Vista 以来,某些内容发生了变化,当从服务中凝视时,表单是不可见的:P

    static Process myProcess;

    [STAThread]
    static void Main(string[] args)
    {
        for (int i = 0; i < 10000; i++)
        {
            //count how many procesess with this name are active if more than zero its still alive
            Process[] proc = Process.GetProcessesByName("myprog");
            if (proc.Length > 0)
            {
                //its alive check if it has focus
                if (proc[0].MainWindowHandle != GetForegroundWindow())
                {
                    SetFocus(proc[0].MainWindowHandle);
                }
            }
            //no process start new one and focus on it
            else
            {
                myProcess = new Process();
                myProcess.StartInfo.FileName = "C:\\aa\\myprog.exe";
                myProcess.Start();

                SetFocus(myProcess.Handle);
            }
            Thread.Sleep(1000);
        }
    }

    private static void SetFocus(IntPtr handle)
    {
        SwitchToThisWindow(handle, true);
    }

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


    [DllImport("user32.dll", SetLastError = true)]
    public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);

There are many ways you can solve this ie you can run on startup console app that will run and focus your program:

    [STAThread]
    static void Main(string[] args)
    {
        System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
        myProcess.StartInfo.FileName = "calc";
        myProcess.Start();
        IntPtr hWnd = myProcess.Handle;
        SetFocus(new HandleRef(null, hWnd));
    }

    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern IntPtr SetFocus(HandleRef hWnd);

You can host a windows service application and using timer check if your app is alive and is focused or you can use hotkeys to bring it back focused: http://www.codeproject.com/KB/miscctrl/ashsimplehotkeys.aspx

Edited

this is console application, that will keep your app alive and focused (tested). i need to find walkaround for windows service becouse since vista something changed and form is invisible when stared from service :P

    static Process myProcess;

    [STAThread]
    static void Main(string[] args)
    {
        for (int i = 0; i < 10000; i++)
        {
            //count how many procesess with this name are active if more than zero its still alive
            Process[] proc = Process.GetProcessesByName("myprog");
            if (proc.Length > 0)
            {
                //its alive check if it has focus
                if (proc[0].MainWindowHandle != GetForegroundWindow())
                {
                    SetFocus(proc[0].MainWindowHandle);
                }
            }
            //no process start new one and focus on it
            else
            {
                myProcess = new Process();
                myProcess.StartInfo.FileName = "C:\\aa\\myprog.exe";
                myProcess.Start();

                SetFocus(myProcess.Handle);
            }
            Thread.Sleep(1000);
        }
    }

    private static void SetFocus(IntPtr handle)
    {
        SwitchToThisWindow(handle, true);
    }

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


    [DllImport("user32.dll", SetLastError = true)]
    public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文