请问在hook中为何无法处理WM_MEASUREITEM消息?

zouguangxian 2003-08-20 09:26:23
我用setwindowshook安装一个钩子,希望能够捕捉到线程内的消息
然后通过处理WM_MEASUREITEM消息,实现自画菜单。
程序如下:
问题在于:WM_MEASUREITEM消息从我的钩子处理子程序中流过,
但没有得到期望的结果。

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;
using System.Runtime.InteropServices;
using MS;
namespace CustomMenu
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private IntPtr hookHandle;
private HOOKPROC myHookProc;
private Hashtable wndTable;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.ContextMenu contextMenu2;
private System.Windows.Forms.MenuItem menuItem2;
private SubBlassMenu w;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.button2 = new System.Windows.Forms.Button();
this.contextMenu2 = new System.Windows.Forms.ContextMenu();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.SuspendLayout();
//
// button1
//
this.button1.ContextMenu = this.contextMenu1;
this.button1.Location = new System.Drawing.Point(176, 80);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// contextMenu1
//
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[]
{
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.OwnerDraw = true;
this.menuItem1.Text = " 测试";
this.menuItem1.MeasureItem += new
System.Windows.Forms.MeasureItemEventHandler(this.menuItem1_MeasureItem);
//
// button2
//
this.button2.ContextMenu = this.contextMenu2;
this.button2.Location = new System.Drawing.Point(48, 152);
this.button2.Name = "button2";
this.button2.TabIndex = 1;
this.button2.Text = "button2";
//
// contextMenu2
//
this.contextMenu2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[]
{
this.menuItem2});
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.OwnerDraw = true;
this.menuItem2.Text = "From Yuanfen";
this.menuItem2.DrawItem += new
System.Windows.Forms.DrawItemEventHandler(this.menuItem2_DrawItem);
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
this.menuItem2.MeasureItem += new
System.Windows.Forms.MeasureItemEventHandler(this.menuItem2_MeasureItem);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Closing += new
System.ComponentModel.CancelEventHandler(this.Form1_Closing);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
myHookProc = new HOOKPROC(hookProc);
hookHandle =
Win32.SetWindowsHookEx(4,myHookProc,IntPtr.Zero,Win32.GetWindowThreadProcess
Id(this.Handle,0));
//hookHandle =
Win32.SetWindowsHookEx(4,myHookProc,IntPtr.Zero,Win32.GetCurrentThreadId());
wndTable = new Hashtable();
}

private void menuItem1_MeasureItem(object sender,
System.Windows.Forms.MeasureItemEventArgs e)
{
}

private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
Win32.UnhookWindowsHookEx(hookHandle);
}
private Int32 hookProc(int nCode, UInt32 wParam,ref CWPSTRUCT lParam)
{
int result = Win32.CallNextHookEx(hookHandle,nCode,wParam,ref lParam);
switch (nCode)
{
case 0 :
switch (lParam.message)
{
case 0x002C: //wm_measureitem
if (wParam == 0)
{
Debug.WriteLine("enter wm_measure");
MEASUREITEMSTRUCT tmp ;
tmp =
(MEASUREITEMSTRUCT)Marshal.PtrToStructure((IntPtr)lParam.lParam,typeof(MEASU
REITEMSTRUCT));
tmp.itemHeight= 24;
tmp.itemWidth = 100;
Marshal.StructureToPtr(tmp, (IntPtr)lParam.lParam, false);
return result; //if application handle this message
}
break;
}
break;
default:
break;
}
return result;
}

[ StructLayout( LayoutKind.Sequential )]
public struct RECT
{
public System.Int32 left;
public System.Int32 top;
public System.Int32 right;
public System.Int32 bottom;
}

[ StructLayout( LayoutKind.Sequential )]
public struct DRAWITEMSTRUCT
{
public System.Int32 CtlType;
public System.Int32 CtlID;
public System.Int32 itemID;
public System.Int32 itemAction;
public System.Int32 itemState;
public IntPtr hwndItem;
public IntPtr hDC;
public RECT rcItem;
public IntPtr itemData;
}

谢谢!

...全文
59 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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