Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[C++ Win32] Basic Windows Form [TuT]
05-26-2010, 09:49 AM
Post: #1
[C++ Win32] Basic Windows Form [TuT]
TUT 1: A Simple Window.

-Start-
Hello, and welcome to my tutorial on how to make a Basic Window, also know as a GUI.
First of all to make a Win32 Windows Application, you must create a project.

Follow these first steps:
1. Open Visual Studio
2. File > New > Project
3. Select Win32
4. Click "Win32 Console Application", Enter Name and click "ok".
5. Click "Next"
6. Application type: Select "Windows Application"
7. Additional Options: Select "Empty Project"
8. Right Click "Source Files" in the Solution Explorer
9. Add > New Item
10. Then add a "C++ File (.cpp)" Enter Name and click "ok"

Now you should have a blank .cpp you can edit. (Simple eh?)

Alright, now for the code.

In console like you should all know, it starts off with int main(). Well in Windows Application its:

Code:
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nShowCmd)

Heres the code for a Basic 300x300 Window.

Code:
#define WIN32_LEAN_AND_MEAN /*excludes APIs such as Cryptography, DDE, RPC, Shell, and Windows Sockets.*/
#include <windows.h> // Windows API

LRESULT CALLBACK WinProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam) // MSG Loop Callback
{
    switch(msg)  
    {
        case WM_COMMAND: // Sent here when a button is clicked, etc.
            {
                switch(wParam)
                    {
                        /*
                        case <Control ID>:
                        {
                            //Don't worry about this for now (Will be used later when creating controls IE: Buttons, TextBox)
                        }
                        */
                    }
                break;

            case WM_CLOSE:
                {
                    ExitProcess(0);
                }
            break;

            case WM_QUIT:
                {
                    ExitProcess(0);
                }
                break;
                
            }
            break;
    }
    return DefWindowProc(hWnd,msg,wParam,lParam);
}
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nShowCmd)
{
    WNDCLASSEXW wClass;
    ZeroMemory(&wClass,sizeof(WNDCLASSEXW));

    /*Settings*/
    wClass.cbSize = sizeof(WNDCLASSEXW); // Set size of WinClass before using it
    wClass.hbrBackground = GetSysColorBrush(COLOR_3DFACE); // Background
    wClass.hCursor = LoadCursor(NULL,IDC_ARROW); // Cursor
    wClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); // Icon
    wClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION); // Icon
    wClass.hInstance = hInst;
    wClass.lpfnWndProc = (WNDPROC)WinProc;
    wClass.lpszClassName = L"Window Class";

    if(!RegisterClassEx(&wClass)) // Class registration
    {
        MessageBoxA(NULL,"Window class creation failed","Window Class Failed",MB_OK|MB_ICONERROR);
        return 0;
    }

    HWND hWnd = CreateWindowExW(NULL,
                L"Window Class",
                L"BlueMelon's Simple GUI", // Form Title
                WS_OVERLAPPEDWINDOW, // dwStyles
                CW_USEDEFAULT,CW_USEDEFAULT,300,300,/*x,y,w,h*/
                NULL,NULL,hInst,NULL);

    ShowWindow(hWnd,nShowCmd);

    MSG msg;
    ZeroMemory(&msg,sizeof(MSG));

    while(GetMessage(&msg,NULL,0,0)) // Send MSG to CallBack
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0;
}

The program works as the following.

Main:
1. Create Class with settings
2. Registers it
3. Creates Window with Class Settings and styles in CreateWindowEX()
4. Send MSG to CallBack where it will be processed.

CallBack (msg loop):
1. Receive the MSG
2. Process the received MSG.
wParam and lParam contain extra information about whats going on.(Can be NULL)

I.E. Win Form with a button, you click the button, it sends a MSG to WM_COMMAND, there you can catch it with a switch (wParam) and modifie what happens when a user clicks a button.

Press F5(Debug), and you should get this:
[Image: vq49q8.png]

Hope you enjoyed my tutorial. And please, do not leach.

Your's Truly,
- BlueMelon

Note: I'm re-writing most of my tuts, so stay tuned for more! Also, PM me with requests.

Don't forget to say thanks!, Keep the thread Alive as this may help ALOT of people!

[Image: 30ifbbd.png]
Find all posts by this user
Quote this message in a reply
05-26-2010, 05:59 PM
Post: #2
RE: [C++ Win32] Basic Windows Form [TuT]
When you select Console application and try to make a simple GUI from that, you need to write a lot of code and to feel how it works. But in object orientated drag & drop programming languages form is already there.
But the difference is that in this case you have better understanding.
Very good tut !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
05-27-2010, 05:05 AM
Post: #3
RE: [C++ Win32] Basic Windows Form [TuT]
(05-26-2010 05:59 PM)drdebcol Wrote:  When you select Console application and try to make a simple GUI from that, you need to write a lot of code and to feel how it works. But in object orientated drag & drop programming languages form is already there.
But the difference is that in this case you have better understanding.
Very good tut !

Drag&Drag requires .net framework, might as well use Vb.net.

This is Win32 C++ Coding =D

[Image: 30ifbbd.png]
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: