Getting Active CDocument's File Icon - c++

I have an MFC MDI application in which I need to use the active CDocument's file icon. I know that the icon ID is set when specifying the document template (it is the first parameter in the CMultiDocTemplate constructor), but having searched the documentation (and header files) for CDocument, CMultiDocTemplate and CDocTemplate I can find no way to retrieve the icon's ID. Unfortunately all are defined in afxwin.h, so the source code isn't accessible. CDocTemplate does have a protected member variable, m_nIDResource which I'm guessing is set to the resource ID passed to the constructor, but there is no accessor for this variable.
I could write a thin wrapper class for CMultiDocTemplate and provide an accessor for the icon ID, but I thought I'd ask first if anyone has found a way to get the file icon?

Creating a wrapper class for CMultiDocTemplate and adding an accessor for m_nIDResource worked well.
As CMDIChildWnd uses the active document's file icon for its frames, there must be some in-built way of getting the icon, but oh well, this is a pretty simple work-around.

Related

Rewrite CListBox as a CCheckListBox

I am working on a Windows application using MFC and Visual Studio C++ 17.
I have multiple tabs in the application, one of which is currently implemented using CListBox and needs to be reimplemented using CCheckListBox.
CCheckListBox is a child class of CListBox.
I have a vector of unique CString's I want to display in the box.
To start simple, I tried making the tab with CListBox and by using CListBox::AddString(), this worked exactly as I wanted it to. It sorted the strings alphabetically, and automatically added a scroll bar on the side when the vector had too many CString's to display all at once in the list.
However, when swapping the CListBox variable to a CCheckListBox variable, I have come across the error when I press run along the lines of:
Debug Assertion Failed! ..... \mfc\winctrl3.cpp Line: 588
I found this GitHub link that has the winctrl3.cpp file and on line 588, there is a function called OnLButtonDblClk which is somewhat explained here.
The trouble is I am unsure of how to swap a variable from a parent class to a child class. Is it so different for Windows classes? In the courses and programs I have taken and written in the past, switching from a parent to child variable was straightforward but this is quite the opposite !
Any help is appreciated, thank you in advance ! :D
There is no assertion in line 588 of the source file in your link. That github upload dates back to 2014. Why not search the winctrl3.cpp source file in your own Visual Studio installation instead? In my own installation, it is in function PreSubclassWindow() and there is indeed an assertion there:
// CCheckListBoxes must be owner drawn
ASSERT(GetStyle() & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE));
That is the CCheckListBox must be owner-drawn. Find the CCheckListBox constructor code above, you will notice that it calls the CListBox (parent-class) constructor, applying either of the two owner-drawn styles. Instead, the CListBox constructor calls CWnd::Create() with the "LISTBOX" Windows Class-Name as a parameter. That is, it seems to be a MFC-implemented control, overriding the CListBox class, with an owner-drawn style. There is no mention of a native "Check-ListBox" class or style in the Win32 documentation either. Also the CCheckListBox documentation (in the link you posted) clearly states: CCheckListBox is only for owner-drawn controls because the list contains more than text strings.
Therefore:
If you are creating the control using a resource script, add an owner-drawn style (eg LBS_OWNERDRAWFIXED) to the control (or set the Owner-Drawn property in the property editor).
If you are creating the control programmatically make sure you are calling the CCheckListBox constructor, not the CListBox one.
Not sure what you mean about the scroll-bar (and why you mention it), doesn't the list-box display a scroll-bar automatically (WS_VSCROLL style - or property), do you have to do something on your own?
EDIT:
Forgot to mention, if you are creating the control using a resource script use the Class Wizard to add a member variable (of "Control" type, not "Value") to the dialog class. The new variable must be of type CCheckListBox (MFC class). This will subclass the control. If the old project already had a CListBox member variable change it to CCheckListBox.

How can I open an *modal* file dialog with IFileOpenDialog?

