相关疑难解决方法(0)

将Windows窗体应用程序带到前台的"正确"方法是什么?

我正在用C#编写Windows窗体应用程序.我需要能够把它带到前台.经过一些谷歌搜索和实验,我有一个看起来非常h​​acky的工作解决方案.

如果有的话,我想知道这样做的优雅方式.我需要应用程序恢复并前往前台,无论它是最小化,还是最小化,但在后台.

当前代码如下所示:

WindowState = FormWindowState.Minimized;
WindowState = FormWindowState.Normal;
BringToFront();
Focus();
Run Code Online (Sandbox Code Playgroud)

c# visual-studio-2008 winforms

31
推荐指数
4
解决办法
4万
查看次数

C#强制表格焦点

所以,在询问这个问题之前,我确实搜索了谷歌和SO.基本上我有一个DLL,它有一个编译成它的表单.表单将用于向屏幕显示信息.最终它将是异步的,并在dll中暴露了大量的自定义.现在我只想让它正确显示.我遇到的问题是我通过在Powershell会话中加载它来使用dll.因此,当我尝试显示表单并让它到达顶部并具有焦点时,显示在所有其他应用程序上没有问题,但我不能在我的生活中让它显示在Powershell窗口上.这是我目前用来尝试显示的代码.我确信,一旦我弄明白,大多数都不会被要求,这只是代表我通过谷歌找到的所有东西.

CLass Blah
{
        [DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
        public static extern bool SystemParametersInfo(uint uiAction, uint uiParam, uint pvParam, uint fWinIni);

        [DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("User32.dll", EntryPoint = "ShowWindowAsync")]
        private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
        private const int WS_SHOWNORMAL = 1;

    public void ShowMessage(string msg)
    {
            MessageForm msgFrm = new MessageForm();
            msgFrm.lblMessage.Text = "FOO";
            msgFrm.ShowDialog();
            msgFrm.BringToFront();
            msgFrm.TopMost = true;
            msgFrm.Activate();

            SystemParametersInfo((uint)0x2001, 0, 0, 0x0002 | 0x0001);
            ShowWindowAsync(msgFrm.Handle, WS_SHOWNORMAL);
            SetForegroundWindow(msgFrm.Handle);
            SystemParametersInfo((uint)0x2001, 200000, 200000, 0x0002 …
Run Code Online (Sandbox Code Playgroud)

c# winforms

22
推荐指数
2
解决办法
5万
查看次数

标签 统计

c# ×2

winforms ×2

visual-studio-2008 ×1