Basic WinAPI Tutorial

C / C++ Discuss, Basic WinAPI Tutorial at Programmers Lounge forum; I just needed to refresh my WinAPI Knowledge. All written/paraphrased by me. What is WinAPI? WinAPI is the API to ...


Go Back   Gamerz Needs - For All Your Gaming Needs! > Technology Zone > Programmers Lounge > C / C++
Forgot Password? | Sign Up!

Notices

Advertisement
   

Reply
 
Bookmark this Thread Tools Display Modes
  #1  
Old 08-18-2008, 07:03 AM
1k Points Wasted
 
Last Online: Yesterday 02:08 PM
Join Date: Aug 2006
Location: over there
Posts: 2,219
Blog Entries: 6
Thanks: 48
Thanked 172 Times in 134 Posts
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
Latest Blog: Very random blog entries (based on time)
Rep Power: 13
cosmeo3000 will become famous soon enoughcosmeo3000 will become famous soon enough
Points: 6,003.76
Bank: 89,660.72
Total Points: 95,664.48
Black - cosmeo3000 
Send a message via MSN to cosmeo3000
Basic WinAPI Tutorial

I just needed to refresh my WinAPI Knowledge.

All written/paraphrased by me.

What is WinAPI?

WinAPI is the API to create windows programs. An API stands for Application Programming Interface. In other words, the WinAPI Allows you to create Windows Programs. With C++, there are 3 different ways to create Form programs.

MFC: MFC stands for Microsoft Foundation Class. It can pretty much create the basics of what you need for you. The only problem is that MFC is only best when creating word processor type programs, and who needs more word processing programs?

Managed C++: Managed C++ Is just a .NET version of C++. It's managed, which gives you less things to do with it. Creating forms with this is very easy because all you need to do is add a windows form and add controls and the managed code will be automatically generated.

WinAPI: WinAPI is what I'm going to be teaching you. You will be using pure native code to create forms. It's the hardest and longest way to create forms.

