How can I create a button with default behavior in win32 - winapi

I have been trying to create a button with the default behavior i.e when the user press ENTER, the button is fired.
I created the button with the WS_TABSTOP style and sent it the BM_SETSTYLE message with BS_DEFPUSHBUTTON has WPARAM parameter
but it's still not working.
HWND hwnd_Ok = CreateWindow("button", "Ok", WS_VISIBLE | WS_CHILD | WS_TABSTOP, 285, 195, 70, 25, hwnd, (HMENU)OK_BUTTON, NULL, NULL);
SendMessage(hwnd_Ok, BM_SETSTYLE, (WPARAM)BS_DEFPUSHBUTTON, TRUE);

I am trying to handle WM_GETDLGCODE for getting WM_KEYDOWN with VK_RETURN message in your control's WndProc. Sample code:
case WM_GETDLGCODE: {
if(wParam==VK_RETURN) {
return DLGC_WANTALLKEYS;
}
}
break;

The BS_DEFPUSHBUTTON is just a flag added to the button. The behavior you describe (along with lots of other field navigation behavior) is actually implemented by IsDialogMessage, which you get for free is a modal dialog box.
If you're trying to handle this in your own window class (or a modeless dialog), you can add IsDialogMessage to your message loop to get the dialog-style handling.

Related

how to find disabled-parent window from dialog

