C++ MFC EditBox Handling Double Click - c++

The scenario I have is as follow:
I have a CDialog class that contains two EditBox controls, lets say it's IDC_EB1 and IDC_EB2.
I want to be able to listen to a Double Click event on IDC_EB1 and IDC_EB2 to open up a FileDialog and display the selected file location on IDC_EB1 and IDC_EB1.
IDC_EB1 and IDC_EB2 is doing through the DDX Control mechanism and stored into their respective member variables.
The member variables type is of CMyEdit (derived from CEdit) in order to handle double click event.
The dialog runs and I was able to open up a FileDialog when I double click on IDC_EB1 or IDC_EB2 to browse. The problem is within CMyEdit, I no longer know which EditBox triggered the double click event in order to display the file path selected from the FileDialog.
What would be a good way to resolve this problem? I'm running Visual Studio 2010 SP1.
PS: There is the MfcEditBrowse Control which would solve the problem above nicely but it doesn't fit my need because the plan is to reuse this CDialog window at various stages in my program and apparently you cannot invoke DoModal more than once on a Dialog that has MfcEditBrowse Control. the MfcEditBrowse Control i nthat Dialog will throw an assertion failure (known problem apparently according to what I found on Microsoft documentation).

I no longer know which EditBox triggered the double click event in order to display the file path selected from the FileDialog.
You can use CWnd::ChildWindowFromPoint(point) which will return the clicked child control within the body of OnLButtonDblClk().

You know the ID inside the double click handler. Just call GetDlgCtrlID from the message handler.
You can easily add some data to CMyEdit to make it easy to identify the control for the action of the double click
You can send a WM_COMMAND message upon the double click from the CMyEdit to the parent and the parent handle the rest. The parent knows the control by its Id and should know what to do.

Related

Drag and drop in MFC Dialog

I'm trying to add drag and drop functionality to a control on a property page which is programmatically "wrapped" inside an MFC dialog at runtime. This "wrapping" involves instantiating the property page as a member of the dialog, and then resizing it and positioning it to the dimensions of a picture control which is on the dialog in the .rc file. I'm not sure why things were done this way, other than to maybe standardise the appearance of property pages in the app, but in any case I end up with this complicated hierarchy of windows, a control inside a property page inside a picture control inside a dialog.
The control which I want to accept dragged files has DragAcceptFiles invoked on it, but I think the WM_DROPFILES message for the control is being discarded before it reaches the control, because one of the parent windows doesn't accept dragged files. I considered calling DragAcceptFiles on all the parents, and passing WM_DROPFILES down the chain of windows, but I don't want to do that because they won't have handlers to accept the files, and will show the icon indicating files can be dropped on them regardless.
Any help on figuring out how to get the WM_DROPFILES message to my control would be much appreciated.
At the parent level, implement an override for virtual CDialog::PreTranslateMessage(MSG* pMsg)
This gives you a chance to see the WM_DROPFILES message and redirect them to a handler in the property page.
I figured this out in the end by calling CWnd::BringWindowToTop() on both the control I wanted to drag files onto, and also the dialog that the control was on.

MFC one parent dialog, multiple child dialogs

I want to build a MFC application, with one main dialog, and all the other dialogs are child of this main dialog (and embedded in it).
Now, i was able to embed the first child in the main dialog, but i want to pass to the next dialog (note that the order of opened dialogs is random), so i need to hide the first dialog and show another. To know which dialog is shown at the moment and hide it, i've tried using a CDialog variable to store the current opened dialog, but i get a CObject::operator =' : cannot access private member declared in class 'CObject' error.
Is there another way to do this "hide and show dialogs" game?
EDIT: Could i store some ID of the dialogs and use it to acomplish this task?
So i managed to accomplish this task using classes IDDs.
First, i store the last opened dialog's IDD
m_dlgStartPage.Create(CStartPageDlg::IDD, this);
m_openedWin.nDialogIDD = m_dlgStartPage.IDD;
m_dlgStartPage.ShowWindow(SW_SHOW);
Then, when a new dialog needs to be shown, i send a message to my main dialog (nIDD is the IDD of pending dialog to show):
AfxGetApp()->m_pMainWnd->SendMessage(WM_COMMAND_CHANGE_WINDOW, nIDD, 0);
And last, in my main dialog, i parse all the child dialogs and check if m_openedWin.nDialogIDD matches with each dialog's IDD, so i can hide it. After this, i parse once again all the chid dialogs and use the nIDD from the sent message to show the correct one.
I don't really like this approach, because of all the parsing and sent messages to the main dialog's class, so if anyone has a better idea or method, please post it.

