手敲代码,机械键盘就是爽。
几个函数的理解:
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) ;
}