一,開發環境
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);