Function to center app in the middle of the screen in Win32Api

Enjoy. 🙂

/*must include this header for the win32api interface*/
#include <windows.h>

/*define your screen_x_and_y_size*/
#define WIN_X_SIZ 640
#define WIN_Y_SIZ 480


int GetMiddle(int wh)
{
    int x, y, ret=0;
    x = GetSystemMetrics(SM_CXSCREEN)/2;
    y = GetSystemMetrics(SM_CYSCREEN)/2;
    
    switch(wh)
    {
        case 0: ret=x-(WIN_X_SIZE/2);break;
        case 1: ret=y-(WIN_Y_SIZE/2);break;
        case 2: ret=(WIN_X_SIZE);break;
        case 3: ret=(WIN_Y_SIZE);break; 
    }
    return ret;
}

/*use it like this*/
hwndMain = CreateWindow(
    "Class Name",
    "Window Title",
    WS_SYSMENU | WS_TILED, /*type of windows*/
    GetMiddle(0), //x window location
    GetMiddle(1), //y window location
    GetMiddle(2), //cx window location
    GetMiddle(3), //cy window location
    NULL, // No parent for this window
    NULL, //The class Menu
    hInst, //Who created this window
    NULL //no params to pass on
);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s