Requirements
Half a brain (Remembering barely anything that is said is fine, since all forms are basically made this way and you can always come back to a tutorial or the help files)
A C++ Compiler (No MFC Or .NET is needed )
A decent knowledge of C++ (I won't be giving word for word, just telling you what to add, ex. add case WM_CLOSE)

Windows API works by creating a class of the form, which is different from a C++ class. The class has everything about the form you need. Then it creates the form. After it creates a form it starts to wait for a message. A message is anything you do with the form whether it's clicking on it, or resizing it, or w/e. Then once it gets the message, it translates it and finds the block of code that it needs.

The WinAPI Resource is called windows.h. So you will start out with:
Code:
#include <windows.h>
Now you will need to declare the class name. So you will use:
Code:
const char g_szClassName[] = "myWindowClass"
We will now start adding the message loop. The message loop is the last part, that registers what you do and does something. It doesn't matter what order you put it in, as long as you get it all in. So next add:
[LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)[/code]
HWND is the window handle, in other words, which window is it for.
UINT is what the message was.
WPARAM is a parameter for the message.
LPARAM is another parameter. This isn't used as often anymore in 32 bit programming.

This is where whatever you did gets sent to this function. So you add a switch.
Code:
switch(msg)
Now whenever it receives a message, it tries to find what to do.

Now lets add some commands.
add the case: WM_DESTROY and WM_CLOSE.

Inside WM_CLOSE, make it do:
Code:
DestroyWindow(hwnd);
This will call the WM_DESTROY part of the hwnd window, which is the one you're going to create.
and in WM_DESTROY:
Code:
PostQuitMessage(0);
Which is like a return 0; in a console program.
Make the default do:
Code:
     return DefWindowProc(hWnd, Msg, wParam, lParam); // which says if it doesn't recognize the message, just do nothing
End that switch then make the function return 0;

Now it's time for the WinMain Function. The WinMain function is the WinAPI Version of Main, but you don't add custom code in there
So add:
Code:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
hInstance - the instance of the program
hPrevInstance - Back in 16 bit, each program would have its own region of memory, so the PrevInstance would have the memory of the previous instance because if a program ran twice, it'd still share that region of memory.
lpCmdLine - If you were to open this program from a command line prompt, you would use this to control command line parameters.
nCmdShow - This is the parameter for showing the form

Now declare 3 variables.
Code:
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
wc is for the Window Class.
hwnd is the window handle you used in all the other functions too.
Msg is the message that gets sent.

Now you register the window class. What I do is just use Intellisence and register every variable in the class
But if you don't have Visual Studio, you would add:
Code:
wc.cbSize = sizeof(WNDCLASSEX); // Size of the Window Class
wc.style = 0; // Class Styles, usually set to 0
wc.lpfnWndProc = WndProc; // What the WndProc is called pretty much
wc.cbClsExtra = 0; // Extra memory allocated, if any
wc.cbWndExtra = 0; // Extra memory allocated per window, if any
wc.hInstance = hInstance; // The first parameter of WinMain
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);  // Which icon to use, IDI_APPLICATION is just the normal one
wc.hCursor = LoadCursor(NULL, IDC_ARROW); // What Cursor to use, IDC_ARROW is the normal arrow.
wc.hbrBackGround = (HBRUSH)(COLOR_WINDOW+1); // Background color
wc.lpszMenuName =  NULL; // The name of the menu resource, if any
wc.lpszClassName = g_szClassName // The class name, you can just use a regular string, but it's sometimes easier to declare a constant
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); // The 16x16 icon, if you were to alt-tab in Windows XP
Now we should add something just in case something went wrong.
Code:
 if(!RegisterClassEx(&wc))
{
     MessageBox(NULL, "Window registration failed", "Error", MB_OK);
     return 0; //Can't use PostQuitMessage because it wasn't created properly
}
This is for if the class wasn't able to be registered.
Now we create the window.
Code:
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE, // The style of the window
g_szClassName, // The name of the class, this is where making it a const comes in handy
"Title", // The title of the window that comes up in the front
WS_OVERLAPPEDWINDOW, // Another Windows Style 
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120, // The first 2 are for where the window shows up, and the last two are the size of the window in pixels.
NULL, NULL, hInstance, NULL); // First null is the parent window, if you are creating a MDI, 2nd is menu, if you had a menu, 3rd is the instance, first parameter of WinMain, and 4th null is any extra data which is rarely used.
Now we check if it was able to be created.
Code:
if(hwnd == NULL) // We set hwnd to be equal to CreateWindowEx() so if it's null, it hasn't been created.
{
     MessageBox(NULL, "Window Creation Failed", "Error", MB_OK);
     return 0;
}
So now we created it. Now we show it.
Code:
ShowWindow(hwnd, nCmdShow); // To show the window
UpdateWindow(hwnd); // To update the window with w/e it has
Now we wait for messages.
Code:
while(GetMessage(&msg, NULL, 0, 0) > 0)
{
     TranslateMessage(&msg); // Translates what was received into a WM type command
     DispatchMessage(&msg); // Sends the message to the switch loop that we first made
}
return msg.wParam;
}
And that's it Now to add stuff you would go into the WM codes. Of course you haven't added controls yet




Ow that was long =P Half my battery gone and like 45 minutes xD
__________________

Dam my other one won't animate for some reason o_O

Last edited by cosmeo3000; 08-18-2008 at 07:06 AM..
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Advertisement
   


Main Navigation
Home
GzN Forums
GzN Games
GzN News
Top Games
GzN Cheats
GzN Articles
GzN Reviews
GzN Downloads
User Control Panel
Advertising
RSS Feed
2Moons
Adventure Quest
AirRivals
America's Army
Anarchy Online
Archlord
Audition
Battlefield Series
Cabal Online
Call Of Duty Series
Combat Arms
Conquer Online
Counter Strike
Day of Defeat
Deicide Online
Diablo Series
Doom Series
Drift City
Enemy Territory
Eudemons Online
Final Fantasy
Flyff (Fly For Fun)
General Game Discussion
Ghost Online
Granado Espada
Grand Theft Auto Series
Guild Wars
Gunbound
Gunz Online
Habbo Hotel
Half-Life 2
Hero Online
KartRider
Knights Online
Maple Story
Medal of Honor
MU Online
Neopets
Pangya
Quake Series
Ragnarok Online
Rappelz
Rakion
Red Orchestra
Rose Online
Runescape
Scions of Fate
Silkroad Online
Sims Series
Soldier Front
Starcraft
Tales of Pirates
Tibia
The Ship
Trickster Online
TS Online
Unreal Tournament
War Rock
WolfTeam
World of Warcraft & Series
Affiliates
COD4 Hacks
BF2 Hacks


All times are GMT -8. The time now is 06:14 PM.