I've been trying to implement a file dialog into my C++ application for a while now, and I achieved good success with the code described in this article (It is german, but the code should be understandable):
https://msdn.microsoft.com/de-de/library/windows/desktop/ff485843(v=vs.85).aspx
However, using this code in my window class, which is a CDialogImpl, I just can't find out how to make this file picker modal. If I use this code, I can just spawn multiple file picker.
Of course, I could just keep track of the state by adding a member variable representing the state, but it would still not solve the problem of being able to click around in the main window while the dialog is opened.
Is there any way which would allow me to make this window modal? I've been trying to scan through all available methods, but I couldn't find anything. I didn't find any flags which could be passed in the creation, neither any options which I could set after creation.
Any help is appreciated!
The example you link to is very simple and has no UI other than the file dialog. Your program is more complex, having a window from which the file dialog is invoked.
You need to provide an owner for the file dialog. When you do that the owner is disabled, part of what makes the dialog modal. Failing to pass an owner means that the other windows are not disabled and so still respond to user input.
The example code provides no owner, but since there are no other windows in that program, that is benign. Modality is only meaningful when there are multiple windows.
So, to solve the problem, pass the owner, the handle of your window, to the Show method of the file dialog.
Disabling owner windows is one of the key parts of a modal dialog. You will find that any API for modal dialogs expects you to specify an owner. Get into the habit of expecting to provide that ownwr window, and looking for the means to do so.

In MFC how to access CView from CWinAppEx

I do not have great expertise in MFC. I have a MFC application with the basic setup: single document application. Now In the project I have the most standard setup: one ...View class one ...Doc class and one ...WinAppEx class. These three represents the model-view-controller design principle respectively. I do not find a way to access View class from the WinAppEx class.
My actual use case is I have some command line arguments passed to the app. I want to display these arguments on my primary view. To access those arguments I have in my ...WinAppEx::InitInstance() method
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
Now I need to pass arguments to the view. I have tried ::GetMainWnd() and traverse its siblings and its children. I also tried ::GetNextDocTemplate() and then ::GetNextDoc() and then ::GetNextView(). I still did not find my ...View instance. Can you please shed some light?
Why do you HAVE to access the view from the application? Why not access the application from the view? From the view class you can call AfxGetApp() and get the command line arguments. Also, for a very simple app, usually there is a global variable defined by the wizard called "theApp".
You may be trying to access the CView before it has been created. At the end of the InitInstance function you can cast the m_pMainWnd to a CMainFrame* and use that pointer to call CMainFrame's GetActiveView() function.

using SHAutoComplete with CEdit control

I am developing an MFC application, can i use SHAutoComplete with a CEdit control? Also is there any ready made auto complete controls are available? or i need to use write all the code for creating the list box below the edit control as user types in edit control?
Just pass CEdit's m_hwnd member to SHAutoComplete. I don't think that extension warrant another class. The listbox is created by the AutoComplete object created by SHAutoComplete.
SHAutoComplete helps to autocomplete paths (system or URL).
If this is a combo box and you want to use autocomplete for suggesting string contained in the combo, you have to write a code to handle it.
There are samples you can find. One I found (working):
http://www.ucancode.net/Visual_C_MFC_COM_faq/Visual-C-Auto-completion-ComboBox-CComboBox.htm

How to change ActiveX control's property programmatically with MFC?

I added a activex control to my MFC project, I don't use the dialog editor to add the control, I just used MFC to generate a wrapper class for the control, and call the "create" member in the wrapper class to create the control programmatically, the code is more or less like:
class CMyView
{
CCalendar m_ctl;
//other members.....
}
int CMyView::OnCreate
{
m_ctl.create("",WS_CHILD|WS_VISIBLE,this,CRect(50,50,100,100));
//.....
}
But I found that the wrapper class provide no way for me to change the control's property, so if I want to change the control's property programmatically, what should I do? Can I achieve this through a wrapper class? Or can it be done programmatically at all? Or is it only can be done via a dialog editor? Thank you.
Yes, wrapper only includes functions, if you create it via class wizard.
To change properties, i.e. variables, you could instantiate ActiveX in a form or a dialog and you would have the ability to modify values of properties in properties window.
If you want to do it on-the-run, you can right click to activeX object and then click on add variable. You will see that it will also create wrapper class for the object. This class will automatically include getters and setters for the activex, visible in the newly generated header file.
If you have already created a wrapper class for your activex, it may not work, try this in a fresh project. You can then copy generated .cpp and .h files to your own project afterwards.