ClientRect的大小问题

本文详细介绍了在特定应用程序界面中屏幕尺寸的具体数值,包括状态栏高度、主界面高度等,并解释了如何通过CEikAppUi::ClientRect()获取状态栏下方的有效显示区域。

屏幕大小:176*208
状态栏高度为:44
主界面高度:144
CBA高度:20

CEikAppUi::ClientRect() 得到大小为176*144 TRect(TPoint(0,44),TSize(176,144))
若在资源文件中去掉cba CEikAppUi::ClientRect() 得到大小为176*164 TRect(TPoint(0,44),TSize(176,164))
说明CEikAppUi::ClientRect()返回的是状态栏下面的区域
 

/ 导入必要的依赖项 #include <windows.h> // 全局变量用于存储当前选择的图形类型 int currentShape = 0; // 函数声明 LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); void DrawSquare(HDC hdc); void DrawRectangle(HDC hdc); void DrawIsoscelesTriangle(HDC hdc); void DrawCircle(HDC hdc); // 主函数入口点 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow) { // 注册窗口类 const char CLASS_NAME[] = "Drawing Window Class"; WNDCLASS wc = { }; wc.lpfnWndProc = WindowProc; wc.hInstance = hInstance; wc.lpszClassName = CLASS_NAME; RegisterClass(&wc); // 创建窗口 HWND hwnd = CreateWindowEx( 0, // 扩展样式 CLASS_NAME, // 窗口类名 "简单画图工具", // 窗口标题 WS_OVERLAPPEDWINDOW, // 窗口风格 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, // 位置和大小 NULL, // 父窗口句柄 NULL, // 菜单句柄 hInstance, // 实例句柄 NULL // 附加参数 ); if (hwnd == NULL) { return 0; } ShowWindow(hwnd, nCmdShow); // 消息循环 MSG msg = { }; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0; } // 窗口过程函数 LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { static bool isDrawing = false; HDC hdc; PAINTSTRUCT ps; switch (uMsg) { case WM_KEYDOWN: switch ((int)wParam) { case '1': currentShape = 1; InvalidateRect(hwnd, NULL, TRUE); break; case '2': currentShape = 2; InvalidateRect(hwnd, NULL, TRUE); break; case '3': currentShape = 3; InvalidateRect(hwnd, NULL, TRUE); break; case '4': currentShape = 4; InvalidateRect(hwnd, NULL, TRUE); break; default: break; } return 0; case WM_PAINT: hdc = BeginPaint(hwnd, &ps); RECT clientRect; GetClientRect(hwnd, &clientRect); switch (currentShape) { case 1: DrawSquare(hdc); break; case 2: DrawRectangle(hdc); break; case 3: DrawIsoscelesTriangle(hdc); break; case 4: DrawCircle(hdc); break; default: break; } EndPaint(hwnd, &ps); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; default: return DefWindowProc(hwnd, uMsg, wParam, lParam); } } // 绘制正方形 void DrawSquare(HDC hdc) { RECT clientRect; GetClientRect(NULL, &clientRect); // 获取客户区大小 int side = min(clientRect.right, clientRect.bottom) / 2; int left = (clientRect.right - side) / 2; int top = (clientRect.bottom - side) / 2; Rectangle(hdc, left, top, left + side, top + side); } // 绘制长方形 void DrawRectangle(HDC hdc) { RECT clientRect; GetClientRect(NULL, &clientRect); // 获取客户区大小 int width = clientRect.right / 2; int height = clientRect.bottom / 3; int left = (clientRect.right - width) / 2; int top = (clientRect.bottom - height) / 2; Rectangle(hdc, left, top, left + width, top + height); } // 绘制等腰三角形 void DrawIsoscelesTriangle(HDC hdc) { RECT clientRect; GetClientRect(NULL, &clientRect); // 获取客户区大小 int base = clientRect.right / 2; int height = clientRect.bottom / 4; POINT points[3]; points[0].x = (clientRect.right - base) / 2; points[0].y = clientRect.bottom - height; points[1].x = (clientRect.right + base) / 2; points[1].y = clientRect.bottom - height; points[2].x = clientRect.right / 2; points[2].y = clientRect.bottom - (height * 2); Polygon(hdc, points, 3); } // 绘制圆形 void DrawCircle(HDC hdc) { RECT clientRect; GetClientRect(NULL, &clientRect); // 获取客户区大小 int radius = min(clientRect.right, clientRect.bottom) / 4; int centerX = clientRect.right / 2; int centerY = clientRect.bottom / 2; Ellipse(hdc, centerX - radius, centerY - radius, centerX + radius, centerY + radius); }
07-24
<think>好的,我需要回答用户关于如何在MFC中获取控件大小的方法并提供示例。首先,用户提到了引用[1]中的内容,里面提到了通过扩展方法GetClientWidth,使用Control类的Width属性减去Decoration来获取客户区宽度。这说明在MFC中,可以直接访问控件的Width和Height属性来获取尺寸,或者使用GetWindowRect等方法。 接下来,我需要确认MFC中常用的获取控件大小的方式。通常,可以使用CWnd类的成员函数,比如GetWindowRect和GetClientRect。GetWindowRect获取的是屏幕坐标下的控件位置和大小,而GetClientRect获取的是客户区的大小,不包括边框和标题栏。此外,用户提到的引用[1]中使用了theControl.Width,这可能是MFC中Control类的属性,或者可能是用户自定义的扩展方法。 另外,用户提供的代码示例中有GetClientWidth方法,其中使用了theControl.Width,这可能对应MFC中的GetWindowRect或GetClientRect。需要注意,MFC中可能需要先获取控件的指针,然后调用相关方法。 为了正确回答,我需要分步骤说明如何获取控件的大小,并给出示例代码。首先,通过GetDlgItem获取控件指针,然后使用GetWindowRect获取相对于屏幕的尺寸,或者使用GetClientRect获取客户区尺寸。如果需要转换为对话框内的坐标,可以使用ScreenToClient转换。 最后,根据引用[1]中的方法,用户可能希望直接获取宽度和高度,所以示例中应展示如何获取Width和Height,或者通过Rect的right和bottom来得到尺寸。同时,注意是否需要处理边框等装饰部分的调整,如引用[1]中的Decoration变量。</think>在MFC中获取控件大小可以通过以下方法实现,具体步骤如下: 1. **使用GetWindowRect获取屏幕坐标尺寸** ```cpp CRect rect; CWnd* pCtrl = GetDlgItem(IDC_YOUR_CONTROL); // 获取控件指针 pCtrl->GetWindowRect(&rect); // 获取屏幕坐标系下的矩形 ScreenToClient(rect); // 转换为对话框客户区坐标 int width = rect.Width(); // 获取控件宽度 int height = rect.Height(); // 获取控件高度 ``` 2. **直接获取客户区尺寸(不含边框)** ```cpp CRect clientRect; pCtrl->GetClientRect(&clientRect); // 获取客户区尺寸 int clientWidth = clientRect.right - clientRect.left; int clientHeight = clientRect.bottom - clientRect.top; ``` 3. **通过扩展方法获取(如引用[1]示例)** ```cpp // 需要先定义扩展方法 int GetClientWidth(CWnd* pCtrl) { CRect rect; pCtrl->GetWindowRect(&rect); return rect.Width() - 2; // 假设边框占2像素 } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值