Here’s how easy it is to code a Windows GUI application Blowing Smoke. You don’t even need Visual Studio, Code::Blocks, QT, wxWidgets, .NET, Windows Forms, C++/Cli or or anything else besides a compiler to translate your source code to an executable binary file, i.e., an exe. I have Microsoft’s Windows 7 SDK (free download as far as I know) installed on my wife’s Net Book – an Acer Aspire One with an Atom x64 processor. Its pretty weak, but more than enough to do the job.
When you install the Sdk you should end up with various options on your Start Menu, if you are running some version of Windows with a Start Menu. For me I chose…
|
Microsoft Windows Sdk Version 7.1 >>> Windows SDK 7.1 Command Prompt
|
Then a command prompt console window opens. There is a help file with the installation of my version that tells you various switches to set various options such as whether you want x86 or x64 executables, whether you want Debug or Release builds done. Lets go for broke and so x64 Release builds. The command looks like so…
When I executed that I got this on my console screen…
1 2 3 4 5
|
Setting SDK environment relative to C:\Program Files\Microsoft SDKs\Windows\v7.1
\.
Targeting Windows 7 x64 Release
C:\Program Files\Microsoft SDKs\Windows\v7.1>
|
Next I created a directory/folder for a Hello, World! Program. Got to admire Dennis Ritchie. If you don’t know who he is you had better look it up. Here’s the directory I created…
C:\Code\Win7Sdk\Hello
Then I executed a change directory command to move to that directory…
1 2
|
C:\Program Files\Microsoft SDKs\Windows\v7.1>CD\
C:\>CD C:\Code\Win7Sdk\Hello
|
At that point here’s what I’m seeing in my console window…
1 2 3 4 5 6 7 8 9
|
Setting SDK environment relative to C:\Program Files\Microsoft SDKs\Windows\v7.1
\.
Targeting Windows 7 x64 Release
C:\Program Files\Microsoft SDKs\Windows\v7.1>cd\
C:\>CD C:\Code\Win7Sdk\Hello
C:\Code\Win7Sdk\Hello>
|
Now open Notepad.exe and type this in there…
1 2 3 4 5 6 7 8 9
|
#include <cstdio>
int main(void)
{
printf("Hello, World!\n");
getchar();
return 0;
}
|
Save the file as Main.cpp to C:\Code\Win7Sdk\Hello\Hello.cpp, or to wherever you have created a directory on your computer for it, and have navigated to there with my directions above. I saved my file as described above and executed a dir command which reveals what’s in my folder, and here’s what my console window is showing me…
1 2 3 4 5 6 7 8 9 10 11 12 13
|
C:\Code\Win7Sdk\Hello>dir
Volume in drive C is Acer
Volume Serial Number is 1C3F-BA6D
Directory of C:\Code\Win7Sdk\Hello
10/26/2015 07:34 PM <DIR> .
10/26/2015 07:34 PM <DIR> ..
10/26/2015 06:54 PM 97 Hello.cpp
1 File(s) 97 bytes
2 Dir(s) 254,879,039,488 bytes free
C:\Code\Win7Sdk\Hello>
|
So now I’ve a 97 byte file and am ready to test my compiler. So I type this at the command prompt…
cl Hello.cpp [ENTER]
You don’t type the [ENTER]. Just hit the key after typing cl Hello.cpp. Here’s my screen now…
1 2 3 4 5 6 7 8 9 10 11 12
|
C:\Code\Win7Sdk\Hello>cl Hello.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 16.00.30319.01 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
Hello.cpp
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:Hello.exe
Hello.obj
C:\Code\Win7Sdk\Hello>
|
If I type a dir now I’ll get this…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
C:\Code\Win7Sdk\Hello>dir
Volume in drive C is Acer
Volume Serial Number is 1C3F-BA6D
Directory of C:\Code\Win7Sdk\Hello
10/26/2015 07:38 PM <DIR> .
10/26/2015 07:38 PM <DIR> ..
10/26/2015 06:54 PM 97 Hello.cpp
10/26/2015 07:38 PM 54,272 Hello.exe
10/26/2015 07:38 PM 1,027 Hello.obj
3 File(s) 55,396 bytes
2 Dir(s) 254,878,912,512 bytes free
C:\Code\Win7Sdk\Hello>
|
As you can see, it made my Hello.exe. It’s a whooping 54272 bytes. Ridiculous. With my VS 2008 VC installation I can cut that down to about 3.5 k in x64, believe it or not, and that’s with /MT static linking. But that’s another story.
Now, you wanted a GUI program. Make another directory named \Forms. Then make a directory under that named Form1. I have this…
C:\Code\Win7Sdk\Forms\Form1
Into \Form1 type this into Notepad and save it as Main.cpp…
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
|
// Main.cpp
// cl Main.cpp Kernel32.lib User32.lib Gdi32.lib /O1 /Os /MT /GA /FeForm1.exe
#include <windows.h>
LRESULT CALLBACK fnWndProc(HWND hwnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
}
return (DefWindowProc(hwnd, msg, wParam, lParam));
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
{
char szClassName[]="Form1";
WNDCLASSEX wc={};
MSG messages;
HWND hWnd;
wc.lpszClassName = szClassName;
wc.lpfnWndProc = fnWndProc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.hbrBackground = (HBRUSH)COLOR_BTNSHADOW;
wc.hInstance = hInstance;
RegisterClassEx(&wc);
hWnd=CreateWindowEx(0,szClassName,szClassName,WS_OVERLAPPEDWINDOW,200,175,320,200,HWND_DESKTOP,0,hInstance,0);
ShowWindow(hWnd,iShow);
while(GetMessage(&messages,NULL,0,0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
|
Then do a CD\ followed by another change directory to bring you to wherever you need to be. For me its…
CD C:\Code\Win7Sdk\Forms\Form1
Then type this at the command prompt…
|
cl Main.cpp Kernel32.lib User32.lib Gdi32.lib /O1 /Os /MT /GA /FeForm1.exe
|
Then hit [ENTER]. I then got this…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
C:\Code\Win7Sdk\Forms\Form1>cl Main.cpp Kernel32.lib User32.lib Gdi32.lib /O1 /O
s /MT /GA /FeForm1.exe
Microsoft (R) C/C++ Optimizing Compiler Version 16.00.30319.01 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
Main.cpp
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:Form1.exe
Main.obj
Kernel32.lib
User32.lib
Gdi32.lib
C:\Code\Win7Sdk\Forms\Form1>
|
Then execute Form1.exe and you’ll see your first GUI program! Its that easy! I can actually cut that down to about 5k, but it takes some work!