getclientrect l

本文介绍了GetClientRect函数的功能及用法,此函数用于获取窗口客户区的坐标范围,包括左上角和右下角坐标。文章详细解释了函数原型、参数及返回值,并指出在不同操作系统版本中的适用情况。
  name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-5572165936844014&dt=1193665761703&lmt=1193665780&format=336x280_as&output=html&correlator=1193665761687&url=http%3A%2F%2Fwww.codeguru.cn%2Fpublic%2Fiframe%2Fwinapiiframe.htm&color_bg=FFFFFF&color_text=000000&color_link=000000&color_url=FFFFFF&color_border=FFFFFF&ad_type=text&ga_vid=1285758818.1193665762&ga_sid=1193665762&ga_hid=111695597&flash=9&u_h=768&u_w=1024&u_ah=740&u_aw=1024&u_cd=32&u_tz=480&u_his=8&u_java=true" frameborder="0" width="336" scrolling="no" height="280" allowtransparency="allowtransparency">     函数功能:该函数获取窗口客户区的坐标。客户区坐标指定客户区的左上角和右下角。由于客户区坐标是相对子窗口客户区的左上角而言的,因此左上角坐标为(0,0)

    函数原型:BOOL GetClientRect(HWND hWnd,LPRECT lpRect);

    参数:

    GetLastError 函数。

    备注:Windows CE:命令条包含在客户区中。

    速查:Windows NT: 3.1以上版本:Windows:95以上版本: Windows CE:1.0以上版本:头文件:winuser.h;库文件:user32.lib

