I am trying to get the font of button in other applications.
When I try WM_GETFONT
, it returns 0. This my code:
[DllImport("U开发者_如何学编程ser32.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.
精彩评论