使用子窗口和键盘
功能 :创建25个子窗口通过键盘实现移动和点击的操作
在主窗口中
当主窗口获取焦点时,
WM_SETFOCUS时
设置默认的子窗口焦点
默认的子窗口ID值为0;
即idFocus =0;
SetFocus(GetDLgItem(hWnd,idFocus));
此时子窗口获得焦点
那么在子窗口中
WM_CREATE:
SetWindowLongPtr(hWnd,0,0); //子窗口的预留空间置为零,相当于初始化
SetFocus(hWnd);//该句我的理解时保持获取焦点
此时点击的话就会有如下代码
按下按钮时
WM_KEYDOWN:
if(wParam !=VK_RETURN && wParam != VK_SPACE)
{
SendMessage(GetParent(hWnd),WM_KEYDOWN,wParam,0);
return 0;
}
若按下回车和空格键,表示点击了。那么就在当前的子窗口点击 ,因此没有return
WM_LBUTTONDOWN:
SetWindowLongPtr(hWnd,0,1^GetWindowLongPtr(hWnd,0)); //设置和预留空间相与
InvalidateRect(hWnd,NULL,TRUE);
当其获取焦点时可以把窗口的ID赋值给IdFocus;
WM_SETFOCUS:
IdFocus = GetWindowLongPtr(hWnd,GWL_ID);
当时失去焦点时记得刷新画布
WM_KILLFOCUS:
InvalidateRect(hWnd,NULL,TRUE);
break;
最后时绘制函数
WM_PAINT:
hdc=BeginPaint(hWnd,&ps);
GetClientRect(hWnd,&rect);
Rectangle(hdc,0,0,rect.right,rect.bottom);
if(GetWindowLongPtr(hWnd,0))
{
MoveToEx(hdc,0,0,NULL);
LineTo(hdc,rect.right,rect.bottom);
MoveToEx(hdc,rect.right,0,NULL);
LineTo(hdc,0,rect.bottom);
}
if(GetFocus()==hWnd)
{
rect.left += rect.right / 10;
rect.right -= rect.left;
rect.top += rect.bottom / 10;
rect.bottom -= rect.top;
SelectObject(hdc, GetStockObject(NULL_BRUSH));
SelectObject(hdc, CreatePen(PS_DASH, 0, 0));
Rectangle(hdc, rect.left, rect.top, rect.right, rect.bottom);
DeleteObject(SelectObject(hdc, GetStockObject(BLACK_PEN)));
}
EndPaint(hWnd,&ps); 沙发 Fatsun 发表于 2019-10-24 11:22
沙发
2333
页:
[1]