【问题标题】:How can I process a win32 control's message in Winforms?如何在 Winforms 中处理 win32 控件的消息?
【发布时间】:2014-05-01 18:00:53
【问题描述】:

我有一个位于 Winforms 窗口内的 win32 控件。我想处理这个控件的消息,例如我可以选择忽略某些消息。 win32 控件是 ListView 控件。

有没有办法做到这一点?

【问题讨论】:

  • 你的意思是System.Windows.Forms.Message?覆盖WndProc。编辑:不要介意Windows.Forms.Message 部分。 Win32 控件可能不会有这个。我的错,
  • 谢谢 我不确定它是如何工作的,但是发送的消息窗口是否仅限于此 win32 控件或 winforms 应用程序?我只是想处理win32控件的消息。
  • 我认为 winforms 有一个 ListView 不是吗?不管怎样,你有hWnd 来控制吗?
  • 是的,但我需要托管此 win32 列表视图,因为我的应用程序位于此 win32 旧版应用程序下。我有 hwnd,我用它把它的父级设置为我的 winforms 应用程序,所以现在它跟随我的 winforms 窗口。

标签: c# .net winforms winapi


【解决方案1】:

你可以重写Form类中的方法WndProc

public class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    protected override void WndProc(ref Message m)
    {
         // use the 'm' struct
    }

}

一个详细的例子是可用的here

using System;
using System.Drawing;
using System.Windows.Forms;

namespace csTempWindowsApplication1
{
    public class Form1 : System.Windows.Forms.Form
    {
        // Constant value was found in the "windows.h" header file.
        private const int WM_ACTIVATEAPP = 0x001C;
        private bool appActive = true;

        [STAThread]
        static void Main() 
        {
            Application.Run(new Form1());
        }

        public Form1()
        {
            this.Size = new System.Drawing.Size(300,300);
            this.Text = "Form1";
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
        }

        protected override void OnPaint(PaintEventArgs e) 
        {
            // Paint a string in different styles depending on whether the 
            // application is active. 
            if (appActive) 
            {
                e.Graphics.FillRectangle(SystemBrushes.ActiveCaption,20,20,260,50);
                e.Graphics.DrawString("Application is active", this.Font, SystemBrushes.ActiveCaptionText, 20,20);
            }
            else 
            {
                e.Graphics.FillRectangle(SystemBrushes.InactiveCaption,20,20,260,50);
                e.Graphics.DrawString("Application is Inactive", this.Font, SystemBrushes.ActiveCaptionText, 20,20);
            }
        }

    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
        protected override void WndProc(ref Message m) 
        {
            // Listen for operating system messages. 
            switch (m.Msg)
            {
                // The WM_ACTIVATEAPP message occurs when the application 
                // becomes the active application or becomes inactive. 
                case WM_ACTIVATEAPP:

                    // The WParam value identifies what is occurring.
                    appActive = (((int)m.WParam != 0));

                    // Invalidate to get new text painted. 
                    this.Invalidate();

                    break;                
            }
            base.WndProc(ref m);
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-17
    • 1970-01-01
    • 2018-01-22
    相关资源
    最近更新 更多