I have a page (render in IE) that creates a model dialog with style: WS_POPUP - showModalDialog. I can find HWND of the dialog but how I can find the HWND of parent?
Because the model dialog and its parent run on different processes, API function getParent does not work. In addition! sometimes another window can stand between the dialog and parent page :(, This prevents me from using 'next window' :(.
There is no code, and "API function getParent does not work" isn't helpful. You should check with Spy++ parent value in Properties->Windows.
Please check also Remarks section in GetParent documentation
To obtain a window's owner window, instead of using GetParent, use GetWindow with the GW_OWNER flag. To obtain the parent window and not the owner, instead of using GetParent, use GetAncestor with the GA_PARENT flag.
You have written:
In addition! sometimes another window can stand between the dialog and parent page
This may suggest that you don't have true owner-owned relationship. Maybe it is only simulated by disabling your owner window and making your dialog topmost. You can verify this by using Spy++ looking for WS_EX_TOPMOST in Properties->Styles. With HideThatWindow application you can manipulate window styles on runtime and further confirm this.
For getting HWND I would post custom message from one window to other.
UINT MyWmMessage = RegisterWindowMessage( TEXT( "HereIsMyHWND.mydomainname.pl" ) );
...
PostMessage( DialogHWND, MyWmMessage, 0, (LPARAM) ParentHWND );
And in dialog window:
// If this is in another process, reregister custom message.
UINT MyWmMessage = RegisterWindowMessage( TEXT( "HereIsMyHWND.mydomainname.pl" ) );
...
LRESULT CALLBACK DialogWindowProc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
{
...
if ( msg == MyWmMessage )
{
WeGotParentHWND = (HWND) lparam;
}
...
}

GWT: PushButton is not removing hovering if the click opens dialog

I've got a simple GWT PushButton which opens some dialog by click. The CSS styles are written in such a way that onHovering changes the appearance of the button.
The problem starts when I open some dialog by clicking on this button and close it. The onHovering styles remain!
This happens because in the CustomButtom which is the parent of PushButton there is a code which removes the hovering only when MouseOut event occurs. The problem is that if you open dialog with some dark semi-transparent screen it never occurs. The corresponding event is not issued and the button remains in onHovered state.
I try to fix this by firing this event manually. Fortunately, when the user closes that dialog I can catch onBlur event and try to do something in that moment. I try to create OnMouseOut event manually:
#Override
public void onBrowserEvent(Event event) {
int type = DOM.eventGetType(event);
if (type == Event.ONBLUR)
{
NativeEvent evt2 = Document.get().createMouseOutEvent(1, 0, 0, 0, 0, false, false, false, false, NativeEvent.BUTTON_LEFT, MyPushButton.this.getElement());
MyPushButton.this.getElement().dispatchEvent(evt2);
}
logger.info(event.getType());
super.onBrowserEvent(event);
}
... but for some reasons this code never sends an event. New MouseOut event is not logged in console and the onHovering styles are not changed. No exceptions are thrown.
Why I cannot fire the event?
Is there other ways to solve this issue?
GWT 2.5.1
This is a known issue: https://code.google.com/p/google-web-toolkit/issues/detail?id=2228
Actually, there are several issues with custom buttons, and you're encouraged to use Button (or SubmitButton or ResetButton) and CSS if possible; or maybe TextButton (or a custom cell-based ButtonBase subclass).
ToggleButton might be an exception as it has no real equivalent, other than a checkbox.
Finally I've made it working. Please notice Thomas Broyer's answer which is more broad.
#Override
/**
* In case onClick opens dialog, mouseOutEvent is not issued, thus preventing the button
* from removing the onHovering styles. Firing that event manually onBlur.
*/
public void onBrowserEvent(Event event)
{
if (DOM.eventGetType(event) == Event.ONBLUR)
{
NativeEvent mouseOutEvent = Document.get().createMouseOutEvent(1, 0, 0, 0, 0, false, false, false, false, NativeEvent.BUTTON_LEFT, getElement().getParentElement());
getElement().dispatchEvent(mouseOutEvent);
super.onBrowserEvent(Event.as(mouseOutEvent));
}
super.onBrowserEvent(event);
}

how to inject custom window into windows explorer

RT.as the picture below.
I try several ways as below, but not work :
Explorer Bars, Tool Bands, and Desk Bands, see here
Namespace extensions,see here
Implementing a Folder View, see here
I have resolve this problem, just resize the folder view window and create a new custom window and move it to the right postion, show below, this code will show custom window in top of folder view window:
HWND phwnd = (HWND)explorer SHELLDLL_DefView handle;
HWND chwnd = (HWND)explorer DirectUIHWND handle;
RECT *rcClient = new RECT();
GetClientRect(phwnd, rcClient);
MoveWindow(chwnd, rcClient->left, rcClient->top+39, rcClient->right, rcClient->bottom-39, TRUE);
HWND haddwnd = ::CreateDialogParam(hInst,
MAKEINTRESOURCE(IDD_DIALOG1),
phwnd,
(DLGPROC)About,
(LPARAM)rcClient);
ShowWindow(haddwnd, SW_SHOWNOACTIVATE);
MoveWindow(haddwnd, 0, 0, rcClient->right, 39, TRUE);
<pre>

Let my MFC dialog receive keystroke events before its controls (MFC/Win32 equivalent of WinForms "KeyPreview")

I have an MFC dialog containing a dozen or so buttons, radio buttons and readonly edit controls.
I'd like to know when the user hits Ctrl+V in that dialog, regardless of which control has the focus.
If this were C#, I could set the KeyPreview proprety and my form would receive all the keystrokes before the individual controls - but how do I do that in my MFC dialog?
JTeagle is right. You should override PreTranslateMessage().
// Example
BOOL CDlgFoo::PreTranslateMessage( MSG* pMsg )
{
// Add your specialized code here and/or call the base class
if ( pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN )
{
int idCtrl= this->GetFocus()->GetDlgCtrlID();
if ( idCtrl == IDC_MY_EDIT ) {
// do something <--------------------
return TRUE; // eat the message
}
}
return CDialog::PreTranslateMessage( pMsg );
}
Add a handler to override PreTranslateMessage() in the dialog class, and check the details of the MSG struct received there. Be sure to call the base class to get the right return value, unless you want to eat the keystroke to prevent it going further.

How to attach CFormView derived class to CMFCTabCrtl?

I have created MFC MDI project with base class CFormView, ribbon, caption bar etc.
In CMainFrame, OnCreate() calls EnableMDITabbedGroups() which automatically adds one tab
and attaches CMyProjectView view. Now I want to add second tab and attach second view to that tab. I created new dialog and added CFormView derived class to it.
Here is the code:
void CMainFrame::CreateViews()
{
const CObList &tabGroups = GetMDITabGroups();
CMFCTabCtrl *wndTab = (CMFCTabCtrl*)tabGroups.GetHead();
wndTab->m_bEnableWrapping = TRUE;
CRect dummyRect;
CNewFormView *pNewView = (CNewFormView*)RUNTIME_CLASS(CNewFormView)->CreateObject();
((CWnd*)pNewView)->Create(NULL, NULL, WS_CHILD, dummyRect, wndTab, IDD_NEWFORMVIEW);
wndTab->AddTab(pNewView, _T("NewTab"), -1, TRUE);
}
Now, I'm pretty sure I missed something trivial or went in wrong direction all together,
but I can't get new view to show up. Also, I can't catch AFX_WM_CHANGE_ACTIVE_TAB or AFX_WM_CHANGING_ACTIVE_TAB in CMainFrame. Message gets send from CMFCBaseTabCtrl::FireChangingActiveTab but nothing happens.
WS_VISIBLE is missing from the view's styles?
You may want to set the active view for the frame too.

Resources