Implementing notification dialogs that don't steal focus from full-screen apps? - c++

I want to implement a notification window by subclassing QDialog. It should be on top of other windows, but I don't want it to steal focus from other windows for obvious reasons. I'm also concerned that it would interfere with full-screen applications like videos and games.
How do I go about implementing this? Are there any common programming and UX practices I might want to know about?

It appears quite an old topic. However, I did not see anyone posting a proper answer that just works, so I am posting my solution to the same problem I have been facing recently.
First of all, if you want your dialog not to steal focus from other dialogs or input fields, you should set the following property: Qt::WA_ShowWithoutActivating. Using this property, window (dialog is also a window) will be shown without being activated. Then, probably you will want to customize your dialog to your needs, and you will want this dialog to be shown on top. So, the following Window flags can be set in order to achieve such a result in a cross-platform manner: Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint | Qt::X11BypassWindowManagerHint | Qt::Tool | Qt::WindowStaysOnTopHint | Qt::WindowTransparentForInput | Qt::WindowDoesNotAcceptFocus.
The code below is one of the examples to achieve a dialog that is modeless, and does not steal focus from anyone (assuming dialog is a variable pointing to the valid instance of QDialog):
dialog->setAttribute(Qt::WA_ShowWithoutActivating, true);
dialog->setWindowFlags(dialog.windowFlags() | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint | Qt::X11BypassWindowManagerHint | Qt::Tool | Qt::WindowStaysOnTopHint | Qt::WindowTransparentForInput | Qt::WindowDoesNotAcceptFocus);

Haven't tried it but it looks like
my_dialog->setWindowFlags(Qt::CustomizeWindowFlags | ... | Qt::WindowStaysOnTopHint);
should work, in conjunction with making it modeless.

First of all you need to create a non modal dialog:
A modeless dialog is a dialog that operates independently of other
windows in the same application. Find and replace dialogs in
word-processors are often modeless to allow the user to interact with
both the application's main window and with the dialog.
In order to achive that you need to call the show function and not the exec one.

Related

QtDialog with close button but hidden toolbar, minimize and maximize buttons

