For some reason, almost all C++ programming tutorials start with the command-line version of Hello World (the console application). However, for today’s youngsters, who have grown up using a mouse to click on a window or icon to operate a computer, the text in that mysterious black window is like a book. For those of us from the DOS era, that black window may evoke many memories, but for today’s GUI era, that window has little meaning. I do not usually tell young people what is in that black window except to show that I am knowledgeable and inscrutable.
Since C++ programming is now taught by young people, let me tell you how to build a GUI version of Hello World.
If you haven’t built Code::Blocks yet, take a look at my other post:
Build a 64-bit C++ development platform with Code::Blocks
“.
Write a Windows GUI program in C++ in Code::Blocks and compile it in both 32-bit and 64-bit versions.
1. Create a Windows GUI program using the wizard
Creating a Windows GUI program is pretty much the same as creating a Console Application, so before looking at the steps below, take a look at my other article”
Experience C++ programming under Code::Blocks
“.
In the following description, I will briefly or omit any of the same actions as creating a console program. This is not just to save me time, but also to save a little space on the server.
Step 1: Open the New Project wizard
This step is the same as creating a console program, but the rest is different. There are two ways:
① Main menu: File->New->Projects
② Click Create New Project on the Start Here page
Step 2: Select the project type Win32 GUI Project in the wizard
Step 3: Select the window type and click Next
Provide two window types, the meaning of which will be explained later, for now just choose one (for example, select Dialog Based).
Step 4: Enter the project name and file
Select the location to save the file, enter the title of the project, the project file name automatically generated according to the title.
The interface and operation are the same as the console program, and the program name is set to gHello.
Step 5: Select the compiler
This step has the same interface, content, and actions as the console application. Just leave the default and click Finish.
The generated files are a bit more than console files, including source code, header files, resource files, and so on.
Step 6: Compile and run
Click the compile run button in the image above to execute the compile process. As long as there are no errors, the program will run after the compile link is complete and the following window appears. This is just an empty window, and although it has no content, it looks the same as the window of a normal program.
At this point, your first Windows GUI application has been created, isn’t it easy?
2. Hello World
Although the generated window is empty, at least there is a familiar appearance. Later, as needed, you can add code to achieve more functionality.
Now change the title of the window from Dialog to Hello World
Double-click on the resource. Rc file in the left pane of Code::Blocks and you will see the following Code in the right Code edit area
[url=]
[/url]
//
Generated by ResEdit 1.5.10
//
Copyright (C) 2006-2012
//
http://www.resedit.net
#include <windows.h>#include <commctrl.h>#include <richedit.h>#include
”
resource.h
”
//
//
Dialog resources
//
LANGUAGE
0
, SUBLANG_NEUTRALDLG_MAIN DIALOG
0
.
0
.
186
.
95
STYLE DS_3DLOOK | DS_CENTER | DS_MODALFRAME | DS_SHELLFONT | WS_CAPTION | WS_VISIBLE | WS_POPUP | WS_SYSMENUCAPTION
”
Dialog
”
FONT
8
.
”
Ms Shell Dlg
”
{}
//
//
Manifest resources
//
LANGUAGE
0
, SUBLANG_NEUTRAL
1
RT_MANIFEST
”
.\\manifest.xml
”
[url=]
[/url]
Change CAPTION “Dialog” to CAPTION “Hello World”. Then compile and run as described in step 6. Is Hello World programmed into the title of the results window?
Although this revision has no practical significance, at least it has changed as we intended. Later, we can modify or add code to achieve more meaningful functions.
3. Compile to 32bit and 64bit programs respectively
For native code, compiling to generate 32bit and 64bit programs is simple.
If you are using 32bit TDM-gcc, the result will be a 32bit application. If you are using 64-bit TDM-gcc, follow these steps to compile 32bit and 64bit programs.
3.1 Compilation Generates 32-bit applications
Step 1: Right-click on the project name ghello in the left pane of Code::Blocks and select Build Options from the pop-up menu to open the project Build options window.
Step 2: Select the build target and click [OK]
As shown, you see the Build target option in the Project Build Options window. If you select 32bit, the generated program is 32bit. If you select 64bit or neither, the generated program is 64bit.
Select 32bit and click [OK] to close the Build Target options window.
Step 3: Recompile
Right-click on the project name ghello in the admin bar to the left of Code::Blocks and select Rebuild from the pop-up menu to begin recompiling the program. Then, wait patiently until the compilation is complete.
Step 4: Run and verify the compilation results
Click on the Run button (green triangle) in the Code::Blocks toolbar to run the application, and a Hello World window appears in the title bar.
Right-click the operating system main menu button in the lower left corner of the computer screen, and choose Task Manager from the pop-up menu to open the task Manager window.
Go to Gheloo.exe in the Apps bar of the window and you’ll find a (32-bit) tag at the end indicating that the application is 32-bit.
3.2 Compiling and Generating 64-bit Applications
Repeat steps 1 through 4 in 4.1, except that in step 1, select 64bit in the Build target options window, and otherwise do the same.
You will notice that the parentheses after gheloo.exe in the task manager window in step 4 are missing after the recompilation, indicating that the compiled application is 64-bit.