I have created an mfc activex control and want to handle the keystrokes. To handle keystrokes in child dialog. I override the pretranslate message in my child dialog class. To use pretranslate function in an activex control I have added hook.
As solution for the similar problem described at http://support.microsoft.com/kb/194294.
Now pretranslate function is calling but the problem when I press the ESC key or enter key, an assertion come at ASSERT(::IsWindow(m_hWnd));at wincore.cpp line 880.
I guess you use a function in PreTranslateMessage that assumes that the window is created. Put
if (!::IsWindow(m_hWnd)) { return 0; }
in the beginning of your PreTranslateMessage and see if that helps.
Related
I create an app using MFC Framework that auto hide on startup, and if I press SHIFT+W then it shows the Windows.
I inherited the function PreTranslateMessage() like that:
BOOL CTestAppDlg::PreTranslateMessage(MSG* pMsg){
if (pMsg->message == WM_KEYDOWN){
if (pMsg->wParam == 0x57){
if (GetKeyState(VK_SHIFT) & 0x8000) {
ShowWindow(SW_SHOW);
}
}
}
return CDialog::PreTranslateMessage(pMsg);
}
But this function only catches the keypresses if MFC App is active. So if this App is hide in OnPaint() function with ShowWindow(SW_HIDE) then it cannot catch the SHIFT+W to show windows normally. How can I do for it? Thank all
As explained under Keyboard Focus and Activation:
The system posts keyboard messages to the message queue of the foreground thread that created the window with the keyboard focus.
As a window gets hidden the system transfers keyboard focus to the next eligible window, causing your window to no longer receive keyboard input as you observed.
There are several ways to observe input globally. In this case the most appropriate solution is to just call RegisterHotKey and provide a CWnd::OnHotKey implementation for the respective receiver of the WM_HOTKEY message.
Can I hide close button from CDockablePane and prevent it from closing?
I cannot find CanBeClosed method in the headers that I've just googled.
I suppose I can use PreTranslateMessage to filter WM_CLOSE event. But I am not sure that this is correct solution.
I am not sure that the dockable pane is really closed, AFAIK it is only hidden when the close button is pressed.
Note: WM_CLOSE is not send via a a PostMessage, it is directly delivered to the window.
There are several ways.
Handle OnClose (WM_CLOSE) yourself in your derived CDockablePane
The virtual function OnPressCloseButton is called. You migh overwrite it.
In your parent frame class (usually your CMainFrame) handle AFX_WM_ON_PRESS_CLOSE_BUTTON, you receive this message with the lParam set with the pointer to the CDockablePane. Return non zero to prevent closing.
I think method 3. is the one you should use...
I'm not sure what version of Visual Studio you have, but, that method is defined and implemented in AfxBasePane.h. Since it's virtual, you need to override it to remove the button. Details are here on how to work with that method.
I have "n" dialogs which have the same base dialog. Each dialog has its own controls
edit boxes
combo boxes
list controls
etc.
In base dialog, how do I set focus messages of each control and,for example, give a Message box with
text("Hello I got focus, my ID is %d")?
The easiest way is using the classical subclassing method. The problem is that WM_SETFOCUS is not pumped through the message Loop, so PreTranslateMessage will not help.
Thee are some nice classes that help to do additional subclassing without disturbing the MFC stuff.
Paul Di Lascia wrote CSubclassWnd. PJ Naughter wrote CHookWnd. And with the ATL has CWindowsImpl.
All this classes allow easy additional subclassing even if a window is already subclassed by the MFC.
You can use "standard subclassing" GetWindowLong/SetWindowLong too.
According to this SO article, you can hook the WM_SETFOCUS message.
You can get the Control ID by using GetDlgCtrlID with the hwnd returned by the hook.
But beware of popping up a MessageBox, that will change the focus and trigger your hook proc, making it go into a loop!
As Jerry already said make a hook, get parent window handler via GetParent() and SendMessage(hParentWND, WM_MESSAGE, lParam, wParam).
Of course, you should handle WM_MESSAGE in your parent window.
Btw, framework calls OnSetFocus function when window gained focus.
Using a CDialogImpl derived class as a dialog-based WTL/ATL app, I want to hide the main window when the user clicks the upper right "X" button (or presses Esc or Alt+F4).
Currently the "X" closes the dialog and ends the application.
I want to change this behaviour and only hide the dialog box instead of closing it. Is this possible?
I'm not sure if ATL/WTL provides some wrapper for this but in WinAPI, the function you are looking for is ShowWindow, which you would invoke as so:
ShowWindow(hwnd, SW_HIDE);
If you want this to happen when the application is closed, the message you need to handle is WM_CLOSE.
After a quick search, MSDN reveals CWindow::ShowWindow, which is the wrapper I mentioned earlier.
I have a first dialog with a simple button on it and while clicking the button, a second dialog is created using CDialog::Create(IDD,this). I would like the parent to be notified when the second dialog is destroyed but without adding any code to the second dialog i.e., without adding a m_pParent->Notify() line in OnDestroy method.
I have tried OnParentNotify, PreTranslateMessage, SubclassWindow in the parent dialog with no success. I have not used the WS_CHILD style for the second dialog. Any idea?
To complete: in fact, I have a ComboBox derived class (but the issue is the same with buttons) and I'm displaying a modeless Dialog instead of displaying the listbox. But I would like the control to be as generic as possible so that any modeless dialog could be used. That's why I do not want to add a specific notification in the second dialog. If I'm obliged, I will use this trick but I asked for a more generic solution. PreTranslateMessage only catches WM_PAINT, WM_NCMOUSELEAVE and WM_NCMOUSEMOVE.
Use a base class and have your parent refer to the modeless child by base class only. In the base PostNcDestroy have it post to the parent.
It doesn't make sense to have the parent do a bunch of filtering / spying on all messages. It does make sense to implement behavior in a base class that you want to have common to all the different future flavors you might have of the modeless child.
OnParentNotify() is not called since dialog2 is not a child of dialog1.
PreTranslateMessage() should help here (although I don't like this bullet). The trick is that a modeless dialog doesn't destroy itself when it's closed. If you want the dialog to die, it must call DestroyWindow() when it closes, such in an OnCancel() override.
Of course, the first thing that comes to mind is t wonder why you don't want to add custom notification in your modeless dialog code.
EDIT: Another method would consist in installing a message hook (for the current thread, Not the whole system!). This would help you catch all messages for all windows associated to the same thread as dialog1. See SetWindowsHookEx()
How about posting a main parent form event to the message queue?