Handle in a console app
I have a console app in .net that I'm doing some processing of fonts. I'm using Win32APIs for this and one of them requires a device context for loading 开发者_JAVA技巧a font - actually a IntPtr hdc = GetDC(handle of screen element)
. Obviously, my app doesn't have these handles as it's a console app. Is there a way to get around this?
In win32 GetDC( null ) should give a context back (for the entire screen)
MSDN
So you should be able to do something like
IntPtr hdc = GetDC( null );
if( hdc == null )
{
OopsError();
}
GetConsoleWindow()
(http://msdn.microsoft.com/en-us/library/ms683175.aspx):
Retrieves the window handle used by the console associated with the calling process.
Alternatively, passing NULL might work. From the GetDC()
docs (http://msdn.microsoft.com/en-us/library/dd144871.aspx):
A handle to the window whose DC is to be retrieved. If this value is NULL, GetDC retrieves the DC for the entire screen.
IntPtr hdc = GetDC(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle);
works in .Net console apps just fine.
I don't believe null
will work in .Net as it kicks Error, Argument: cannot convert from '<null>'
to 'System.IntPtr'
精彩评论