Lập trình c trên windows ví dụ và bài tập (t4)

20 589 1
Lập trình c trên windows ví dụ và bài tập (t4)

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

• 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 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

1 Lập trình C trên Windowsdụ Bài tập (T4) Nguyễn Đức Hoàng Hạ Khoa CNTT –Trường ĐHKHTN Email: ndhha@fit.hcmuns.edu.vn 2 Graphics Device Interface (GDI) 3 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); 4 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); 5 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); … 6 HFONT •BOOL ChooseFont(LPCHOOSEFONT lpcf ); •HFONT CreateFontIndirect( CONST LOGFONT* lplf); (xem thêm tại GDI.pdf-tr26) 7 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 8 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 9 Robot 123 Chuỗi chuyển hình 1213-1213-… 10 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 [...]... GetLocalTime( LPSYSTEMTIME lpSystemTime ); typedef struct _SYSTEMTIME { WORD wYear; WORD wMonth; WORD wDayOfWeek; WORD wDay; WORD wHour; WORD wMinute; WORD wSecond; WORD wMilliseconds; } SYSTEMTIME 16 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);... 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...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));... MoveTo(POINT); void LineTo(POINT); void Line(POINT, POINT); … }; 17 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 ... 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, . 1 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: ndhha@fit.hcmuns.edu.vn 2 Graphics Device Interface. 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);

Ngày đăng: 21/08/2013, 10:29

Hình ảnh liên quan

Chuỗi chuyển hình 1213-1213-… - Lập trình c trên windows ví dụ và bài tập (t4)

hu.

ỗi chuyển hình 1213-1213-… Xem tại trang 9 của tài liệu.

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan