当我显示另一个表单时,如何防止原始表单失去焦点? [英] How do I prevent the original form from losing focus when I show another form?

查看:176
本文介绍了当我显示另一个表单时,如何防止原始表单失去焦点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,当我的主表单失去焦点时,打开一个新的表单。我知道我可以通过使用 mainForm.focus()来恢复焦点,但是如果我希望主窗体在新窗口时永不放弃焦点,我该如何处理是否打开?

I am having a problem where my main form loses focus when opening a new form. I know I can revert the focus back by using mainForm.focus(), but how do I handle things if I want the main form to never give up its focus when new window is opened?

推荐答案

具有编辑权限的人可以在那里复制它,并删除所有我关心的;)

pinvoke.net的ShowWindow方法。:

pinvoke.net's ShowWindow method.:

    private const int SW_SHOWNOACTIVATE = 4;
    private const int HWND_TOPMOST = -1;
    private const uint SWP_NOACTIVATE = 0x0010;

    [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
    static extern bool SetWindowPos(
         int hWnd,           // window handle
         int hWndInsertAfter,    // placement-order handle
         int X,          // horizontal position
         int Y,          // vertical position
         int cx,         // width
         int cy,         // height
         uint uFlags);       // window positioning flags

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

    static void ShowInactiveTopmost(Form frm)
    {
        ShowWindow(frm.Handle, SW_SHOWNOACTIVATE);
        SetWindowPos(frm.Handle.ToInt32(), HWND_TOPMOST,frm.Left, frm.Top, frm.Width, frm.Height,SWP_NOACTIVATE);
        frm.TopMost = false;
    }

这篇关于当我显示另一个表单时,如何防止原始表单失去焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