图形学基础--画直线

一,开发环境

IDE:dev-c++

语言:C++

二,说明

单击鼠标左键选择直线起始点,然后移动鼠标,再单击选择直线终点,然后双击鼠标左键,即绘制直线。

源代码:

#include <windows.h>

//定义鼠标左键点击次数
int leftCount=0; 
//定义点结构体
struct point{
    int x;
    int y;
    };
point ptStart,ptEnd; 
/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
char szClassName[ ] = "WindowsApp";

//定义WinMain函数,WinMain:入口函数 
int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Windows App",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           //50,
           //50,
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nFunsterStil);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_LBUTTONDOWN:
            leftCount++;
            /*HDC hdc;
            hdc=GetDC(hwnd);*/
            /*struct point{
                int x;
                int y;
            };
            point ptStart,ptEnd;*/
            if(leftCount%2!=0){
                ptStart.x=LOWORD(lParam);
                ptStart.y=HIWORD(lParam);
            }else{
                ptEnd.x=LOWORD(lParam);
                ptEnd.y=HIWORD(lParam);
            }       
            /*ptStart.x=100;
            ptStart.y=100;
            ptEnd.x=200;
            ptEnd.y=200;*/
            /*float x,y,xincre,yincre;
            int k;
            k= abs(ptEnd.x-ptStart.x);
            if(abs(ptEnd.y-ptStart.y)>k){
                k=abs(ptEnd.y-ptStart.y);
            }
            xincre=(float)(ptEnd.x-ptStart.x)/k;
            yincre=(float)(ptEnd.y-ptStart.y)/k;
            x=ptStart.x;
            y=ptStart.y;
            for(int i=1;i<k;i++){
                SetPixel(hdc,x,y,RGB(255,0,0));
                x=x+xincre;
                y=y+yincre;
            }*/
            break;        
        case WM_LBUTTONDBLCLK:
            HDC hdc;
            hdc=GetDC(hwnd);
            float x,y,xincre,yincre;
            int k;
            k= abs(ptEnd.x-ptStart.x);
            if(abs(ptEnd.y-ptStart.y)>k){
                k=abs(ptEnd.y-ptStart.y);
            }
            xincre=(float)(ptEnd.x-ptStart.x)/k;
            yincre=(float)(ptEnd.y-ptStart.y)/k;
            x=ptStart.x;
            y=ptStart.y;
            for(int i=1;i<k;i++){
                SetPixel(hdc,x,y,RGB(255,0,0));
                x=x+xincre;
                y=y+yincre;
            }
            break;
        case WM_SYSCOMMAND:
            break; 
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}
三,参考资料

1,DDA算法:

void CDDALineView::dda(CPoint ptStart, CPoint ptEnd, CDC* pDC)
{
    float x, y, xincre, yincre;
    int k = abs(ptEnd.x - ptStart.x);
    if (abs(ptEnd.y - ptStart.y) > k)
    {
        k = abs(ptEnd.y - ptStart.y);
    }

    xincre = (float)(ptEnd.x - ptStart.x)/k;
    yincre = (float)(ptEnd.y - ptStart.y)/k;

    x = ptStart.x;
    y = ptStart.y;
    for (int i = 1; i < k; i++)
    {
        pDC->SetPixel(x, y, RGB(255, 0, 0));
        x = x + xincre;
        y = y + yincre;
    }
}
原帖地址: http://blog.csdn.net/rabbit729/article/details/4063709

2,鼠标消息

鼠标消息有21种,可以分为3类:

(1)命中消息:WM_NCHITTEST

它是优先于任何接下来要说到的客户区和非客户区的鼠标消息。因为其余20种鼠标消息都是基于它的基础之上产生的。也就是说user用鼠标点击之后首先产生的是WM_NCHITTEST消息,然后经窗口函数的DefWindowProc返回一个值,这个值将成为新的鼠标消息的wParam值。

(2)客户区鼠标消息(10个):

左:WM_LBUTTONDOWN    WM_LBUTTONUP     WM_LBUTTONDBLCLK

中:WM_MBUTTONDOWN   WM_MBUTTONUP     WM_MBUTTONDBLCLK

右:WM_RBUTTONDOWN    WM_RBUTTONUP     WM_RBUTTONDBLCLK

鼠标移动消息:WM_MOUSEMOVE

lParam含有鼠标位置的坐标,低字位是x坐标,高字位是y坐标,当然此时它们是客户区坐标。可以用x=LOWORD(lParam);y=HIWORD(lParam);来表示。

如果要转化为屏幕坐标,可用ClientToScreen(hWnd,&pt);

(3)非客户区消息(10个):

左:WM_NCLBUTTONDOWN    WM_NCLBUTTONUP     WM_NCLBUTTONDBLCLK

中:WM_NCMBUTTONDOWN   WM_NCMBUTTONUP     WM_NCMBUTTONDBLCLK

右:WM_NCRBUTTONDOWN    WM_NCRBUTTONUP     WM_NCRBUTTONDBLCLK

鼠标移动消息:WM_NCMOUSEMOVE

同样的,lParam含有鼠标位置的坐标,低字位是x坐标,高字位是y坐标,但是此时它们是屏幕坐标。也可以用x=LOWORD(lParam);y=HIWORD(lParam);来表示。如果要转化为客户区坐标,可用:ScreenToClient(hWnd,&pt);



你可能感兴趣的:(图形学基础--画直线)