Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. how to create a temporary DC using ATL without a Parent Window?

how to create a temporary DC using ATL without a Parent Window?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++graphicshelptutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    ThinkingPrometheus
    wrote on last edited by
    #1

    hiho@ll what i have: a HBITMAP handle no parent window a browser helper object, which should do the job what i need: the upper left pixel of the HBITMAP handle what i think how it could work: (well it doesn't work ;-)) CWindow *tmpwin=new CWindow(::GetDesktopWindow()); // i don't have a parent HDC hdc=tmpwin->GetDC(); SelectObject(hdc,hbmp); // select the HBITMAP COLORREF cf=GetPixel(hdc,0,0); return cf; what i get: the return RGB Values are 255,255,255 using GetLastError after the GetPixel and before Return i get ErrorCodes 6 and 8 (invalid handle, not enough space) i'm calling the method a few times, so i get some error codes and not just one question: how can hdc be an invalid handle, if i use GetDesktopWindow? how can i get "not enough space" for a bitmap which is 16x16? a workaround i'm thinking of, but i don't know how to do: i make my own window, use selectobject on my window, get the pixel, and destroy the window question: how can i make my own window? (some code would be great) and how do i destroy the window? btw, the only purpose of this window will be to get a valid DC to select the HBITMAP and get the pixel thx@ll

    B 1 Reply Last reply
    0
    • T ThinkingPrometheus

      hiho@ll what i have: a HBITMAP handle no parent window a browser helper object, which should do the job what i need: the upper left pixel of the HBITMAP handle what i think how it could work: (well it doesn't work ;-)) CWindow *tmpwin=new CWindow(::GetDesktopWindow()); // i don't have a parent HDC hdc=tmpwin->GetDC(); SelectObject(hdc,hbmp); // select the HBITMAP COLORREF cf=GetPixel(hdc,0,0); return cf; what i get: the return RGB Values are 255,255,255 using GetLastError after the GetPixel and before Return i get ErrorCodes 6 and 8 (invalid handle, not enough space) i'm calling the method a few times, so i get some error codes and not just one question: how can hdc be an invalid handle, if i use GetDesktopWindow? how can i get "not enough space" for a bitmap which is 16x16? a workaround i'm thinking of, but i don't know how to do: i make my own window, use selectobject on my window, get the pixel, and destroy the window question: how can i make my own window? (some code would be great) and how do i destroy the window? btw, the only purpose of this window will be to get a valid DC to select the HBITMAP and get the pixel thx@ll

      B Offline
      B Offline
      Bob Stanneveld
      wrote on last edited by
      #2

      Hello, Me again! I was searching through MSDN and found the following functions usefull for your problem:* GetDC()[^] for retrieving the DC of the desktop.

      • CreateCompatibleDC()[[^](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wceshellui5/html/wce50lrfCreateCompatibleDC.asp "New Window")] for creating a memory DC
      • [`SelectObject()`](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/devcons_9v3o.asp)[[^](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/devcons_9v3o.asp "New Window")] for placing the bitmap in the DC (Note that the bitmap can only be selected in one DC at a time!) Using these functions, one can do the following: HDC hMemDC = ::CreateCompatibleDC(GetDC(NULL)); HGDIOBJ hOldObj = ::SelectObject(hMemDC, hbmp); COLORREF cf = GetPixel(hMemDC, 0, 0); // release resources ::SelectObject(hMemDC, hOldObj); ::DeleteDC(hMemDC); return cf; Hope this helps :-D P.S. Some of your problems could be: tmpwin has no DC, which results in invalid handle errors, hbmp is already selected in a DC, hbmp is not created correctly,.. _Behind every great black man...             ... is the police. - Conspiracy brother_ [Blog](http://www.codeproject.com/script/profile/whos_who.asp?id=418947&id=418947&df=100&forumid=27215&exp=6&select=1050632)[[^](http://www.codeproject.com/script/profile/whos_who.asp?id=418947&id=418947&df=100&forumid=27215&exp=6&select=1050632)]
      T 1 Reply Last reply
      0
      • B Bob Stanneveld

        Hello, Me again! I was searching through MSDN and found the following functions usefull for your problem:* GetDC()[^] for retrieving the DC of the desktop.

        • CreateCompatibleDC()[[^](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wceshellui5/html/wce50lrfCreateCompatibleDC.asp "New Window")] for creating a memory DC
        • [`SelectObject()`](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/devcons_9v3o.asp)[[^](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/devcons_9v3o.asp "New Window")] for placing the bitmap in the DC (Note that the bitmap can only be selected in one DC at a time!) Using these functions, one can do the following: HDC hMemDC = ::CreateCompatibleDC(GetDC(NULL)); HGDIOBJ hOldObj = ::SelectObject(hMemDC, hbmp); COLORREF cf = GetPixel(hMemDC, 0, 0); // release resources ::SelectObject(hMemDC, hOldObj); ::DeleteDC(hMemDC); return cf; Hope this helps :-D P.S. Some of your problems could be: tmpwin has no DC, which results in invalid handle errors, hbmp is already selected in a DC, hbmp is not created correctly,.. _Behind every great black man...             ... is the police. - Conspiracy brother_ [Blog](http://www.codeproject.com/script/profile/whos_who.asp?id=418947&id=418947&df=100&forumid=27215&exp=6&select=1050632)[[^](http://www.codeproject.com/script/profile/whos_who.asp?id=418947&id=418947&df=100&forumid=27215&exp=6&select=1050632)]
        T Offline
        T Offline
        ThinkingPrometheus
        wrote on last edited by
        #3

        hiho tried your code the same problem tried to make my own window with CreateWindow("STATIC",NULL,WS_CHILD|WS_VISIBLE,0,0,16,16,NULL,(HMENU)NULL,g_hThisModule,NULL); the same problem hmm 1. how do i know if the window has a DC? 2. how do i check if hbmp is already selected? (hmm, i don't think it is, but how do i check, just to be sure?)

        B 1 Reply Last reply
        0
        • T ThinkingPrometheus

          hiho tried your code the same problem tried to make my own window with CreateWindow("STATIC",NULL,WS_CHILD|WS_VISIBLE,0,0,16,16,NULL,(HMENU)NULL,g_hThisModule,NULL); the same problem hmm 1. how do i know if the window has a DC? 2. how do i check if hbmp is already selected? (hmm, i don't think it is, but how do i check, just to be sure?)

          B Offline
          B Offline
          Bob Stanneveld
          wrote on last edited by
          #4

          ThinkingPrometheus wrote: 1. how do i know if the window has a DC? All windows have a DC, as long as they are visible. I don't know if they have a DC when they are invisible. ThinkingPrometheus wrote: 2. how do i check if hbmp is already selected? (hmm, i don't think it is, but how do i check, just to be sure?) From where do you get the handle? I think the problem lies there, since all the other windows and dc's should be valid. You can validate this by loading your own bitmap from disk and see what that gives you. Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups