对于第七章 CONNECT程序的简单思考

本文介绍了一个使用Windows GDI绘图的应用实例,通过代码详细解释了如何利用InvalidateRect函数进行窗口重绘,SetPixel函数绘制像素点,并通过记录鼠标移动轨迹实现连续绘图的功能。文章还分享了一个常见bug及其解决办法。

手敲代码,机械键盘就是爽。
几个函数的理解:
InvalidateRect(hwnd,NULL, TRUE)
true:擦出背景
false:不擦出背景。
false相当于在原图上添加新内容,true则表示换了张画质,重新画内容
LOWORD(lParam)保存了鼠标点的x坐标
HIWORD(lParam)保存了鼠标的y坐标
与此类似的,在<windowsx.h>头文件中,2个函数分别被:GET_X_LPARAM (lParam)和GET_Y_LPARAM (lParam)实现
SetPixel函数第四个参数是点的颜色


这里着重记录一下一个常识性的bug:
程序写好后,可以画点,但无法连线:
手动debug,查看point[]:


明显point[]异常,想了半天,point前没有添加static,哎,让你不好好学c++
正确的point[]:


//define后面不需要+;
#define NUM 1000
#define TWOPI 2*3.1415926


#include <windows.h>
#include <windowsx.h>
#include <tchar.h>
#include <string>
#include <time.h>
#include <iostream>

using namespace std;

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

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
					PSTR szCmdLine, int iCmdShow)
{
	static TCHAR szAppName[] = TEXT ("test_GDI") ;
	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_APPLICATION) ;
	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,                  // window class name
		TEXT ("TEST_GDI"), // window caption
		WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL,        // window style
		CW_USEDEFAULT,              // initial x position
		CW_USEDEFAULT,              // initial y position
		CW_USEDEFAULT,              // initial x size
		CW_USEDEFAULT,              // initial y size
		NULL,                       // parent window handle
		NULL,                       // window menu handle
		hInstance,                  // program instance handle
		NULL) ;                     // creation parameters

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

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

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	HDC         hdc ;
	PAINTSTRUCT ps ;
	RECT        rect ;
	static  POINT	point[NUM];
	/*static*/ int cxClient , cyClient;
	static int iCount;
	//int i, j;

	switch (message)
	{
	case WM_LBUTTONDOWN:
		InvalidateRect(hwnd,NULL, TRUE);
		iCount = 0;
		return 0;

	case WM_MOUSEMOVE:
		if((wParam & MK_LBUTTON) && iCount <= NUM)
		{
			point[iCount].x = LOWORD(lParam);
		   	point[iCount++].y = HIWORD(lParam);

			hdc = GetDC(hwnd);
			SetPixel(hdc, LOWORD(lParam), HIWORD(lParam), RGB(0, 0, 0));
			ReleaseDC(hwnd, hdc);
		}
		return 0;
	
	case WM_LBUTTONUP:
		InvalidateRect(hwnd, NULL, FALSE);
		return 0;

	case WM_PAINT:
		{
			hdc = BeginPaint (hwnd, &ps) ;
		//	SetCursor(LoadCursor(NULL, IDC_WAIT));
		//	ShowCursor(TRUE);
			for( int i = 0; i < iCount - 1; i++)
				for( int j = i + 1; j < iCount; j++)
				{
					MoveToEx(hdc, point[i].x, point[i].y, NULL);
					LineTo(hdc, point[j].x, point[j].y);
				}
			//	ShowCursor(FALSE);
			//	SetCursor(LoadCursor(NULL, IDC_ARROW));
			
			EndPaint(hwnd, &ps);
			return 0 ;
		}
	case WM_DESTROY:
		PostQuitMessage (0) ;
		return 0 ;
	}
	return DefWindowProc (hwnd, message, wParam, lParam) ;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值