Full Version: Introduction to the WinAPI
Dream.In.Code > Programming Tutorials > C++ Tutorials
KYA
Well, you have done console program after console program, but now you feel ready to move onto windowed C++ programs. For those of you who have used java, I'm sure you had a rude awakening as i did, that C++ has no built in methods for GUI creation. As the programmer you will have to pick a library and run with it. In this tutorial we will cover the WinAPI.

The entry point for console programs was int main()

For the WinAPI program you will use:

cpp

int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, int nShow)
{
//code
}


Let's go over each parameter:

HINSTANCE hInst -- the current instance of your program (i.e. the window)
HINSTANCE hPrev -- the previous instance of your program (if you needed it for example)
LPSTR -- long pointer to a string can be used to insert a command
int ncmdshow -- is your window showing? show it? etc... (don't really worry about this one for now).

The other important part of a winAPI program is the Window Procedure (Usually called WndProc():

cpp

LRESULT CALLBACK WinProc(HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
//handle messages
}


HWND -- your window
UINT msg -- message to be processed UINT = unsigned integer
WPARAM, LPARAM parameters that can be interpreted (such as mouse clicks)

Ok, now we have the main components, let's do a hello world program:

cpp

#include <windows.h>

int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, int nShow)
{
MessageBox(NULL, "Hello World!", "Dream in code", MB_OK);
return 0;
}


A little box will pop up and say hello world! Pretty easy huh? Let's create a basic window that tells us when we clicked the left and right mouse buttons:

cpp

#include <windows.h>
//Globals for reference
HWND hwndStatic;
TCHAR* Simple = TEXT("A window for <Dream in Code>");

void CreateWindowItems(HWND hwnd, HINSTANCE hInst)
{
//Text "area"
hwndStatic = CreateWindow(TEXT("static"), TEXT(" This is some text"),
WS_CHILD | WS_VISIBLE,
150, 30, 400, 200, hwnd, NULL, hInst, NULL);
return;
}

//message handler
LRESULT CALLBACK WinProc(HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{

switch(Msg)
{
case WM_CREATE:
//empty for now
break;
case WM_LBUTTONDOWN:
SetWindowText(hwndStatic, "You clicked the left mouse button!");
break;
case WM_RBUTTONDOWN:
SetWindowText(hwndStatic, "You clicked the right mouse button!");
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, Msg, wParam, lParam);
}

BOOL InitializeWindow(HWND hwnd, HINSTANCE hInst, int CmdShow)
{ //windows basic handlers
WNDCLASS wc;
//The WNDCLASS struct wc is filled here by assigning all members of the struct
wc.lpfnWndProc = WinProc;
wc.hInstance = hInst;
wc.style = CS_BYTEALIGNCLIENT;
wc.lpszMenuName = NULL;
wc.lpszClassName = Simple;
wc.hIcon = LoadIcon(hInst, NULL);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.cbWndExtra = 0; wc.cbClsExtra = 0;


if (!RegisterClass(&wc))
MessageBox(hwnd, "Register class failed!", "Yay!", MB_OK);
//Create Window
hwnd = CreateWindow(Simple, Simple, WS_OVERLAPPEDWINDOW |WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, HWND_DESKTOP, NULL, hInst, NULL);

if (!hwnd){
MessageBox(hwnd, "It did not work!", "Yay!", MB_OK);
}
CreateWindowItems(hwnd, hInst);
ShowWindow(hwnd, CmdShow);
UpdateWindow(hwnd);
//All is good
return TRUE;

}

int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, int nShow)
{
HWND hwnd = NULL;
MSG Msg;
InitializeWindow(hwnd, hInst, nShow);

while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return (int)Msg.wParam;
}


Note how we move all the messy window creation to a separate function (which would normally be in a separate header file, but out here for display purposes). This code will create a window, paint the background black and will have the title of "A window for <Dream in Code>"


IPB Image


Now click the left and right mouse buttons and watch the "text box" as you do. Neat huh? In the next few tutorials we will work on encapsulating the boiler plate windows code, buttons, etc... Happy coding!

--kya

edited to add picture and a typo
purewal27
i didn't undestand this. i am just looking for a simple dialog box to enter first name, last name, address etc. i appreciate that you tried to help me. but i m not that smart. i couldn't understand ur code. it is little complicated for me.





This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.