Code Rulers Software - English Version

Programming through Life

  • Home
  • Programming
    • C#
    • C++
  • Download
  • Services
  • About Me

Simple WinAPI Window Sample in C++

By Real_Noname on 24/10/2013

So to make the title true, this is the code:

#include <Windows.h>

LRESULT WindowProc(HWND hWindow, UINT uMessage, WPARAM wParam, LPARAM lParam){
	switch (uMessage)
	{
	case WM_CLOSE:
		PostQuitMessage(0);
		return 0;

	default:
		return DefWindowProcW(hWindow, uMessage, wParam, lParam);
	}
}

int _stdcall wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR lpCmdLine, int nShowCmd){
	LPCWSTR szClassName = L"SampleWindowClass";

	WNDCLASSEXW windowClass = { 0 };

	windowClass.cbSize = sizeof(WNDCLASSEX);
	windowClass.lpszClassName = szClassName;
	windowClass.hInstance = hInstance;
	windowClass.lpfnWndProc = WNDPROC(WindowProc);
	windowClass.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;

	if(!RegisterClassExW(&windowClass))
		return -1;

	HWND hWindow = CreateWindowExW(WS_EX_OVERLAPPEDWINDOW, szClassName, L"Window Sample", WS_OVERLAPPED | WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU, 0, 0, 640, 480, 0, 0, hInstance, 0);
	if(hWindow == 0 || hWindow == INVALID_HANDLE_VALUE)
		return -1;

	ShowWindow(hWindow, nShowCmd);

	MSG msg = { 0 };
	do{
		if(PeekMessageW(&msg, 0, 0, 0, PM_REMOVE)){
			TranslateMessage(&msg);
			DispatchMessageW(&msg);
		}
	}
	while(msg.message != WM_QUIT);

	DestroyWindow(hWindow);
	UnregisterClassW(szClassName, hInstance);

	return msg.wParam;
}

It’s very simple, and straight to the point. If you create a Win32 Project in Visual Studio (an empty one), add an main.cpp source file and then paste this code into it – it should compile without problems. The program runs with a very basic window, with no background colour and it will terminate the application when you try to close it.

A neat trick to initialize a class or structure is to use the table initialization with single zero:

WNDCLASSEX windowClass = { 0 };

This will assign the single value to each field of the class (or structure). Unfortunately this method is limited only to field types that can have zeros assigned to – that leaves out any type with strong typing (like C++0×11 enum class type) and have to by initialized properly.

In future posts this code will be the base for samples that initialize graphic API’s like OpenGL, DirectX 9, DirectX 11, etc.

Posted in C++, Programming | Tagged c++, programming, sample, winapi, windows | Leave a response

Template definition and declaration separation

By Real_Noname on 08/10/2013

One of the more annoying things about templates is their requirement  of one source file per object definition. You cannot have separate declaration in an header and a definition in separate source file. At the time of template usage ALL its definition must be known. But there is a neat trick I found while reading Doom 3 BFG source code for better template classes maintenance. Continue reading “Template definition and declaration separation”

Posted in C++, Programming | Tagged c++, code design, programming, templates | Leave a response

Template dynamic casting… or better trust your time-proofed code

By Real_Noname on 08/10/2013

While writing my little API framework I run into a rather strange problem – my smart pointer code started to throw null pointer exception. It only did that when there was an attempt to assign a null pointer to it (that should never happen).

Continue reading “Template dynamic casting… or better trust your time-proofed code”

Posted in C++, Programming | Tagged c++, errors, polymorphism, programming, templates | Leave a response

Polymorphism and its Destructor, or how to know, that virtual isn’t working

By Real_Noname on 10/10/2010

Recently, it took me to prototype  some of my old libraries, which I wrote as a young coder. I wanted to see which of them are still usable, and in the meantime I was going to check a few things about C + +, which I never had time to test on my own. One of the more surprising things is class inheritance combined with polymorphism and a destructor. Continue reading “Polymorphism and its Destructor, or how to know, that virtual isn’t working”

Posted in C++, Programming | Tagged c++, polymorphism, programming | Leave a response

Delegate and Events in C#

By Real_Noname on 01/09/2010

As I have been working in C # a long time as a tool where you can quickly make tools (ironically) with a friendly interface - quickly and conveniently too, at some point I had to add to a new class its own, so-called, Event (trigger, action, or whatever it is called). How to do it was obviously written in the MSDN Library,but you had to know exactly what you’re looking for to get there. Continue reading “Delegate and Events in C#”

Posted in C#, Programming | Tagged c#, delegate, event, programming | Leave a response

Search

Archives

  • October 2013
  • October 2010
  • September 2010

Tags

c# c++ code design delegate errors event polymorphism programming sample templates winapi windows

Copyright © 2023 Code Rulers Software - English Version.

Powered by WordPress and Hybrid.