Finding a window and setting focus on it - mfc

1.I want to find a window and set focus on it , but window is not taking the focus.
2.If i use HWND_TOP then it does not make the window active and if i use HWND_TOPMOST then it make window always on top.
Can anyone help me ??
HWND hwndAppDlg = ::FindWindowEx(hwndDesktop,NULL,lpszClass,lpszWindow);
if(hwndAppDlg && IsWindow(hwndAppDlg))
{
CRect rcAppDlg;
if( 0 == ::GetWindowRect(hwndAppDlg,rcAppDlg))
{
OutputDebugString(L"\n GetWindowRect failed...");
return FALSE;
}
if(0 == ::SetWindowPos(hwndAppDlg,HWND_TOPMOST,rcAppDlg.left,rcAppDlg.top,rcAppDlg.Width(),rcAppDlg.Height(),SWP_SHOWWINDOW))
{
OutputDebugString(L"\n SetWindowPos failed...");
return FALSE;
}
if(0 == ::PostMessage(hwndAppDlg,WM_SETFOCUS,0,0))
{
OutputDebugString(L"\n WM_SETFOCUS failed");
return FALSE;
}
return TRUE;
}

You're sending WM_SETFOCUS, but that does not set the focus. That message is sent to a control if it gained or lost the focus, but when that message is sent/received, the focus change has already happened.
To actually set the focus (you don't need to send the WM_SETFOCUS message), use SetFocus() if you know which control in the dialog should get the focus, or SetForegroundWindow() to set the focus to the dialog itself and let the dialog determine which subcontrol actually will get the focus.
Both of these APIs will send WM_SETFOCUS automatically.

How about ShowWindow( hwndAppDlg, SW_SHOW );

I have used ::SetForegroundWindow(hwndAppDlg) to activate and set focus on the dialog and it works cool.

Related

ShowWindow() showing blank app until is interacted with via mouse/keyboard after restoring from systray in C++ - Flutter App

I've been wanting to incorporate systray functionality into my Flutter app so I went to modify the native C++ code that initiates the window etc to see if I could hook into it.
Despite not having much prior experience in C++ I have been able to create an icon for my app in the systray with a menu that allows the window to be shown again when hidden (using ShowWindow(hwnd, SW_HIDE);) and to quit entirely.
However when an option in my systray menu is selected to show the window again using ShowWindow(hwnd, SW_NORMAL); after being hidden, the app stays blank like this:
Then, when the window is finally interacted with, the contents of the window show again:
Here is the code that I have added so far to my win32_window.cpp (from a default Flutter application). I haven't included the entire functions because I thought it would make things less clear, but I will also attach the full win32_window.cpp at the end of this post.
Win32Window::CreateAndShow():
//Systray:
HICON hMainIcon;
hMainIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_APP_ICON));
nidApp.cbSize = sizeof(NOTIFYICONDATA); // sizeof the struct in bytes
nidApp.hWnd = (HWND) window; //handle of the window which will process this app. messages
nidApp.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; //ORing of all the flags
nidApp.hIcon = hMainIcon; // handle of the Icon to be displayed, obtained from LoadIcon
nidApp.uCallbackMessage = WM_USER_SHELLICON;
StringCchCopy(nidApp.szTip, ARRAYSIZE(nidApp.szTip), L"All Platforms Test");
Shell_NotifyIcon(NIM_ADD, &nidApp);
return OnCreate();
Win32Window::WndProc():
if (message == WM_NCCREATE) { ... }
else if (message == WM_USER_SHELLICON) { //interacting with systray icon
if (LOWORD(lparam) == WM_RBUTTONDOWN) { //right clicked
POINT lpClickPoint;
GetCursorPos(&lpClickPoint);
hPopMenu = CreatePopupMenu();
InsertMenu(hPopMenu,0xFFFFFFFF,MF_BYPOSITION|MF_STRING,IDM_SHOW,_T("Show"));
InsertMenu(hPopMenu,0xFFFFFFFF,MF_BYPOSITION|MF_STRING,IDM_EXIT,_T("Quit"));
SetForegroundWindow(window);
TrackPopupMenu(hPopMenu,TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_BOTTOMALIGN,lpClickPoint.x, lpClickPoint.y,0,window,NULL);
}
else if (LOWORD(lparam) == WM_LBUTTONDOWN) { //left clicked
ShowWindow(window, SW_NORMAL);
//LOOK: works but shows blank screen until is interacted with (mouse enters or key is pressed etc)
}
}
else if (message == WM_COMMAND) { //if message is a command event such as a click on the exit menu option
int wmId;
wmId = LOWORD(wparam);
if (wmId == IDM_EXIT) { //if quit has been pressed
Shell_NotifyIcon(NIM_DELETE,&nidApp);
DestroyWindow(window);
}
else if (wmId == IDM_SHOW) {
ShowWindow(window, SW_NORMAL);
//LOOK: works but shows blank screen until is interacted with (mouse enters or key is pressed etc)
}
Win32Window::MessageHandler():
switch (message) {
...
case WM_CLOSE: //stop window from closing normally, can only be closed when DestroyWindow() is run from systray
//Hide window and continue running in background.
ShowWindow(hwnd, SW_HIDE);
return 0;
}
Link to full win32_window.cpp here.
What's going on here? I thought using UpdateWindow() would help but then I realise the app is painted upon ShowWindow() anyway. My guess is that this has something to do with Flutter's run loop being blocked but I can't figure out where to go next, especially considering I usually don't dabble in C++ but just wanted to add an extra feature to my app when running on Windows.
Any help would be greatly appreciated, thanks.
Ok so I've worked out why it wasn't working. When closing the window, I couldn't just use SW_HIDE, but SW_MINIMIZE too. Otherwise attempting to redraw the window wouldn't work correctly:
ShowWindow(hwnd, SW_MINIMIZE);
ShowWindow(hwnd, SW_HIDE);
After that, when showing the window it got drawn but wasn't the active window, but adding SetForegroundWindow() fixed that:
ShowWindow(window, SW_NORMAL);
SetForegroundWindow(window);
Thanks for everyone's help :)

How to move Borderless DhtmlDialog with other click events

I am moving Borderless DHtmlDialog of MFC with this method:
BOOL CMyDlg::preTranslateMessage(MSG* pMsg)
{
if(pMsg->message == WM_LBUTTONDOWN)
{
PostMessage( WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM( pMsg->pt.x, pMsg->pt.y));
return false;
}
return CDHtmlDialog::preTranslateMessage(pMsg);
}
But the problem is that it is moving but couldn't picking up any click event for tags and ids given in DHTML_EVENT_MAP.
Can any one please guide what is the main problem in this? Please help.
This is because DHTML_EVENT_MAP will not handle click events on tags.
MFC macros are not supposed to do that. You will need event sink in your code.
Refer Handling HTML Element Events

Switch between edit controls using Tab?

The Window is non DialogBox based so WS_TABSTOP doesn't work. Moreover I don't want to Tab through all the controls, I just want to Tab through few Edit controls.
What I did is I superclassed the Edit control and handled the WM_KEYDOWN message, switching between edit controls, by getting next window in the line thorugh ::GetWindow(hwnd,GW_HWNDNEXT); Also I would like to switch focus back to the first Edit control when I have reached the last one.
The Code doesn't work for when I have reached the last Edit control, the ::GetWindow simply returns the next window in the line(?), which happens to be a non superclassed edit control. And there are more hidden child windows(SW_HIDE).
Maybe if I know how to know the class name of the window's HWND ?
Note: Pure Win32 api, c++ oop.
else if ( ( int ) wParam == VK_TAB )
{
HWND nextInLine;
nextInLine = ::GetWindow ( hwnd, GW_HWNDNEXT );
if ( hwnd == NULL ) nextInLine = ::GetWindow ( hwnd, GW_HWNDPREV );
::SendMessage ( nextInLine, EM_SETSEL, ( WPARAM ) 0, ( LPARAM ) -1 );
::SetFocus ( nextInLine );
return 0;
}
You get keyboard navigation for free in any window by using the IsDialogMessage API call. To consume the service a window message loop has to be modified to include a call to IsDialogMessage and only pass the message on to regular message handling if it hasn't been handled by the dialog manager already.
MSG msg = { 0 };
while (GetMessage(&msg, NULL, 0, 0)) {
if (IsDialogMessage(hwnd, &msg)) {
/* Already handled by dialog manager */
} else {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
Don't forget to set the WS_EX_CONTROLPARENT extended window style on the parent window, so that the dialog manager recurses into child windows.
It's possible to get away with just calling IsDialogMessage, but the result isn't quite 100% dialog-like. To make an ordinary window behave like a dialog:
Specify DLGWINDOWEXTRA as the cbWndExtra field of your WNDCLASS (don't forget to add on extra space you might already be using and offset your data's indexes)
Call DefDlgProc rather than DefWindowProc
Since this makes your window a dialog, you need to use the DWLP_USER window long instead of GWLP_USERDATA, if you're using that, when calling GetWindowLongPtr or SetWindowLongPtr.
(From memory, the main thing you get from doing the above is support for WM_NEXTDLGCTL, which I've found useful to use for supporting changing focus using the Enter key, using Method I described in http://support.microsoft.com/kb/102589.)
Then in your message pump, call IsDialogMessage for each dialog-like window in your message pump.
Finally, when creating controls for your dialog-like window, set the WS_TABSTOP window style for each window you want to participate in the tabbing, and set the WS_EX_CONTROLPARENT window exstyle (aka Control Parent in the resource editor) for child windows that contain dialog controls.

How to check if my window gets hidden/visible?

If i press the "show desktop" button in Windows7, my program will still think its not minimized, and if i press WIN+D while my program is focused, only then my program will catch this minimize command. How can i check for 100% sure that my program is visible or not?
Here is my main loop:
while(!done){
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
if(msg.message == WM_QUIT){
done = TRUE;
}else{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}else if(active){
render();
}
}
Edit3: Is this good? looks like its working:
case WM_WINDOWPOSCHANGED:
{
flags = ((PWINDOWPOS)lParam)->flags;
if((flags & 0x8000) && (flags & SWP_NOCOPYBITS) && (flags & SWP_FRAMECHANGED)){
active = !(flags & SWP_NOACTIVATE);
}
if((flags & 0x1000) && (flags & 0x0800) && (flags & SWP_NOMOVE) && (flags & SWP_NOSIZE)){
active = 1;
}
}
case WM_ACTIVATE:
{
active = !HIWORD(wParam);
return 0;
}
WM_ACTIVATE is sent when another window becomes active. When you say show desktop, no other window becomes active, so technically, your app is still active, even though it has been minimized.
You probably want to watch for WM_WINDOWPOSCHANGED. You can look at the flags to see what type of position event it was, or you can check IsIconic and IsWindowVisible whenever the window position changes.
There are a variety of potential functions that may get you the information you need depending on exactly what you want to do:
GetForegroundWindow() : Gets the window the user is currently "working" with. You could use this if you only wanted to draw things when a user is using your application but not another one.
GetActiveWindow() : Returns the active window within the calling thread which is probably not what you want. This might be useful if you wish to enable/disable drawing depending on which window was active within your own application.
GetFocus() : Returns the window with the current keyboard focus in the calling thread. Probably not what you want and use GetForegorundWindow() instead.
IsWindowVisible(): Returns whether the show/hide flag of the window is set to visible. This doesn't actually tell you whether the window is actually visible on the screen.
GetTopWindow(): Tells you the highest window in the z-order but not whether it actually has the focus/foreground. It may be possible for your window to be focused/activate/foreground but not have the highest z-order (I think anyways).
From your comments, however, you seem to actually want to see if there is at least one pixel of your window actually visible on the screen. For that I would probably use the technique mentioned in this SO question using the strangely named GetRandomRgn() although a simpler check may be to use GetClipBox() and check the return code for NULLREGION.
With Windows 8/10, there's another window visibility flag which is separate from IsWindowVisible. Check DwmGetWindowAttribute and the DWMWA_CLOAKED attribute.
Additionally, windows can be semitransparent, and GetLayeredWindowAttributes can tell you what the alpha level of a window is.
IsWindowVisible tells you if your window is visible. GetTopWindow tells you if it is the uppermost one in the Z order.
Try WM_ACTIVATEAPP.
wParam will be false if a window from any other app is getting focus. This includes pressing the "show desktop" button.

Why doesn't OnKeyDown catch key events in a dialog-based MFC project?

I just create a dialog-based project in MFC (VS2008) and add OnKeyDown event to the dialog.
When I run the project and press the keys on the keyboard, nothing happens. But, if I remove all the controls from the dialog and rerun the project it works.
What should I do to get key events even when I have controls on the dialog?
Here's a piece of code:
void CgDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
AfxMessageBox(L"Key down!");
CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}
When a dialog has controls on it, the dialog itself never gets the focus. It's stolen by the child controls. When you press a button, a WM_KEYDOWN message is sent to the control with focus so your CgDlg::OnKeyDown is never called. Override the dialog's PreTranslateMessage function if you want dialog to handle the WM_KEYDOWN message:
BOOL CgDlg::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message == WM_KEYDOWN )
{
if(pMsg->wParam == VK_DOWN)
{
...
}
else if(pMsg->wParam == ...)
{
...
}
...
else
{
...
}
}
return CDialog::PreTranslateMessage(pMsg);
}
Also see this article on CodeProject: http://www.codeproject.com/KB/dialog/pretransdialog01.aspx
Many of my CDialog apps use OnKeyDown(). As long you only want to receive key presses and draw on the screen (as in make a game), delete the default buttons and static text (the CDialog must be empty) and OnKeyDown() will start working. Once controls are placed on the CDialog, OnKeyDown() will no longer be called.