#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#define SEC_MAIN_WINDOW_CLASS "My (CGSG) sample class name"
LRESULT CALLBACK WinFunc( HWND hWnd, UINT Msg,
WPARAM wParam, LPARAM lParam );
typedef struct tagVIEWEXT
{
INT W, H;
HBITMAP hDib;
DWORD *Bits;
} VIEWEXT;
INT WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
CHAR *CmdLine, INT CmdShow )
{
WNDCLASSEX wc;
HWND hWnd;
INT ret_code;
MSG msg;
if (hPrevInstance != NULL || CmdLine == NULL || CmdShow == -1)
return -1;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.cbClsExtra = 0;
wc.cbWndExtra = sizeof(VIEWEXT *);
wc.hInstance = hInstance;
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wc.lpfnWndProc = WinFunc;
wc.lpszMenuName = NULL;
wc.lpszClassName = SEC_MAIN_WINDOW_CLASS;
if (!RegisterClassEx(&wc))
return 0;
hWnd = CreateWindow(SEC_MAIN_WINDOW_CLASS,
"Second CGSG sample",
WS_OVERLAPPEDWINDOW,
0, 0, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, CmdShow);
UpdateWindow(hWnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
ret_code = msg.wParam;
return ret_code;
}
LRESULT CALLBACK WinFunc( HWND hWnd, UINT Msg,
WPARAM wParam, LPARAM lParam )
{
INT x, y, w, h, l;
HDC hDC, hMemDC;
CREATESTRUCT *cs;
PAINTSTRUCT ps;
BITMAPINFOHEADER bmi;
VIEWEXT *Ex;
RECT rc;
static CHAR Buf[100];
switch (Msg)
{
case WM_CREATE:
cs = (CREATESTRUCT *)lParam;
if ((Ex = malloc(sizeof(VIEWEXT))) == NULL)
return -1;
memset(Ex, 0, sizeof(VIEWEXT));
SetWindowLong(hWnd, 0, (LONG)Ex);
return 0;
case WM_SIZE:
Ex = (VIEWEXT *)GetWindowLong(hWnd, 0);
w = LOWORD(lParam);
h = HIWORD(lParam);
if (Ex->hDib != NULL)
DeleteObject(Ex->hDib);
bmi.biSize = sizeof(BITMAPINFOHEADER);
bmi.biWidth = w;
bmi.biHeight = -h;
bmi.biBitCount = 32;
bmi.biPlanes = 1;
bmi.biCompression = 0;
bmi.biSizeImage = w * h * 4;
bmi.biXPelsPerMeter = bmi.biYPelsPerMeter = 1;
bmi.biClrUsed = 0;
bmi.biClrImportant = 0;
Ex->hDib = CreateDIBSection(NULL, (BITMAPINFO *)&bmi,
DIB_RGB_COLORS, (VOID **)&Ex->Bits, NULL, 0);
if (Ex->hDib != NULL)
Ex->W = w, Ex->H = h;
else
Ex->W = Ex->H = 0;
return 0;
case WM_MOUSEMOVE:
Ex = (VIEWEXT *)GetWindowLong(hWnd, 0);
x = lParam & 0xFFFF;
y = (lParam >> 16) & 0xFFFF;
if (wParam & MK_LBUTTON)
{
Ex->Bits[y * Ex->W + x] = 0xFFFFFF;
rc.left = x;
rc.top = y;
rc.right = x + 1;
rc.bottom = y + 1;
InvalidateRect(hWnd, &rc, FALSE);
}
hDC = GetDC(hWnd);
l = sprintf(Buf, "X:%07d Y:%07d", x, y);
TextOut(hDC, 10, 10, Buf, l);
MoveToEx(hDC, x, y, NULL);
LineTo(hDC, 0, 0);
ReleaseDC(hWnd, hDC);
return 0;
case WM_PAINT:
Ex = (VIEWEXT *)GetWindowLong(hWnd, 0);
hDC = BeginPaint(hWnd, &ps);
hMemDC = CreateCompatibleDC(hDC);
SelectObject(hMemDC, Ex->hDib);
BitBlt(hDC, 0, 0, Ex->W, Ex->H, hMemDC, 0, 0, SRCCOPY);
DeleteDC(hMemDC);
EndPaint(hWnd, &ps);
return 0;
case WM_DESTROY:
Ex = (VIEWEXT *)GetWindowLong(hWnd, 0);
DeleteObject(Ex->hDib);
free(Ex);
PostQuitMessage(10);
return 0;
}
return DefWindowProc(hWnd, Msg, wParam, lParam);
}