MFC beginner - generated functions in header file?

I need a simple Windows app with just a few buttons. I decided to go with MFC and started with this tutorial http://www.bogotobogo.com/cplusplus/application_visual_studio_2013.php.

When you create a button or whatever and add a Click Action using the properties window Visual Studio will generate code in MyForm.h. Example,

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {}

Why in the tutorial and with the generated function is the functionality of the button put in the header file? Can someone please explain this to me?

Shouldn't the function code go in the .cpp file?

What is normally done with MyForm.cpp?

And what should MyForm.h look like? Do I turn it into a definition, and have my .cpp file that uses the MyForm class?

Thanks!
Last edited on
closed account (z05DSL3A)
That is not MFC.
In this section, we will build UI application using Windows Form provided by Visual Studio 2013.


This is managed code [.net] and it is not C++, it is C++/CLI.

Last edited on
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {}
This code has nothing to do with MFC, it is C++/CLI.
In C++/CLI it is normal that the code for the form is in the header file. A .cpp file normally does not exist for Forms - though in theory you can create one. All non-UI code can go in a normal .cpp file.

A freshly generated form.h looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#pragma once

namespace Test 
{

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->components = gcnew System::ComponentModel::Container();
			this->Size = System::Drawing::Size(300,300);
			this->Text = L"Form1";
			this->Padding = System::Windows::Forms::Padding(0);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
		}
#pragma endregion
	};
}

You should have a .cpp file with the main function which looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Test.cpp : main project file.

#include "stdafx.h"
#include "Form1.h"

using namespace Test;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
	// Enabling Windows XP visual effects before any controls are created
	Application::EnableVisualStyles();
	Application::SetCompatibleTextRenderingDefault(false); 

	// Create the main window and run it
	Application::Run(gcnew Form1());
	return 0;
}

In the beginning it is probably the best to use the IDE to do most of the work, i.e. drag the controls from the toolbox onto the form and create the event handlers in the properties tab.

Here are some tutorials:
https://www.youtube.com/playlist?list=PL29CAA8B8290D2CB7
https://www.youtube.com/watch?v=YR6fxe1wa8g&list=PLS1QulWo1RIZz6uDid--I09EOImRmPHS0
Well crap, thanks guys!

No wonder I was so confused... so obviously I'm new to C++/CLI as well.

Last edited on
closed account (z05DSL3A)
If you want to learn the foundations of windows programming, I would suggest starting here:

Learn to Program for Windows in C++
https://msdn.microsoft.com/library/windows/desktop/ff381399(v=vs.85).aspx

I don't recommend C++/CLI for user interface programming. It is more for creating wrappers to interface the managed and unmanaged world. C# would be a better choice for UI. It is very capable and worth while learning (at some point).

I found MFC a lot easier to understand once I learnt a bit about WinAPI programming. I don't know if it is worth spending the time learning MFC.

Universal Windows Platform (UWP) might be something you are is will become interested in. I haven't looked into this too much yet but your choices, C++ wise, seem to be C++/CX (the CX part is a Microsoft extension) or C++ with Windows Runtime C++ Template Library (WRL).

Universal Windows Apps (C++)
https://msdn.microsoft.com/en-us/library/hh875012.aspx

Windows Runtime C++ Template Library (WRL)
https://msdn.microsoft.com/en-us/library/hh438466.aspx

Topic archived. No new replies allowed.