lParam获取的鼠标位LOWORD,HIWORD有问题,有的时候点了没有显示

qq_30519181 2015-12-06 09:23:08
#include<windows.h>
#include<stdio.h>
#define sr 15
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);


int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
{
static char szappname[]="五子棋";
HBRUSH hbrush;
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
hbrush=CreateSolidBrush(RGB(255,132,9));
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc=WndProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor=LoadCursor(NULL,IDC_CROSS);
wndclass.hbrBackground=hbrush;
wndclass.lpszClassName=szappname;
wndclass.lpszMenuName=NULL;



if(!RegisterClass(&wndclass))
{
MessageBox(NULL,"error",szappname,MB_ICONERROR);
return 0;
}


hwnd=CreateWindow(
szappname,
"五子棋",
WS_OVERLAPPEDWINDOW,
200,
0,
700,
700,
NULL,
NULL,
hInstance,
NULL
);


ShowWindow(hwnd,iCmdShow);
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)
{
static BOOL f=TRUE;
int x,px,py,y,i,j;
HDC hdc;
PAINTSTRUCT ps;
static int qizi[15][15];
RECT rect;
HBRUSH hbrush1,hbrush2;
hbrush1=CreateSolidBrush(RGB(0,0,0));
hbrush2=CreateSolidBrush(RGB(255,255,255));
switch(message)
{
case WM_CREATE:
for(i=0;i<15;i++)
for(j=0;j<15;j++)
qizi[i][j]=0;
return 0;

case WM_LBUTTONDOWN:
px=(LOWORD(lParam)-115)/30+1;
py=(HIWORD(lParam)-115)/30+1;
if(px<=15&&py<=15&&f&&qizi[px][py]==0)
{
qizi[px][py]=1;
f=!f;
InvalidateRect(hwnd,NULL,FALSE);
}
else
MessageBeep(0);
return 0;

case WM_RBUTTONDOWN:
px=(LOWORD(lParam)-115)/30+1;
py=(HIWORD(lParam)-115)/30+1;
if(px<=15&&py<=15&&!f&&qizi[px][py]==0)
{
qizi[px][py]=2;
f=!f;
InvalidateRect(hwnd,NULL,FALSE);
}
else
MessageBeep(0);
return 0;

case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
for(x=0;x<15;x++)
{
MoveToEx(hdc,100+30*x,100,NULL);
LineTo(hdc,100+30*x,520);
MoveToEx(hdc,100,100+30*x,NULL);
LineTo(hdc,520,100+30*x);
}
for(x=0;x<15;x++)
for(y=0;y<15;y++)
{
if(qizi[x][y]==1)
{
SelectObject(hdc,hbrush1);
Ellipse(hdc,(100+30*x)-sr,(100+30*y)-sr,(100+30*x)+sr,(100+30*y)+sr);
}
else if(qizi[x][y]==2)
{
SelectObject(hdc,hbrush2);
Ellipse(hdc,(100+30*x)-sr,(100+30*y)-sr,(100+30*x)+sr,(100+30*y)+sr);
}
}
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;

}
return DefWindowProc(hwnd,message,wParam,lParam);
}



每次运行的时候点了20左右个点就点了无效了~看了一下午都没有看出来问题出在那,求大神指导
...全文
194 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
paschen 2015-12-07
  • 打赏
  • 举报
回复
f 之后一直是 FALSE
paschen 2015-12-07
  • 打赏
  • 举报
回复
引用 3 楼 qq_30519181 的回复:
[quote=引用 1 楼 paschen 的回复:] 获得的是屏幕坐标,要转换成客户区坐标在操作!
额。。。我修改了一下 point.x=LOWORD(lParam); point.y=HIWORD(lParam); ScreenToClient(hwnd,&point); px=(point.x-115)/30+1; py=(point.y-115)/30+1;可是改了之后更加不对了[/quote]

case WM_LBUTTONDOWN:
		px=(LOWORD(lParam)-115)/30+1;
		py=(HIWORD(lParam)-115)/30+1;
		if(px<=15&&py<=15&&f&&qizi[px][py]==0)
		{
			qizi[px][py]=1;
			f=!f;
			InvalidateRect(hwnd,NULL,FALSE);
		}
		else
			MessageBeep(0);  
		return 0;
我知道你什么原因了,之前代码没细看,你代码中if(px<=15&&py<=15&&f&&qizi[px][py]==0) 这句第二次一直是是FALSE,导致即使点击了左键,也不会运行qizi[px][py]=1;这一句
赵4老师 2015-12-07
  • 打赏
  • 举报
