C++ 如何在不使用句柄或HWND的情况下从内存hdc获取矩形(或&;rc)

C++ 如何在不使用句柄或HWND的情况下从内存hdc获取矩形(或&;rc),c++,c++11,C++,C++11,我正在使用Microsoft Windows(32位)。请不要使用Visual Studio或.net 如果要使用windows句柄,我有以下功能: // I have a handle to a window. // HWND Handle_Of_SomeWindow // I previously assigned a handle for that and use it. // I have some Unicode text that I am using. wstring S

我正在使用Microsoft Windows(32位)。请不要使用Visual Studio或.net

如果要使用windows句柄,我有以下功能:

// I have a handle to a window.    
// HWND Handle_Of_SomeWindow
// I previously assigned a handle for that and use it.

// I have some Unicode text that I am using.
wstring SomeWideStringText = L"C++ stole my lunch money.";

// I convert that wstring to LPWSTR using 
LPWSTR Text_Being_Added = const_cast<wchar_t*>(SomeWideStringText.c_str());
    

//I create a rectangle to use in my DrawTextExW
RECT rc;    

// If I have a handle to a window then I can do this.
GetClientRect(Handle_Of_SomeWindow, & rc);

//But, if I do not have a handle to a window and if I only have a hdc, then that does not work.
?

也许除了矩形&rc之外,我还可以使用其他东西,但我还没有找到它


我一直在研究这个问题,我不知道如何使用矩形或a&rc。

您误解了您提供给
DrawTextEx
的矩形是什么,它不是位图的大小,而是您希望文本占据的大小。它显然应该小于或等于背景位图的大小,但与此没有其他关系

那么就没有手柄可供使用了。而且,我找不到它的把柄。从DC获取句柄不起作用

真的不知道你想表达什么

我不知道如何使用矩形或a&rc


同样,您没有收到这个,而是提供了它。

内存DC中选择了一个位图,该位图有一个大小。在大多数情况下,您的代码创建了该位图,因此它应该已经知道该位图的大小

但如果没有,你有两个选择

选项1:您可以从DC中选择位图,获取其大小,然后将其重新选择。这有点笨拙,我忽略了错误检查:

// assuming you're given hdcMem...
HBITMAP hbmpTemp = CreateCompatibleDC(hdcMem, 1, 1);
HBITMAP hbmpActual = SelectObject(hdcMem, hdcTemp);
BITMAP bm = {0};
GetObject(hbmpActual, sizeof(bm), &bm);
// now your size is in bm.bmWidth and bm.bmHeight,
RECT rc = {0, 0, bm.bmWidth, bm.bmHeight};
SelectObject(hdcMem, hdcActual);  // put the memory DC back
DeleteObject(hbmpTemp);
选项2:[未测试]您可以尝试查询DC的“分辨率”上限。我知道这适用于设备DCs,如显示器或打印机。我不知道它是否适用于内存DC

int width = GetDeviceCaps(hdcMem, HORZRES);
int height = GetDeviceCaps(hdcMem, VERTRES);
?
int width = GetDeviceCaps(hdcMem, HORZRES);
int height = GetDeviceCaps(hdcMem, VERTRES);