Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Using GetForegroundWindow

Hi All,
I am a novice and would appreciate any help you guys can give me here. I want to write a code which will tell me with which window the user is currently interacting. The code I am using is

#include

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
int test;
HWND hwnd;
MSG Msg;
TCHAR szBuf[80];
int nMaxCount;
DWORD d;
int nCmdShow;

while(1)
{
hwnd=GetForegroundWindow();

if(hwnd == NULL)
{
MessageBox(NULL, "No window is currently active!","Error!" MB_ICONEXCLAMATION | MB_OK);
return 0;
}

ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);

test=GetWindowText(hwnd,szBuf,80);

}

}

If I use GetForegroundWindow, I get the handle of the IDE i.e VC++. How do I write a code which will tell me which is the window the user is currently interacting with?. The issue is something like this...anytime a user is interacting with a window, I need to get a handle to that window and then selectively capture some graphics from that window. How do i proceed in this case? . So in this case would I need to run an exe from my main code which will give me the handle to the active window? Any better idea how do I do it? Waiting for replies from the experts out there!! Thanks!!

-confused newbie

Comments

  • stephlstephl Member Posts: 422
    : Hi All,
    : I am a novice and would appreciate any help you guys can give me here. I want to write a code which will tell me with which window the user is currently interacting. The code I am using is
    :
    : #include
    :
    : int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    : LPSTR lpCmdLine, int nCmdShow)
    : {
    : int test;
    : HWND hwnd;
    : MSG Msg;
    : TCHAR szBuf[80];
    : int nMaxCount;
    : DWORD d;
    : int nCmdShow;
    :
    : while(1)
    : {
    : hwnd=GetForegroundWindow();
    :
    : if(hwnd == NULL)
    : {
    : MessageBox(NULL, "No window is currently active!","Error!" MB_ICONEXCLAMATION | MB_OK);
    : return 0;
    : }
    :
    : ShowWindow(hwnd, nCmdShow);
    : UpdateWindow(hwnd);
    :
    : test=GetWindowText(hwnd,szBuf,80);
    :
    : }
    :
    : }
    :
    : If I use GetForegroundWindow, I get the handle of the IDE i.e VC++. How do I write a code which will tell me which is the window the user is currently interacting with?. The issue is something like this...anytime a user is interacting with a window, I need to get a handle to that window and then selectively capture some graphics from that window. How do i proceed in this case? . So in this case would I need to run an exe from my main code which will give me the handle to the active window? Any better idea how do I do it? Waiting for replies from the experts out there!! Thanks!!
    :
    : -confused newbie
    :
    I think you should not call ShowWindow with the nCmdShow and hwnd parameters. Indeed, nCmdShow is passed to WinMain by the system and is intended to be used with the window your process creates, not with a window from another process. To know what is the active window, you may try the following code.
    [code]#include
    #include


    int main(void)
    {
    HWND currenthwnd,hwnd;
    char s[1024];

    hwnd=NULL;
    while (1)
    {
    currenthwnd=GetForegroundWindow();
    if (currenthwnd!=hwnd)
    {
    hwnd=currenthwnd;
    if (hwnd==NULL) printf("No window
    ");
    else
    {
    GetWindowText(hwnd,s,1024);
    printf("%s
    ",s);
    }
    }
    }
    return 0;
    }[/code]

    It's far from being perfect (it loops infinitely, press ctrl+C to stop it when the window is active), but it works.
    Is that what you wanted?

    Take care, Steph.
  • tsagldtsagld Member Posts: 621
    : : Hi All,
    : : I am a novice and would appreciate any help you guys can give me here. I want to write a code which will tell me with which window the user is currently interacting. The code I am using is
    : :
    : : #include
    : :
    : : int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    : : LPSTR lpCmdLine, int nCmdShow)
    : : {
    : : int test;
    : : HWND hwnd;
    : : MSG Msg;
    : : TCHAR szBuf[80];
    : : int nMaxCount;
    : : DWORD d;
    : : int nCmdShow;
    : :
    : : while(1)
    : : {
    : : hwnd=GetForegroundWindow();
    : :
    : : if(hwnd == NULL)
    : : {
    : : MessageBox(NULL, "No window is currently active!","Error!" MB_ICONEXCLAMATION | MB_OK);
    : : return 0;
    : : }
    : :
    : : ShowWindow(hwnd, nCmdShow);
    : : UpdateWindow(hwnd);
    : :
    : : test=GetWindowText(hwnd,szBuf,80);
    : :
    : : }
    : :
    : : }
    : :
    : : If I use GetForegroundWindow, I get the handle of the IDE i.e VC++. How do I write a code which will tell me which is the window the user is currently interacting with?. The issue is something like this...anytime a user is interacting with a window, I need to get a handle to that window and then selectively capture some graphics from that window. How do i proceed in this case? . So in this case would I need to run an exe from my main code which will give me the handle to the active window? Any better idea how do I do it? Waiting for replies from the experts out there!! Thanks!!
    : :
    : : -confused newbie
    : :
    : I think you should not call ShowWindow with the nCmdShow and hwnd parameters. Indeed, nCmdShow is passed to WinMain by the system and is intended to be used with the window your process creates, not with a window from another process. To know what is the active window, you may try the following code.
    : [code]#include
    : #include
    :
    :
    : int main(void)
    : {
    : HWND currenthwnd,hwnd;
    : char s[1024];
    :
    : hwnd=NULL;
    : while (1)
    : {
    : currenthwnd=GetForegroundWindow();
    : if (currenthwnd!=hwnd)
    : {
    : hwnd=currenthwnd;
    : if (hwnd==NULL) printf("No window
    ");
    : else
    : {
    : GetWindowText(hwnd,s,1024);
    : printf("%s
    ",s);
    : }
    : }
    : }
    : return 0;
    : }[/code]
    :
    : It's far from being perfect (it loops infinitely, press ctrl+C to stop it when the window is active), but it works.
    : Is that what you wanted?
    :
    : Take care, Steph.
    :

    You get the handle to VC++'s IDE probably because you are running your code from the IDE (in debug-mode, I assume). The IDE is the currently active window at that moment.
    Try running the release-exe by double-clicking it in Explorer. The first active window should be Explorer in that case.

    You will probable have difficulties switching to another window as well, since the while-loop sucks up 99% of the CPU-usage (see Taskmanager).
    Consider putting a Sleep(10) (standard WinApi) or something into the loop. That causes your loop to wait 10 miliseconds every iteration, releasing the CPU to other processes. The CPU-usage of your process will drop to almost 0% and you will be able to test your code properly.

    Greets,
    Eric Goldstein
    www.gvh-maatwerk.nl

Sign In or Register to comment.