如何在.net程序中处理windows消息?

dreambird 2004-02-02 12:15:32
各位大虾,小弟求教一个问题。我以前用VC写的两个程序是靠互发自定义消息通讯的。现在其中一个我需要用c#重写(另一个保持不变),可是我不知道怎么处理发到我的新程序的windows消息。有没有哪位大哥有这方面的尝试,说来听听吧!谢谢。
...全文
27 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
巍巍清风 2004-02-02
  • 打赏
  • 举报
回复
调用API
[DllImport("User32.dll", EntryPoint="PostMessageW")]
public static extern bool PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

[DllImport("User32.dll", EntryPoint="SendMessageW")]
public static extern int SendMessage(int msg, IntPtr wParam, IntPtr lParam);

再把需要的常量和结构从 winuser.h 里倒过来。

Montaque 2004-02-02
  • 打赏
  • 举报
回复
: IMessagePrefilter
巍巍清风 2004-02-02
  • 打赏
  • 举报
回复
protected override void WndProc(ref Message m)
{
Graphics g;

IntPtr hdc;

Point pt;

Message newMsg = new Message();
newMsg.HWnd = m.HWnd;

switch (m.Msg)
{
case WMConsts.WM_NCHITTEST :
pt = new Point(m.LParam.ToInt32());
pt = PointToClient(pt);

OffsetClientPoint(m.HWnd, ref pt);

if ( m_ncIconRect.Contains(pt) )
m.Result = new IntPtr(WMConsts.HTSYSMENU);
else if ( m_ncCloseButtonRect.Contains(pt) )
m.Result = new IntPtr(WMConsts.HTCLOSE);
else if ( m_ncMaxButtonRect.Contains(pt) )
m.Result = new IntPtr(WMConsts.HTMAXBUTTON);
else if ( m_ncMinButtonRect.Contains(pt) )
m.Result = new IntPtr(WMConsts.HTMINBUTTON);

// top left
else if ( ( pt.X < m_borderLeftTopRect.Right && pt.Y < m_borderWidth) || ( pt.X < m_borderWidth && pt.Y < m_borderLeftTopRect.Bottom) )
m.Result = new IntPtr(WMConsts.HTTOPLEFT);
//top right
else if ( ( pt.X > m_borderRightTopRect.Left && pt.Y < m_borderWidth) || ( pt.X > m_borderRightTopRect.Right - m_borderWidth && pt.Y < m_borderRightTopRect.Bottom) )
m.Result = new IntPtr(WMConsts.HTTOPRIGHT);
// top
else if ( pt.Y < m_borderWidth )
m.Result = new IntPtr(WMConsts.HTTOP);

else if ( m_ncRect.Contains(pt) )
m.Result = new IntPtr(WMConsts.HTCAPTION);
else
base.WndProc(ref m);
break;


case WMConsts.WM_NCLBUTTONDOWN :
pt = new Point(m.LParam.ToInt32());
pt = PointToClient(pt);

OffsetClientPoint(m.HWnd, ref pt);

if ( m_ncIconRect.Contains(pt) )
{
base.WndProc(ref m);
//newMsg.WParam = new IntPtr(WMConsts.SC_ICON);
//DefWndProc(ref newMsg);
}
else if ( m_ncCloseButtonRect.Contains(pt) )
{
m_ncCloseButtonState = WMConsts.ButtonState.Pushed;
m_ncMaxButtonState = WMConsts.ButtonState.Normal;
m_ncMinButtonState = WMConsts.ButtonState.Normal;
}
else if ( m_ncMaxButtonRect.Contains(pt) )
{
m_ncCloseButtonState = WMConsts.ButtonState.Normal;
m_ncMaxButtonState = WMConsts.ButtonState.Pushed;
m_ncMinButtonState = WMConsts.ButtonState.Normal;
}
else if ( m_ncMinButtonRect.Contains(pt) )
{
m_ncCloseButtonState = WMConsts.ButtonState.Normal;
m_ncMaxButtonState = WMConsts.ButtonState.Normal;
m_ncMinButtonState = WMConsts.ButtonState.Pushed;
}
else
{
base.WndProc(ref m);

newMsg.Msg = WMConsts.WM_SYSCOMMAND;
newMsg.WParam = new IntPtr(WMConsts.SC_MOVE);
newMsg.LParam = m.LParam;
//DefWndProc(ref newMsg);
//WMConsts.PostMessage(newMsg.HWnd, newMsg.Msg, newMsg.WParam, newMsg.LParam);

}


// paint buttons
hdc = WMConsts.GetWindowDC(m.HWnd);
g = Graphics.FromHdc(hdc);

PaintNonClientArea(g, m_ncRect, true);

g.Dispose();
WMConsts.ReleaseDC(m.HWnd, hdc);
//

break;
default :
base.WndProc (ref m);
break;
}
}
dreambird 2004-02-02
  • 打赏
  • 举报
回复
app1_vc6 app2_c#

app1_vc6向app2_c#发送自定义windows消息,我想在app2_c#中处理这个自定义消息,而不是用P/Invoke方式从app2_c#向其他程序发消息。
着类似于c++ builder中混合使用了消息和事件的编程方式一样,不知道在c#程序里怎么写。
哪位有过这方面的尝试?

17,741

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