Good to answer my question: The code above (see Question) gives too high a value for Size.cx, because MESSAGE_SIZE is 1000, not the size of the actual row, so instead I used strMessage.c_str and strMessage.size () . This still gave some slight inaccuracies with the output, I assumed that it was due to the wrong font, so I manually made the font. Now it gives the correct value for Size.cx. Now the code is as follows:
int iHorExt=0; SIZE Size; int iCurHorExt=0 // iCurHorExt is actually a global var to prevent it from being reset to 0 evertime the code executes string strMessage="Random user input here!" HDC hdc=GetDC(hDlg); //Random font HFONT hFont=CreateFont(15, 5, NULL, NULL, FW_MEDIUM, false, false, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, FF_ROMAN, "Times New Roman"); //change font of the control SendDlgItemMessage(hDlg, IDC_LIST1, WM_SETFONT, (WPARAM)hFont, true); SelectObject(hdc, hFont); int iResult=GetTextExtentPoint32(hdc, strMessage.c_str(), strMessage.size(), &Size); if(iResult!=0) { iHorExt=Size.cx; if(iHorExt>iCurHorExt) { iCurHorExt=iHorExt; } }
further in the code:
SendDlgItemMessage(hDlg, IDC_LIST1, LB_SETHORIZONTALEXTENT, iCurHorExt, NULL);
Edit:
SelectObject(hdc, (HFONT)SendDlgItemMessage(hDlg, IDC_LIST1, WM_GETFONT, NULL, NULL));
It works too and does not require you to make a font or edit the font of the control
BioCycle
source share