LẬP TRÌNH WINDOWS - Ví dụ và Bài tập (T4)
Số trang: 20
Loại file: pdf
Dung lượng: 79.55 KB
Lượt xem: 14
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Tham khảo tài liệu lập trình windows - ví dụ và bài tập (t4), công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
LẬP TRÌNH WINDOWS - Ví dụ và Bài tập (T4)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 ĐHKHTNEmail: ndhha@fit.hcmuns.edu.vn 1Graphics Device Interface (GDI) 2 HDCWM_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 Robot1 2 3 Chuỗi chuyển hình 1213-1213-… 9 Robottypedef struct { HBITMAP hBmp; int next; int dx,dy;} CANH;CANH robot[4];int nMAX = 4;int n;int x,y; //vi tri anh 10 Robotvoid 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 Robotcase 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 Robotcase 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ớ ...
Nội dung trích xuất từ tài liệu:
LẬP TRÌNH WINDOWS - Ví dụ và Bài tập (T4)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 ĐHKHTNEmail: ndhha@fit.hcmuns.edu.vn 1Graphics Device Interface (GDI) 2 HDCWM_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 Robot1 2 3 Chuỗi chuyển hình 1213-1213-… 9 Robottypedef struct { HBITMAP hBmp; int next; int dx,dy;} CANH;CANH robot[4];int nMAX = 4;int n;int x,y; //vi tri anh 10 Robotvoid 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 Robotcase 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 Robotcase 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ớ ...
Tìm kiếm theo từ khóa liên quan:
bài tập lập trình windows lập trình windows c sharp ngôn ngữ C lập trình với CTài liệu liên quan:
-
Bài tập lập trình Windows dùng C# - Bài thực hành
13 trang 188 0 0 -
bảo mật mạng các phương thức giả mạo địa chỉ IP fake IP
13 trang 160 0 0 -
Giáo trình Lập trình C căn bản - HanoiAptech Computer Education Center
136 trang 134 0 0 -
Giáo trình Tin học đại cương: Phần 2 - Trần Đình Khang
118 trang 121 0 0 -
information technology outsourcing transactions process strategies and contracts 2nd ed phần 3
65 trang 111 0 0 -
Excel add in development in c and c phần 9
0 trang 110 0 0 -
Giáo trình Lập trình Windows 1 - Trường CĐN Đà Lạt
117 trang 96 0 0 -
101 thuật toán chương trình C: Phần 2
130 trang 91 0 0 -
91 trang 85 0 0
-
Hướng dẫn lập trình OpenGL căn bản
33 trang 53 0 0