#include <windows.h> #include <commdlg.h> #include <vector> #include <string> // 菜单命令标识符 #define IDM_OPEN 1001 #define IDM_SAVE 1002 #define IDM_EXIT 1003 #define IDM_ABOUT 1004 // 控件ID定义 #define IDC_INPUT_EDIT 105 #define IDC_DISPLAY_EDIT 106 #define IDC_LOCATION_LIST 107 #define IDC_CHECKBOX 108 #define IDC_SUBMIT_BUTTON 109 // 广东地名列表 const std::vector<std::wstring> guangdongCities = { L"广州市", L"深圳市", L"珠海市", L"汕头市", L"佛山市", L"韶关市", L"湛江市", L"肇庆市", L"江门市", L"茂名市", L"惠州市", L"梅州市", L"汕尾市", L"河源市", L"阳江市", L"清远市", L"东莞市", L"中山市", L"潮州市", L"揭阳市", L"云浮市" }; // 窗口过程函数 LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_CREATE: { // 创建文本标签 CreateWindow(L"STATIC", L"输入内容:", WS_VISIBLE | WS_CHILD, 20, 20, 80, 20, hWnd, NULL, NULL, NULL); CreateWindow(L"STATIC", L"选择地点:", WS_VISIBLE | WS_CHILD, 20, 60, 80, 20, hWnd, NULL, NULL, NULL); CreateWindow(L"STATIC", L"输出结果:", WS_VISIBLE | WS_CHILD, 250, 20, 80, 20, hWnd, NULL, NULL, NULL); // 创建文本输入框 CreateWindow(L"EDIT", L"", WS_CHILD | WS_VISIBLE | WS_BORDER, 100, 20, 120, 25, hWnd, (HMENU)IDC_INPUT_EDIT, NULL, NULL); // 创建列表框(含广东地名) HWND hList = CreateWindow(L"LISTBOX", L"", WS_CHILD | WS_VISIBLE | WS_BORDER | LBS_STANDARD | LBS_HASSTRINGS | WS_VSCROLL, 20, 85, 200, 150, hWnd, (HMENU)IDC_LOCATION_LIST, NULL, NULL); // 填充广东地名 for (const auto& city : guangdongCities) { SendMessage(hList, LB_ADDSTRING, 0, (LPARAM)city.c_str()); } SendMessage(hList, LB_SETCURSEL, 0, 0); // 默认选中第一项 // 创建复选框 CreateWindow(L"BUTTON", L"包含详细信息", WS_CHILD | WS_VISIBLE | BS_CHECKBOX, 20, 250, 150, 25, hWnd, (HMENU)IDC_CHECKBOX, NULL, NULL); // 创建提交按钮 CreateWindow(L"BUTTON", L"显示选择结果", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 20, 285, 120, 30, hWnd, (HMENU)IDC_SUBMIT_BUTTON, NULL, NULL); // 创建文本框(显示结果) CreateWindow(L"EDIT", L"选择结果将显示在这里", WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE | WS_VSCROLL | ES_READONLY, 250, 40, 300, 280, hWnd, (HMENU)IDC_DISPLAY_EDIT, NULL, NULL); break; } case WM_COMMAND: { if (LOWORD(wParam) == IDC_SUBMIT_BUTTON) { // 获取输入框内容 wchar_t inputText[256] = { 0 }; GetWindowText(GetDlgItem(hWnd, IDC_INPUT_EDIT), inputText, 256); // 获取列表选中项 wchar_t selectedCity[256] = { 0 }; int selIndex = SendMessage(GetDlgItem(hWnd, IDC_LOCATION_LIST), LB_GETCURSEL, 0, 0); if (selIndex != LB_ERR) { SendMessage(GetDlgItem(hWnd, IDC_LOCATION_LIST), LB_GETTEXT, selIndex, (LPARAM)selectedCity); } // 获取复选框状态 bool isChecked = (SendMessage(GetDlgItem(hWnd, IDC_CHECKBOX), BM_GETCHECK, 0, 0) == BST_CHECKED); // 组合结果显示 wchar_t result[1024] = { 0 }; if (isChecked) { wsprintf(result, L"您输入的内容是: %s\n\n您选择的地点是: %s\n\n当前时间: %s\n操作状态: 成功完成", inputText, selectedCity, __TIMEW__); } else { wsprintf(result, L"输入内容: %s\n选择城市: %s", inputText, selectedCity); } SetWindowText(GetDlgItem(hWnd, IDC_DISPLAY_EDIT), result); } else if (LOWORD(wParam) == IDM_OPEN) { // 创建打开文件对话框 OPENFILENAMEW ofn; wchar_t szFile[260] = { 0 }; ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = hWnd; ofn.lpstrFile = szFile; ofn.nMaxFile = sizeof(szFile); ofn.lpstrFilter = L"文本文件 (*.txt)\0*.txt\0所有文件 (*.*)\0*.*\0"; ofn.nFilterIndex = 1; ofn.lpstrTitle = L"选择要打开的文件"; ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; if (GetOpenFileNameW(&ofn)) { MessageBoxW(hWnd, szFile, L"已选择文件", MB_OK | MB_ICONINFORMATION); } break; } else if (LOWORD(wParam) == IDM_SAVE) { // 创建保存文件对话框 OPENFILENAMEW ofn; wchar_t szFile[260] = { 0 }; ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = hWnd; ofn.lpstrFile = szFile; ofn.nMaxFile = sizeof(szFile); ofn.lpstrFilter = L"文本文件 (*.txt)\0*.txt\0所有文件 (*.*)\0*.*\0"; ofn.nFilterIndex = 1; ofn.lpstrDefExt = L"txt"; ofn.lpstrTitle = L"保存文件"; ofn.Flags = OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST; if (GetSaveFileNameW(&ofn)) { wchar_t message[300]; swprintf(message, 300, L"文件已保存到:\n%s", szFile); MessageBoxW(hWnd, message, L"保存成功", MB_OK | MB_ICONINFORMATION); } break; } else if (LOWORD(wParam) == IDM_EXIT) { PostQuitMessage(0); break; } else if (LOWORD(wParam) == IDM_ABOUT) { MessageBoxW(hWnd, L"广东城市选择器\n版本 2.0\n添加了城市选择功能", L"关于", MB_OK | MB_ICONINFORMATION); break; } break; } case WM_SIZE: { // 调整文本框大小以适应窗口 RECT rect; GetClientRect(hWnd, &rect); MoveWindow(GetDlgItem(hWnd, IDC_DISPLAY_EDIT), rect.right - 320, 40, 300, rect.bottom - 60, TRUE); break; } case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProcW(hWnd, msg, wParam, lParam); } return 0; } // 创建主菜单 HMENU CreateMainMenu() { HMENU hMenu = CreateMenu(); HMENU hFileMenu = CreatePopupMenu(); HMENU hHelpMenu = CreatePopupMenu(); // 文件菜单 AppendMenuW(hFileMenu, MF_STRING, IDM_OPEN, L"打开(&O)"); AppendMenuW(hFileMenu, MF_STRING, IDM_SAVE, L"保存(&S)"); AppendMenuW(hFileMenu, MF_SEPARATOR, 0, NULL); AppendMenuW(hFileMenu, MF_STRING, IDM_EXIT, L"退出(&X)"); // 帮助菜单 AppendMenuW(hHelpMenu, MF_STRING, IDM_ABOUT, L"关于(&A)"); // 主菜单栏 AppendMenuW(hMenu, MF_POPUP, (UINT_PTR)hFileMenu, L"文件(&F)"); AppendMenuW(hMenu, MF_POPUP, (UINT_PTR)hHelpMenu, L"帮助(&H)"); return hMenu; } // 程序入口点 int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) { // 注册窗口类 const wchar_t CLASS_NAME[] = L"CitySelectorClass"; WNDCLASSW wc = { 0 }; wc.lpfnWndProc = WndProc; wc.hInstance = hInstance; wc.lpszClassName = CLASS_NAME; wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wc.hCursor = LoadCursor(NULL, IDC_ARROW); if (!RegisterClassW(&wc)) { MessageBoxW(NULL, L"窗口类注册失败!", L"错误", MB_ICONERROR); return 1; } // 创建菜单 HMENU hMenu = CreateMainMenu(); // 创建窗口 HWND hWnd = CreateWindowExW( 0, CLASS_NAME, L"广东城市选择器", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 800, 500, NULL, hMenu, // 附加菜单 hInstance, NULL ); if (hWnd == NULL) { MessageBoxW(NULL, L"窗口创建失败!", L"错误", MB_ICONERROR); return 1; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); // 消息循环 MSG msg; while (GetMessageW(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessageW(&msg); } return (int)msg.wParam; } 提示“__TIMEW__”: 未声明的标识符
09-28
下面程序的主界面上加上一个文本输入框,一个文本框,一个列表(含有广东各地地名),一个复选键,一个按钮(按键后在文本框显示文本输入框和列表选中内容)#include <windows.h> #include <commdlg.h> #include <stdio.h> // 菜单命令标识符 #define IDM_OPEN 1001 #define IDM_SAVE 1002 #define IDM_EXIT 1003 #define IDM_ABOUT 1004 // 窗口过程函数 LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_COMMAND: { // 处理菜单命令 switch (LOWORD(wParam)) { case IDM_OPEN: { // 创建打开文件对话框 OPENFILENAMEW ofn; wchar_t szFile[260] = { 0 }; ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = hWnd; ofn.lpstrFile = szFile; ofn.nMaxFile = sizeof(szFile); ofn.lpstrFilter = L"文本文件 (*.txt)\0*.txt\0所有文件 (*.*)\0*.*\0"; ofn.nFilterIndex = 1; ofn.lpstrTitle = L"选择要打开的文件"; ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; if (GetOpenFileNameW(&ofn)) { MessageBoxW(hWnd, szFile, L"已选择文件", MB_OK | MB_ICONINFORMATION); } break; } case IDM_SAVE: { // 创建保存文件对话框 OPENFILENAMEW ofn; wchar_t szFile[260] = { 0 }; ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = hWnd; ofn.lpstrFile = szFile; ofn.nMaxFile = sizeof(szFile); ofn.lpstrFilter = L"文本文件 (*.txt)\0*.txt\0所有文件 (*.*)\0*.*\0"; ofn.nFilterIndex = 1; ofn.lpstrDefExt = L"txt"; ofn.lpstrTitle = L"保存文件"; ofn.Flags = OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST; if (GetSaveFileNameW(&ofn)) { wchar_t message[300]; swprintf(message, 300, L"文件已保存到:\n%s", szFile); MessageBoxW(hWnd, message, L"保存成功", MB_OK | MB_ICONINFORMATION); } break; } case IDM_EXIT: PostQuitMessage(0); break; case IDM_ABOUT: MessageBoxW(hWnd, L"Windows GUI 示例程序\n版本 1.0", L"关于", MB_OK | MB_ICONINFORMATION); break; } break; } case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hWnd, &ps); // 绘制窗口内容 RECT rect; GetClientRect(hWnd, &rect); DrawTextW(hdc, L"欢迎使用 Windows GUI 程序", -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER); EndPaint(hWnd, &ps); break; } case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProcW(hWnd, msg, wParam, lParam); } return 0; } // 创建主菜单 HMENU CreateMainMenu() { HMENU hMenu = CreateMenu(); HMENU hFileMenu = CreatePopupMenu(); HMENU hHelpMenu = CreatePopupMenu(); // 文件菜单 AppendMenuW(hFileMenu, MF_STRING, IDM_OPEN, L"打开(&O)"); AppendMenuW(hFileMenu, MF_STRING, IDM_SAVE, L"保存(&S)"); AppendMenuW(hFileMenu, MF_SEPARATOR, 0, NULL); AppendMenuW(hFileMenu, MF_STRING, IDM_EXIT, L"退出(&X)"); // 帮助菜单 AppendMenuW(hHelpMenu, MF_STRING, IDM_ABOUT, L"关于(&A)"); // 主菜单栏 AppendMenuW(hMenu, MF_POPUP, (UINT_PTR)hFileMenu, L"文件(&F)"); AppendMenuW(hMenu, MF_POPUP, (UINT_PTR)hHelpMenu, L"帮助(&H)"); return hMenu; } // 程序入口点 int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) { // 注册窗口类 const wchar_t CLASS_NAME[] = L"MainWindowClass"; WNDCLASSW wc = { 0 }; wc.lpfnWndProc = WndProc; wc.hInstance = hInstance; wc.lpszClassName = CLASS_NAME; wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wc.hCursor = LoadCursor(NULL, IDC_ARROW); if (!RegisterClassW(&wc)) { MessageBoxW(NULL, L"窗口类注册失败!", L"错误", MB_ICONERROR); return 1; } // 创建菜单 HMENU hMenu = CreateMainMenu(); // 创建窗口 HWND hWnd = CreateWindowExW( 0, CLASS_NAME, L"Windows GUI 示例程序", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 600, 400, NULL, hMenu, // 附加菜单 hInstance, NULL ); if (hWnd == NULL) { MessageBoxW(NULL, L"窗口创建失败!", L"错误", MB_ICONERROR); return 1; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); // 消息循环 MSG msg; while (GetMessageW(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessageW(&msg); } return (int)msg.wParam; }
09-28
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值