Jump to content





Recent Status Updates

  • Photo
      16 Apr
    Kadence

    If you're reading this, you're on my profile and I know you're on my profile because I'm probably viewing yours.

    Show comments (6)
  • Photo
      10 Apr
    Poe

    Finally (and hopefully) i'm getting a team together that knows a little of this and a little of that; and maybe all my open source projects that are half written can begin to be released. :)

View All Updates
Photo
- - - - -

WinApi tutorial

form

  • Please log in to reply
12 replies to this topic

#1 Moudi

Moudi

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 131 posts

Posted 25 March 2010 - 05:58 AM

Alright lets make our first empty useless widow xDDD
Now to do the windows stuff we must include the windows header file
windows.h
uhmm so
as any normal snippet, we have a main() function, in WINAPI its like the following:

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
            LPSTR lpCmdLine, int nCmdShow )
Alright lets explain.

HINSTANCE hInstance handles the current instance of application running.

HINSTANCE hPrevInstance handles the previous instance running or ran or w/e.

LPSTR lpCmdLine The command line arguments as a single string. NOT including the program name.

int nCmdShow is the current status of the window, either hidden, system tray maximised, whatever, we'll learn about them in another tut xD

now that we know what all that stuff was about, lets see what "msg" is

a message, well you can consider it as a loop that keeps checking for activity in window, any buttons were pressed, windows created, windows quit, or stuff.. we declare it like so
MSG msg;
So untill now we have
#include<windows.h>
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
            LPSTR lpCmdLine, int nCmdShow )
{
MSG msg;
now to register our window ^_^

what forms a window my fellow coders ?
The class name, its handle, its background color, its cursor and the procedure used by this window. All of that is saved in a structure, then the structure is registered .unless you want to register it one by one >.>

so lets say our structure is wc.
WNDCLASS wc = {0};
wc.lpszClassName = TEXT( "My Window" );
wc.hInstance = hInstance ;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc ;
wc.hCursor = LoadCursor(0,IDC_ARROW);

explanation ? there you go"
lpsz class name is the class name of our window.
hInstance is the instance for our window.
background color is the backgounr color for our window
hCursor is the cursor used in this window, in our case its the normal IDC_ARROW.

now we must register our struct xD We do like so

RegisterClass(&wc); , the wc is the structures name, kkay ? kay, move on..

now to create the window, ths thing we can see =)

we use Createwindow() to create a window, textbox or any object in our win32 application.

our former code is
#include<windows.h>
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
            LPSTR lpCmdLine, int nCmdShow )
{
MSG msg;
WNDCLASS wc = {0};
  wc.lpszClassName = TEXT( "My Window" );
  wc.hInstance     = hInstance ;
  wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
  wc.lpfnWndProc   = WndProc ;
  wc.hCursor       = LoadCursor(0,IDC_ARROW);
RegisterClass(&wc);
Now creating our window
CreateWindow( wc.lpszClassName, TEXT("Our first window xD!"),
                 WS_VISIBLE|WS_OVERLAPPEDWINDOW,
                100, 100, 500, 500, 0, 0, hInstance, 0);
Just add them together i'm not going to add them now,

now we should get the message, get the message ?
as i have said earlier, we use a loop to see any action happening in our window, by using the msg;
while( GetMessage(&msg, NULL, 0, 0)) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
This will read the msg, translate it then pass it on,

now that we're done with that WinMain function we should return a value aight xD ?

return (int) msg.wParam;
}

here we are casting msg.wParam as an integer..

so our WinMain source is now
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
            LPSTR lpCmdLine, int nCmdShow )
{
  MSG  msg ;
  WNDCLASS wc = {0};
  wc.lpszClassName = TEXT( "Static Control" );
  wc.hInstance     = hInstance ;
  wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
  wc.lpfnWndProc   = WndProc ;
  wc.hCursor       = LoadCursor(0,IDC_ARROW);


  RegisterClass(&wc);
  CreateWindow( wc.lpszClassName, TEXT("Our first window xD"),
                 WS_VISIBLE|WS_OVERLAPPEDWINDOW,
                100, 100, 500, 500, 0, 0, hInstance, 0);

  while( GetMessage(&msg, NULL, 0, 0)) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
  return (int) msg.wParam;
}
oh my bad btw, WS_VISIBLE is a property, it tells the window to be visible, WS_OVERLAPPEDWINDOW puts the minimize,maximize and close button. kay ? kay.

