Windows programming to write their own WinMain function

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

This is my reference books MSDN and write their own window procedure,

Entrance, the function of WinMain all windows applications like like main language in the C function, its function is: the completion of a series of definition and initialization, and generates the message loop. The news cycle is the core of the program operation .

The WinMain function is mainly to achieve the following functions:

1 register window, a window, perform other necessary initialization operation,

2 enter the message loop, according to the application message queue message, call the corresponding treatment process,

3 when the message loop to retrieve WM_QUIT message, stop the running program.

The following code:


#include<windows.h>
#include<stdio.h>

LRESULT CALLBACK WndProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
);

int WINAPI WinMain(
  HINSTANCE hInstance,      // handle to current instance
  HINSTANCE hPrevInstance,  // handle to previous instance
  LPSTR lpCmdLine,          // command line
  int nCmdShow              // show state
)
{
	WNDCLASS wndclass;

	wndclass.cbClsExtra = 0;
	wndclass.cbWndExtra = 0;
	wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wndclass.hInstance = hInstance;
	wndclass.lpfnWndProc = WndProc;
	wndclass.lpszClassName = "My window";
	wndclass.lpszMenuName = NULL;
	wndclass.style = CS_HREDRAW | CS_VREDRAW;
	RegisterClass(&wndclass); //Register window class

	HWND hwnd;
	hwnd = CreateWindow("My window, window " "", WS_OVERLAPPEDWINDOW, 
		0, 0, 640, 480, NULL, NULL, hInstance, NULL);

	ShowWindow(hwnd, SW_SHOWNORMAL);
	UpdateWindow(hwnd);

	MSG Msg;
	while(GetMessage(&Msg, NULL, 0, 0))
	{
		TranslateMessage(&Msg); 
        DispatchMessage(&Msg); 
	}

	return 0;
}

LRESULT CALLBACK WndProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
)
{
	switch(uMsg)
	{
	case WM_CHAR:
		char strChar[20];
		sprintf(strChar, "char is %d", wParam);
		MessageBox(hwnd, strChar, "window", MB_OK);
		break;

	case WM_LBUTTONDOWN:
		MessageBox(hwnd, "mouse left-click", "window", MB_OK);
		HDC hdc;
		hdc = GetDC(hwnd);
		TextOut(hdc, 0, 50, "hello mouse left-click", strlen("hello mouse left-click"));
		ReleaseDC(hwnd, hdc);
		break;

	case WM_RBUTTONUP:
		MessageBox(hwnd, "release mouse", "window", MB_OK);
		hdc = GetDC(hwnd);
		TextOut(hdc, 0, 80, "hello release mouse", strlen("hello release mouse"));
		ReleaseDC(hwnd, hdc);
		break;

	case WM_QUIT:
		PostQuitMessage(0);
		break;

	case WM_DESTROY:
		DestroyWindow(hwnd);
	break;

	default:
		return DefWindowProc(hwnd, uMsg, wParam, lParam);
	}

	return 0;
}


Summary:

Feel yourself to write the window is a bit difficult, because writing is not much, a lot of things not to remember, to check the information.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Lori at December 18, 2013 - 10:27 PM