Ask Your Question

Using DT_WORDBREAK, how do you apply DT_CALCRECT to DrawText?

asked 2022-09-11 11:00:00 +0000

plato gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2021-09-12 11:00:00 +0000

nofretete gravatar image

To use DTWORDBREAK and DTCALCRECT together with DrawText, you can use the following code:

RECT rect = { 0, 0, 100, 0 }; // Set the initial rectangle
DWORD flags = DT_WORDBREAK | DT_CALCRECT; // Set the flags for word wrapping and calculating the rectangle
HDC hdc = GetDC(hwnd); // Get the device context
DrawText(hdc, TEXT("Your text here"), -1, &rect, flags); // Call DrawText with the text, the length (-1 to indicate null-termination), the rectangle, and the flags
ReleaseDC(hwnd, hdc); // Release the device context

// The resulting rectangle will now contain the size of the text after word wrapping

In this example, the rectangle's height will be automatically calculated based on the width and the text's contents, accounting for word wrapping with DT_WORDBREAK. The resulting rectangle size will be stored in the original rectangle variable, which can then be used for further layout or drawing purposes.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer

Question Tools


Asked: 2022-09-11 11:00:00 +0000

Seen: 1 times

Last updated: Sep 12 '21