dan369
July 24th, 2010, 08:23 PM
Hi, guys/gals will rather new here to the windows API but been following a few tutorials online, looking more in-depth at each function i use using MSDN.
So got a bit bored earlier, so decided to create my very own window using my other code as a template, looking at msdn in particular i was looking more at all the other options available to me to use in creating my window.
So after about half an hour, finally got the code done and compiled up without any errors, when to run it and well the window doesn't show :(
So anyway here's the code:
/*
Creating a simple Window using windows API.
*/
//Includes
#include <windows.h>
//Setup Callbacks
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
//Closing the Window by clicking the X
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_KEYDOWN:
switch(wParam)
{
case VK_ESCAPE:
DestroyWindow(hwnd);
break;
default:
break;
}
//Destoy Open Windows and any Child windows
case WM_DESTROY:
//Indicate to the system that we want to quit
PostQuitMessage(0);
break;
default:
//Use Default window procedure
DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
//Main Window Function
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
//Window handler
HWND hWnd;
//Create Windows ClassEX struct
WNDCLASSEX wc;
//Create message struct
MSG Msg;
//Register our Window Class
wc.cbSize = sizeof(WNDCLASSEX); //Size of the structrure in bytes needed
wc.style = CS_HREDRAW | CS_VREDRAW; //Redraw if window width/height is ever changed. Also have a shadow effect here
wc.lpfnWndProc = WindowProc; //Use our window procedure function above
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW); //load default cursor
wc.hbrBackground = (HBRUSH)COLOR_WINDOW; //Background colour for our window
wc.lpszClassName = L"WindowClass"; //String of our class name
RegisterClassEx(&wc); //Finally register the window classEX structure
//Create Our window to overlap also with a vertical scroller, width and height = 300.
hWnd = CreateWindow(L"WindowClass", L"My Window", WS_OVERLAPPEDWINDOW, 300, 300, 800, 600, NULL, NULL, hInstance, NULL);
//Show our window, controls how the window is to be shown, default is "Normal".
ShowWindow(hWnd, nCmdShow);
//Update Our Window
UpdateWindow(hWnd);
//GetMessage waits until a posted message is available, PeekMessage doesn't wait
while(GetMessage(&Msg, NULL, 0, 0))
{
//Translates Virtual Key messages into character ones so they can be used
TranslateMessage(&Msg);
//Dispatches a message to the window procedure
DispatchMessage(&Msg);
}
return Msg.wParam;
}
Please excuse the heavy commenting, just helps me personally remember :). Would love to now what exactly i'm doing wrong here.
So got a bit bored earlier, so decided to create my very own window using my other code as a template, looking at msdn in particular i was looking more at all the other options available to me to use in creating my window.
So after about half an hour, finally got the code done and compiled up without any errors, when to run it and well the window doesn't show :(
So anyway here's the code:
/*
Creating a simple Window using windows API.
*/
//Includes
#include <windows.h>
//Setup Callbacks
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
//Closing the Window by clicking the X
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_KEYDOWN:
switch(wParam)
{
case VK_ESCAPE:
DestroyWindow(hwnd);
break;
default:
break;
}
//Destoy Open Windows and any Child windows
case WM_DESTROY:
//Indicate to the system that we want to quit
PostQuitMessage(0);
break;
default:
//Use Default window procedure
DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
//Main Window Function
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
//Window handler
HWND hWnd;
//Create Windows ClassEX struct
WNDCLASSEX wc;
//Create message struct
MSG Msg;
//Register our Window Class
wc.cbSize = sizeof(WNDCLASSEX); //Size of the structrure in bytes needed
wc.style = CS_HREDRAW | CS_VREDRAW; //Redraw if window width/height is ever changed. Also have a shadow effect here
wc.lpfnWndProc = WindowProc; //Use our window procedure function above
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW); //load default cursor
wc.hbrBackground = (HBRUSH)COLOR_WINDOW; //Background colour for our window
wc.lpszClassName = L"WindowClass"; //String of our class name
RegisterClassEx(&wc); //Finally register the window classEX structure
//Create Our window to overlap also with a vertical scroller, width and height = 300.
hWnd = CreateWindow(L"WindowClass", L"My Window", WS_OVERLAPPEDWINDOW, 300, 300, 800, 600, NULL, NULL, hInstance, NULL);
//Show our window, controls how the window is to be shown, default is "Normal".
ShowWindow(hWnd, nCmdShow);
//Update Our Window
UpdateWindow(hWnd);
//GetMessage waits until a posted message is available, PeekMessage doesn't wait
while(GetMessage(&Msg, NULL, 0, 0))
{
//Translates Virtual Key messages into character ones so they can be used
TranslateMessage(&Msg);
//Dispatches a message to the window procedure
DispatchMessage(&Msg);
}
return Msg.wParam;
}
Please excuse the heavy commenting, just helps me personally remember :). Would love to now what exactly i'm doing wrong here.