回复
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
    static BOOL f=TRUE;
    int x,px,py,y,i,j;
    static int qizi[15][15];
    RECT rect;
    switch(message) {
    case WM_CREATE:
        for(i=0;i<15;i++)
            for(j=0;j<15;j++)
                qizi[i][j]=0;
        return 0;

    case WM_LBUTTONDOWN:
        px=(LOWORD(lParam)-115)/30+1;
        py=(HIWORD(lParam)-115)/30+1;
        if(px<=15&&py<=15&&f&&qizi[px][py]==0) {
            qizi[px][py]=1;
            f=!f;
            InvalidateRect(hwnd,NULL,FALSE);
        } else
            MessageBeep(0);
        return 0;

    case WM_RBUTTONDOWN:
        px=(LOWORD(lParam)-115)/30+1;
        py=(HIWORD(lParam)-115)/30+1;
        if(px<=15&&py<=15&&!f&&qizi[px][py]==0) {
            qizi[px][py]=2;
            f=!f;
            InvalidateRect(hwnd,NULL,FALSE);
        } else
            MessageBeep(0);
        return 0;

    case WM_PAINT:
        HDC hdc;
        PAINTSTRUCT ps;
        hdc=BeginPaint(hwnd,&ps);
        HBRUSH hobrush,hbrush1,hbrush2;
        hbrush1=CreateSolidBrush(RGB(0,0,0));
        hbrush2=CreateSolidBrush(RGB(255,255,255));
        for(x=0;x<15;x++) {
            MoveToEx(hdc,100+30*x,100,NULL);
            LineTo(hdc,100+30*x,520);
            MoveToEx(hdc,100,100+30*x,NULL);
            LineTo(hdc,520,100+30*x);
        }
        hobrush=SelectObject(hdc,hbrush1);
        for(x=0;x<15;x++)
            for(y=0;y<15;y++) {
                if(qizi[x][y]==1) {
                    SelectObject(hdc,hbrush1);
                    Ellipse(hdc,(100+30*x)-sr,(100+30*y)-sr,(100+30*x)+sr,(100+30*y)+sr);
                } else if(qizi[x][y]==2) {
                    SelectObject(hdc,hbrush2);
                    Ellipse(hdc,(100+30*x)-sr,(100+30*y)-sr,(100+30*x)+sr,(100+30*y)+sr);
                }
            }
        SelectObject(hdc,hobrush);
        DeleteObject(hbrush2);
        DeleteObject(hbrush1);
        EndPaint(hwnd,&ps);
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;

    }
    return DefWindowProc(hwnd,message,wParam,lParam);
}
赵4老师 2015-12-07
  • 打赏
  • 举报
回复
DeleteObject The DeleteObject function deletes a logical pen, brush, font, bitmap, region, or palette, freeing all system resources associated with the object. After the object is deleted, the specified handle is no longer valid. BOOL DeleteObject( HGDIOBJ hObject // handle to graphic object ); Parameters hObject Handle to a logical pen, brush, font, bitmap, region, or palette. Return Values If the function succeeds, the return value is nonzero. If the specified handle is not valid or is currently selected into a device context, the return value is zero. Windows NT: To get extended error information, callGetLastError. Remarks Do not delete a drawing object (pen or brush) while it is still selected into a device context. When a pattern brush is deleted, the bitmap associated with the brush is not deleted. The bitmap must be deleted independently. Windows CE: The DeleteObject function returns false if the object is currently selected into a device context. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Requires version 1.0 or later. Header: Declared in wingdi.h. Import Library: Use gdi32.lib. See Also Device Contexts Overview, Device Context Functions, SelectObject
qq_30519181 2015-12-06
  • 打赏
  • 举报
回复
额。。。我修改了一下 point.x=LOWORD(lParam); point.y=HIWORD(lParam); ScreenToClient(hwnd,&point); px=(point.x-115)/30+1; py=(point.y-115)/30+1;可是改了之后更加不对了
qq_30519181 2015-12-06
  • 打赏
  • 举报
回复
引用 1 楼 paschen 的回复:
获得的是屏幕坐标,要转换成客户区坐标在操作!
额。。。我修改了一下 point.x=LOWORD(lParam); point.y=HIWORD(lParam); ScreenToClient(hwnd,&point); px=(point.x-115)/30+1; py=(point.y-115)/30+1;可是改了之后更加不对了
paschen 2015-12-06
  • 打赏
  • 举报
回复
获得的是屏幕坐标,要转换成客户区坐标在操作!

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