Post: WINAPI Tutorial [C++]
03-11-2011, 12:08 AM #1
(adsbygoogle = window.adsbygoogle || []).push({}); I have recently been reading an online WINAPI tutorial, its really great.

The first few times i quickly ran through this i was just playing around, kind of got the gist on how to use it, not write but get the Gist.. you know what i mean?

After playing around, i read it fully in depth going through each step writing it all into DevCpp and trying to understand it all, finally understood the concept.

The website is : You must login or register to view this content.

heres my example code of a small form:

    #include <windows.h>

const char ClassName[] = "MyWindowClass";

LRESULT CALLBACK WndProc(HWND WindowHandle, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE: DestroyWindow(WindowHandle); break;
case WM_DESTROY: PostQuitMessage(0); break;
case WM_LBUTTONDOWN: MessageBox(NULL,"You Called? With teh Left click?","Ey?",NULL); break;
case WM_RBUTTONDOWN: MessageBox(NULL,"You Called? With teh Right click?","Ey?",NULL); break;
default: return DefWindowProc(WindowHandle, msg, wParam, lParam);
}
return 0;
}

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, LPSTR lpCmdShow, int nCmdShow)
{
WNDCLASSEX wc;
MSG Msg;

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW);
wc.lpszMenuName = NULL;
wc.lpszClassName = ClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Windows Registration Failed!", "Error", NULL);
return 0;
}

HWND WindowHandle;

WindowHandle = CreateWindowEx(
WS_EX_CLIENTEDGE,
ClassName,
"My Window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 240,
NULL, NULL, hInstance, NULL
);

if(WindowHandle == NULL)
{
MessageBox(NULL, "Create Window Failed!","Error",NULL);
return 0;
}

ShowWindow(WindowHandle, nCmdShow);
UpdateWindow(WindowHandle);

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


Extra Tips:

To begin this tutorial you need have a good understanding of:

[]Structures
[]Pointerss
[]Functions
[]Windows API Variables (LPSTR, UINT, Ect).

If you need help, Google is your friend.
03-12-2011, 03:06 AM #2
kiwimoosical
Bounty hunter
It's all simple, it's just a new-ish syntax you need to get used to along with calling conventions (WINAPI, _stdcall, those guys), and methods xP

Like I said, I usually don't do important work in C++, most of my free-lance programming is done in either java for android or in C# for making the GUI quickly, although I love C++ more, C# is so much easier to make a GUI in >_< Whatevz though, nice tut for beginners. I feel bad that you post these and then no one thanks them because they don't understand them, I feel for yah :'(

I'll rep yah Happy

The following user thanked kiwimoosical for this useful post:

TheUberFail

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo