Click here to Skip to main content
16,004,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
static HDC  hdcScreen ;
 
hdcScreen = CreateDC (TEXT ("DISPLAY"), NULL, NULL, NULL) ;
 
//I saved a screen-hdc in static.
//I use it whenever I want, without creating again.


static COLORREF cr ;
 
GetCursorPos (&pt) ;
cr = GetPixel (hdcScreen, pt.x, pt.y) ;
 
//I used screen-hdc to pick color value


but when Another program called LockWindowUpdate (hwndScreen), "hdc-screen" isn't useable again in my program.

I want know How to determines if a hdc is useable , before I used it.
Posted

The display is a common DC, so you can't expect to hold it infinitely.

See: http://msdn.microsoft.com/en-us/library/dd144871(v=vs.85).aspx[^]

So use
C#
HDC hdcScreen = GetDC(NULL);
// Do you stuff
ReleaseDC(hdcScreen);

Regards
Espen Harlinn
 
Share this answer
 
Comments
Manfred Rudolf Bihy 31-Dec-10 6:30am    
Good one! 5+
Espen Harlinn 31-Dec-10 7:03am    
Thanks :)
thank your answer very much Espen

I googled MSDN and watched some text

screen-DC and print-DC and so on are common-DC

DC of window you created is private-DC

and GetDC to get DC from Cache(windows memory)

when it is out of all space of Cache

the next GetDC will alloc DC-memory from heap of your program,

till all available space of memory is taken.

but DC be gotten from heap , will be fail to release.

this is why you have to Release a DC

So I tried to get 200 screen-hdc at one time, and they worked pretty well
int i ;
static HDC hdc[200] ;
 
for (i = 0 ; i < 200 ; i++)
     hdc[i] = GetDC (NULL) ;
 
//I drawed 200 Rectangles on Screen 
for (i = 0 ; i < 200 ; i++) 
     Rectangle (hdc[i], 0, 0, 500 - i * 2, 500 - i * 2) ;
   
for (i = 0 ; i < 200 ; i++)
     if (!ReleaseDC (NULL, hdc[i]))
         MessageBox (hwnd, TEXT ("failed to release"), NULL, MB_OK) ;

difference between common-DC and private-DC

attributes of common-DC you get every time are alway unchanged.

but private-DC could remember attributes modifed by last time.

arosed My problem isn't out of Cache

it is LockWindowUpdate(hwndScreen) (it will disable or enable hwndscreen)
or the screen resolution has been changed

my hdc isn't available anymore
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900