#pragma comment(linker, "/subsystem:windows")
#include <windows.h>
// ----------------------------------------------------------------------------
static HDC g_hDC = NULL;
static HDC g_memDC = NULL;
static HBITMAP g_memBM = NULL;
static POINTS g_cPointsBegin = {0};
static BOOL g_bDrawEnable = FALSE;
// ----------------------------------------------------------------------------
static void GetCurClientSize(HWND hWnd, int& nWidth, int& nHeight)
{
RECT rc = {0};
GetClientRect(hWnd, &rc);
nWidth = rc.right - rc.left;
nHeight = rc.bottom - rc.top;
}
static void ChangeMemDCSize(HWND hWnd, HDC hDC, HDC& memDC, HBITMAP& memBM)
{
if ( hWnd && hDC && memDC && memBM )
{
int nWidth = 0;
int nHeight = 0;
GetCurClientSize(hWnd, nWidth, nHeight);
HDC memDCNew = CreateCompatibleDC(hDC);
HBITMAP memBMNew = CreateCompatibleBitmap(hDC, nWidth, nHeight);
HBITMAP memBMOld = (HBITMAP)SelectObject(memDCNew, memBMNew);
DeleteObject(memBMOld);
HPEN hPen = CreatePen(PS_SOLID, 0, RGB(255,255,255));
HPEN hPenOld = (HPEN)SelectObject(memDCNew, hPen);
DeleteObject(hPenOld);
HBRUSH hBrush = CreateSolidBrush(RGB(255,255,255));
HBRUSH hBrushOld = (HBRUSH)SelectObject(memDCNew, hBrush);
DeleteObject(hBrushOld);
Rectangle(memDCNew, 0, 0, nWidth, nHeight);
BitBlt(
memDCNew, 0, 0, nWidth, nHeight,
memDC, 0, 0,
SRCCOPY
);
DeleteObject(memBM);
memBM = memBMNew;
DeleteDC(memDC);
memDC = memDCNew;
}
}
// ----------------------------------------------------------------------------
LRESULT MyInit(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
LRESULT ret = FALSE;
g_hDC = GetDC(hWnd);
if ( g_hDC != NULL )
{
g_memDC = CreateCompatibleDC(g_hDC);
if ( g_memDC != NULL )
{
int nWidth = 0;
int nHeight = 0;
GetCurClientSize(hWnd, nWidth, nHeight);
g_memBM = CreateCompatibleBitmap(g_hDC, nWidth, nHeight);
if ( g_memBM != NULL )
{
HBITMAP memBMOld = (HBITMAP)SelectObject(g_memDC, g_memBM);
DeleteObject(memBMOld);
SendMessage(hWnd, WM_KEYDOWN, VK_F1, 0);
ret = TRUE;
}
}
}
return ret;
}
LRESULT UnInit(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if ( g_memBM != NULL )
{
DeleteObject(g_memBM);
g_memBM = NULL;
}
if ( g_memDC != NULL )
{
DeleteDC(g_memDC);
g_memDC = NULL;
}
if ( g_hDC != NULL )
{
ReleaseDC(hWnd, g_hDC);
g_hDC = NULL;
}
return TRUE;
}
// ----------------------------------------------------------------------------
LRESULT OnLButtonDown(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
g_cPointsBegin = MAKEPOINTS(lParam);
g_bDrawEnable = TRUE;
SetCapture(hWnd);
return TRUE;
}
LRESULT OnMouseMove(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if ( MK_LBUTTON == wParam )
{
if ( g_bDrawEnable )
{
POINTS pt = {0};
pt = MAKEPOINTS(lParam);
SendMessage(hWnd, WM_PAINT, 0, 0);
MoveToEx(g_hDC, g_cPointsBegin.x, g_cPointsBegin.y, NULL);
LineTo(g_hDC, pt.x, pt.y);
}
}
return TRUE;
}
LRESULT OnLButtonUp(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if ( TRUE == g_bDrawEnable )
{
g_bDrawEnable = FALSE;
POINTS pt = {0};
pt = MAKEPOINTS(lParam);
HPEN hPen = CreatePen(PS_SOLID, 0, RGB(0,255,0));
HPEN hPenOld = (HPEN)SelectObject(g_memDC, hPen);
MoveToEx(g_memDC, g_cPointsBegin.x, g_cPointsBegin.y, NULL);
LineTo(g_memDC, pt.x, pt.y);
SelectObject(g_memDC, hPenOld);
DeleteObject(hPen);
SendMessage(hWnd, WM_PAINT, 0, 0);
}
ReleaseCapture();
return TRUE;
}
// ----------------------------------------------------------------------------
LRESULT OnKeyDown(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if ( VK_F1 == wParam )
{
if ( g_memDC != NULL )
{
int nWidth = 0;
int nHeight = 0;
GetCurClientSize(hWnd, nWidth, nHeight);
HPEN hPen = CreatePen(PS_SOLID, 0, RGB(255,255,255));
HPEN hPenOld = (HPEN)SelectObject(g_memDC, hPen);
HBRUSH hBrush = CreateSolidBrush(RGB(255,255,255));
HBRUSH hBrushOld = (HBRUSH)SelectObject(g_memDC, hBrush);
Rectangle(g_memDC, 0, 0, nWidth, nHeight);
SelectObject(g_memDC, hPenOld);
DeleteObject(hPen);
SelectObject(g_memDC, hBrushOld);
DeleteObject(hBrush);
InvalidateRect(hWnd, NULL, TRUE);
}
}
else if ( VK_ESCAPE == wParam )
{
SendMessage(hWnd, WM_CLOSE, 0, 0);
}
return TRUE;
}
// ----------------------------------------------------------------------------
LRESULT OnCreate(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
return MyInit(hWnd, message, wParam, lParam);
}
LRESULT OnClose(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
UnInit(hWnd, message, wParam, lParam);
DestroyWindow(hWnd);
return TRUE;
}
LRESULT OnSize(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if ( SIZE_MINIMIZED != wParam )
{
ChangeMemDCSize(hWnd, g_hDC, g_memDC, g_memBM);
}
return TRUE;
}
LRESULT OnPaint(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if ( g_hDC != NULL && g_memDC != NULL )
{
int nWidth = 0;
int nHeight = 0;
GetCurClientSize(hWnd, nWidth, nHeight);
BitBlt(
g_hDC, 0, 0, nWidth, nHeight,
g_memDC, 0, 0,
SRCCOPY
);
}
return TRUE;
}
LRESULT OnOtherMessage(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_LBUTTONDOWN:
OnLButtonDown(hWnd, message, wParam, lParam);
break;
case WM_MOUSEMOVE:
OnMouseMove(hWnd, message, wParam, lParam);
break;
case WM_LBUTTONUP:
OnLButtonUp(hWnd, message, wParam, lParam);
break;
case WM_KEYDOWN:
OnKeyDown(hWnd, message, wParam, lParam);
break;
}
return TRUE;
}
// ----------------------------------------------------------------------------
LRESULT CALLBACK MainWndProc(
HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam
)
{
switch (message)
{
case WM_CREATE:
if ( TRUE != OnCreate(hWnd, message, wParam, lParam) )
{
SendMessage(hWnd, WM_CLOSE, 0, 0);
}
break;
case WM_CLOSE:
OnClose(hWnd, message, wParam, lParam);
break;
case WM_SIZE:
OnSize(hWnd, message, wParam, lParam);
break;
case WM_PAINT:
OnPaint(hWnd, message, wParam, lParam);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
OnOtherMessage(hWnd, message, wParam, lParam);
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
// ----------------------------------------------------------------------------
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
MSG msg;
BOOL bRet;
WNDCLASS wc;
UNREFERENCED_PARAMETER(lpCmdLine);
// Register the main window class.
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)MainWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "MyDraw";
if ( !RegisterClass(&wc) )
{
return FALSE;
}
HWND hWnd = CreateWindow(
wc.lpszClassName,
"画图板",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL
);
if ( NULL == hWnd )
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
while ( (bRet = GetMessage(&msg, NULL, 0, 0)) != 0 )
{
if ( -1 == bRet )
{
// handle the error and possibly exit
return FALSE;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
// Return the exit code to the system.
return msg.wParam;
}
一个基于Win32SDK写的超简单画图板
最新推荐文章于 2020-08-14 17:19:10 发布