我想获得按钮的字体在其他应用程序。
当我尝试 WM_GETFONT
,则返回0。这是我的code:
[的DllImport(user32.dll中)]公共静态外部的IntPtr SendMessage函数(IntPtr的的HWND,INT消息,INT的wParam,lParam的INT);
IntPtr的FX = SendMessage消息(button1.Handle,0X31,0,0);
我已经知道了问题的原因,这是由于FlatStyle属性
请参阅此链接
http://www.siao2.com/2008/09/26/ 8965526.aspx
我该如何解决这个问题呢?
我使用Windows 7系统下的C#。
解决方案你得到的价值,因为你检查控制使用默认的系统字体。 如果自定义字体没有明确规定,这是precisely,你应该期望的值。
如果默认的系统字体被用于绘制控件的文本 WM_GETFONT
消息总是返回0(或NULL)。按照文档:
返回值是一个句柄,如果控制使用系统字体使用的控制,或NULL的字体。
同样对于 GetTextFace
功能。在这种情况下,相关 WM_SETFONT
消息提供了一些明确的见解:如果的wParam
值为NULL,则控制会使用默认的系统字体绘制的文本。
很显然,你的没有的改变了字体,或至少是Windows不认为你有。你提到,你想获得的字体的等的应用程序,你是如何改变用来借鉴控件中的文本在另一个应用程序的字体?
如果您更改默认的系统字体,行为解释上面仍然适用,不论您是否在屏幕上看到的有什么不同。
如果你发送的 WM_SETFONT
消息,你可能会忘记告诉控制应重绘自身。再次,从联系到上面的文档拉:
lParam的的
lParam参数的低位字指定控件是否应 可以在设置立即重绘 的字体。如果这个参数为TRUE, 控制重绘自己。I am trying to get the font of button in other applications.
When I try WM_GETFONT
, it returns 0. This my code:
[DllImport("User32.DLL")] public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
IntPtr fx = SendMessage(button1.Handle, 0x31, 0, 0);
I have known the cause of the problem, it due to FlatStyle property
See this link
http://www.siao2.com/2008/09/26/8965526.aspx
How can I solve this problem?
I am using C# under Windows 7.
解决方案You're getting that value because the control you're checking uses the default system font. If a custom font hasn't been explicitly specified, these are precisely the values that you should expect.
The WM_GETFONT
message will always return 0 (or NULL) if the default system font is being used to draw the control's text. According to the documentation:
The return value is a handle to the font used by the control, or NULL if the control is using the system font.
Likewise for the GetTextFace
function. In this case, the documentation for the related WM_SETFONT
message provides some clarifying insight: If the wParam
value is NULL, the control will use the default system font to draw its text.
Obviously you haven't changed the font, or at least Windows doesn't think you have. You mention that you're trying to get the font of other applications—how are you changing the font that is used to draw the text on controls in another application?
If you change your default system font, the behavior explained above still applies, regardless of whether or not what you see on your screen is different.
If you're sending the WM_SETFONT
message, you might have forgotten to tell the control it should redraw itself. Again, pulling from the documentation linked to above:
lParam
The low-order word of lParam specifies whether the control should be redrawn immediately upon setting the font. If this parameter is TRUE, the control redraws itself.