Click here to Skip to main content
11,435,451 members (58,121 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ MFC VC++
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 1:44am

1 solution

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

Solution 1

Hi,

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)
       {
          pComponent->SetFont(pFont);
       }
   }
}
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,
J.K.
  Permalink  
Comments
p.uday kishore at 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)
{
pComponent->SetFont(Sourcefont);
}
}
i did like this but am getting null into pComponent.Is there any problem????
Kucera Jan at 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
Dialog
|
+-ImmediateComponent
|
+- 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 at 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 at 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 at 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)
pComponent->SetFont(Sourcefont);
}
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)



Advertise | Privacy | Mobile
Web04 | 2.8.150428.2 | Last Updated 22 May 2013
Copyright © CodeProject, 1999-2015
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