Ali Damirchy
Ali Damirchy

Reputation: 33

sendMessage to a hidden window did not work

i want to send a click message on button of a window that it's caption is "Form1" and button's caption is "button1" here is my code:

i checked this handles by SPY++ seems correct..

    #include "stdafx.h"
    #include <windows.h>
    #include <conio.h>

    int main()
    {
     while(1)
     {
       HWND HWin, btn;
       HWin = FindWindowExA(NULL,NULL,NULL,"Form1");
       btn = FindWindowExA(HWin,0,NULL,"button1");
       SendMessage(btn,BM_CLICK,0,0);
       _getch();
      }
    }

this code worked well and every thing is good... but when "Form1" is set to hidden sendMessage seems not work! Why???

notice that FindWindowExA(...) still return same handle while "Form1" is hidden, and is equal with previous (when is shown)

setting hidden/show of "Form1" is done by this lines:

to shown:

    ShowWindow(HWin,SW_SHOW);

to hidden:

    ShowWindow(HWin,SW_HIDE);

i'm running on VS 2010

i want to know if sendMessage not work on hidden window is there other way to do that??

thanks All

Upvotes: 0

Views: 1575

Answers (1)

selbie
selbie

Reputation: 104559

I'm assuming you are judiciously checking to make sure that your FindWindow calls are not returning NULL when the window is hidden before executing the message passing code.

Assuming you are getting valid HWNDs returned from FindWindow, instead of BM_CLICK message sent to the button itself. Try sending a WM_COMMAND message to the parent window.

if (HWin && btn)
{
    LONG ctrlId = GetWindowLong(btn, GWL_ID);
    SendMessage(HWin, WM_COMMAND, MAKEWORD((WORD)ctrlId, BN_CLICKED), (LPARAM)btn);
}

If the application itself is in a state where it's not expecting click notifications while hidden, you risk some really weird behavior.

I would think this would work for a win32 app or dialog. Not sure about a .NET forms app.

Upvotes: 2

Related Questions