简体   繁体   中英

::PostMessage doesn't work when I am tabbed in to another program

In our Program we have a Dialog from a separate dll open to display infomation. I need to close this dialog when our system timer causes the system to lock.

I send information to the dll by registering a system message in both my MainFrm and EditDisplayDll

SYSTEMLOCK = RegisterWindowMessage("SystemLock");

When I sent the message via

::PostMessage(GetActiveWindow()->m_hWnd,SYSTEMLOCK,0,0);

The message correctly sends to my EditDisplayDll and closes the dialog when the system locks; however, if I alt tab while waiting for the timeout and use another program(firefox, outlook, etc.) the message never correctly calls to the EditDisplayDll. The MainFrm and other windows inside of the MainFrm correctly lockout and hide themselves in either case.

I have tried also using HWND_BROADCAST with PostMessage and SendNotifyMessage . I have also tried to use FindWindow() and FindWindowEx() to specifically call the EditDisplayDll.

I cannot use something like GetDlgItem() because my MainFrm.cpp doesn't have access to this dll.

My decision to use GetActiveWindow() was because I believe it looks to windows specific to my program no matter what window I am in as seen in the imagery in Foreground Vs Active window

Finally, my question is, is there a way to call all Windows in my program no matter what program I am currently in or is there another way I could get access to the specific IDD of the EditDisplayDll in order to send the SYSTEMLOCK message to it?

CWnd *cWndED = FindWindow(_T("EditDisplay"),_T("EditDisplay")); HWND hwnd = (HWND)cWndED;

You should use win32 API ::FindWindow with proper class, window name. And do not cast CWnd pointer to HWND. Your code should look like:

HWND hWnd = ::FindWindow(_T("ProperClass"), _T("ProperNmae"));
if (hWnd != NULL)
{
  ::PostMessage(hWnd, YOUR_MESSAGE, ....);
}

I will suggest you to find your Dll window class and name using Spy++ and then try to find that using above method. Remember it's always better to use native API for this kind of tasks.

FindWindow is a good solution if you know both, name of the window and the element.
If you want to get the HWND of your window - no element inside the window -, you can pass as first parameter NULL .

::FindWindow(NULL, _T("WindowName"));

Back to your code: If you are lucky your PostMessage does nothing, otherwise the active window may catch your message. Who knows how/if it is handled in the active window ? Use the PostMessage if you have a valid IsWindow(HWND) from FindWindow or FindWindowEx .
In case you want a CWnd from your HWND take a look at this . (The call may be slow)

HWND hWnd = ::FindWindow(_T("ClassName"), _T("WindowName"));
if (hWnd && IsWindow(hWnd))
{
    ::PostMessage(hWnd, MESSAGE_TO_BE_SEND, lParam_or_Flags);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM