我在获取控件的正确
CRect
坐标时遇到了一些问题,始终如一地在Windows 11
和Windows 10
上工作。对于Windows 11
我必须操纵CRect
.
我正在使用
CRect
设置缩略图预览剪辑,在任务栏中使用 ITaskbarList3
和 SetThumbnailClip
.
看来,对于
Windows 11
,我必须补偿窗口顶部标题栏、左侧、右侧和底部边距。而在Windows 10
,我不必这样做以获得正确的坐标。
我怀疑我做错了,无论操作系统如何,我都应该看到一致的行为。我不会想到这些 API 调用中的任何一个都会改变。
这是我目前使用的代码片段。
ITaskbarList3* getTaskBar() {
HRESULT hr;
ITaskbarList3* taskBar = NULL;
CoInitialize(NULL);
hr = CoCreateInstance(CLSID_TaskbarList, NULL, CLSCTX_SERVER,
IID_ITaskbarList3, (LPVOID*)&taskBar);
if (SUCCEEDED(hr)) {
taskBar->HrInit();
}
CoUninitialize(); // maybe not ok here...
return taskBar;
}
设置缩略图:
ITaskbarList3 *taskbar = getTaskBar();
if (taskbar == NULL) return;
HRESULT result = taskbar->SetThumbnailClip(hWnd, &rect);
taskbar->Release();
Windows 10
CRect rect;
HWND wnd = GetDlgItem(hwnd, ctrlID); // hwnd is the application wnd
GetWindowRect(wnd, &rect); // get the window rect, includes the frame
HWND pwnd = GetParent(wnd); // get the parent window
POINT rectTL;
rectTL.x = rect.left;
rectTL.y = rect.top;
ScreenToClient(pwnd, &rectTL); // convert the window to screen coordinates
POINT rectBR;
rectBR.x = rect.right;
rectBR.y = rect.bottom;
ScreenToClient(pwnd, &rectBR);
rect.left = rectTL.x;
rect.top = rectTL.y;
rect.right = rectBR.x;
rect.bottom = rectBR.y;
return rect; // for windows 10, for windows 11 do more...
Windows 11,这感觉不对……
// for windows 11 I have to do a bit more.
int leftM, rightM, topM, bottomM;
CWnd *mainWnd = AfxGetMainWnd();
CalculateWndMargin(*mainWnd, leftM, rightM, topM, bottomM);
// adjsut the window frame offset for top and bottom
rect.top += topM + bottomM;
rect.bottom += bottomM + topM;
// adjust the window frame offset for the right and left
rect.right += rightM;
rect.left += leftM;
return rect;
从堆栈溢出中借用函数...
void CalculateWndMargin(const CWnd& wnd, int& leftM, int& rightM, int& topM, int& bottomM)
{
CRect wndRect;
wnd.GetWindowRect(wndRect);
CRect screenRect;
wnd.GetClientRect(screenRect);
wnd.ClientToScreen(screenRect);
leftM = screenRect.left - wndRect.left;
rightM = wndRect.right - screenRect.right;
topM = screenRect.top - wndRect.top;
bottomM = wndRect.bottom - screenRect.bottom;
}