can i get the font information from Graphics System.Drawing.Graphics in c#
i get the Graphics from
Graphics g= System.Drawing.Graphics.FromHwnd(button1.Handle);
can i get the font information from 开发者_开发知识库this Graphics
i was try to get a font by using GetTextFace api function but it return "system" it mean default font in OS
and i was try to use SendMessage(button1.Handle, WM_GETFONT, 0, 0); bu it return me 0 also it is mean default font in OS
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
thanks
What you're throwing together, as described in the latest comment, seems somewhat like a giant, messy hack. But I don't really know if I understand well enough to offer a better design.
Since you apparently seem to have some experience with the Windows API, you might take a look at Raymond Chen's blog post on How to retrieve text under the cursor (mouse pointer), as it sounds quite germane to what you're trying to accomplish. The code sample provided is not in C#, but it shouldn't be too difficult to follow the logic (specifically of RecalcText
) to reimplement it using the same API calls in your preferred C#. Essentially, he walks you through taking advantage of Microsoft Active Accessibility technology that is designed to expose information about the objects on the screen for reading devices and other similar technology, rather than trying to reinvent the wheel yourself.
Otherwise, continuing down the path that you've already begun, it sounds to me like you need to read up on the GetTextExtentPoint32
function. Once you've sent the WM_GETTEXT
message as I suggested above to get the text that is displayed on the control, you can get the position of the mouse relative to the control (using a combination of GetCursorPos
and WindowFromPoint
), and check that against the width and height of the text string returned by GetTextExtentPoint32
.
Finally, to answer the question you explicitly asked concerning how to determine which font is in use to draw a specified control, you need to recall the knowledge gleaned from my previous answer regarding the default system font. As you discovered, a control will either tell you which font it is using to draw its text, or it will tell you that it's using the default system font. The simple question then becomes, how do I know which font is the default system font?
That's a pretty straightforward question to answer, and the Windows API provides functionality to do just that. Specifically, you need to call the SystemParametersInfo
function and pass in the SPI_GETNONCLIENTMETRICS
flag. This will cause the function to fill a NONCLIENTMETRICS
structure, which among other things, will tell you which font is in use as the default system font. Specifically, you'll be interested in the lfMessageFont
member of the structure, which is a pointer to a LOGFONT
structure that defines the attributes of the font used on message boxes and controls.
Again, see a post on Raymond Chen's blog explaining the history of SYSTEM_FONT
, and his suggestion to use the SystemParametersInfo
function to retrieve information about the actual font in use.
You can use Graphics.GetHDC
and then use the HDC
to get the font information throught a PInvoke call to native Win API.
EDIT
Look for GetCurrentObject
: http://msdn.microsoft.com/en-us/library/dd144869(VS.85).aspx
This function will let you get an HFONT
from the HDC
. Then you can use Font.FromHfont
to get the .NET Font
object.
If the Graphics
object does have a font, you'll be able to get it through this API. Note that you need to release unmanaged resources that you would allocate (the HDC
, and the HFONT
).
精彩评论