#include <windows.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // some CString constructors will be explicit
#include <atlbase.h>
#include <atlstr.h>
#define MAX_LOADSTRING 100
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING] = "Test Windows Message"; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING] = "MyTestWindows"; // the main window class name
HWND main_hWnd;
HWND btn_hwnd;
DWORD btn_style = WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|WS_CHILD;
int times = 0;
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
#define MY_BUTTON_ID (1001)
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
MSG msg;
MyRegisterClass(hInstance);
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
while (GetMessage(&msg, NULL, 0, 0))
{
if (msg.hwnd == main_hWnd)
{
ATLTRACE("%04u,[Loop Main]MSG:0x%04X,HWND:0x%06x \r\n",times++,msg.message,msg.hwnd);
}
if (msg.hwnd==btn_hwnd)
{
ATLTRACE("%04u,[Loop Button]MSG:0x%04X,HWND:0x%06x \r\n",times++,msg.message,msg.hwnd);
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
//wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN05));
wcex.hIcon = NULL;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
//wcex.lpszMenuName = MAKEINTRESOURCE(IDC_WIN05);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
//wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
wcex.hIconSm = NULL;
return RegisterClassEx(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance;
main_hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, 300, 200, NULL, NULL, hInstance, NULL);
if (!main_hWnd)
{
return FALSE;
}
ShowWindow(main_hWnd, nCmdShow);
UpdateWindow(main_hWnd);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
ATLTRACE("%04u,[Main WndProc]MSG:0x%04X,HWND:0x%06x \r\n",times++,message,hWnd);
switch (message)
{
case WM_CREATE:
{
//ATLTRACE("Main HWND:0x%06x \r\n",main_hWnd);
//ATLTRACE("WndProc HWND:0x%06x \r\n",hWnd);
btn_hwnd = CreateWindowEx(0,"BUTTON","My button",btn_style,10,10,100,50,hWnd,(HMENU)(UINT_PTR)MY_BUTTON_ID,hInst,NULL);
//ATLTRACE("Button HWND:0x%06x \r\n",btn_hwnd);
}
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case 1001:
//MessageBox(hWnd,"abc","bcd",MB_OK);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}