Qt 5 - How to send data back from child dialog in real time

I have a settings dialog that has some settings that require another dialog to fully configure. My window shows a preview of the data as it's being tweaked by these settings. Upon clicking on the configuration button I launch another modal dialog with some knobs to twist to fine tune the particular setting.
I wish to send the result of the twisting of the knobs on the child dialog back to the parent dialog so that it can show the new preview data as the knobs on the child are being played with. The way I imagine it is I have a "refresh preview" function in the parent that is called after modification of it's data preview member variables. The question is, how do I do this? How can I access getters/setters from the parent dialog as a modal child dialog? If I do access them will the preview change or will it be blocked because of the modality of the child?
Thank you!
In Qt world, it is generally encouraged to exploit the Signal/Slot mechanism. In short, classes can send signals when something changes within that class. And slots can receive such signals provided we notified the receiving classes appropriately.
Let us look at how we can do it for our present case.
In our settings dialog constructor, we include this code (assumption is that you display the "another" dialog when a button is pressed):
Dialog *dialog = new Dialog();
connect(dialog->dial(), &QDial::valueChanged, this, &QSettingsDialog::changeTemp);
Code walkthrough:
Our QDialog has been constructed with a QDial object, dial. We access that member pointer with dialog->dial().
We tie the signal that emits the value changed on the dial to the slot called changeTemp that receives the value changed and sets the display widget on the settings dialog (parent) accordingly.
The changeTemp method might be like so:
void QSettingsDialog::changeTemp(int val)
{
lineEdit->setText(QString::number(val));
}
Notes:
You need to declare the Q_OBJECT macro on all classes that need to implement Signals and slot. In this case, both the settings dialog and the child dialog.
The above signal/slot signature is the new Qt5 signature. If you are on a version below 5.0, the signature is different. Refer to the docs.

OnLButtonDown() is not fired if I click an item in my dialog (ListBox, CheckBox etc) but it fires ok if I click background or Static Text

If I click anywhere on the dialog window, like in the "background", or on a Static Text, the function OnLButtonDown() is fired. But if I click a ListBox/EditBox/Combo/Calendar/Checkbox etc is not fired.
I thought that because these have control variables atached to it, and the Static Text don't. But adding a new Listbox to my Dialog and testing, I see that it doesn't fire either, so now I am confused...
I added OnLButtonDown() with the Class Wizard, it appears in:
BEGIN_MESSAGE_MAP(CMFCTesting2Dlg, CDialogEx)
ON_WM_SYSCOMMAND()
// other handlers, etc
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
END_MESSAGE_MAP()
My function:
void CMFCTesting2Dlg::OnLButtonDown(UINT nFlags, CPoint point)
{
AfxMessageBox(CString("BUTTON DOWN"));
CDialogEx::OnLButtonDown(nFlags, point);
}
I tried calling CDialogEx:: ... before AfxMessageBox... but same result.
The CListBox item, has the Notify option set to True, as I seen some advices in other posts.
I suspect that the WM notification message is captured somehow by the ListBox, and not sent "forward" to my CMFCTesting2Dlg, but I understood that this should happen with the Notify option set to True, on the ListBox, no? (well, obviously, not...)
Sorry, I am new to dealing with WM in MFC.
BTW, I use Visual Studio 2010 Ultimate, and this is a Visual C++ - MFC- MFC Application - Dialog Based project.
How can I capture this mouse down event if clicked on a listbox / combo / etc?
On the LONG STORY, I am actually trying to accomplish this issue:
I have two listboxes (lets say), and I want to scroll them synchronously, when user scrolls one of them, others must scroll the same (or update in the next moment). And I thought of using on mouse down to track the position of every Listbox (with the GetTopIndex), and on mouse up, to GetTopIndex again and compare with the previous ones. If a change was made, then a listbox was scrolled and then update all listboxes with SetTopIndex. The unfriendly solution for the user, but simpler for me, would be clicking a button that does this verification / update, but its not elegant at all, and it can only set them at the same level, but can't determine which one was scrolled last time. This automatically scrolling of the listboxes should be made just for displaying, it is not important the selections in the listboxes. I tried using the Message Type in the Add Event Handler for the Listbox, but none that are displayed work for my problem, KillFocus and SetFocus, are not fired if the scroll-bar is dragged, only if an item in the listbox is clicked... and I didn't succeed on the others message type either, from the Add Event Handler.
Once a window handles a message, it stops being sent to the other windows beneath it. A static window doesn't handle a mouse down, so it goes to the dialog. The other controls all handle it in some fashion (setting focus at least) so it never gets to the dialog.
You can override the OnLButtonDown handler in each of the controls to do something else once they've finished their default handling in the base class. You might also get to see the message in the PreTranslateMessage method of the dialog.
As far as getting the List Controls to sync scrolling goes, your best bet would be to intercept WM_VSCROLL by subclassing CListCtrl or perhaps by PreTranslateMessage, and then calling SetScrollInfo on the other list. If the number of items in the lists are the same, it should be fairly simple.

