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

Thread: DC not working

  1. #1
    Join Date
    Feb 2008
    Posts
    42

    DC not working

    I want to use one of my programs to write on top of the window for another of my programs. I'll try to be as clear as I can. This is a sample of the code I used.

    HANDLE hwindow;
    HDC hdc;

    if ((hwindow = FindWindow(NULL, TEXT("Target Window"))) != NULL) {
    hdc = GetDC (hwindow);

    TextOut (hdc, 0, 0, TEXT ("Hello Window"), 12);

    ReleaseDC(hwindow, hdc);
    }

    Now I open the program with the caption "Target Window" and low and behold, the words "Hello Window" are no where to be seen. I am 100% sure that FindWindow() did find the window with the right caption. What I do not understand is this: What is the actual coordinate system (map mode) of hdc? Is hdc a device context for the whole window or for the window's client area, and whichever one it's for, how do I get the DC for the other? And most importantly of course, why do I not see the words "Hello Window" anywhere on top of the "Target Window"?

    Thank you!

  2. #2
    Join Date
    Jan 2008
    Posts
    178

    Re: DC not working

    It's so basic that you have to read Petzold's book and painting part...

  3. #3
    Join Date
    Feb 2008
    Posts
    42

    Re: DC not working

    I'm sorry if I didn't find that statement to be at all helpful. Do you actually know what the problem is? At least point me to a specific topic --- "painting part" is very obscure, and who knows if I have that book.
    Last edited by shadybyte; February 26th, 2008 at 09:03 AM.

  4. #4
    Join Date
    Nov 2007
    Posts
    129

    Smile Re: DC not working

    Quote Originally Posted by shadybyte
    I want to use one of my programs to write on top of the window for another of my programs. I'll try to be as clear as I can. This is a sample of the code I used.

    HANDLE hwindow;
    HDC hdc;

    if ((hwindow = FindWindow(NULL, TEXT("Target Window"))) != NULL) {
    hdc = GetDC (hwindow);

    TextOut (hdc, 0, 0, TEXT ("Hello Window"), 12);

    ReleaseDC(hwindow, hdc);
    }

    Now I open the program with the caption "Target Window" and low and behold, the words "Hello Window" are no where to be seen. I am 100% sure that FindWindow() did find the window with the right caption. What I do not understand is this: What is the actual coordinate system (map mode) of hdc? Is hdc a device context for the whole window or for the window's client area, and whichever one it's for, how do I get the DC for the other? And most importantly of course, why do I not see the words "Hello Window" anywhere on top of the "Target Window"?

    Thank you!
    Where's the rest of your code?
    This is not a complete program.
    Where's the WinMain?
    Where do you handle Windows messages?
    This is what fred100 meant by needing Charles Petzold's book "Programming Windows".
    I need to see how and where you create your window. Oh, you are writing to a window previously created.
    Well, this is not a complete program anyway and I need to see more to help you.

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

    Re: DC not working

    Are you sure a valid handle was returned?

    GetDC returns a DC to the window's client area, or NULL if it fails.

    Viggy

  6. #6
    Join Date
    Feb 2008
    Posts
    42

    Re: DC not working

    The reason I'm sure that FindWindow() returns a valid handle is because I have other code in the statement below that does take place only when the "Target Window" exists.

    if ((wnd = FindWindow (NULL, TEXT ("Target Window"))) != NULL) {
    .........
    .........
    }

    I am not close the computer that I work on at the moment, but I promise that messages are taken care of in a legitimate manner. But then again this code (which is the only thing that doesn't work) is very straightforward, I might be missing something rather important. So after being sure that FindWindow() did not return a NULL value, I call --- hdc = GetDC(wnd) --- immediately after, and then call the TextOut statement ---- TextOut (hdc, 0, 0, TEXT ("Hello Window"), 12) immediately after that. This is a very simple, yet unorthodox program, that tests the power that I can have over a window that I did not create. So I choose a target window, this window is simply a folder I created entitled "Target Window". When I open the folder (meaning the window now exists) everything happens except the TextOut statement. I would also like to know if I can cause the window (folder) to repaint itself among other things. (It's like having telepathic powers over another window, and would show how two programs can communicate uniquely)
    Last edited by shadybyte; February 26th, 2008 at 06:33 PM.

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

    Re: DC not working

    Yes, but just because FindWindow returns a valid handle, that does not mean GetDC returns a valid DC.

    Viggy

  8. #8
    Join Date
    Feb 2008
    Posts
    42

    Talking Re: DC not working

    Wow, that's really nice to know! In which cases does this happen? That's definitely the root of my problem then.

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