Hi

I am learning windows programming in C++ . I just did a tutorial learning simply making a window appear & I am surprised by the all the things necessary to make a window appear. Maybe its just the tutorial that is too complicated with unecessary code.

Is all this code necessary just to create a window?

Is there a simpler code, is all windows coding this complicated? Does anyone know of any good windows programming tutorials? :)

#include "winnie.h"

// This is the entry point of every Windows program
int WINAPI WinMain
(HINSTANCE hInst, HINSTANCE hPrevInst, char * cmdParam, int cmdShow)
{
	char className [] = "Winnie";

	// Define a Window Class and register it under the name "Winnie"
	WinClass winClass (WindowProcedure, className, hInst);
	winClass.Register ();

	// Create and show a window
	WinMaker win ("Hello Windows!", className, hInst);
	win.Show (cmdShow);

	MSG  msg;
	int status;
	// Keep pumping messages--they end up in our Window Procedure
	while ((status = ::GetMessage (&msg, 0, 0, 0)) != 0)
	{
		if (status == -1)
			return -1;
		::DispatchMessage (&msg);
	}

	return msg.wParam;
}

WinClass::WinClass (WNDPROC wndProc, char const * className, HINSTANCE hInst)
{
	_class.style = 0;
	_class.lpfnWndProc = wndProc;  // Window Procedure: mandatory
	_class.cbClsExtra = 0;
	_class.cbWndExtra = 0;
	_class.hInstance = hInst;           // owner of the class: mandatory
	_class.hIcon = 0;
	_class.hCursor = ::LoadCursor (0, IDC_ARROW); // optional
	_class.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1); // optional
	_class.lpszMenuName = 0;
	_class.lpszClassName = className;   // mandatory
}

WinMaker::WinMaker
(char const * caption, char const * className, HINSTANCE hInstance)
{
	_hwnd = ::CreateWindow (
			className,              // name of a registered window class
			caption,                // window caption
			WS_OVERLAPPEDWINDOW,    // window style
			CW_USEDEFAULT,          // x position
			CW_USEDEFAULT,          // y position
			CW_USEDEFAULT,          // witdh
			CW_USEDEFAULT,          // height
			0,                      // handle to parent window
			0,                      // handle to menu
			hInstance,              // application instance
			0 );                    // window creation data
}

// Window Procedure called by Windows with all kinds of messages

LRESULT CALLBACK WindowProcedure
(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	// In this simple program, this is the only message we are processing
	case WM_DESTROY:
		::PostQuitMessage (0);
		return 0; // return zero when processed

	}
	// All the unprocessed messages go there, to be dealt in some default way
	return ::DefWindowProc (hwnd, message, wParam, lParam );
}

Is this your first time with GUIs??? If so, i suggest learning Gtk+ first, it makes understanding Win32 much easier

Technically, you could make a window with just this:

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int CmdShow)
{

    MessageBox(NULL, TEXT("First Program"), TEXT("First"), MB_OK );

    return 0;
}

But normally a Win32 window looks like this:

#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
  MSG  msg ;    
  HWND hwnd;
  WNDCLASS wc;
	
  wc.style         = CS_HREDRAW | CS_VREDRAW;
  wc.cbClsExtra    = 0;
  wc.cbWndExtra    = 0;
  wc.lpszClassName = TEXT( "Window" );
  wc.hInstance     = hInstance ;
  wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
  wc.lpszMenuName  = NULL;
  wc.lpfnWndProc   = WndProc;
  wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  
  RegisterClass(&wc);
  hwnd = CreateWindow( wc.lpszClassName, TEXT("Window"),
                WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                100, 100, 250, 150, NULL, NULL, hInstance, NULL);  

  ShowWindow(hwnd, nCmdShow);
  UpdateWindow(hwnd);

  while( GetMessage(&msg, NULL, 0, 0)) {
    DispatchMessage(&msg);
  }
  return (int) msg.wParam;
}

LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
  switch(msg)  
  {
    case WM_DESTROY:
      {
        PostQuitMessage(0);
        return 0;
      }
  }
  return DefWindowProc(hwnd, msg, wParam, lParam);
}

seems like a lot of crap for just a simple window, right? Well everything in that example has a purpose.

This explains it very well:
http://zetcode.com/tutorials/winapi/window/

As for a good tutorial look at this one:
http://zetcode.com/tutorials/winapi/

thanks for links & info :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.