Click here to Skip to main content
13,044,467 members (58,031 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi my application is having 4 dialogs.Am maintaining the font for the controls in each dialog.

Now i wanted to move all the font to a single method and from there i wanted to apply the fonts.
Can any one give me suggestion how to do it.
Posted 22-May-13 0:44am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1


I can see no trouble. just create a methos that will accepst a an ID of the compopmnent to which you want to set the font. The in the method, you load/create the font for given component/window and set it to that component.

void CMyDialogWindow::SetComponentFont(intiComponentId)
   CWnd* pComponent = this->GetDlgItem();
   if (pComponent != NULL)
       // here you load or create some Font
       CFont* pFont = CreateOrLoadMyWonderfulFont();
       if (pFont != NULL)

For more info about the CWnd::GetDlgItem(int nId) see MSDN. For more info about the CWnd::SetFont(CFont* pFont) see MSDN.

Hope this helps.

Best regards,
p.uday kishore 23-May-13 2:26am
CFont* Sourcefont = new CFont;
Sourcefont->CreatePointFont(75, _T("Lucida Grande Bold"));
CWnd* pComponent= this->GetDlgItem(nID);
if (pComponent != NULL)
// here you load or create some Font
if (Sourcefont != NULL)
i did like this but am getting null into pComponent.Is there any problem????
Kucera Jan 23-May-13 4:05am
Well, that perhaps means that the dialogbox (to whuich you have pointer in pointer this) cannot find the component. This might be cause by one of the following reason:
- the components parent is not directly the dialog but some different class, like
+- Your component // this cannot be found by the DialogBox

- You are using wrong nId of the component and so it cannot be found

- make sure that the dialog is really pointed to by |this| pointer and that it properly derives from MFC CDialog.
p.uday kishore 23-May-13 5:11am
its working am passing the control id directly in the call.
now i wanted to minimize the code still that,i wanted to move this block to main class of application from there i wanted to apply for each dialog.
can u suggest any idea for this....
Kucera Jan 23-May-13 8:57am
Aha, I see. So you just call the GetDlgItem(nID) not on pointer |this| (which is the main window), but you shall call it on each of the dialogs - like pDialog->GetDlgItem(nId)...

Or you might find handy a pure WinAPI call that acceps an extra argument the dialog handle (HWND) ::GetDlgitem(). but I would recoment the first posiiblility.
p.uday kishore 29-May-13 7:55am
Main class:

int Common::SetComponentFont(int nSize,HWND hWnd)
CFont* Sourcefont = new CFont;
Sourcefont->CreatePointFont(nSize,_T("Lucida Grande Bold"));
CWnd* pComponent= FromHandle(hWnd);
if (pComponent != NULL);
// here you load or create some Font
if(Sourcefont != NULL)
return 0;

derived class:
CCommon* csk = new CCommon();
CEdit* variable = (CEdit*) GetDlgItem(IDC_EDIT);
csk->SetComponentFont(75,variable ->m_hWnd);

this worked for me,when am calling from base class.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 22 May 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100