CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: Global HDC?

  1. #1
    Join Date
    Feb 2004
    Location
    Texas, USA
    Posts
    1,206

    Global HDC?

    Is there a way to be able to write to a client rect from anywhere in my application? I'm guessing create a global memory DC?

    How would I create this and write to it? I basically ask because I need a way to display text to the HDC from a different function

    Can I get some small example code?

  2. #2
    Join Date
    Dec 2004
    Location
    Ann Arbor, MI
    Posts
    281

    Re: Global HDC?

    It sounds like you might be best off looking into a singleton class that can manage the lifetime of the hDC.

    Header:
    Code:
    class CMyDCHolder
       {
       public:
       	static CMyDCHolder& Instance(void) {return(m_Inst);}
       
       	void InitializeDC(void);
       	HDC GetDC(void);
       	void DestroyDC(void);
       
       protected:
       	HDC m_hDC;
       	bool m_bInitialized;
       	
       	CMyDCHolder();
       	CMyDCHolder(const CMyDCHolder&){}
       	
       	static CMyDCHolder m_Inst;
       };
    implementation:
    Code:
    CMyDCHolder CMyDCHolder::m_Inst;
       
       CMyDCHolder::CMyDCHolder()
       {
       	m_bInitialized = false;
       	m_hDC = NULL;
       }
       
       void CMyDCHolder::InitializeDC(void)
       {
       	//initialize  m_hDC
       	//set m_bInitialized to true
       }
       
       HDC CMyDCHolder::GetDC(void)
       {
       	ASSERT(m_bInitialized);
       	return(m_hDC);
       }
       
       void CMyDCHolder::DestroyDC(void)
       {
       	//DC cleanup
       	//set m_bInitialized to false
       }
    Access functions like this:
    Code:
    	CMyDCHolder::Instance().InitializeDC();
      	HDC hDC = CMyDCHolder::Instance().GetDC();
      	CMyDCHolder::Instance().DestroyDC();
    --EJMW

  3. #3
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Global HDC?

    Well, if you have the handle to the window you want to draw on, just use "HDC GetDC(HWND)".

    You need some kind of window handle in order to get a DC, otherwise, where are you going to draw? Actually, I believe if you pass NULL for the HWND param, you get the DC for the desktop (and can draw anywhere you like!).

    Viggy

  4. #4
    Join Date
    Dec 2004
    Location
    Ann Arbor, MI
    Posts
    281

    Re: Global HDC?

    Quote Originally Posted by MrViggy
    Well, if you have the handle to the window you want to draw on, just use "HDC GetDC(HWND)".
    Well, he could also be drawing to a memory DC or enhanced metafile
    --EJMW

  5. #5
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Global HDC?

    Quote Originally Posted by ejmw
    Well, he could also be drawing to a memory DC or enhanced metafile
    True. In that case, the memory DC needs to be "based" on the window that he intends to draw on.

    Otherwise, the memory DC may not be compatible with the DC he bit blits to.



    Viggy

  6. #6
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: Global HDC?

    Quote Originally Posted by ejmw
    Well, he could also be drawing to a memory DC or enhanced metafile
    Well, of course he can share it: global variable, singleton class there are many ways. Your solution is one (the better) solution.

    The only thing you should know is:

    Quote Originally Posted by MSDN
    Note that the handle to the DC can only be used by a single thread at one time.
    @Mr Viggy: You are right:

    Quote Originally Posted by MSDN
    Handle to the window whose DC is to be retrieved.If this value is NULL, GetDC retrieves the DC for the entire screen.
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  7. #7
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Global HDC?

    I knew there was a reason that storing a DC (handle or CDC) was a bad thing. They are thread dependent (i.e can't access the same handle from two or more threads). Actually, I believe that this goes for any handle, not just DC's (any Windows resource handle).

    Viggy
    Last edited by MrViggy; March 4th, 2005 at 03:43 PM. Reason: Clarified "any handle"...

  8. #8
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Global HDC?

    Quote Originally Posted by MrViggy
    They are thread dependent (i.e can't access the same handle from two or more threads). Actually, I believe that this goes for any handle, not just DC's (any Windows resource handle).
    HWNDs aren't thread dependent. I am not sure about HDCs though. Haven't tried it..

  9. #9
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: Global HDC?

    Quote Originally Posted by MrViggy
    I knew there was a reason that storing a DC (handle or CDC) was a bad thing. They are thread dependent (i.e can't access the same handle from two or more threads). Actually, I believe that this goes for any handle, not just DC's (any Windows resource handle).

    Viggy
    They are not local to the corresponding thread. This just means that is impossible that two threads open a DC and draw on it simultaneously. One has to "ReleaseDC" on it, otherwise the second one cannot draw it.
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  10. #10
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Global HDC?

    Quote Originally Posted by NoHero
    They are not local to the corresponding thread. This just means that is impossible that two threads open a DC and draw on it simultaneously. One has to "ReleaseDC" on it, otherwise the second one cannot draw it.
    Duh, right! I was thinking of MFC objects!

    Viggy

  11. #11
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: Global HDC?

    And by the way: GDI objects are not thread local and not covered by the user address space. So you can easily modify GDI objects of another application from your application. The only problem, without a loop through the GDI object's table (from 0xFFFF to 0x4FFFFF) you won't find them.
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  12. #12
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Global HDC?

    Quote Originally Posted by NoHero
    And by the way: GDI objects are not thread local and not covered by the user address space. So you can easily modify GDI objects of another application from your application. The only problem, without a loop through the GDI object's table (from 0xFFFF to 0x4FFFFF) you won't find them.
    Right. There's only one display!



    Viggy

  13. #13
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: Global HDC?

    Quote Originally Posted by MrViggy
    Right. There's only one display!



    Viggy
    Well, that is incorrect. Sorry I have missed that too: If you are using CreateDesktop() you create a new desktop which has a new set of GDI objects. Only two applications that belongs to the same desktop can modify their GDI objects one another. But multiple displays does not provide that protection.

    I shall not be so lazy in writing.
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  14. #14
    Join Date
    Feb 2004
    Location
    Texas, USA
    Posts
    1,206

    Re: Global HDC?

    hmmm... so, how can I write to my DC from a separate thread?

    I have 1 thread which simply does nothing but run my message loop, and another does the actual TextOut() on the HDC. I have created a window class, so my window is created with this class and all HDC's (memory and regular) are created/maintained/removed via this class.

    It is a requirement of a lab of mine that I must create 40 windows, each with its own thread, and each thread must textout() to that specific window... this means the only possible way I can process messages is by having a 41th thread to run my message loop...

    Also I notice my message loop not getting messages when I create windows from a separate thread..................... Multithreading is such a b***h lol

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