Code:
// Include pre-written code to help us out
#include <Windows.h>
// Globals
HINSTANCE hInstance; // Holds the .exe in computer's memory
HWND hwnd; // Handles the window
MSG msg; // Message structure
WNDCLASSEX WindowClass; // WindowClass, this will hold some information on the window
// Function prototypes
LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
bool InitWindow ();
// hInstance: Holds the .exe in computer's memory
// hPrevInstance: Not used in Win32
// lpCmdLine: Not used in this program
// nCmdShow: An integer to show how the window will be displayed
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lPCmdLine, int nCmdShow)
{
// Call InitWindow ()
// If it returns false then display a message box telling us then close the program
if (!InitWindow ())
{
MessageBox (NULL,
L"InitWindow () Returned False",
NULL,
MB_OK | MB_ICONWARNING);
return 0;
}
// Message loop, this will loop around and send messages to the Window Procedure from the message qeueu to be processed
while (GetMessage (&msg, NULL, 0, 0) > 0)
{
TranslateMessage (&msg); // Changes the msg around a little
DispatchMessage (&msg); // Sends the message to the Window procedure
}
// End the program
return msg.wParam;
}
// Window Procedure, where all the messages are handled
LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
// These three case statements are for if the message is to close the window
// If it is, then we will actually close the window
case WM_CLOSE:
DestroyWindow (hwnd);
break;
case WM_DESTROY:
PostQuitMessage (0);
break;
case WM_KEYDOWN:
switch (wParam)
{
case VK_ESCAPE:
DestroyWindow (hwnd);
}
break;
}
// Send all messages not handled to the Default Window procedure
return DefWindowProc (hwnd, msg, wParam, lParam);
}
// InitWindow, this will create the window for us
bool InitWindow ()
{
WindowClass.cbClsExtra = 0; // Extra bytes to allocate for the class
WindowClass.cbSize = sizeof (WNDCLASSEX); // Size of the structure
WindowClass.cbWndExtra = 0; // Extra bytes to allocate for the window
WindowClass.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1); // background color, right now it's white, edit the 1
WindowClass.hCursor = LoadCursor (NULL, IDC_ARROW); // Cursor for the window, loads the default cursor
WindowClass.hIcon = LoadIcon (NULL, IDI_APPLICATION); // Icon
WindowClass.hIconSm = LoadIcon (NULL, IDI_APPLICATION); // Small Icon
WindowClass.hInstance = hInstance; // hInstance
WindowClass.lpfnWndProc = WndProc; // The wndproc for the class
WindowClass.lpszClassName = L"1"; // name of the class, will be refered to later
WindowClass.lpszMenuName = NULL; // no menu so no menu name
WindowClass.style = 0; // N/A
if (!RegisterClassEx (&WindowClass)) // call registerclassex function, if it fails return false
{
return false;
}
hwnd = CreateWindowEx (WS_EX_CLIENTEDGE, // One variable of the style
L"1", // name of the class
L"Win32 Example", // title of the window
WS_OVERLAPPEDWINDOW, // Another variable of the style
315, 115, // X, Y co-ordinate values of where to start your window at
640, 480, // Width, Height of your window
NULL, // parent window
NULL, // menu handle
hInstance, // instance
NULL); // N/A
if (hwnd == NULL) // Check to see if everything went ok
{
return false;
}
ShowWindow (hwnd, SW_SHOWNORMAL); // Show the window to use
return true; // return true, because a window was created good
}
Note: A messsage is a key press or a mouse click. If you have ANY questions at all let me know here or with a PM and I will try my best to help
Bookmarks