CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Jun 2009
    Posts
    118

    GetPixel() in Fullscreen mode

    Code:
            HDC myhdc;
    	HWND myhwnd;
    	DWORD color;
            int r, g, b;
    	
    	myhwnd= GetForegroundWindow();
    	myhdc= GetWindowDC(myhwnd);
    
    	POINT lp;
    	GetCursorPos(&lp);
    
    	color = GetPixel(myhdc, lp.x, lp.y);
    
    	r = GetRValue(color );
    	g = GetGValue(color );
    	b = GetBValue(color );
    	ReleaseDC(myhwnd,myhdc);
    
            FillRect(10,10,10,10, r,g,b,255);
    This works fine in window mode, I know it has something to do with getting the DC but Im not quite sure how I would make it work for fullscreen or make it cross compatible between fullscreen/window modeif needed.

    Thanks :]

  2. #2
    Join Date
    Mar 2006
    Posts
    151

    Re: GetPixel() in Fullscreen mode

    Um... when is this code getting executed? This looks like Win32 code rather than MFC code (?). You rarely want to do any graphics work except in response to a WM_PAINT message, in which case you'll be able to get the device context by calling BeginPaint.

    Otherwise, you can call GetDC using the HWND you got from CreateWindow, but you shouldn't be writing to the screen outside of responding to WM_PAINT. I would put the GetPixel in there as well and store the value for later use if that's what you need.

  3. #3
    Join Date
    Jun 2009
    Posts
    118

    Re: GetPixel() in Fullscreen mode

    Its written in endscene in direct x

    I write out the rgb values and they are messed. its not just the drawing
    Last edited by RogerThat123; October 27th, 2009 at 11:44 PM.

  4. #4
    Join Date
    Mar 2006
    Posts
    151

    Re: GetPixel() in Fullscreen mode

    I know almost nothing about DirectX, but from the little bit I've toyed with it, isn't there a call to CreateWindow at the beginning of the program to create an always-on-top window? You should be able to get the HWND from that and thereby its DC.

    Hopefully someone knowing more about DirectX can help. Sorry. :-(

  5. #5
    Join Date
    Jun 2009
    Posts
    118

    Re: GetPixel() in Fullscreen mode

    Well it works in windowed mode, thats the thing ? So i have no idea why that would affect it,

  6. #6
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: GetPixel() in Fullscreen mode

    Since it's DirectX. The better solution would be to avoid using GDI altogether and retrive the pixel you need directly from the DirectX canvas you used to draw the image on.

    GDI's GetPixel is terribly terribly bad performance, especially when Desktop Composition is enabled. It'll just ruin any kind of performance you're trying to achieve.

    Then again, it's fairly safe to say that any kind of "get pixel" approach to just about anything is most likely already ridden by poor design issues. There's very little need to ever use GetPixel or similar in a proper graphics program.

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: GetPixel() in Fullscreen mode

    Also note that while full screen DirectX needs you to make a top level window. Even obtaining a handle to that window does not guarantee you can read pixel values from DirectX.
    Acceleration features in the graphic card may circumvent the GDI and the window entirely and accelerate screen updates by writing to the videoframe buffer directly.

  8. #8
    Join Date
    Jun 2009
    Posts
    118

    Re: GetPixel() in Fullscreen mode

    Well im not concerned about performance really. Its for a project.

  9. #9
    Join Date
    Nov 2007
    Posts
    613

    Re: GetPixel() in Fullscreen mode

    To get a DC for the entire screen you need the ::GetDCEx from the Win32 (not the one from the MFC).

  10. #10
    Join Date
    Jun 2009
    Posts
    118

    Re: GetPixel() in Fullscreen mode

    TargethDC = GetDCEx (TargetHwnd, NULL, NULL);

    sorta works, doesnt give the correct color but it does change color at least, the color sort of resembles what its over sometimes but not a lot.

    Its ok to pass NULL Im assuming

  11. #11
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: GetPixel() in Fullscreen mode

    The above probably won't work as expected if desktop composition is enabled (in Vista/ Win7).

    you can force a read on a composed pixel by requesting a screen DC (not a window DC)
    Code:
    CDC dcScreen;
    dcScreen.CreateDC(_T("DISPLAY"), NULL, NULL, NULL);
    But expect this to be slowing down things a lot. Depending on your videocard, this could be as bad as slowing down the framerate of your DirectX program by a factor 10. If you need to read several pixels, you probably want to look for alternate solutions.

  12. #12
    Join Date
    May 2005
    Posts
    4,954

    Post Re: GetPixel() in Fullscreen mode

    Quote Originally Posted by RogerThat123 View Post
    Code:
            HDC myhdc;
    	HWND myhwnd;
    	DWORD color;
            int r, g, b;
    	
    	myhwnd= GetForegroundWindow();
    	myhdc= GetWindowDC(myhwnd);
    
    	POINT lp;
    	GetCursorPos(&lp);
    
    	color = GetPixel(myhdc, lp.x, lp.y);
    
    	r = GetRValue(color );
    	g = GetGValue(color );
    	b = GetBValue(color );
    	ReleaseDC(myhwnd,myhdc);
    
            FillRect(10,10,10,10, r,g,b,255);
    This works fine in window mode, I know it has something to do with getting the DC but Im not quite sure how I would make it work for fullscreen or make it cross compatible between fullscreen/window modeif needed.

    Thanks :]
    Is this what you looking for?
    Code:
    HDC dc = ::GetDC(0); // will get dc to entire screen
    color = GetPixel(dc, lp.x, lp.y);
    ::ReleaseDC(0,dc);
    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured