Help screen capturing and drawing a bitmap

Posted on 2007-10-11
Last Modified: 2008-01-09

I wish to take a screenshot of a particular screen window and save it to file from a program.

I have hooked into the program and when the particular screen i want the screen grab of loads ( i do this using the window title ) i grab the screen.

now, this is working and i'm using WM_WINDOWPOSCHANGING as the event to do the creation, however as it looks like the screen hasn't finished drawing when this message is sent i don't get a screen shot with all the components on it. i only get half of them.

Can anyone suggest a message or method for when or where i can grab the screen and it will have finished drawing??

many thanks,

Question by:flynny
    LVL 30

    Expert Comment

    Hi flynny,

    maybe you should use WM_WINDOWPOSCHANGED instead of WM_WINDOWPOSCHANGING - WM_WINDOWPOSCHANGED should be sent after the window has been moved/sized.

    Hope that helps,

    LVL 39

    Expert Comment

    Hi Matt,

    The suggestion of Zoppo wouldn't work if there are more than one WM_WINDOWPOSCHANGING  (therefore WM_WINDOWPOSCHANGED as well). If you do some of the drawing yourself (?), you could call a PostMessage with a private message id (WM_USER + something) to the window yourself. PostMessage means that it was put at end of queue. Then when handling the private message it should grab all  (hopefully).

    Regards, Alex
    LVL 86

    Expert Comment

    Just an idea - everything should be drawn when you call 'WaitForInputIdle()' on the process whose window you want to capture and the call returns...

    Author Comment

    Hi Guys,

    thanks for the replie. As Itsme says catching th WM_WINDOWPOSCHANGED doesn't work.

    ok, itsme in order to use the WM_USER message, i tried PostMessage(hwnd, WM_USER+10, 0, 0);
    where i process the WM_WINDOWPOSCHANGED message, this does call my method however it seems to only be drawing a small pane of the window for some reason. (even though i know the window i have sent is the correct window.

    jkr, if i were to use the waitforidleinput(), would i again for example at the end of my WM_WINDOWPOSCHANGED process be calling a <processname>.WaitForIdleInput() and then calling my bitmap write? would this cause problems as i'm waiting for no iput when im actually processing a message there too? (also how do i get the process name from the hwnd will GetWindowThreadProcessId do th trick?)

    many thanks guys.

    LVL 39

    Accepted Solution

    >>>> however it seems to only be drawing a small pane of the window for some reason.
    it maybe means that between the WM_WINDOWPOSCHANGED and handling of the WM_USER + 10 there were more PostMessage calls which were queued later. You can come out of that timing issues by invoking a timer, e. g. by calling SetTimer(123, 1000);   what invokes a timer with id 123 in about 1000 milliseconds (that should enough to let happen any drawing). You would handle the timer in OnTimer if using MFC

       void MyDialog::OnTimer(UINT id)
              if (id == 123)
                     // get screenshot

    or by passing a  callback function to SetTimer as 3rd argument. In the latter case your callback function was called after time had expired. Note, you need to kill timer after handled as it is a periodic timer.

    Regards, Alex

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
    This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now