帮我写一个c++使用GetWindowRect获取到窗口坐标后,在该窗口绘制一个边框作为显示
时间: 2023-04-06 10:03:32 浏览: 119
VC++自绘窗口, 自绘控件,自绘边框,高速绘图
好的,以下是使用GetWindowRect获取窗口坐标并绘制边框的C代码:
```
#include <Windows.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// 创建窗口
WNDCLASS wc = { 0 };
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszClassName = "MyWindowClass";
RegisterClass(&wc);
HWND hWnd = CreateWindow("MyWindowClass", "My Window", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 500, 500, NULL, NULL, hInstance, NULL);
// 显示窗口
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
// 消息循环
MSG msg = { 0 };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_PAINT:
{
// 获取窗口坐标
RECT rect;
GetWindowRect(hWnd, &rect);
// 转换为客户区坐标
POINT pt1 = { rect.left, rect.top };
POINT pt2 = { rect.right, rect.bottom };
ScreenToClient(hWnd, &pt1);
ScreenToClient(hWnd, &pt2);
rect.left = pt1.x;
rect.top = pt1.y;
rect.right = pt2.x;
rect.bottom = pt2.y;
// 绘制边框
HDC hdc = GetDC(hWnd);
HPEN hPen = CreatePen(PS_SOLID, 3, RGB(255, 0, 0));
SelectObject(hdc, hPen);
SelectObject(hdc, GetStockObject(NULL_BRUSH));
Rectangle(hdc, rect.left, rect.top, rect.right, rect.bottom);
DeleteObject(hPen);
ReleaseDC(hWnd, hdc);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
```
这段代码会创建一个窗口,并在窗口的客户区绘制一个红色边框,边框的大小和位置与窗口相同。使用GetWindowRect获取窗口坐标后,需要将其转换为客户区坐标,然后使用Rectangle函数绘制矩形边框。
阅读全文