请问在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;
}

谢谢!

...全文
62 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
消息,就是指Windows发出的一个通知,告诉应用程序某个事情发生了。例如,单击鼠标、改变窗口尺寸、按下键盘上的一个键都会使Windows发送一个消息给应用程序。 消息本身是作为一个记录传递给应用程序的,这个记录包含了消息的类型以及其他信息。例如,对于单击鼠标所产生的消息来说,这个记录包含了单击鼠标时的坐标。这个记录类型叫做TMsg,它在Windows单元是这样声明的: type TMsg = packed record hwnd: HWND; //窗口句柄 message: UINT;//消息常量标识符 wParam: WPARAM ;// 32位消息的特定附加信息 lParam: LPARAM ;// 32位消息的特定附加信息 time: DWORD;//消息创建时的时间 pt: TPoint; //消息创建时的鼠标位置 end ; 消息有什么? 是否觉得一个消息记录的信息像希腊语一样?如果是这样,那么看一看下面的解释:hwnd 32位的窗口句柄。窗口可以是任何类型的屏幕对象,因为Win32能够维护大多数可 视对象的句柄(窗口、对话框、按钮、编辑框等)。message 用于区别其他消息的常量值,这些常量可以是Windows单元预定义的常量,也 可以是自定义的常量。 wParam 通常是一个与消息有关的常量值,也可能是窗口或控件的句柄。 lParam 通常是一个指向内存数据的指针。由于WParam、lParam和Pointer都是32位的,因此,它们之间可以相互转换。 WM_NULL =$0000 // WM_CREATE =$0001 //应用程序创建一个窗口 WM_DESTROY = $0002 //一个窗口被销毁 WM_MOVE = $0003 //移动一个窗口 WM_SIZE= $0005 //改变一个窗口的大小 WM_ACTIVATE= $0006 //一个窗口被激活或失去激活状态; WM_SETFOCUS= $0007 //获得焦点后 WM_KILLFOCUS= $0008 //失去焦点 WM_ENABLE= $000A //改变enable状态 WM_SETREDRAW= $000B //设置窗口是否能重画 WM_SETTEXT= $000C //应用程序发送此消息来设置一个窗口的文本 WM_GETTEXT = $000D //应用程序发送此消息来复制对应窗口的文本到缓冲区 WM_GETTEXTLENGTH = $000E //得到与一个窗口有关的文本的长度(不包含空字符) WM_PAINT = $000F //要求一个窗口重画自己 WM_CLOSE = $0010 //当一个窗口或应用程序要关闭时发送一个信号 WM_QUERYENDSESSION= $0011 //当用户选择结束对话框或程序自己调用ExitWindows函数 WM_QUIT= $0012 //用来结束程序运行或当程序调用postquitmessage函数 WM_QUERYOPEN = $0013 //当用户窗口恢复以前的大小位置时,把此消息发送给某个图标 WM_ERASEBKGND = $0014 //当窗口背景必须被擦除时(例在窗口改变大小时) WM_SYSCOLORCHANGE = $0015 //当系统颜色改变时,发送此消息给所有顶级窗口 WM_ENDSESSION = $0016 // 当系统进程发出WM_QUERYENDSESSION消息后,此消息发送给应用程序,通知它对话是否结束 WM_SYSTEMERROR = $0017 // WM_SHOWWINDOW= $0018 //当隐藏或显示窗口是发送此消息给这个窗口 WM_ACTIVATEAPP = $001C //发此消息给应用程序哪个窗口是激活的,哪个是非激活的; WM_FONTCHANGE= $001D //当系统的字体资源库变化时发送此消息给所有顶级窗口 WM_TIMECHANGE= $001E //当系统的时间变化时发送此消息给所有顶级窗口 WM_CANCELMODE= $001F //发送此消息来取消某种正在进行的摸态(操作) WM_SETCURSOR = $0020 //如果鼠标引起光标在某个窗口移动且鼠标输入没有被捕获时,就发消息给某个窗口 WM_MOUSEACTIVATE = $0021 //当光标在某个非激活的窗口而用户正按着鼠标的某个键发送此消息给当前窗口 WM_CHILDACTIVATE = $0022 //发送此消息给MDI子窗口当用户点击此窗口的标题栏,或当窗口被激活,移动,改变大小 WM_QUEUESYNC= $0023 //此消息由基
目前这方面的软件很多,但大部分都是收费的,不收费大部分又换的不全,对于一个学生来说花钱买是有些奢侈了,所以我一直就想做一个换肤软件提供给学生,让他们做课程设计或毕业设计时能轻易给自己软件美化界面。 但是一直苦于时间有限。工作太忙有时只能在周末或晚上写上两行代码。现在终于成形了本打算开源,但是有些地方还不完善(现只支持VC MFC, Windows Type: Dialog, SDI),所以现在只讲下原理,提供部分源码供感兴趣的人研究。现在发出来与大家共享。 现在商业的换肤软件大部分都是采用的Hook技术(呵呵,猜的,也许采用的更高深的技术)。Hook窗体消息,对窗体消息进行截获最终换成自已的处理方式。所以本人写的SkinMaster也是采用了同样的技术原理。说很简单但做起来有些困难。下面是我做Skin时遇到的问题及处理方式。 1.对于Windows基本控件进行Hook则可完成绘制。 2.对于菜单会制则有些麻烦,程序运行时窗体菜单WM_MEASUREITEM只运行一次,所以会出现在动态换另一套皮肤时菜单项大小不会跟据皮肤改变,解决方法是所有菜单你要动态生成。 3.主窗体的绘制,没啥太深技术就是要处理大量的消息。 4.滚动条的绘制,滚动条全靠Hook消息就没办法完成了,这个东西微软做的不像基本控件那样工作,还要对滚动条的API进行Hook。 先写这些,有时间我会把更详细的方法给大家写出来。下面程序TestSkin程序提供源码,并完成了按钮等控件的换肤。

110,533

社区成员

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

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

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