窗口操作函数的使用
通过窗口实现移动按钮、设置文本框内容、获取文本框内容、设置文件父窗口功能
代码如下:
#include<Windows.h>
#include<CommCtrl.h>
void print(LPCWSTR format, ...)
{
WCHAR outBuff[1024] = { 0 };
va_list args;
va_start(args, format);
wvsprintfW(outBuff, format, args);
va_end(args);
OutputDebugStringW(outBuff);
}
BOOL CALLBACK EnumChildProc(
_In_ HWND hwnd,
_In_ LPARAM lParam
)
{
WCHAR buff[100]{ 0 };
GetWindowTextW(hwnd,buff,100);
print(L"窗口名称:%ls\n",buff);
return TRUE;
}
LRESULT CALLBACK WindowProc(
_In_ HWND hwnd,
_In_ UINT uMsg,
_In_ WPARAM wParam,//低两位存储控件ID
_In_ LPARAM lParam//存储控件句柄
)
{
static HINSTANCE hInstance = GetModuleHandle(NULL);//获取当前程序的实例句柄
switch (uMsg)
{
case WM_CREATE:
//添加控件
CreateWindowW(WC_BUTTON, L"移动按钮", WS_CHILD | WS_VISIBLE, 10, 10, 120, 40, hwnd, (HMENU)0x100, hInstance, 0);
CreateWindowW(WC_BUTTON, L"获取文本框内容", WS_CHILD | WS_VISIBLE, 10, 60, 120, 40, hwnd, (HMENU)0x101, hInstance, 0);
CreateWindowW(WC_BUTTON, L"设置文本框内容", WS_CHILD | WS_VISIBLE, 10, 110, 120, 40, hwnd, (HMENU)0x102, hInstance, 0);
CreateWindowW(WC_BUTTON, L"设置父窗口", WS_CHILD | WS_VISIBLE, 10, 160, 120, 40, hwnd, (HMENU)0x103, hInstance, 0);
CreateWindowW(WC_BUTTON, L"枚举窗口", WS_CHILD | WS_VISIBLE, 10, 210, 120, 40, hwnd, (HMENU)0x105, hInstance, 0);
CreateWindowW(WC_EDIT, L"文本框内容", WS_CHILD | WS_BORDER | WS_VISIBLE, 10, 260, 320, 80, hwnd, (HMENU)0x104, hInstance, 0);
break;
case WM_CLOSE:
DestroyWindow(hwnd);
PostQuitMessage(0);
break;
case WM_COMMAND://标准控件的消息
{
WORD ControlId = LOWORD(wParam);//wParam低两个字节存储的是每个控件的不同IP
switch (ControlId)
{
case 0x100:
{
RECT rect{ 0 };
GetClientRect(hwnd, &rect);
int x = rand() % (rect.right-120);
int y = rand() % (rect.bottom-40);
print(L"left=%d\n", rect.left);
print(L"right=%d\n", rect.right);
print(L"top=%d\n", rect.top);
print(L"bottom=%d\n", rect.bottom);
MoveWindow((HWND)lParam,//0x100ID控件的句柄
x,y,120,40,
TRUE);//移动完后是否重绘句柄
break;
}
case 0x101:
{
WCHAR buff[100] = { 0 };
HWND hedit = GetDlgItem(hwnd, 0x104);//获取想要获取的文本框的句柄,第一个参数是该窗口父窗口的句柄,第二个是ID
GetWindowTextW(hedit,buff,100);
MessageBoxW(hwnd, buff, L"提示", MB_OK);
break;
}
case 0x102:
{
//HWND hedit = GetDlgItem(hwnd, 0x104);
//SetWindowTextW(hedit, L"设置了文本内容");//设置文本框内的内容,文本框的句柄,第一个参数是该窗口的句柄,第二个是内容
SetDlgItemTextW(hwnd, 0x104, L"SetDlgItemTextW");//将前两步合为一步
break;
}
case 0x103:
{
HWND hnote = FindWindowW(NULL, L"无标题 - 记事本");//如果 lpClassName 为 NULL,它将查找其标题与 lpWindowName 参数匹配的任何窗口。窗口名称 (窗口的标题) 。 如果此参数为 NULL,则所有窗口名称都匹配。
SetParent((HWND)lParam, hnote);//设置父窗口,1、当前窗口句柄 2、父窗口句柄
break;
}
case 0x105:
{
EnumChildWindows(hwnd,EnumChildProc,0);
break;
}
default:
break;
}
break;
}
}
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPSTR lpCmdLine, int nCmdShow)
{
//1、创建窗口类
WNDCLASSW myWndClass{ 0 };
myWndClass.lpfnWndProc = WindowProc;
myWndClass.lpszClassName = L"dsdWindows";
myWndClass.hbrBackground = CreateSolidBrush(RGB(255, 255, 255, 255));//背景画布
//2、注册窗口类
RegisterClassW(&myWndClass);
//3、创建窗口
HWND hwnd = CreateWindowW(
myWndClass.lpszClassName,
L"dsdWindows",
WS_OVERLAPPEDWINDOW,//重叠窗口样式,其他窗口写在该窗口上方即16行代码
CW_USEDEFAULT,
0,
CW_USEDEFAULT,
0,
NULL,
NULL,
hInstance,
0
);
//4、显示窗口
ShowWindow(hwnd, SW_SHOWNORMAL);//SHOWWINDOWS展示样式
MSG msg{ 0 };
while (GetMessageW(&msg, 0, 0, 0)) {
TranslateMessage(&msg);//翻译消息为WM_CHAR 可以在文本框控件中对文字进行输入
DispatchMessageW(&msg);
}
return 0;
}
设置父窗口的时候需要获取到该窗口的句柄,可以用lParam设置当前窗口句柄。
使用FindWindowW()函数来进行全窗口的句柄查找。