简单的屏幕绘图工具

/*****************************************************
sketch.C


(c) 2023
*****************************************************/

#include<Windows.h>

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

int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance,
	PSTR szCmdLine, int iCmdShow)
{
	static TCHAR szAppName[] = TEXT("Sketch");//移植你更改的地方
	HWND hwnd;
	MSG msg;
	WNDCLASS wndclass;

	wndclass.style = CS_HREDRAW | CS_VREDRAW;
	wndclass.lpfnWndProc = WndProc;
	wndclass.cbClsExtra = 0;
	wndclass.cbWndExtra = 0;
	wndclass.hInstance = hinstance;
	wndclass.hIcon = LoadIcon(NULL, IDI_INFORMATION);
	wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wndclass.lpszMenuName = NULL;
	wndclass.lpszClassName = szAppName;

	if (!RegisterClass(&wndclass))
	{
		MessageBox(NULL, TEXT("this program requires Windows NT!"),
			szAppName, MB_ICONERROR);
		return 0;
	}

	hwnd = CreateWindow(szAppName, TEXT("Sketch"),//更新
		WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL,
		CW_USEDEFAULT, CW_USEDEFAULT,
		CW_USEDEFAULT, CW_USEDEFAULT,
		NULL, NULL, hinstance, NULL);


	if (hwnd==NULL)
	{
		MessageBox(NULL, TEXT("Not enough memory to create bitmap!"),
			szAppName, MB_ICONERROR);
		return 0;
	}

	ShowWindow(hwnd, iCmdShow);
	UpdateWindow(hwnd);

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


void GetLargestDisplayMode(int* pcxBitmap, int* pcyBitmap)
{
	DEVMODE devmode;
	int iModeNum = 0;
	*pcxBitmap = *pcyBitmap = 0;
	ZeroMemory(&devmode, sizeof(DEVMODE));
	devmode.dmSize = sizeof(DEVMODE);

	while (EnumDisplaySettings(NULL,iModeNum++,&devmode))
	{
		*pcxBitmap = max(*pcxBitmap, (int)devmode.dmPelsWidth);
		*pcyBitmap = max(*pcyBitmap, (int)devmode.dmPelsHeight);
	}
}


LRESULT  CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	static BOOL fLeftButtonDown, fRightButtonDown;
	static HBITMAP hBitmap;
	static HDC hdcMem;
	static int cxBitmap, cyBitmap, cxClient, cyClient, xMouse,yMouse;
	HDC hdc;
	PAINTSTRUCT ps;

	switch (message)
	{
	case WM_CREATE:
		GetLargestDisplayMode(&cxBitmap, &cyBitmap);

		hdc = GetDC(hwnd);
		hBitmap = CreateCompatibleBitmap(hdc, cxBitmap, cyBitmap);
		hdcMem = CreateCompatibleDC(hdc);
		ReleaseDC(hwnd, hdc);

		if (!hBitmap)
		{
			DeleteDC(hdcMem);
			return -1;
		}

		SelectObject(hdcMem, hBitmap);
		PatBlt(hdcMem, 0, 0, cxBitmap, cyBitmap, WHITENESS);
		return 0;

	case WM_SIZE:
		cxClient = LOWORD(lParam);
		cyClient = HIWORD(lParam);
		//add my
		InvalidateRect(hwnd, NULL, TRUE);
		return 0;

	case WM_LBUTTONDOWN:
		if (!fRightButtonDown)
			SetCapture(hwnd);

		xMouse = LOWORD(lParam);
		yMouse = HIWORD(lParam);
		fLeftButtonDown = TRUE;
		return 0;

	case WM_LBUTTONUP:
		if (fLeftButtonDown)
		{
			SetCapture(NULL);
		}
		fLeftButtonDown = FALSE;
		return 0;

	case WM_RBUTTONDOWN:
		if (!fLeftButtonDown)
			SetCapture(hwnd);

		xMouse = LOWORD(lParam);
		yMouse = HIWORD(lParam);
		fRightButtonDown = TRUE;
		return 0;

	case WM_RBUTTONUP:
		if (fRightButtonDown)
		{
			SetCapture(NULL);
		}
		fRightButtonDown = FALSE;
		return 0;

	case WM_MOUSEMOVE:
		if (!fLeftButtonDown&&!fRightButtonDown)
		{
			return 0;
		}

		hdc = GetDC(hwnd);

		SelectObject(hdc,
			GetStockObject(fLeftButtonDown ? BLACK_PEN : WHITE_PEN));
		SelectObject(hdcMem,
			GetStockObject(fLeftButtonDown ? BLACK_PEN : WHITE_PEN));

		MoveToEx(hdc, xMouse, yMouse, NULL);
		MoveToEx(hdcMem, xMouse, yMouse, NULL);
		
		xMouse=(short)LOWORD(lParam);
		yMouse = (short)HIWORD(lParam);

		LineTo(hdc, xMouse, yMouse);
		LineTo(hdcMem, xMouse, yMouse);

		ReleaseDC(hwnd, hdc);
		return 0;

	case WM_PAINT:
		hdc = BeginPaint(hwnd, &ps);
		BitBlt(hdc, 0, 0, cxClient, cyClient,
			hdcMem, 0, 0, SRCCOPY);

		EndPaint(hwnd, &ps);
		return 0;

	case WM_DESTROY:
		DeleteDC(hdcMem);
		DeleteObject(hBitmap);//删除画刷
		PostQuitMessage(0);
		return 0;
	}

	return DefWindowProc(hwnd, message, wParam, lParam);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值