windows 使用 WM_SHOWWINDOW 代替 ShowWindow() 显示窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3631470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:10:50  来源:igfitidea点击:

Using WM_SHOWWINDOW to Show a Window instead of ShowWindow()

windowswinapisendmessagewindows-messages

提问by Synetech

I'm trying to use the SendMessage function of a hotkey utility (or NirCMD, etc.) to get a hidden window to pop up. I can for example get windows to close by sending 0x0010 (WM_CLOSE), but when I try sending 0x0018 (WM_SHOWWINDOW) with a wParam of 1 and an lParam of 0, nothing happens.

我正在尝试使用热键实用程序(或 NirCMD 等)的 SendMessage 函数来弹出隐藏窗口。例如,我可以通过发送 0x0010 (WM_CLOSE) 来关闭窗口,但是当我尝试发送 wParam 为 1 且 lParam 为 0 的 0x0018 (WM_SHOWWINDOW) 时,没有任何反应。

I've looked around, and the few places where someone complained that WM_SHOWWINDOW did not work, they happily took the suggestion to use ShowWindow() instead.

我环顾四周,在有人抱怨 WM_SHOWWINDOW 不起作用的几个地方,他们很高兴地接受了改用 ShowWindow() 的建议。

However I don't have ShowWindow() available; I can only send Windows messages. But ShowWindow() is not magic, surely it works by SendMessage-ing a WM_SHOWWINDOW or something under the covers.

但是我没有 ShowWindow() 可用;我只能发送 Windows 消息。但是 ShowWindow() 不是魔术,它肯定是通过 SendMessage 来工作的,它是一个 WM_SHOWWINDOW 或隐藏的东西。

How can I get a window to display itself by sending it a message?

如何通过向窗口发送消息来让窗口自行显示?

Thanks.

谢谢。

回答by Anders

Try these two messages:

试试这两条消息:

SendMessage(h,WM_SYSCOMMAND,SC_MINIMIZE,0);
SendMessage(h,WM_SYSCOMMAND,SC_RESTORE,0);

Or if using 3rd party apps is ok, try cmdow

或者,如果使用 3rd 方应用程序没问题,请尝试cmdow

回答by Peter Ruderman

WM_SHOWWINDOW is a notification, not a command. From MSDN:

WM_SHOWWINDOW 是通知,而不是命令。来自 MSDN:

The WM_SHOWWINDOW message is sent to a window when the window is about to be hidden or shown.

当窗口即将被隐藏或显示时,WM_SHOWWINDOW 消息被发送到窗口。

I don't believe there is any message that you can use to make a window show itself. Actually, the very idea seems a little strange to me. The window manager is the system component responsible for showing and hiding windows. To show a window, you must use one of the window manager APIs.

我不相信有任何消息可以用来让窗口显示出来。实际上,这个想法对我来说似乎有点奇怪。窗口管理器是负责显示和隐藏窗口的系统组件。要显示窗口,您必须使用窗口管理器 API 之一。

回答by Bitterblue

I think there is no wayto achieve that with SendMessage(WM_SYSCOMMAND didn't work for me). I tried actually in C#. You click the button, the window will be minimized via ShowWindow()and then you can see what messages are sent:

我认为没有办法通过SendMessage(WM_SYSCOMMAND 对我不起作用)来实现这一点。我实际上在 C# 中尝试过。您单击按钮,窗口将通过以下方式最小化ShowWindow(),然后您可以看到发送了哪些消息:

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public class Form1: Form
    {
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool ShowWindow(IntPtr window, int showCommand);

        private const int SW_MINIMIZE = 6;
        private bool print = false;

        public Form1()
        {
            Button button = new Button();
            button.Click += onButtonsClick;
            Controls.Add(button);
        }

        private void onButtonsClick(object sender, EventArgs e)
        {
            print = true;
            ShowWindow(Handle, SW_MINIMIZE);
            print = false;
        }

        protected override void WndProc(ref Message m)
        {
            if (print)
                Console.WriteLine(m.Msg.ToString() + "\t0x" + m.Msg.ToString("x4") + "\t" + m.WParam + "\t" + m.LParam);
            base.WndProc(ref m);
        }
    }
}