Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

1 solution

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.

C++
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.
 
Share this answer
 
Comments
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)
{
pComponent->SetFont(Sourcefont);
}
}
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
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 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)
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900