Everyone probably knows the 'Hello world' code when you started coding.
It is the first turorial you should read whenever you start a new
coding language.
As i haven't read any 'Hello world' example written for use in Windows,
i thought i might just write my own tutorial, so here it is:
First of all we include the windows handler
Next thing we do is hooking the main function, which is called
WinMain for use in Windows. And save the instance outside the function
This is the main function we are going to use, now in this function, we have
to call a function which sets up our main window. We also have to leave the window open
as long as we didn't exit it and keep it updated. So now the main function looks like this:
In the function initApplication which we call in WinMain, we will have to initialize the main window.
Now we have to declare the process function and write it.
Project is attached,
Enjoy!
It is the first turorial you should read whenever you start a new
coding language.
As i haven't read any 'Hello world' example written for use in Windows,
i thought i might just write my own tutorial, so here it is:
First of all we include the windows handler
Code:
#include <windows.h>
Next thing we do is hooking the main function, which is called
WinMain for use in Windows. And save the instance outside the function
Code:
HINSTANCE hInst;
int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
hInst = hInstance;
return 0;
}
This is the main function we are going to use, now in this function, we have
to call a function which sets up our main window. We also have to leave the window open
as long as we didn't exit it and keep it updated. So now the main function looks like this:
Code:
int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
MSG msg;
hInst = hInstance;
initApplication( hInst );
while( GetMessage( &msg, 0, 0, 0 ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return msg.wParam;
}
In the function initApplication which we call in WinMain, we will have to initialize the main window.
Code:
void initApplication( HINSTANCE hInstance )
{
WNDCLASS wC; // Defines the class
wC.cbClsExtra = 0;
wC.cbWndExtra = 0;
wC.hbrBackground = ( HBRUSH ) GetStockObject( LTGRAY_BRUSH ); // Choose background color
wC.hInstance = hInst; // Mention the instance
wC.hCursor = LoadCursor( NULL, IDC_ARROW ); // Load the cursor
wC.hIcon= LoadIcon( NULL, IDI_APPLICATION ); // Load the icon
wC.lpszClassName = "Main"; // Name of the window
wC.lpfnWndProc = ( WNDPROC ) MainProc; // The window process function
wC.lpszMenuName = NULL;
wC.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass( &wC ); // Register the class
// Setting up the position and size
int x = 300;
int y = 300;
int w = 300;
int h = 300;
// Creating the actual window
Main = CreateWindow( "Main", "Main", WS_OVERLAPPEDWINDOW, x, y, w, h, 0, 0, hInstance, 0 );
ShowWindow( Main, SW_SHOW ); // Displays the main window
UpdateWindow( Main ); // Update it
}
Now we have to declare the process function and write it.
Code:
LRESULT APIENTRY MainProc( HWND, UINT, WPARAM, LPARAM ); // Definition
Code:
LRESULT APIENTRY MainProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam ) // Actual process function
{
#define HELLOWORLD 1
switch ( msg )
{
case WM_CREATE: // When WM_CREATE is used, you can create buttons, etc
{
CreateWindow( "Static", "Hello World", WS_CHILD | WS_VISIBLE, 50, 50, 100, 20, hwnd, ( HMENU ) HELLOWORLD, hInst, 0 ); // Create the static
}
break;
case WM_DESTROY: // If WM_DESTROY is used, you should leave the program
{
ExitProcess( 0 );
}
break;
default:
return DefWindowProc( hwnd, msg, wParam, lParam );
}
return 0;
}
Project is attached,
Enjoy!