简体   繁体   中英

How do I get a rectangle (or &rc) from a memory hdc without using a handle or HWND

I am using Microsoft Windows (32 bit). Please, no Visual Studio anything, or .net.

I have the following that works if I have a windows handle to use for it:

// 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.

When I have an HDC without a window handle (I think that this is a memory dc but I do not yet understand that so well) which I am using in double buffering, then there is no handle to use. And, I can not get a handle for it. Get handle from DC does not work.

So, my question is How do I get a rectangle or &rc for that to use in my command of:

DrawTextExW(HDC_of_FRONT_BUFFER_001, Text_Being_Added, -1, & rc, DT_WORDBREAK, nullptr);

?

Maybe there might be something else other than a rectangle &rc that I could use, but I have not found it.

I have been studying this and I do not understand how to get a rectangle or a &rc to use.

You're misunderstanding what the rectangle you provide to DrawTextEx is, it's not the size of your bitmap, it's the size you want your text to occupy. It should obviously be less than or equal to the size of your backing bitmap, but it has no other relation to it.

then there is no handle to use. And, I can not get a handle for it. Get handle from DC does not work.

Literally no idea what you're trying to express here.

I do not understand how to get a rectangle or a &rc to use

Again, you aren't handed this, you provide it.

A memory DC has a bitmap selected into it, and that bitmap has a size. In most cases your code created that bitmap, so it should already know the size.

But if it doesn't, you have a couple options.

Option 1: You can select the bitmap out of the DC, get its size, and then select it back in. This is kind of kludgy and I've omitted error checking:

// 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);

Option 2: [untested] You could try to query the DC for its "resolution" caps. I know this works for device DCs, like a monitor or printer. I don't know whether it works for a memory DC.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM