Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: ListView: Setting font size in ListView control: a bug?

804 views
Skip to first unread message

David Lowndes

unread,
Jul 15, 2004, 4:43:07 AM7/15/04
to
>I've got a generic app containing a ListView in which I've selected a larger font.
>Normally this works fine and the header control in the ListView is resized to show the lager font.
>However, if the application has a manifest to force use of ComCtl32 v6, the header of the ListView is not resized and the text is clipped.

Mark,

Can you explain a little more about your scenario.

Presumably the list view is in details mode. Is it a control on a
dialog? How are you changing the font?

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq

Mark Roberts

unread,
Jul 15, 2004, 5:02:01 AM7/15/04
to
"David Lowndes" wrote:

I generated a simple app using DevStudio.
I then create a ListView control as a child of the frame window - using WS_CHILD | LVS_REPORT and size it to fill the client area.

I then retrieve the current font from control using WM_GETFONT, extract the LOGFONT, increase the lfHeight (e.g. to 36), create a new font and finally call WM_SETFONT

Mark

David Lowndes

unread,
Jul 15, 2004, 6:56:31 PM7/15/04
to
>I generated a simple app using DevStudio.

So that's a straight MFC SDI application with a CView?

>I then create a ListView control as a child of the frame window - using WS_CHILD | LVS_REPORT and size it to fill the client area.
>
>I then retrieve the current font from control using WM_GETFONT, extract the LOGFONT, increase the lfHeight (e.g. to 36), create a new font and finally call WM_SETFONT
>

Can you show your code so there's no ambiguity, and in what method
you're calling it.

Mark Roberts

unread,
Jul 16, 2004, 3:10:01 AM7/16/04
to
"David Lowndes" wrote:

> So that's a straight MFC SDI application with a CView?

Apologies, I should have been a bit more specific - It was a straight WIN32 app.
Here are the functions that I've added to create the listview & set up the font (just called off a menu item).

Thanks for your responses, but I've actually found a workaround now.
Basically, I subclass the header control within the ListView and trap the HDM_LAYOUT message.
Within this handler I retrieve the TEXTMETRICS for the font and modify the size & position accordingly.
Not ideal, but it seems to work ok.

In case it'll help anybody else, I've put the HDM_LAYOUT handler at the end of this post.

Cheers

Mark


// CreateViewControl()
// hWndParent is just the handle of the app's frame window
void CreateListViewControl(HWND hWndParent)
{
RECT rcClient;

//Create the ListView to fill the client area
GetClientRect(hWndParent, &rcClient);
hWndList = CreateWindow(WC_LISTVIEW, "test", WS_CHILD | LVS_REPORT | WS_VISIBLE,
0,0,rcClient.right,rcClient.bottom, hWndParent, (HMENU)101, hInst, NULL);

if(hWndList == NULL)
return;

//Add a few columns
LVCOLUMN lvc = {0};
lvc.mask = LVCF_TEXT | LVCF_WIDTH;
lvc.iSubItem = 3;
lvc.pszText = "Column 1";
lvc.cx = 150;
ListView_InsertColumn(hWndList, 0, &lvc);

lvc.mask = LVCF_TEXT | LVCF_WIDTH;
lvc.iSubItem = 2;
lvc.pszText = "Column 2";
lvc.cx = 100;
ListView_InsertColumn(hWndList, 1, &lvc);

lvc.mask = LVCF_TEXT | LVCF_WIDTH;
lvc.iSubItem = 1;
lvc.pszText = "Column 3";
lvc.cx = 100;
ListView_InsertColumn(hWndList, 2, &lvc);

lvc.mask = LVCF_TEXT | LVCF_WIDTH;
lvc.iSubItem = 0;
lvc.pszText = "Column 4";
lvc.cx = 100;
ListView_InsertColumn(hWndList, 3, &lvc);

//Add a few items to the list
LV_ITEM lvi;
lvi.iImage = 5;
lvi.mask = LVIF_TEXT;
lvi.iSubItem = 0;

for (UINT i = 0; i < 10; i++)
{
char szText[200];

sprintf(szText, "Line %d", i+1);
lvi.pszText = szText;
lvi.iItem = i;
ListView_InsertItem(hWndList, &lvi);
}
}

void SetListViewFont(void)
{
LOGFONT lf = {0};

if(hWndList == NULL)
return;

HFONT hFont = (HFONT)SendMessage(hWndList, WM_GETFONT, 0, 0);
GetObject(hFont, sizeof(LOGFONT), &lf);
lf.lfHeight = 36;

hFont = CreateFontIndirect(&lf);
if(hFont != NULL)
SendMessage(hWndList, WM_SETFONT, (WPARAM)hFont,TRUE);
}


// Work-around

// At the end of the CreateListViewControl, subclass the header
// (Original WNDPROC stored globally - WNDPROC lpfnHeaderProc):
// HWND hWndHeader = ListView_GetHeader(hWndList);
// if(hWndHeader != NULL)
// {
// lpfnHeaderProc = (WNDPROC)SetWindowLong(hWndHeader, GWL_WNDPROC, (LONG)ListViewHeaderWndProc);
// }

LRESULT CALLBACK ListViewHeaderWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
LRESULT lRes = 0;
HDLAYOUT *pLayout = NULL;

if (message == HDM_LAYOUT)
{
if(hFont != NULL) //Font now stored globally
{
pLayout = (HDLAYOUT *)lParam;

TEXTMETRIC tm = {0};
HDC hDC = GetDC( hWnd );
HFONT hOldFont = (HFONT)SelectObject(hDC, (HFONT)hFont);
GetTextMetrics(hDC, &tm);
SelectObject(hDC, (HFONT)hOldFont);
ReleaseDC( hWnd, hDC);

//If found that if the default processing wasn't called first, things would mess up
lRes = CallWindowProc(lpfnHeaderProc, hWnd, message, wParam, lParam);
pLayout->prc->top = (tm.tmHeight + tm.tmExternalLeading + 4);
pLayout->pwpos->cy = pLayout->prc->top;
pLayout->pwpos->flags |= SWP_FRAMECHANGED;
return lRes;
}
}
if(lpfnHeaderProc != NULL)
{
lRes = CallWindowProc(lpfnHeaderProc, hWnd, message, wParam, lParam);
}
return lRes;
}

0 new messages