• (2 Pages)
  • +
  • 1
  • 2

Introduction to the WinAPI Creating a basic window Rate Topic: -----

#1 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • Joined: 14-September 07

Posted 15 August 2008 - 09:39 PM

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:

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():

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:

#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:

#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>"


Posted 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

This post has been edited by KYA: 16 August 2008 - 03:38 PM


Is This A Good Question/Topic? 4
  • +

Replies To: Introduction to the WinAPI

#2 purewal27  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 03-November 08

Posted 06 December 2008 - 02:22 PM

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.
Was This Post Helpful? 0
  • +
  • -

#3 IcarusFish  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-January 09

Posted 20 January 2009 - 10:24 PM

Hello. I really liked the tutorial, but I have a slight bone to pick. There's a bit that keeps effing up when I try to compile. I've tried a few things, but it's not really helping so much. This bit here
int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, int nShow)
{
	HWND hwnd = NULL;
	MSG Msg; \\so on and so forth...


keeps glitching out on me. My compiler says something about not defining a function before a "{" token. Is this format specifically for Visual Studio or something (I have an open source compiler) or did I eff something up?
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is online

  • Head MFIC
  • member icon

Reputation: 5066
  • View blog
  • Posts: 26,449
  • Joined: 10-May 07

Posted 20 January 2009 - 10:29 PM

I believe that the comment slashes are simply in the wrong direction.
Was This Post Helpful? 0
  • +
  • -

#5 brandolyton  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 14-June 09

Posted 15 June 2009 - 03:50 PM

You didn't really explain it at all. You just said "here is some scripts" basically. I didn't learn anything :(
Was This Post Helpful? 0
  • +
  • -

#6 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Posted 24 November 2009 - 08:50 PM

With VS C++ 2008 would I do: Create New Project>Windows 32 Project>Windows Application(Empty Project)?
Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • Joined: 14-September 07

Posted 25 November 2009 - 09:22 PM

Yes, unless you want a bunch of precompiled crap handed to you.
Was This Post Helpful? 0
  • +
  • -

#8 nautica17  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 155
  • Joined: 28-August 09

Posted 22 March 2010 - 01:03 PM

I've started this tutorial and it looks really nice but from the beginning I have a problem. I used the following code:
#include <windows.h>

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


But when I run it, I get Chinese/Mandarin character. I don't have a Chinese OS, let alone any programs in Chinese. I use VC++ 2008. Can someone help me out with this one?

Thanks.
Was This Post Helpful? 0
  • +
  • -

#9 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • Joined: 14-September 07

Posted 22 March 2010 - 01:09 PM

What multibyte/unicode settings do you have on?
Was This Post Helpful? 0
  • +
  • -

#10 nautica17  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 155
  • Joined: 28-August 09

Posted 22 March 2010 - 01:26 PM

View PostKYA, on 22 March 2010 - 12:09 PM, said:

What multibyte/unicode settings do you have on?

How would I check that in VC++?
Was This Post Helpful? 0
  • +
  • -

#11 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • Joined: 14-September 07

Posted 22 March 2010 - 01:28 PM

Right click on Project title->Properties->Configuration Properties->General->Character Set



If you could post a screenshot that would be helpful as well.
Was This Post Helpful? 0
  • +
  • -

#12 nautica17  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 155
  • Joined: 28-August 09

Posted 22 March 2010 - 01:39 PM

View PostKYA, on 22 March 2010 - 12:28 PM, said:

Right click on Project title->Properties->Configuration Properties->General->Character Set



If you could post a screenshot that would be helpful as well.


http://i42.tinypic.com/161f5fm.png

It says Unicode I guess.


Edit: I changed the setting. Works now. Thanks! :)

This post has been edited by nautica17: 22 March 2010 - 01:42 PM

Was This Post Helpful? 0
  • +
  • -

#13 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • Joined: 14-September 07

Posted 22 March 2010 - 01:44 PM

Good deal!


For the screenshot I was referring to your MsgBox()
Was This Post Helpful? 0
  • +
  • -

#14 nautica17  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 155
  • Joined: 28-August 09

Posted 22 March 2010 - 01:45 PM

View PostKYA, on 22 March 2010 - 12:44 PM, said:

Good deal!


For the screenshot I was referring to your MsgBox()


Haha.. sorry misread that. Basically it was just in Mandarin instead of English.
Was This Post Helpful? 0
  • +
  • -

#15 Guest_Marios Zenios*


Reputation:

Posted 07 February 2011 - 03:54 PM

Hello
The tutorial was very helpful, but what i wanted to ask, is how can you make the window close automatically, without pressing any buttons or touching the computer, after 2 minutes let's say?
Can you answer to my question?
Thanxs in advance

Marios Zenios
Was This Post Helpful? 0

  • (2 Pages)
  • +
  • 1
  • 2