~cpp #include <windows.h> LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nCmdShow) { HWND hWnd; WNDCLASS wc; MSG mSg; char szTitleName[]="Drawing Line"; char szClassName[]="it is a class"; wc.cbClsExtra=NULL; wc.cbWndExtra=NULL; wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); wc.hCursor=LoadCursor(NULL,IDC_ARROW); wc.hIcon=LoadIcon(NULL,IDI_APPLICATION); wc.hInstance=hInst; wc.lpfnWndProc=WndProc; wc.lpszClassName=szClassName; wc.lpszMenuName=NULL; wc.style=NULL; RegisterClass(&wc); hWnd=CreateWindow(szClassName,szTitleName, WS_OVERLAPPEDWINDOW,100,90,320,240,NULL,NULL,hInst,NULL); ShowWindow(hWnd,nCmdShow); UpdateWindow(hWnd); while(GetMessage(&mSg,NULL,0,0)) { TranslateMessage(&mSg); DispatchMessage(&mSg); } return mSg.wParam; } LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { HDC hDC; HPEN hPen; PAINTSTRUCT ps; static int nSX,nSY,nEX,nEY; static POINT pt; static BOOL bTF; switch(uMsg) { case WM_LBUTTONDOWN: { bTF = TRUE; nSX=LOWORD(lParam); nSY=HIWORD(lParam); } return FALSE; case WM_LBUTTONUP: { bTF=FALSE; nEX=LOWORD(lParam); nEY=HIWORD(lParam); InvalidateRect(hWnd,NULL,FALSE); } return FALSE; case WM_PAINT: { if(bTF==FALSE) { hPen=CreatePen(PS_SOLID,0,RGB(0,0,0)); hDC=BeginPaint(hWnd,&ps); SelectObject(hDC,hPen); SelectObject(hDC, GetStockObject(NULL_BRUSH)); MoveToEx(hDC,nSX,nSY,&pt); LineTo(hDC,nEX,nEY); DeleteObject(hPen); EndPaint(hWnd,&ps); } } return FALSE; case WM_DESTROY: PostQuitMessage(0); return FALSE; } return DefWindowProc(hWnd,uMsg,wParam,lParam); }
~cpp hPen=CreatePen(PS_SOLID,0,RGB(0,0,0)); hDC=BeginPaint(hWnd,&ps); Ellipse(hDC,nSX,nSY,nEX,nEY); EndPaint(hWnd,&ps);