How to call GetTextMetrics from a C# function?

  • Thread starter Thread starter tavi
  • Start date Start date
T

tavi

Hi all,

I am learning C# now, I write an application and I am playing with the
menus for now. I need to call GetTextMetrics at a point, I need to
know what is the system font, size and so on.
I've been successfully calling other API functions but with this
particular one I've got problems. I wrote something like this:

class PInvoke1App
{
[DllImport("user32.dll")]
static extern BOOL GetTextMetrics(HDC hdc, LPTEXTMETRIC lptm);
}


but it won't compile, it's complaining about HDC. It says unknown type
or something similar. Sorry, I don't remember the exact error message,
I am at work now ;-)

And it seems that if I declare the TEXTMETRIC structure on my cs file,
it will still not complain anymore.

That was easy, but how to declare the HDC type?

I read on MSDN about passing structures to the function, I believe I
could handle this, but I couldn't find any reference to handles and
HDC and I've got no ideeea what is the solution.

Also,if I need to use some constants from windows.h or other header,
how can I add a reference to these headers without declaring each
constant in my cs file?

Thank you!

Tavi
 
tavi, try an IntPtr like this:

[DllImport("gdi32.dll", CharSet = CharSet.Unicode)]
public static extern bool GetTextMetrics(IntPtr hdc, TEXTMETRIC lptm);
 
Thanks, Greg, I just got back from work andI couldn't go to sleep without
trying..
It works, actually the mistake was that I used user32.dll instead of
gdi32.dll. I don't even need to declare the structure, the type is already
declared in the dll!

Thanks again, I appreciate it!


Greg Ewing said:
tavi, try an IntPtr like this:

[DllImport("gdi32.dll", CharSet = CharSet.Unicode)]
public static extern bool GetTextMetrics(IntPtr hdc, TEXTMETRIC lptm);

--
Greg Ewing [MVP]
http://www.citidc.com

tavi said:
Hi all,

I am learning C# now, I write an application and I am playing with the
menus for now. I need to call GetTextMetrics at a point, I need to
know what is the system font, size and so on.
I've been successfully calling other API functions but with this
particular one I've got problems. I wrote something like this:

class PInvoke1App
{
[DllImport("user32.dll")]
static extern BOOL GetTextMetrics(HDC hdc, LPTEXTMETRIC lptm);
}


but it won't compile, it's complaining about HDC. It says unknown type
or something similar. Sorry, I don't remember the exact error message,
I am at work now ;-)

And it seems that if I declare the TEXTMETRIC structure on my cs file,
it will still not complain anymore.

That was easy, but how to declare the HDC type?

I read on MSDN about passing structures to the function, I believe I
could handle this, but I couldn't find any reference to handles and
HDC and I've got no ideeea what is the solution.

Also,if I need to use some constants from windows.h or other header,
how can I add a reference to these headers without declaring each
constant in my cs file?

Thank you!

Tavi
 
Thanks, Greg, I just got back from work andI couldn't go to sleep without
trying..
It works, actually the mistake was that I used user32.dll instead of
gdi32.dll.

Thanks again, I appreciate it!



Greg Ewing said:
tavi, try an IntPtr like this:

[DllImport("gdi32.dll", CharSet = CharSet.Unicode)]
public static extern bool GetTextMetrics(IntPtr hdc, TEXTMETRIC lptm);

--
Greg Ewing [MVP]
http://www.citidc.com

tavi said:
Hi all,

I am learning C# now, I write an application and I am playing with the
menus for now. I need to call GetTextMetrics at a point, I need to
know what is the system font, size and so on.
I've been successfully calling other API functions but with this
particular one I've got problems. I wrote something like this:

class PInvoke1App
{
[DllImport("user32.dll")]
static extern BOOL GetTextMetrics(HDC hdc, LPTEXTMETRIC lptm);
}


but it won't compile, it's complaining about HDC. It says unknown type
or something similar. Sorry, I don't remember the exact error message,
I am at work now ;-)

And it seems that if I declare the TEXTMETRIC structure on my cs file,
it will still not complain anymore.

That was easy, but how to declare the HDC type?

I read on MSDN about passing structures to the function, I believe I
could handle this, but I couldn't find any reference to handles and
HDC and I've got no ideeea what is the solution.

Also,if I need to use some constants from windows.h or other header,
how can I add a reference to these headers without declaring each
constant in my cs file?

Thank you!

Tavi
 
Back
Top