際際滷

際際滷Share a Scribd company logo
1
L畉p tr狸nh C tr棚n Windows
V鱈 d畛 v Bi t畉p (T4)
Nguy畛n 畛c Hong 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畉 vo 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
V鱈 d畛 4
Robot
 M担 t畉:
H達y vi畉t 1 ch動董ng tr狸nh c坦 m畛t robot b動畛c i
tr棚n mn 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
1 2 3
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
11
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);
}
12
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;
13
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;
14
Bi 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 mn h狸nh. Robot lu担n di
chuy畛n, ng動畛i s畉 i畛u khi畛n h動畛ng i c畛a robot
15
Bi 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.
16
Bi t畉p 5
 H動畛ng d畉n:
S畛 d畛ng hm sau 畛 l畉y ngy 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
17
V鱈 d畛 6
 M担 t畉:
H達y 坦ng g坦i HDC thnh 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);

};
18
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);
}
19
Microsoft Foundation Class (MFC)
Library
Xem chi ti畉t trong MSDN
20
DLL?

More Related Content

12 vidu baitap_c

  • 1. 1 L畉p tr狸nh C tr棚n Windows V鱈 d畛 v Bi t畉p (T4) Nguy畛n 畛c Hong H畉 Khoa CNTT Tr動畛ng HKHTN Email: ndhha@fit.hcmuns.edu.vn
  • 3. 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畉 vo trong DC HGDIOBJ SelectObject( HDC hdc, HGDIOBJ hgdiobj);
  • 4. 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. 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. 6 HFONT BOOL ChooseFont(LPCHOOSEFONT lpcf ); HFONT CreateFontIndirect( CONST LOGFONT* lplf); (xem th棚m t畉i GDI.pdf-tr26)
  • 7. 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. 8 V鱈 d畛 4 Robot M担 t畉: H達y vi畉t 1 ch動董ng tr狸nh c坦 m畛t robot b動畛c i tr棚n mn 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. 9 Robot 1 2 3 Chu畛i chuy畛n h狸nh 1213-1213-
  • 10. 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
  • 11. 11 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); }
  • 12. 12 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;
  • 13. 13 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;
  • 14. 14 Bi 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 mn h狸nh. Robot lu担n di chuy畛n, ng動畛i s畉 i畛u khi畛n h動畛ng i c畛a robot
  • 15. 15 Bi 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.
  • 16. 16 Bi t畉p 5 H動畛ng d畉n: S畛 d畛ng hm sau 畛 l畉y ngy 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
  • 17. 17 V鱈 d畛 6 M担 t畉: H達y 坦ng g坦i HDC thnh 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); };
  • 18. 18 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); }
  • 19. 19 Microsoft Foundation Class (MFC) Library Xem chi ti畉t trong MSDN