Welcome to Hackers Alliance!

[Win32 Programming] Basic Window Tutorial

  • Archived

    The forum is archived and used for testing. It is currently read-only to visitors.
    It has been upgraded from vBulletin 3.8.x to XenForo for security purposes and future-proofing. Proprietary code and modifications (such as code database and HA bot) are broken with XenForo and will stay only with vBulletin.

-LeetGamer-

Coder
Coder
Oct 17, 2010
66
2
0
To run this you need to set up a Win32 project:

1] File > New > Project
2] Select Win32
3] If there is an option, select empty project
4] If you selected empty then add a new .cpp file to the project
5] Add the code shown here
6] Enjoy

Main.cpp:
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

:D

-Leet
 

Double_0_negative

Hackers Alliance Veteran
Administrator
Sep 18, 2010
444
8
0
ive always wanted to make a win32 program in c++. ive got the console part down, but i dont understand what any of this does for the win32 app. could you explain what some of these things do?
 

-LeetGamer-

Coder
Coder
Oct 17, 2010
66
2
0
Do you have a specific question? I tried to explain everything already.

Why is this in Xbox section? It is for windows =/
 

Double_0_negative

Hackers Alliance Veteran
Administrator
Sep 18, 2010
444
8
0
oh, i didn't notice the comments, silly me ;)

as for being in the xbox section... dont ask me lol, pimpin solved this same issue with my hello and goodbye thread going to the ps2 section :/ so ask him about it
 
This site has been archived and is no longer accepting new content.

About us

  • Hackers Alliance is a small community forum about gaming and console hacking. Join our humble community to share ideas, game cheats, mods, and be part of an amazing growing community!

Quick Navigation

User Menu