tags:

views:

47

answers:

3

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 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?

+3  A: 

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();
}
BioBuckyBall
+1  A: 

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.

Michael Burr
+1  A: 

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'

Otaku
thanks, works well.
WinnerWinnerChickenDinner