problem using WM_GETFONT with standard flatstyle

落花浮王杯 提交于 2020-01-15 05:45:07

问题


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.


回答1:


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.


来源:https://stackoverflow.com/questions/4569723/problem-using-wm-getfont-with-standard-flatstyle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!