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

    How to get hdc and pixel from window with sendmessage?

    Hello, I want to get some pixels of a window that doesn't belong to my application, I mean that I have a window of a program and from my own application I want to get pixels of that window.

    Well, so I need the hdc and perhaps using that hdc I could get the pixels I want. But I think that perhaps windows doesn't allow you using functions like getDC(hwnd) or getPixel(hdc, x, y) because that window and my application are part of different applications, does it?

    So probaly I'll have to use sendmessage winapi function... I'm a bit lost.

    There is an application that gives you lots of information about windows only pointing the mouse over the window. It gives hwnd, hdc, name of the class, text... This application is WinID, it is free if you don't use it with commercial purposes. Well if WinID can do it there is at leas one solution.

    Well I use VB.NEt but the WinApi works the same so probably I can translate the calls to VB.NET. Someone knows something about it?

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

    Re: How to get hdc and pixel from window with sendmessage?

    Is this what you are looking for ?
    CodeGuru: Capturing Windows Regardless of Their Z-Order

  3. #3
    Join Date
    Aug 2007
    Posts
    12

    Re: How to get hdc and pixel from window with sendmessage?

    Thanks kirants, the solution you posted is quite similar the one I found in other web.

    Well I think there must be at leas one way to get the pixel directly from the window, but for the moment I don't know how, so this solution probably could work for me, it stores a copy of the window in a image file so now I have to manage that file to get the pixel I want. I hope not to have many problems

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

    Re: How to get hdc and pixel from window with sendmessage?

    Quote Originally Posted by fonx
    Thanks kirants, the solution you posted is quite similar the one I found in other web.

    Well I think there must be at leas one way to get the pixel directly from the window, but for the moment I don't know how, so this solution probably could work for me, it stores a copy of the window in a image file so now I have to manage that file to get the pixel I want. I hope not to have many problems
    If you want to get single pixel from a window look ::GetPixel() api.

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  5. #5
    Join Date
    Aug 2007
    Posts
    12

    Re: How to get hdc and pixel from window with sendmessage?

    Quote Originally Posted by golanshahar
    If you want to get single pixel from a window look ::GetPixel() api.
    But GetPixel only works if you want to capture pixels of a window of your own application, am I wrong Golanshahar?

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

    Re: How to get hdc and pixel from window with sendmessage?

    Quote Originally Posted by fonx
    But GetPixel only works if you want to capture pixels of a window of your own application, am I wrong Golanshahar?
    What you can do is use ::GetPixel() to get pixel from screen dc (0) but for that you have to be sure that the window you want to get the pixel from is visible on the screen.

    here is a sample:
    Code:
      HDC hdc = ::GetDC(0);
      HWND hwnd = ::FindWindow(0,"Calculator");
      if (hwnd )
      {
        RECT r;
        ::GetWindowRect(hwnd,&r);
        COLORREF color = ::GetPixel(hdc,r.left+1,r.top+1);
      }
      ::ReleaseDC(0,hdc);
    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  7. #7
    Join Date
    Aug 2007
    Posts
    12

    Re: How to get hdc and pixel from window with sendmessage?

    Golanshahar, your solution gets a pixel from the screen (dc=0), the problem of this is that I need to know the exact position of the window on the screen, isn't it? So I have other problem which is to know where is the window that I want.

    With the first solution I capture only the window that I want, so I only have to get the pixel I want, I know the exact coordenates x and y where it is. Well I'm using this solution for the moment. Perhaps there is a better solution, but for the moment I'll use this because I want to have the first release (the application is only for me) as soon as possible and then I'll try to improve al the features to the best solution.

    This solution has a problem:

    The window has to be visible to capture it. If another pop up window appears my solution doesn't work ... For instance my free antivirus throws popup windows....and the application which throws the windows that I want to capture also throws popup messages...

    I am wondering If it is possible to know If the window I want to capture is behind other window and if so order the window to move to front position (my english is quite poor, do you understand me?) using the WinApi and sendmessage....is it possible?

  8. #8
    Join Date
    Aug 2007
    Posts
    12

    Re: How to get hdc and pixel from window with sendmessage?

    Quote Originally Posted by golanshahar
    Code:
      HWND hwnd = ::FindWindow(0,"Calculator");
    Well perhaps I'm wrong, your solution find the window that matches with tittle...but I still need that window visible on the screen... :S

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

    Re: How to get hdc and pixel from window with sendmessage?

    Quote Originally Posted by fonx
    Golanshahar, your solution gets a pixel from the screen (dc=0), the problem of this is that I need to know the exact position of the window on the screen, isn't it? So I have other problem which is to know where is the window that I want.
    Yes you need the window to be visible and not covered by any other window and you need to get it bound rect, (I mentioned it in my post )

    This solution has a problem:

    The window has to be visible to capture it. If another pop up window appears my solution doesn't work ... For instance my free antivirus throws popup windows....and the application which throws the windows that I want to capture also throws popup messages...

    I am wondering If it is possible to know If the window I want to capture is behind other window and if so order the window to move to front position (my english is quite poor, do you understand me?) using the WinApi and sendmessage....is it possible?
    In that case read kirants post he gave you an article that can help you capture also "covered" windows

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  10. #10
    Join Date
    Aug 2007
    Posts
    12

    Re: How to get hdc and pixel from window with sendmessage?

    Quote Originally Posted by golanshahar
    In that case read kirants post he gave you an article that can help you capture also "covered" windows
    Well I have to admidt that yesterday I learned what is z-order....I didn't know what it was...

    I'm looking this solution, it looks good, now I have (if it's possible) to translate from c++ to vb.net ...

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

    Re: How to get hdc and pixel from window with sendmessage?

    Quote Originally Posted by fonx
    Well I have to admidt that yesterday I learned what is z-order....I didn't know what it was...

    I'm looking this solution, it looks good, now I have (if it's possible) to translate from c++ to vb.net ...
    Good luck

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  12. #12
    Join Date
    Aug 2007
    Posts
    12

    Re: How to get hdc and pixel from window with sendmessage?

    Hey Golan I got it !!

    I dindn't have to translate your code to VB.NET, just some little changes in my code. Your article put me in the correct way, now I use PrintWindow api call and it works fine, it captures the window even if the window is totally hidden.

    Thanks for the help in this point. My application still needs many hours of work and I have more doubts, so I'll continue around CodeGuru Forums for a long time

    Once more thanks

  13. #13
    Join Date
    Jul 2009
    Posts
    2

    Re: How to get hdc and pixel from window with sendmessage?

    sry for bumping this thread but i have the same problem, could anyone help me get a screenshot from a program that is minimized. Fonx i read u got it fixed u mind sharing?

    i already tried using getpixel api to get pixel from programs in the background but it seems getpixel only works if ur window is focussed. And now i have found that printwindow might be the solution. Could anyone help me out?


    cheers!

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

    Re: How to get hdc and pixel from window with sendmessage?

    Quote Originally Posted by soul444 View Post
    sry for bumping this thread but i have the same problem, could anyone help me get a screenshot from a program that is minimized. Fonx i read u got it fixed u mind sharing?

    i already tried using getpixel api to get pixel from programs in the background but it seems getpixel only works if ur window is focussed. And now i have found that printwindow might be the solution. Could anyone help me out?


    cheers!

    This article shows how to use ::PrintWindow() however minimized window wont work with it.

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  15. #15
    Join Date
    Dec 2008
    Posts
    114

    Re: How to get hdc and pixel from window with sendmessage?

    Quote Originally Posted by golanshahar View Post
    This article shows how to use ::PrintWindow() however minimized window wont work with it.
    Because this article is wrong.
    You must use a hook (among others methods..) to get it from minimized windows..

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