logo

Lập trình C về Windows

CreateBrushIndirect: Creates a brush with a specified style, color, and pattern • CreateDIBPatternBrushPt: Creates a brush with the pattern from a DIB • CreateHatchBrush: Creates a brush with a hatch pattern and color • CreatePatternBrush: Creates a brush with a bitmap pattern • CreateSolidBrush: Creates a brush with a solid color HDC WM_PAINT • HDC BeginPaint( HWND hwnd, //
Lập trình C trên Windows Ví dụ và Bài tập (T4) Nguyễn Đức Hoàng Hạ Khoa CNTT – Trường ĐHKHTN Email: [email protected] 1 Graphics Device Interface (GDI) 2 HDC WM_PAINT • HDC BeginPaint( HWND hwnd, // input LPPAINTSTRUCT lpPaint // output ); • BOOL EndPaint( HWND hWnd, PAINTSTRUCT *lpPaint ); Không phải trong WM_PAINT • HDC GetDC( HWND hWnd); • int ReleaseDC( HWND hWnd, HDC hDC // handle to DC ); Chọn các đối tượng vẽ vào trong DC • HGDIOBJ SelectObject( HDC hdc, HGDIOBJ hgdiobj); 3 HPEN • HPEN CreatePen( int fnPenStyle, int nWidth, COLORREF crColor); • BOOL DeleteObject( HGDIOBJ hObject); • Ví dụ: hdc = BeginPaint(hWnd, &ps); hPen = CreatePen(PS_SOLID,2,0); hOld = SelectObject(hdc,hPen); MoveToEx(hdc,100,200,NULL); LineTo(hdc,200,100); SelectObject(hdc,hOld); DeleteObject(hPen); EndPaint(hWnd, &ps); 4 HBRUSH • CreateBrushIndirect: Creates a brush with a specified style, color, and pattern • CreateDIBPatternBrushPt: Creates a brush with the pattern from a DIB • CreateHatchBrush: Creates a brush with a hatch pattern and color • CreatePatternBrush: Creates a brush with a bitmap pattern • CreateSolidBrush: Creates a brush with a solid color • Ví dụ: hBr = CreateSolidBrush(255); hOldBr = SelectObject(hdc,hBr); Rectangle(hdc,0,0,400,200); … 5 HFONT • BOOL ChooseFont(LPCHOOSEFONT lpcf ); • HFONT CreateFontIndirect( CONST LOGFONT* lplf); (xem thêm tại GDI.pdf-tr26) 6 HBITMAP • HBITMAP LoadBitmap( HINSTANCE hInstance, LPCTSTR lpBitmapName); • HANDLE LoadImage( HINSTANCE hinst,LPCTSTR lpszName, UINT uType, int cxDesired, int cyDesired, UINT fuLoad ); uType: IMAGE_BITMAP,IMAGE_CURSOR,IMAGE_ICON fuLoad: LR_LOADFROMFILE 7 Ví dụ 4 Robot • Mô tả: Hãy viết 1 chương trình có một robot bước đi trên màn hình • Yêu cầu: – Mô tả dữ liệu – Mô tả xử lý các sự kiện cần thiết 8 Robot 1 2 3 Chuỗi chuyển hình 1213-1213-… 9 Robot typedef struct { HBITMAP hBmp; int next; int dx,dy; } CANH; CANH robot[4]; int nMAX = 4; int n; int x,y; //vi tri anh 10 Robot void DrawRobot(HDC hdc) { HDC hRobot = ::CreateCompatibleDC(hdc); HGDIOBJ hOld = SelectObject( hRobot, robot[n].hBmp ); BitBlt(hdc,x,y,50,50,hRobot,0,0,SRCCOPY); SelectObject(hRobot,hOld); DeleteDC(hRobot); } 11 Robot case WM_CREATE: robot[0].hBmp = LoadBitmap(hInst,LPCTSTR(IDB_BITMAP1)); robot[0].next =1; robot[0].dx = 27-13; robot[0].dy =0; robot[1].hBmp = LoadBitmap(hInst,LPCTSTR(IDB_BITMAP2)); robot[1].next =2; robot[1].dx = 38-27; robot[1].dy =0; robot[2].hBmp = LoadBitmap(hInst,LPCTSTR(IDB_BITMAP1)); robot[2].next = 3; robot[2].dx = 27-13; robot[2].dy =0; robot[3].hBmp = LoadBitmap(hInst,LPCTSTR(IDB_BITMAP3)); robot[3].next =0; robot[3].dx = 38-27; robot[3].dy =0; n = 0; x = y = 0; SetTimer(hWnd,1,1000,NULL); break; 12 Robot case WM_TIMER: x += robot[n].dx; y += robot[n].dy; n = robot[n].next ; InvalidateRect(hWnd, NULL,TRUE); break; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); DrawRobot(hdc); EndPaint(hWnd, &ps); break; 13 Bài tập 4 • Mô tả: Viết một chương trình cho phép điều khiển robot di chuyển trên màn hình. Robot luôn di chuyển, người sẽ điều khiển hướng đi của robot 14 Bài tập 5 Đồng hồ KIM • Mô tả Viết chương trình mô phỏng đồng hồ kim trên máy tính. 15 Bài tập 5 • Hướng dẫn: Sử dụng hàm sau để lấy ngày giờ hệ thông: void GetLocalTime( LPSYSTEMTIME lpSystemTime ); typedef struct _SYSTEMTIME { WORD wYear; WORD wMonth; WORD wDayOfWeek; WORD wDay; WORD wHour; WORD wMinute; WORD wSecond; WORD wMilliseconds; } SYSTEMTIME 16 Ví dụ 6 • Mô tả: Hãy đóng gói HDC thành lớp CMyDC nhằm hỗ trợ người lập trình C trên Windows. • Yêu cầu: classCMyDC { Public: PAINTSTRUCT m_ps; HDC m_hdc; HWND m_hwnd; HDC BeginPaint(HWND); void EndPaint(); void MoveTo(POINT); void LineTo(POINT); void Line(POINT, POINT); … }; 17 Ví dụ 6 HDC CMyDC::BeginPaint(HWND hwnd) { m_hwnd = hwnd; m_hdc = BeginPaint(m_hwnd, &m_ps); return m_hdc; } void CMyDC::EndPaint() { EndPaint(m_hwnd,&m_ps); } void CMyDC::Line(POINT p1, POINT p2) { MoveTo(p1); LineTo(p2); } 18 Microsoft Foundation Class (MFC) Library Xem chi tiết trong MSDN 19 DLL? 20
DMCA.com Protection Status Copyright by webtailieu.net