Page 1 of 2

Creating a Windows Form

Posted: Sun Feb 20, 2011 11:49 pm
by RyanTheCoder
This shows you how to make a basic windows form in C++ through code
Code: Select all
#include <windows.h>

/*  Declare Windows a  procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  put class name into a global variable  */
char szClassName[ ] = "WindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    HWND hwnd;               /* handle for our window */
    MSG messages;            /* messages to the application are saved */
    WNDCLASSEX wincl;        /* data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* recognize double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* use your default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* no menu */
    wincl.cbClsExtra = 0;                      /* no extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* windows's default color as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, if fails, quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* class is registered, let's create a program!!*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Windows App",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nFunsterStil);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

Re: Creating a Windows Form

Posted: Sun Feb 20, 2011 11:51 pm
by Axel
Doesn't VC++ Have this as default when you load a win32 project ?

Re: Creating a Windows Form

Posted: Mon Feb 21, 2011 12:04 am
by RyanTheCoder
Axel wrote:
Doesn't VC++ Have this as default when you load a win32 project ?
i dont use VC, i dont even know what it stands for, second, i code in notepad and debug it myself

Re: Creating a Windows Form

Posted: Mon Feb 21, 2011 12:05 am
by RyanTheCoder
RyanTheCoder wrote:
Axel wrote:
Doesn't VC++ Have this as default when you load a win32 project ?
i dont use VC, i dont even know what it stands for, second, i code in notepad and debug it myself
just realised that VC stands for Visual C, :|

Re: Creating a Windows Form

Posted: Mon Feb 21, 2011 12:05 am
by Snyper345
Most program other than Visual Studio C++ require you to make the form from scratch

Re: Creating a Windows Form

Posted: Mon Feb 21, 2011 12:06 am
by RyanTheCoder
Snyper345 wrote:
Most program other than Visual Studio C++ require you to make the form from scratch
thus this code is here

Re: Creating a Windows Form

Posted: Mon Feb 21, 2011 9:03 pm
by Axel
I know I was just wondering if Visual C++ have got this by default lol

Re: Creating a Windows Form

Posted: Sun Feb 27, 2011 7:34 pm
by GoodGuy17
You should give credits to http://www.daniweb.com/code/snippet216375.html, because it looks like that is where you got this code. My mistake if you didn't get it there.

Re: Creating a Windows Form

Posted: Sat Mar 12, 2011 5:46 pm
by RyanTheCoder
GoodGuy17 wrote:
You should give credits to http://www.daniweb.com/code/snippet216375.html, because it looks like that is where you got this code. My mistake if you didn't get it there.
actually no. my dads a programmer, my moms a programmer, and i like to program. i didn't get it from any websites, sorry.

Re: Creating a Windows Form

Posted: Sat Mar 12, 2011 5:50 pm
by zachman61
RyanTheCoder wrote:
GoodGuy17 wrote:
You should give credits to http://www.daniweb.com/code/snippet216375.html, because it looks like that is where you got this code. My mistake if you didn't get it there.
actually no. my dads a programmer, my moms a programmer, and i like to program. i didn't get it from any websites, sorry.
I think your lying, and this looks extremely close to it if not it exactly