MFC CListCtrl Right Click On Header

I have a MFC app written in Visual Studio 6 which I am adding some new functionality to. What I want to be able to do is display a context menu when the user right clicks on the header column on the list control within a property page. The CListCtrl class was added view the Class Wizard.
Via the ClassWizard for the property page, I have added a handler for the right click on the listctrl. This does get called and I added the following code to work out if the rclick was over the header section and if so which header item. Like this
POINT Point;
GetCursorPos (&Point);
ScreenToClient(&Point);
HDHITTESTINFO HitTest;
//Offset of right scrolling
HitTest.pt.x = Point.x+ m_ctrlRecordList.GetScrollPos(SB_HORZ); //Offset of right scrolling
HitTest.pt.y = Point.y;
//Send the Hit Test Message
m_ctrlRecordList.GetHeaderCtrl()->SendMessage(HDM_HITTEST,0,(LPARAM)&HitTest);
// Check hit test result.
*pResult = 0;
However, the hit test always returns -1.
I tried just on left click instead by handling the HDN_ItemClick message of the header control in the property page. This is all done in the ClassWizard so I expected to be able to handle this notification here. However, from what I have researched so far, there may be a bug in MFC where the ClassWizard puts this code into your code for you but this notification will never get as far as the parent of your list control. Is this the case?
What would be the best way to do this? I would prefer on right click but left click would do if necessary.
You cannot do it trying to handle message from a list’s header in a dialog, nor can you do it in the CListCtrl derived class.
MFC is using message reflection for certain controls and only for certain messages/notification codes.
Most likely you are passing coordinates of the mouse click on the list control, hence hit test fails.
Try this:
Add class derived from CHeaderCtrl. Declare member variable of the derived class in a dialog.
I assume you have already subclassed (have variable inserted by the wizard) list control.
In OnInitDialog write the following:
// m_List is the dialog’s member of the subclassed list control,
// m_header is a member variable of your new header class:
// insert this code after list control is already
// initialized and all columns are added.
CHeaderCtrl* pHeaeder = m_List.GetHeaderCtrl();
m_Header.SubclassWindow(pHeaeder->m_hWnd);
Insert handler for WM_CONTEXTMENU or WM_LBUTTONUP in the derived class and popup menu. You will receive CPoint type for click position.
I have managed to sort this out and thought I would add the answer in case anyone else stumbles upon this with the same problem. The code I posted originally is fine but it needs to go in the OnNotify handler of a class derived from CListCtrl. The ClassWizard allows you to add a reflect handler to the parent of the list control but the message never gets that far.