C++ Windows Forms application -- where to implement OnInitDialog()? - c++

I'm new to programming and I am experimenting with Windows Forms Applications on Visual Studio C++ 2012. I added a comboBox to the form and want to initialize with values determined at runtime. I did some research and found that I need to define the OnInitDialog() function. Where do I implement this (and how)? Visual Studio has created two source files: Form1.h and .cpp.
Thank you.

OnInitDialog() is for MFC dialogs, not Windows Forms -- you can initialize your controls either in each control's constructor or in the form's Load event (OnLoad overridable method) -- some more info in this SO answer.

OnInitDialog need to be part of a class which is derived from CDialog or CDialogEx.
If you used Visual Studio project creation wizard and selected MFC type application and then as Dialog based, you will have OnInitDialog function created by the wizard automatically.

Related

How do I edit my Windows Desktop Application main dialog in VIsual Studio Commun2019?

I have to create a Win32 app for a college class.
I created the "Windows Desktop Application" in the create project section of the Visual Studio Community 2019 and it comes already with a standard dialog that can be tested on the go. Problem is: I cannot edit the main dialog. I can create another dialog and edit it but the main one is not accessible in the resource editor. What can I do? I can't find anything on google. Please help.
The main window of the default application created by the "Windows Desktop Application" project template in Visual Studio is not created with a dialog resource. It is created by registering a window class associated with a window procedure, as is standard when creating a desktop application.
You "edit" that window by changing the source code not with a visual editor.
>>I cannot edit the main dialog. I can create another dialog and edit it but the main one is not accessible in the resource editor.
Yes you can create it, but it has no class and can't be an object.You can try to create a Win32 desktop application, and then create a dialog. When you right-click the dialog box, you will find that you cannot add class.
If you want to use this dialog, you can only use this function DialogBoxW(hInstance, lpTemplate, hWndParent, lpDialogFunc) to create and show the dialog in your program. The third parameter is the Handle of parent window and it can be NULL. The forth parameter is the callback function.
So we test to call DialogBoxW in WinMain. You can check the picture below. We abandoned the traditional Win32 framework and made the custom window our main window. It work.
However, it should be noted that windows created in this form are modal, which is not applicable in many scenarios. What you say and what you want to do may be better done with MFC. Win32 does not encapsulate many interfaces like MFC for you to call.
I use Visual Studio 2013 Enterprise, but the procedures will be the same:
1 - On the Solution Explorer, click on the .RC file
2 - In the new window (Resource View), click in Dialog
3 - Double-click the dialog you want to edit...
That´s it...

Using ListControl in a Dialog window

Can ListControl be used in a dialog in a Non-MFC project? I am using visual c++ 2010.
The examples I have seen so far uses MFC, so it seems to me that ListControl is part of MFC. The code I am working on is not MFC based, however, Visual Studio still allows adding a ListControl to the dialog in the resource view, and generates rc code for the List Control. So my guess is that I should be able to use it. However, I could not use the standard method found online to add variable to the ListControl and use it.
How can I use the ListControl in this case? e.g. adding a column or write something to a cell? Some code example will certainly help.
The CListCtrl class is an MFC class. It can only be used from within an MFC project.
However, CListCtrl is simply a wrapper around the ListView common control, and a ListView control can be used in any Windows application—no MFC required.
The Resource Editor included with Visual C++ (confusingly) refers to a ListView control as a "List Control". You can insert one on your dialog, and all it will do is insert a ListView control.
If you're using MFC, you can choose to create a member variable corresponding to that control. The type of that member variable will be CListCtrl, because it is encapsulating access to a ListView control on your dialog.
If you are not using MFC, you can still use the ListView control, you'll just have to use the standard SDK mechanisms for accessing and manipulating it. For example, to insert an item into the ListView control on your dialog, you would obtain the control's window handle (GetDlgCtrlID) and send it a LVM_INSERTITEM message. The SDK documentation contains sample code listings, but they are a rather poor way to learn. The best resource for good old Windows SDK programming is still Charles Petzold's Programming Windows.

Opening another form in mfc

I need to close the current form dialog and open another form after the user clicks a button in an mfc application. Basically it is for my school project application.
I have decided use mfc in visual c++ for my project and i have obsolutely no guidance regarding the language.
I had chosen single document and CFormView class in mfc application wizard and used the resource editor to design two forms one of them is displayed at start of program and i need the other to open up and replace existing form.
Had tried googling for an answer but couldn't understand anything. I have basic c++ oop knowledge but i am completely new to mfc.
I use visual studio 2013 ultimate.
You don't close current dialog truely, you can just hide it. So to do like this:
CCurrentDialig::OnBtnCliekted()
{
ShowWindow(this->m_hWnd, SW_HIDE);
COtherDialog dlg;
dlg.DoModal();
}

how to add windows form in mfc application?

i wanna know how to add windows form to the MFC application (Dialog-based) and connect it with the MFC Dialogs,so i can program with visual c++ ?
http://www.functionx.com/visualc/Lesson04.htm
I think this is what you are looking for, hope this helps.
The MFC class CWinFormsControl "provides the basic functionality for hosting of a Windows Forms control in a MFC application".
One can encapsulate more Winform standard controls in a .NET custom control (derived from the System.Windows.Forms.Control class) and then use it in the MFC app through the CWinFormsControl.
The MFC application must use the /clr flag (Common Language Runtime Support).

MFC without document/view architecture

I'd like some help on using MFC without the document/view architecture.
I created a project without doc/view support, Visual C++ created a CFrameWnd and a view that inherits from CWnd. I replaced the view inheriting from CWnd with a new view that inherits from CFormView.
However, when I run my program, after I close the window I get a heap corruption error.
If inside where the frame window handles WM_CREATE, you change the code to create the instance of CFormView with the "magic" id of AFX_IDW_PANE_FIRST, you'll find it becomes the view for the frame window. This is due to the behaviour of CFrameWnd::InitialUpdateFrame(), which will be called from within MFC. The MSDN page comments on this helpful little feature:
http://msdn.microsoft.com/en-us/library/ch3t7308.aspx
Since you want to use the dialog editor and you don't want the document/view architecture, then maybe a "Dialog based" application is what you need.
The problem is MFC's lifecycle management. The view declaration (created by Visual C++ wizard) is:
CChildView m_wndView;
I replaced the above code with:
CChildFormView m_wndView;
CChildView inherits from CWnd, CChildFormView inherits from CFormView. Both views were created by the wizard, but only CChildFormView uses the DECLARE_DYNCREATE/IMPLEMENT_DYNCREATE macros.
Since m_wndView is being created in the stack, when MFC automagically calls delete I get the error.