C#Windows消息在控制台应用程序中?

发布于 2025-01-28 19:13:32 字数 230 浏览 2 评论 0原文

我需要使用IPC从另一个过程接收消息。目前,我正在使用WPF应用程序使用WindowsMessages接收消息,但是我想知道该通信是否在ConsoleApp中起作用?乍一看,我注意到hwndsource在consoleapp中找不到,所以问题是是否有一种方法是在consoleapp(首选)中接收windowsmessages创建WPF应用程序会更容易吗?

I need to use IPC to receive messages from another process. Currently I am using WPF application to receive messages using WindowsMessages but I am wondering if that communication would work in a ConsoleApp instead? At first glance I've noticed that HwndSource cannot be found in ConsoleApp so the question is if there is a way to receive WindowsMessages in a ConsoleApp (preferred way) or if it would be easier to create a WPF app instead?

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

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

发布评论

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

评论(2

零時差 2025-02-04 19:13:32

您可以使用步行路线(同意,不是最优质,最干净的方式,但对我有用)。
将引用添加到System.Windows.Forms,并创建一个从表单中衍生的类,如下所示:

public class ConsoleReceiver : Form
{
    public ConsoleReceiver()
    {
        SetFormProperties();
    }

    private void SetFormProperties()
    {
        Text = "YourWindowName";
        Width = 0;
        Height = 0;
        ShowIcon = false;
        ShowInTaskbar = false;
        Opacity = 0.0;
        Visible = false;
        ControlBox = false;
        MaximizeBox = false;
        MinimizeBox = false;
        FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    }

    protected override void WndProc(ref Message m)
    {
        //Wait for your message here
        Console.WriteLine($"Window message received {m.Msg} {m.LParam} {m.WParam}");
        base.WndProc(ref m);
    }
}

上面的代码将创建一个无形的表单窗口,您将可以通过名称找到并将窗口消息发送到。

在控制台应用程序的主要方法中,创建一个消息循环并初始化您的表单:

static void Main(string[] args)
    {
        Application.Run(new ConsoleReceiver());
    }

最后,在发件人应用程序中,按名称查找窗口,一旦找到该窗口,您将能够发送窗口消息。

声明该方法:

[DllImport("user32.dll")]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

并将其使用如下:

FindWindow(null, "YourWindowName");

You could use a walkaround (agree, not the nicest and cleanest way but worked for me).
Add a reference to System.Windows.Forms and create a class that derives from Form as follows:

public class ConsoleReceiver : Form
{
    public ConsoleReceiver()
    {
        SetFormProperties();
    }

    private void SetFormProperties()
    {
        Text = "YourWindowName";
        Width = 0;
        Height = 0;
        ShowIcon = false;
        ShowInTaskbar = false;
        Opacity = 0.0;
        Visible = false;
        ControlBox = false;
        MaximizeBox = false;
        MinimizeBox = false;
        FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    }

    protected override void WndProc(ref Message m)
    {
        //Wait for your message here
        Console.WriteLine(
quot;Window message received {m.Msg} {m.LParam} {m.WParam}");
        base.WndProc(ref m);
    }
}

Above code will create an invisible form window which you will be able to find by name and send you window messages to.

In the Main method of your console app Create a message loop and initialize your form:

static void Main(string[] args)
    {
        Application.Run(new ConsoleReceiver());
    }

Lastly, within your sender app find the window by name and once it is found you will be able to send your window messages.

Declare the method:

[DllImport("user32.dll")]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

And use it as follows:

FindWindow(null, "YourWindowName");
眼趣 2025-02-04 19:13:32

sendmessagepost threadMessage API只有在接收应用程序中运行一个活动的消息循环时才能使用。

如果您想与同一台计算机上的控制台应用程序进行通信,则可以使用名为管道。在文档这应该有用。

The SendMessage and PostThreadMessage APIs will only work when there is an active message loop running in the receiving application.

If you want to communicate with a console app on the same machine, you could use named pipes. There is an example available in the documentation that should be helpful.

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