[[프로그래밍_NOTE]]/Window Programing

SetPixel()을 이용한 LineTo() 함수 구현

갑빠돌격기 2008. 7. 26. 04:20

LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)

{
 ///////////////////////////////////
 HDC hdc;
 static int x1, y1, x2, y2;
 ///////////////////////////////////
 
 switch(iMessage)
 {
      case WM_LBUTTONDOWN:
           x1=LOWORD(lParam);
           y1=HIWORD(lParam);
           return 0;
      case WM_LBUTTONUP:
           x2=LOWORD(lParam);
           y2=HIWORD(lParam);
           hdc=GetDC(hWnd);
           ///////////////////////////////////
           Line(hdc, x1, y1, x2,y2);
          ///////////////////////////////////
          ReleaseDC(hWnd, hdc);
          return 0;
      case WM_DESTROY:
          PostQuitMessage(0);
          return 0;
 }
 return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}

void Line(HDC hdc, int x1, int y1, int x2, int y2)
{
 
    int X = x2 - x1;  //x값의 범위 확인.
    int Y = y2 - y1;  //y값의 범위 확인.
    ///////////////////////////////////
    //abs() -> 정수의 절대값 리턴.
    ///////////////////////////////////

    //축 계산 처리.////////////////////
    int steps = max(abs(X), abs(Y));
    ///////////////////////////////////
 
    float x_incre = (float)X / (float)steps;
    float y_incre = (float)Y / (float)steps;

    float x = (float)x1;  
    float y = (float)y1;

    for(int k=0; k <= steps; k++)
    {
             x = x + x_incre;
             y = y + y_incre;
             SetPixel(hdc,(int)x, (int)y, RGB(0, 0, 0));
    }
}