如何使用WndProc(ref Message msg) 方法??

bluenight88 2009-01-11 10:01:54
我的設計工具: VS2005 C# 智能設備。(windows ce 5.0)消息進行處理。在很多程序上,在msdn上也有是使用這個wndproc()方法接收消息。而且說明是windows ce 獨有的。現在我也把using Microsoft.WindowsCE.Forms; 引入了。

可是總是無法使用 protected override void WndProc(ref Message msg); 當我寫到 protected override void 這裏的時候,下拉菜單沒有WndProc 這個方法啊!

我的源代碼:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using Microsoft.WindowsCE.Forms;



namespace MyApplicationCe
{
public partial class Form1 : Form
{
public const int USER = 0x0400;
public const int TEST1 = USER + 1;
//api
[DllImport("coredll.dll")]

private static extern int SendMessage
(
IntPtr hWnd, // handle to destination window
uint Msg, // message
uint wParam, // first message parameter
uint lParam // second message parameter
);


public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
SendMessage(this.Handle,TEST1,100,200);

}
protected override void ???



這裏就寫不下去了啊!哪位高手指教我一下啊!
...全文
691 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
bluenight88 2009-05-23
  • 打赏
  • 举报
回复
class DspCommunication : MessageWindow
新建一个继承的类,在这个里面使用wndproc()就可以了!
jogholy 2009-03-16
  • 打赏
  • 举报
回复
messageWindow并不能解决你的问题,你可以参考这个帖子.
也许对你有启发.
http://social.msdn.microsoft.com/Forums/en-US/netfxcompact/thread/532d40e3-69a9-4978-83d6-5b84a3f29062/
bluenight88 2009-01-12
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 BEYONDMA 的回复:]
楼上是一种方法,也可以用MESSAGEWINDOW,都行。
[/Quote]
2楼的那个看的不是很懂啊!
你这个messageWindow 什么意思啊!能够说的详细一点吗?
beyondma 2009-01-12
  • 打赏
  • 举报
回复
楼上是一种方法,也可以用MESSAGEWINDOW,都行。
儿大不由爷 2009-01-12
  • 打赏
  • 举报
