TODAY : / TOTAL :

120627_API :: Double Buffering - Bounce 2

  • Share this:
반응형
MemDC 를 이용하면, 빠른 비트맵 출력이 가능하고, 편리하게 출력할 수 있다.


 void DrawBitmap(HDC hdc,int x,int y,HBITMAP hBit)
{
 HDC MemDC;
 HBITMAP OldBitmap;
 int bx,by;
 BITMAP bit;

 MemDC=CreateCompatibleDC(hdc);
 OldBitmap=(HBITMAP)SelectObject(MemDC, hBit);

 GetObject(hBit,sizeof(BITMAP),&bit);               //hit 에 저장된 비트맵 정보 bit복사.
 bx=bit.bmWidth;                                           //bit 에 복사시킨 비트맵의 사이즈
 by=bit.bmHeight;

 BitBlt(hdc,x,y,bx,by,MemDC,0,0,SRCCOPY);

 SelectObject(MemDC,OldBitmap);
 DeleteDC(MemDC);
}

#define R 20
int x,y;
int xi,yi;
HBITMAP hBit;

void OnTimer()
{
 RECT crt;
 HDC hdc,hMemDC;
 HBITMAP OldBit;
 HPEN hPen,OldPen;
 HBRUSH hBrush,OldBrush;
 int i;

 ////////////////////////////////   그리기 초기 준비 작업   ////////////////////////////////////// 
 GetClientRect(hWndMain,&crt);         // ** hWndMain(메인핸들)의 작업영역(사각형) 좌표 얻어오기.
 hdc=GetDC(hWndMain);

 if (hBit==NULL) {
  hBit=CreateCompatibleBitmap(hdc,crt.right,crt.bottom);
  // 비어있는 비트맵 생성 = 메모리에 흰 도화지 생성.
 }
 hMemDC=CreateCompatibleDC(hdc);
 OldBit=(HBITMAP)SelectObject(hMemDC,hBit);

 FillRect(hMemDC,&crt,GetSysColorBrush(COLOR_WINDOW));   // 0,1로 저장되는 메모리에 비트맵을 생성하면, 모두 0으로 채워진다.(검정색 화면 생성)
// FillRect 함수를 이용하여, GetSysColorBrush(COLOR_WINDOW) - 윈도우 화면 색깔로 메모리에 저장된 비트맵 화면을 다시 칠한다.


 //////////////////////////////////////////////////////////////////////////////////////////////
 

if (x <= R || x >= crt.right-R)
 {
  xi*=-1;
 }
 if (y <= R || y >= crt.bottom-R)
 {
  yi*=-1;
 }
 x+=xi;
 y+=yi;

 // 격자 선 긋기
 for (i=0;i<crt.right;i+=10) {
  MoveToEx(hMemDC,i,0,NULL);
  LineTo(hMemDC,i,crt.bottom);
 }

 for (i=0;i<crt.bottom;i+=10) {
  MoveToEx(hMemDC,0,i,NULL);
  LineTo(hMemDC,crt.right,i);
 }

 // 브러쉬 & 펜 생성 / 장착
 hPen=CreatePen(PS_INSIDEFRAME,5,RGB(255,0,0));
 OldPen=(HPEN)SelectObject(hMemDC,hPen);
 hBrush=CreateSolidBrush(RGB(0,0,255));
 OldBrush=(HBRUSH)SelectObject(hMemDC,hBrush);
 
 // 지정된 브러쉬와 펜으로 할 작업.
 Ellipse(hMemDC,x-R,y-R,x+R,y+R);
 
 // 작업 종료 후 브러쉬&펜 반납 / 삭제
 DeleteObject(SelectObject(hMemDC,OldPen));
 DeleteObject(SelectObject(hMemDC,OldBrush));
 SelectObject(hMemDC,OldBit);
 DeleteDC(hMemDC);
 ReleaseDC(hWndMain,hdc);
 InvalidateRect(hWndMain,NULL,FALSE);  // 다시 그리기.
}

LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
 HDC hdc;
 PAINTSTRUCT ps;

 switch (iMessage) {
 case WM_CREATE:
  x=50;
  y=50;
  xi=4;
  yi=5;
  SetTimer(hWnd,1,25,NULL);
  return 0;
 case WM_TIMER:
  OnTimer();
  return 0;
 case WM_PAINT:
  hdc=BeginPaint(hWnd, &ps);
  if (hBit) DrawBitmap(hdc,0,0,hBit);
  EndPaint(hWnd, &ps);
  return 0;
 case WM_DESTROY:
  if (hBit) {
   DeleteObject(hBit);
  }
  PostQuitMessage(0);
  KillTimer(hWnd,1);
  return 0;
 }
 return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}


반응형

SEARCH

태그로 찾아보기