user11847100
user11847100

Reputation:

win32 c++ I want to insert an EDIT control straight after the TEXT of the STATIC label

To make a STATIC label I can use:

            HWND hwnd_ques=CreateWindow(
                TEXT("STATIC"),
                TEXT("Yao happy swallow is"),
                WS_VISIBLE | WS_CHILD,
                10,
                70,
                180,
                30,
                hwnd,
                NULL,
                (HINSTANCE)GetWindowLongPtr(hwnd, GWL_HINSTANCE),
                NULL);

After calling CreateWindow I used WM_SETFONT msg to set the label's font and fontsize (which worked successfully):

            SendMessage(hwnd_ques, WM_SETFONT, (WPARAM)font1, static_cast<LPARAM>(MAKELONG(TRUE, 0)));

//definition of font1 is not shown here but it worked successfully

However, I want to insert an EDIT control straight after the TEXT of the STATIC label, that is, I will make the label width fit its text width and assign the X POSITION of the EDIT according to the label position and width. if I specify the label width manually, it cannot make sure the label width just EXACTLY fits the text width because I don't actually know the text width of that label.

I wonder how I can make the label width just EXACTLY fit its text width. Perhaps I can get the text width since knowing the text content, font and fontsize. After getting the text width then I can turn back to set the label's width afterwards.

-

PURPOSE

I want to insert an EDIT control straight after the TEXT of the STATIC label

-

EDIT

Searched the Internet and it says to use GetTextExtentPoint32

SIZE textSize;
GetTextExtentPoint32(GetDC(hwnd), text, strlen(text), &textSize);

But where can I specify the font/fontsize in this function?

Upvotes: 0

Views: 791

Answers (1)

user11847100
user11847100

Reputation:

By using function GetTextExtentPoint32 like the following:

SIZE textSize;
GetTextExtentPoint32(GetDC(hwnd), text, strlen(text), &textSize);

And by using SelectFont to specify the font before, this problem is solved.

Upvotes: 1

Related Questions