自己想实现一个鼠标移动然后画2条垂直于鼠标的直线,但是自己写的一直无法重绘。
下面是修改过的,一位CSDN网友帮我解决的。谢谢。绿色语句必须在红色之前,具体为什么现在我还没有搞清楚
#include <windows.h>
#include <stdio.h>
LRESULT CALLBACK WndProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
){
static TCHAR szAppName[]=TEXT( "mywin ");
HWND hwnd;
MSG msg;
WNDCLASS wndclass; //实例化一个窗口类
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(hInstance,IDC_CROSS);
wndclass.hIcon=LoadIcon(hInstance,IDI_QUESTION);
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WndProc;
wndclass.lpszClassName=szAppName;
wndclass.lpszMenuName=NULL;
wndclass.style=CS_HREDRAW ¦ CS_VREDRAW;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT( "wndclass is allready register! "),TEXT( "error "),MB_OK);
return 0;
}
//创建窗口
hwnd=CreateWindow(
szAppName, // registered class name
TEXT( "LIULEI "), // window name
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // horizontal position of window
CW_USEDEFAULT, // vertical position of window
CW_USEDEFAULT, // window width
CW_USEDEFAULT, // window height
NULL, // handle to parent or owner window
NULL, // menu handle or child identifier
hInstance, // handle to application instance
NULL // window-creation data
);
// 显示窗口
ShowWindow(hwnd,nCmdShow);
//更新窗口
UpdateWindow(hwnd);
//开始消息循环
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam ;
}
//窗口过程处理函数
LRESULT CALLBACK WndProc(
HWND hwnd, // handle to window
UINT msg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
){
HDC hdc;
// PAINTSTRUCT ps;
RECT rect;
int xPos=0,yPos=0,cxClient=100,cyClient=100;
POINT ptCursor;
switch(msg)
{
case WM_CREATE:
return DefWindowProc(hwnd,msg,wParam,lParam);
case WM_PAINT:
return DefWindowProc(hwnd,msg,wParam,lParam);
case WM_MOUSEMOVE:
//UpdataWindow();
GetClientRect(hwnd,&rect);
cxClient=rect.right;
cyClient=rect.bottom;
InvalidateRect(hwnd,&rect,TRUE);
UpdateWindow(hwnd);
GetCursorPos( &ptCursor );
ScreenToClient(hwnd, &ptCursor );
xPos =ptCursor.x;
yPos = ptCursor.y;
hdc=GetDC(hwnd);
MoveToEx(hdc, xPos, 0, NULL) ; //画一条竖线
LineTo (hdc, xPos, cyClient) ;
MoveToEx(hdc, 0, yPos, NULL) ; //画一条横线
LineTo (hdc, cxClient, yPos) ;
ReleaseDC(hwnd,hdc);
return 0;
case WM_CLOSE:
if(IDYES==MessageBox(NULL,TEXT( "是否要退出程序? "),TEXT( "操作 "),MB_YESNO))
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,msg,wParam,lParam);
}