I'm working on an Qt app. I should create a pop-up window that has only a close button with no toolbar, minimize and maximize buttons.
Any idea how to do this?
I don't know by "no toolbar" you mean "no title bar" or not, but this example can help you. Anyway, by using the following code you can have a window with just close button, and "title bar":
window->setWindowFlags(Qt::Window | Qt::WindowCloseButtonHint);
If you don't want the title bar too, try to remove it and design it by yourself.
you could change the window-flags. But your expected window calls for a QDialog: has no menu-bar and just a close-button (out of the box).
Else refer to this ( https://doc.qt.io/qt-5/qtwidgets-widgets-windowflags-example.html ) and set the flag for Qt::WindowCloseButtonHint.
If you use setWindowFlags(Qt::Window | Qt::FramelessWindowHint); you will get rid of the title bar, but with it all buttons that live on the titlebar, including the close button.
This will mean you will need to implement a way to move the window, as the title bar is used for this purpose. See this answer
Then implement your own titlebar class with a close button that you can put at the top of your dialog.
Unfortunately you don't have any control over the system titlebar appearance, so it's not possible to stylesheet your way out of this problem.

How can I create a Spin Button Control dynamically in MFC using CSpinButtonCtrl class?

I realize that this is a trivial problem and I even looked at an MFC book(Programming Windows with MFC by Prosise). However, I couldn't really find a solution.
I am trying to create a Spin Button Control dynamically and here is a simplified code:
CEdit* m_editControl = new CEdit();
m_EditControl->Create(WS_VISIBLE | WS_CHILD , rectEdit, this, EditID);
CSpinButtonCtrl* m_spinControlCtrl = new CSpinButtonCtrl;
m_spinControlCtrl->Create(WS_VISIBLE | WS_CHILD, rectSpinButton, this, SpinID);
m_spinControlCtrl->SetBase(10);
m_spinControlCtrl->SetBuddy(m_editControl );
m_spinControlCtrl->SetRange(-55, 55);
My problem is that the spin button does not change the value of the CEdit. Am I missing something? How can I create a Spin Button Control dynamically?
Your spin control is missing the style UDS_SETBUDDYINT:
UDS_SETBUDDYINT Causes the up-down control to set the text of the
buddy window (using the WM_SETTEXT message) when the position changes.
The text consists of the position formatted as a decimal or
hexadecimal string.
I also suggest setting UDS_ARROWKEYS so the arrow keys can be used to increment or decrement the value when the focus is on the edit control.
For the edit control I would add WS_TABSTOP so the user can navigate using the TAB key and WS_EX_CLIENTEDGE so the edit control shows the regular themed border.
I also noticed that you use dynamic memory allocation for the controls, which is not necessary. Just create non-pointer member variables like CEdit m_EditControl; so you don't have to worry about deallocation.
Fixed code:
m_EditControl.CreateEx(WS_EX_CLIENTEDGE, L"Edit", L"0", WS_VISIBLE|WS_CHILD|WS_TABSTOP,
rectEdit, this, EditID);
m_spinControlCtrl.Create(WS_VISIBLE|WS_CHILD|UDS_SETBUDDYINT|UDS_ARROWKEYS,
rectSpinButton, this, SpinID);
m_spinControlCtrl.SetBase(10);
m_spinControlCtrl.SetBuddy(&m_EditControl);
m_spinControlCtrl.SetRange(-55, 55);
I also strongly suggest learning to use Spy++. This is how I actually arrived at this answer. Using the resource editor I just dropped an edit control and an up-down control onto a dialog and used Spy++ to observe the default window styles.

Combobox cbn_closeup event

I have combo box created with style CBS_DROPDOWN | CBS_HASSTRINGS | WS_VISIBLE | WS_CHILD.i want to perform some action on command cbn_closeup.But my control is not getting this event.Even in spy++ there is no cbn_closeup sent to combo box.Please somebody help me.
Not your Control gets the WM_COMMAND notification. The parent gets it. So you Need an ON_CBN_CLOSEUP in the parent window code.
If you want that your window gets the notification you Need an ON_CONTROL_REFLECT handler. But this only works if the parent window is created by the MFC too, or at least subclassed.

set styles of a CRichEditView

It's been a while since I've done any work with MFC's and I just got handed a project where I need to add a simple console to show logging messages received by the application. I decided to use a RichEditView because I want to be able to format and color the received messages.
I have been banging my head all day trying to set ES_AUTOHSCROLL and ES_AUTOVSCROLL in my CRichEditView and I just can't get it to work...
I derived a class from CRichEditView called CConsoleView and in CConsolesView::PreCreateWindows I set the style to
- ES_READONLY | ES_MULTILINE | ES_WANTRETURN | ES_AUTOHSCROLL | ES_AUTOVSCROLL
but this didn't work as expected.
OK... nothing wrong with going back to basics so I've tried this in a simple MFC wizard and it worked and I was expecting this to work in my app as well.
From http://msdn.microsoft.com/en-us/library/windows/desktop/bb775464(v=vs.85).aspx I know that autoscroll can't be changed after the object creation so I guess my object is being created before I call get the call to PreCreateWindow...
Can I force the style in my CConsoleView constructor?
When/where is (generic question) the CRichEditView::Create() method being called? shouldn't it be called after PreCreateWindow?
Besides attaching a new object is there any way to get around this?
Any help would be appreciated!
Cheers
If you're using the control in a dialog template, just add the create flags to the resource file that's declaring the control in dialog. If you're dynamically creating the control, you'd be doing the Create... unless you're doing MDI (which doesn't seem to fit based on your description).

how to show both icon and text on button on mfc?

Code Used:
m_pButton->Create(L"ABC", WS_CHILD | WS_VISIBLE| BM_SETIMAGE,CRect(0,0,100,100),this,ID_BUTTON1);
m_pButton->SetIcon(::LoadIcon(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDI_ICON1)));
//above Code show neither showing image nor showing text.
You might use CMFCButton if you are using VS 2008 SP1 or higher.
BM_SETIMAGE is not a button style, but a message which is sent to the window in order to set a bitmap.
What you probably want is the BS_BITMAP style. Unfortunately as far as I know, it is not possible to have both text and a bitmap on a standard button. But you should find plenty of working implementations of a custom button class on sites like codeguru or codeproject.
BS_ICON and BS_BITMAP must be both unset to enable icon and text on the same button.
See https://msdn.microsoft.com/en-us/library/bb761822(VS.85).aspx
WPF might be able to do this. But, changing GUI topkits might not be an option anyway.
You could override the DrawItem method in CButton. For details check out the following links:
CButton::DrawItem
Owner drawn button - step by step