April 18, 2024, 10:08:57 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Changing a combox item font

Started by ExMember001, January 04, 2009, 04:16:56 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ExMember001

Hi,
I'm trying to find a way to change the font of a combox item. (Make a Font selection combobox)
i have the fontname for the item, but i don't know how to set it to the item text.
I have search a lot in google, msdn and this forum but i don't find anything that can really help except for the ownerdraw listbox example by sapero that tell me how to subclass the control.

any help would be appreciated  ;)

sapero

January 04, 2009, 05:05:05 PM #1 Last Edit: January 04, 2009, 05:38:54 PM by sapero
Hello Krypt :)
A short example: (if you have more controls, you'll need to check pdis->CtlType and pdis->CtlID)#include "windows.inc"
#define IDC_FONTS 1000

WNDPROC g_DlgProc;

class CDlg:CDialog
{
declare virtual  OnInitDialog(),int;
declare virtual OnClose(),int;
}

global sub main()
{
CDlg d1;
d1.Create(0,0,300,202,0x80CA0080,0,"Font Picker",0);
d1.AddControl(CTCOMBOBOX,"",67,16,190,154,0x50A10242|CBS_OWNERDRAWFIXED,0,IDC_FONTS);

d1.DoModal();
return 0;
}

CDlg::OnClose(),int
{
CloseDialog(1);
return true;
}

CDlg::OnInitDialog(),int
{
/* Initialize any controls here */
CenterWindow();

// subclass the dialog
SetProp(m_hwnd, "this", this);
g_DlgProc = SetWindowLong(m_hwnd, GWL_WNDPROC, &SubclassDlgProc);
// enum fonts
HDC dc = GetDC(m_hwnd);
EnumFontFamilies(dc, NULL, &EnumFontsCb, GetControl(IDC_FONTS)->m_hwnd);
ReleaseDC(m_hwnd, dc);

return true;
}


sub EnumFontsCb(ENUMLOGFONT *lpelf, NEWTEXTMETRIC *lpntm, DWORD FontType, HWND hwndCombo),int
{
if (SendMessage(hwndCombo, CB_FINDSTRINGEXACT, -1, &lpelf->elfLogFont.lfFaceName) == CB_ERR)
{
SendMessage(hwndCombo, CB_ADDSTRING, 0, &lpelf->elfLogFont.lfFaceName);
}
return 1;
}


sub SubclassDlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam),LRESULT
{
dstring szFace[64];
LOGFONT lf;
CWindow *pWin;
CCombobox *pCombo;

switch (uMsg)
{
case WM_MEASUREITEM:
// not received

case WM_DRAWITEM:
DRAWITEMSTRUCT *pdis = &*(DRAWITEMSTRUCT)lParam;
// erase
HBRUSH hbr = GetSysColorBrush(IIF(pdis->itemState & ODS_SELECTED, COLOR_HIGHLIGHT, COLOR_WINDOW));
FillRect(pdis->hDC, &pdis->rcItem, hbr);
// create font
if (pdis->itemID != -1)
{
pWin          = GetProp(hwnd, "this");
pCombo        = pWin->GetControl(IDC_FONTS);
szFace        = pCombo->GetItemText(pdis->itemID);
// select new font
ZeroMemory(&lf, sizeof(lf));
lf.lfFaceName = szFace;
lf.lfHeight   = -MulDiv(8, GetDeviceCaps(pdis->hDC, LOGPIXELSY), 72);
HFONT font    = SelectObject(pdis->hDC, CreateFontIndirect(&lf));
// draw transparent text
SetBkMode(pdis->hDC, TRANSPARENT);
pdis->rcItem.left += 2;
DrawText(pdis->hDC, szFace, len(szFace), &pdis->rcItem, DT_SINGLELINE | DT_VCENTER);
// restore old, delete new font
DeleteObject(SelectObject(pdis->hDC, font));
}
return 0;
}
return CallWindowProc(g_DlgProc, hWnd, uMsg, wParam, lParam);
}



Or better:#include "windows.inc"
#include "commctrl.inc"
#define IDC_FONTS 1000

#define FONTSIZE 16
WNDPROC g_DlgProc;

class CDlg:CDialog
{
declare virtual  OnInitDialog(),int;
declare virtual OnClose(),int;
CCombobox m_fonts;
}

global sub main()
{
CDlg d1;
d1.Create(0,0,360,202,0x80CA0080,0,"Font Picker",0);
d1.DoModal();
return 0;
}

CDlg::OnClose(),int
{
CloseDialog(1);
return true;
}

CDlg::OnInitDialog(),int
{
/* Initialize any controls here */
CenterWindow();

// subclass the dialog
SetProp(m_hwnd, "this", this);
g_DlgProc = SetWindowLong(m_hwnd, GWL_WNDPROC, &SubclassDlgProc);

m_fonts.Create(6,16,350,350,0x50A10242|CBS_OWNERDRAWFIXED,IDC_FONTS,"",this);

// enum fonts
HDC dc = GetDC(m_hwnd);
EnumFontFamilies(dc, NULL, &EnumFontsCb, m_fonts.m_hwnd);
ReleaseDC(m_hwnd, dc);

return true;
}


sub EnumFontsCb(ENUMLOGFONT *lpelf, NEWTEXTMETRIC *lpntm, DWORD FontType, HWND hwndCombo),int
{
if (SendMessage(hwndCombo, CB_FINDSTRINGEXACT, -1, &lpelf->elfLogFont.lfFaceName) == CB_ERR)
{
SendMessage(hwndCombo, CB_ADDSTRING, 0, &lpelf->elfLogFont.lfFaceName);
}
return 1;
}


sub SubclassDlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam),LRESULT
{
dstring szFace[64];
LOGFONT lf;
CWindow *pWin;
CCombobox *pCombo;

switch (uMsg)
{
case WM_MEASUREITEM:
*(MEASUREITEMSTRUCT)lParam.itemWidth  = 1;
*(MEASUREITEMSTRUCT)lParam.itemHeight = FONTSIZE + 2;

case WM_DRAWITEM:
DRAWITEMSTRUCT *pdis = &*(DRAWITEMSTRUCT)lParam;
// erase
HBRUSH hbr = GetSysColorBrush(IIF(pdis->itemState & ODS_SELECTED, COLOR_HIGHLIGHT, COLOR_WINDOW));
FillRect(pdis->hDC, &pdis->rcItem, hbr);

pWin          = GetProp(hwnd, "this");
pCombo        = pWin->GetControl(IDC_FONTS);

if (pdis->itemID != -1)
{
szFace        = pCombo->GetItemText(pdis->itemID);
ZeroMemory(&lf, sizeof(lf));
lf.lfFaceName = szFace;
lf.lfHeight   = -MulDiv(FONTSIZE, GetDeviceCaps(pdis->hDC, LOGPIXELSY), 72);
// create and select new font
HFONT font    = SelectObject(pdis->hDC, CreateFontIndirect(&lf));
// draw transparent text
SetBkMode(pdis->hDC, TRANSPARENT);
pdis->rcItem.left += 2;
DrawText(pdis->hDC, szFace, len(szFace), &pdis->rcItem, DT_SINGLELINE | DT_VCENTER);
// restore old, delete new font
DeleteObject(SelectObject(pdis->hDC, font));
}
return 0;
}
return CallWindowProc(g_DlgProc, hWnd, uMsg, wParam, lParam);
}

ExMember001

Fantastik !!!
Thanks Sapero  ;D
i'll learn a lots from that