now to our new function to handle the sent 'msg'
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
This is the Window procedure function, that handles any procedure done by the window,

HWND = handle,
UINT msg , is the msg we used earlier xD

so to read the message we're going to use a switch statement,

  switch(msg)
  {
    case WM_CREATE:
    {
   break;
}
case WM_COMMAND:
        {}
break;
case WM_DESTROY:
 PostQuitMessage(0);
return 0;
}
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
Alright i rammed them all at once but i'm sleepy, so case WM_CREATE is if the window was successfully created, here you can place if you want to create any more textboxs or anythihng.

case WM_COMMAND handles if any commands were given..
case WM_DESTROY is if the window was " DESTROYED "

I will write another part on handling WM_COMMAND xD

basically after reading this, you should know how a windowed program works, but not how to create one, after my second tutorial maybe you'll be able to create your own :)

If you enjoyed it, thank me with a +rep ^_^

Edited by Moudi, 26 March 2010 - 01:01 AM.

  • -1

#2 Ancient Dragon

Ancient Dragon

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 378 posts

Posted 25 March 2010 - 08:00 PM

I don't think anyone can write a better tutorial than this one.
  • 0

#3 Moudi

Moudi

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 131 posts

Posted 26 March 2010 - 12:59 AM

Well i tried !

Edited by Moudi, 26 March 2010 - 03:47 AM.

  • 0

#4 genux

genux

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 76 posts

Posted 26 March 2010 - 01:16 AM

great tutorial :).

I am mainly a Linux developer, but going I am needing to do some Windows stuff and this is a great :).
  • 0
int coffeePerDay = 10; // need to cut down!!!
Codingfriends

#5 James.H

James.H

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 569 posts

Posted 26 March 2010 - 05:47 AM

Great tut Moudi, very informative +rep
  • 0

#6 Moudi

Moudi

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 131 posts

Posted 26 March 2010 - 06:30 AM

Thanks james :)
  • 0

#7 TEDSON

TEDSON

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 30 March 2010 - 10:57 AM

Superb tut.

I'm brand new to programming and the forums, but never expected to see this level of detail
and explaination.

Thank you.
  • 0

#8 Moudi

Moudi

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 131 posts

Posted 30 March 2010 - 11:24 AM

Thanks TED
  • 0

#9 empty_kinse

empty_kinse

    CC Lurker

  • Just Joined
  • Pip
  • 6 posts

Posted 15 April 2010 - 12:47 AM

At first I thought this isn't that good of a tut, but after reading other WinApi tuts from other sites, this actually is the best one around. Thanks
  • 0

#10 Blaxus

Blaxus

    CC Lurker

  • Just Joined
  • Pip
  • 5 posts

Posted 11 June 2010 - 03:47 AM

I'm guessing that this is for coding windows forms in NON - Visual C++ environment?
Either way, very detailed, i will have a go at this, so thanks ;)
  • 0

#11 Sysop_fb

Sysop_fb

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 151 posts
  • Location:Missouri

Posted 22 June 2010 - 10:22 AM

Should get into a habbit of error checking registerclass.

Other words good simple introduction.

You should probably redo the tabbing and beautify the message loop code. Since the message loop is basically the heart of a winapi program.
  • 0
"The best optimizer is between your ears" - Michael Abrash
Saying you can optimize a program is like saying you understand how a program works on every level of every facet on a specific machines configuration.

#12 bobmacans

bobmacans

    CC Lurker

  • Just Joined
  • Pip
  • 7 posts

Posted 01 July 2010 - 09:32 PM

It is difficult but it is very informative.
  • 0





Also tagged with one or more of these keywords: form