回复
class WndProcHooker
{
public delegate int WndProcCallback(
IntPtr hwnd, uint msg, uint wParam, int lParam, ref bool handled);
private static Dictionary<IntPtr, HookedProcInformation> hwndDict =
new Dictionary<IntPtr, HookedProcInformation>();
private static Dictionary<Control, HookedProcInformation> ctlDict =
new Dictionary<Control, HookedProcInformation>();
public static void HookWndProc(
Control ctl, WndProcCallback callback, uint msg)
{
HookedProcInformation hpi = null;
if (ctlDict.ContainsKey(ctl))
hpi = ctlDict[ctl];
else if (hwndDict.ContainsKey(ctl.Handle))
hpi = hwndDict[ctl.Handle];
if (hpi == null)
{
hpi = new HookedProcInformation(ctl,
new Win32.WndProc(WndProcHooker.WindowProc));
ctl.HandleCreated += new EventHandler(ctl_HandleCreated);
ctl.HandleDestroyed += new EventHandler(ctl_HandleDestroyed);
ctl.Disposed += new EventHandler(ctl_Disposed);

if (ctl.Handle != IntPtr.Zero)
hpi.SetHook();
}

// stick hpi into the correct dictionary
if (ctl.Handle == IntPtr.Zero)
ctlDict[ctl] = hpi;
else
hwndDict[ctl.Handle] = hpi;

// add the message/callback into the message map
hpi.messageMap[msg] = callback;
}

static void ctl_Disposed(object sender, EventArgs e)
{
Control ctl = sender as Control;
if (ctlDict.ContainsKey(ctl))
ctlDict.Remove(ctl);
else
System.Diagnostics.Debug.Assert(false);
}

static void ctl_HandleDestroyed(object sender, EventArgs e)
{
// When the handle for a control is destroyed, we want to
// unhook its wndproc and update our lists
Control ctl = sender as Control;
if (hwndDict.ContainsKey(ctl.Handle))
{
HookedProcInformation hpi = hwndDict[ctl.Handle];
UnhookWndProc(ctl, false);
}
else
System.Diagnostics.Debug.Assert(false);
}

static void ctl_HandleCreated(object sender, EventArgs e)
{
Control ctl = sender as Control;
if (ctlDict.ContainsKey(ctl))
{
HookedProcInformation hpi = ctlDict[ctl];
hwndDict[ctl.Handle] = hpi;
ctlDict.Remove(ctl);
hpi.SetHook();
}
else
System.Diagnostics.Debug.Assert(false);
}

private static int WindowProc(
IntPtr hwnd, uint msg, uint wParam, int lParam)
{
if (hwndDict.ContainsKey(hwnd))
{
HookedProcInformation hpi = hwndDict[hwnd];
if (hpi.messageMap.ContainsKey(msg))
{
WndProcCallback callback = hpi.messageMap[msg];
bool handled = false;
int retval = callback(hwnd, msg, wParam, lParam, ref handled);
if (handled)
return retval;
}

return hpi.CallOldWindowProc(hwnd, msg, wParam, lParam);
}
System.Diagnostics.Debug.Assert(
false, "WindowProc called for hwnd we don't know about");
return Win32.DefWindowProc(hwnd, msg, wParam, lParam);
}

public static void UnhookWndProc(Control ctl, uint msg)
{
// look for the HookedProcInformation in the control and hwnd
// dictionaries
HookedProcInformation hpi = null;
if (ctlDict.ContainsKey(ctl))
hpi = ctlDict[ctl];
else if (hwndDict.ContainsKey(ctl.Handle))
hpi = hwndDict[ctl.Handle];
// if we couldn't find a HookedProcInformation, throw
if (hpi == null)
throw new ArgumentException("No hook exists for this control");

// look for the message we are removing in the messageMap
if (hpi.messageMap.ContainsKey(msg))
hpi.messageMap.Remove(msg);
else
// if we couldn't find the message, throw
throw new ArgumentException(
string.Format(
"No hook exists for message ({0}) on this control",
msg));
}
public static void UnhookWndProc(Control ctl, bool disposing)
{
HookedProcInformation hpi = null;
if (ctlDict.ContainsKey(ctl))
hpi = ctlDict[ctl];
else if (hwndDict.ContainsKey(ctl.Handle))
hpi = hwndDict[ctl.Handle];
if (hpi == null)
throw new ArgumentException("No hook exists for this control");

// If we found our HookedProcInformation in ctlDict and we are
// disposing remove it from ctlDict
if (ctlDict.ContainsKey(ctl) && disposing)
ctlDict.Remove(ctl);

// If we found our HookedProcInformation in hwndDict, remove it
// and if we are not disposing stick it in ctlDict
if (hwndDict.ContainsKey(ctl.Handle))
{
hpi.Unhook();
hwndDict.Remove(ctl.Handle);
if (!disposing)
ctlDict[ctl] = hpi;
}
}

class HookedProcInformation
{
/// <summary>
/// The message map for the window
/// </summary>
public Dictionary<uint, WndProcCallback> messageMap;
/// <summary>
/// The old window procedure for the window
/// </summary>
private IntPtr oldWndProc;
/// <summary>
/// The delegate that gets called in place of this window's
/// wndproc.
/// </summary>
private Win32.WndProc newWndProc;
/// <summary>
/// Control whose wndproc we are hooking
/// </summary>
private Control control;

/// <summary>
/// Constructs a new HookedProcInformation object
/// </summary>
/// <param name="ctl">The handle to the window being hooked</param>
/// <param name="wndproc">The window procedure to replace the
/// original one for the control</param>
public HookedProcInformation(Control ctl, Win32.WndProc wndproc)
{
control = ctl;
newWndProc = wndproc;
messageMap = new Dictionary<uint, WndProcCallback>();
}

/// <summary>
/// Replaces the windows procedure for <see>control</see> with the
/// one specified in the constructor.
/// </summary>
public void SetHook()
{
IntPtr hwnd = control.Handle;
if (hwnd == IntPtr.Zero)
throw new InvalidOperationException(
"Handle for control has not been created");

oldWndProc = Win32.SetWindowLong(hwnd, Win32.GWL_WNDPROC,
Marshal.GetFunctionPointerForDelegate(newWndProc));
}
/// <summary>
/// Restores the original window procedure for the control.
/// </summary>
public void Unhook()
{
IntPtr hwnd = control.Handle;
if (hwnd == IntPtr.Zero)
throw new InvalidOperationException(
"Handle for control has not been created");

Win32.SetWindowLong(hwnd, Win32.GWL_WNDPROC, oldWndProc);
}
public int CallOldWindowProc(
IntPtr hwnd, uint msg, uint wParam, int lParam)
{
return Win32.CallWindowProc(
oldWndProc, hwnd, msg, wParam, lParam);
}
}
}

MSDN 中的例子,不知道是否是LZ想要的。

19,500

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 嵌入开发(WinCE)
社区管理员
  • 嵌入开发(WinCE)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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