WINDOWS SDK编程问题
我想编一个窗口应用程序,实现画线的功能,即鼠标左键按下时记录坐标位置,起来时记录坐标位置,然后连成直线,请问这个程序中的窗口过程函数我应该用哪些函数来实现。...
我想编一个窗口应用程序,实现画线的功能,即鼠标左键按下时记录坐标位置,起来时记录坐标位置,然后连成直线,请问这个程序中的窗口过程函数我应该用哪些函数来实现。
展开
1个回答
展开全部
楼主你好
具体代码如下:
/*----------------------------
Line
------------------------------*/
#include<windows.h>
LRESULT CALLBACK WndProc(HWND , UINT , WPARAM , LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow) // show state
{
static TCHARszAppName[] = "Line";
HWNDhwnd;
MSGmsg;
WNDCLASSwndclass;
wndclass.cbClsExtra= 0;
wndclass.cbWndExtra= 0;
wndclass.hbrBackground= (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor= LoadCursor(NULL , IDC_ARROW);
wndclass.hIcon= LoadIcon(NULL , IDI_APPLICATION);
wndclass.hInstance= hInstance;
wndclass.lpfnWndProc= WndProc;
wndclass.lpszClassName= szAppName;
wndclass.lpszMenuName= NULL;
wndclass.style= CS_HREDRAW | CS_VREDRAW;
/*注册窗口类*/
RegisterClass(&wndclass);
/*获取窗口句柄 创建窗口*/
hwnd = CreateWindow(szAppName , szAppName , WS_OVERLAPPEDWINDOW , CW_USEDEFAULT , CW_USEDEFAULT ,
CW_USEDEFAULT , CW_USEDEFAULT , NULL , NULL , hInstance , NULL);
/*显示窗口*/
ShowWindow(hwnd , SW_SHOWNORMAL);
UpdateWindow(hwnd);
/*消息循环机制*/
while(GetMessage(&msg , NULL , 0 , 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
/*窗口过程*/
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDChdc;//设备句柄
PAINTSTRUCTps;//paint结构体 用于获取hdc
static POINTpStart,pEnd;//直线的起点和终点
switch(message)
{
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);//获取设备句柄
MoveToEx(hdc, pStart.x, pStart.y, NULL);//设定起点
LineTo(hdc, pEnd.x, pEnd.y);//画直线
EndPaint(hwnd, &ps);//回收设备句柄
return 0;
case WM_LBUTTONDOWN://响应鼠标左键按下消息
pStart.x = LOWORD(lParam);//记录鼠标左键按下的坐标 作为直线的起点
pStart.y = HIWORD(lParam);
return 0;
case WM_LBUTTONUP://响应鼠标左键按起消息
pEnd.x = LOWORD(lParam);//记录鼠标左键起来时的坐标 作为直线的终点
pEnd.y = HIWORD(lParam);
InvalidateRect(hwnd, NULL, TRUE);//此时应该重绘客户区
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd , message , wParam , lParam);
}
希望能帮助你哈
具体代码如下:
/*----------------------------
Line
------------------------------*/
#include<windows.h>
LRESULT CALLBACK WndProc(HWND , UINT , WPARAM , LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow) // show state
{
static TCHARszAppName[] = "Line";
HWNDhwnd;
MSGmsg;
WNDCLASSwndclass;
wndclass.cbClsExtra= 0;
wndclass.cbWndExtra= 0;
wndclass.hbrBackground= (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor= LoadCursor(NULL , IDC_ARROW);
wndclass.hIcon= LoadIcon(NULL , IDI_APPLICATION);
wndclass.hInstance= hInstance;
wndclass.lpfnWndProc= WndProc;
wndclass.lpszClassName= szAppName;
wndclass.lpszMenuName= NULL;
wndclass.style= CS_HREDRAW | CS_VREDRAW;
/*注册窗口类*/
RegisterClass(&wndclass);
/*获取窗口句柄 创建窗口*/
hwnd = CreateWindow(szAppName , szAppName , WS_OVERLAPPEDWINDOW , CW_USEDEFAULT , CW_USEDEFAULT ,
CW_USEDEFAULT , CW_USEDEFAULT , NULL , NULL , hInstance , NULL);
/*显示窗口*/
ShowWindow(hwnd , SW_SHOWNORMAL);
UpdateWindow(hwnd);
/*消息循环机制*/
while(GetMessage(&msg , NULL , 0 , 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
/*窗口过程*/
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDChdc;//设备句柄
PAINTSTRUCTps;//paint结构体 用于获取hdc
static POINTpStart,pEnd;//直线的起点和终点
switch(message)
{
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);//获取设备句柄
MoveToEx(hdc, pStart.x, pStart.y, NULL);//设定起点
LineTo(hdc, pEnd.x, pEnd.y);//画直线
EndPaint(hwnd, &ps);//回收设备句柄
return 0;
case WM_LBUTTONDOWN://响应鼠标左键按下消息
pStart.x = LOWORD(lParam);//记录鼠标左键按下的坐标 作为直线的起点
pStart.y = HIWORD(lParam);
return 0;
case WM_LBUTTONUP://响应鼠标左键按起消息
pEnd.x = LOWORD(lParam);//记录鼠标左键起来时的坐标 作为直线的终点
pEnd.y = HIWORD(lParam);
InvalidateRect(hwnd, NULL, TRUE);//此时应该重绘客户区
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd , message , wParam , lParam);
}
希望能帮助你哈
追问
您好,谢谢你了,我就差这两个函数了:
MoveToEx(hdc,xpos,ypos,NULL);
LineTo(hdc,xxpos,yypos);
想再请问一下如果我想实现的不是画直线,而是像画图工具一样按下鼠标可以画出任意曲线,我该用哪个函数呢?
追答
用函数吧Polyline(
HDChdc, // handle to device context
CONST POINT*lppt, // array of endpoints
intcPoints)
连接所有的点
具体的窗口过程:
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static BOOLisL;
static intpCount;
static POINTpoint[N];//记录移动的点
HDChdc;//设备句柄
PAINTSTRUCTps;//paint结构体 用于获取hdc
HPENhPen;
switch(message)
{
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);//获取设备句柄
hPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
SelectObject(hdc, hPen);
Polyline(hdc, point, pCount);
EndPaint(hwnd, &ps);//回收设备句柄
return 0;
case WM_LBUTTONDOWN://响应鼠标左键按下消息
isL = true;
point[pCount].x = LOWORD(lParam);
point[pCount++].y = HIWORD(lParam);
return 0;
case WM_MOUSEMOVE:
if(isL)
{
point[pCount].x = LOWORD(lParam);
point[pCount++].y = HIWORD(lParam);
InvalidateRect(hwnd, NULL, TRUE);//此时应该重绘客户区
}
return 0;
case WM_LBUTTONUP://响应鼠标左键按起消息
isL = false;
int i;
for(i=0; i<pCount; i++)
{
point[i].x = 0;
point[i].y = 0;
}
pCount = 0;
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd , message , wParam , lParam);
}
lz加分采纳吧^_^
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询