Click here to Skip to main content
15,861,367 members
Please Sign up or sign in to vote.
3.25/5 (4 votes)
See more:
I have sucessfully changed the color and the back ground of a CEdit control, but I am unable to change its text font to bold.

Help me please to accomplish this.

Thank you anyway.
Posted
Updated 24-Feb-11 11:51am
v4

Post the code you have now, it'll be easier to spot what's wrong that way.

[Update]
~~~~~~~~~

Been a while since I did anything like that with MFC, but I quickly gave it a test, and it's as simple as calling SetFont on the edit control.

C++
GetDlgItem(IDC_EDIT1)->SetFont(&m_editFont);
 
Share this answer
 
v2
Comments
Mr. Tomay 24-Feb-11 17:53pm    
Thanks for the quick reply. Should I implement it on the OnInitDialog() ?
Nish Nishant 24-Feb-11 17:57pm    
That would be a good place to do it.
Mr. Tomay 25-Feb-11 6:02am    
it's working now ;)
Nish Nishant 25-Feb-11 8:15am    
Glad to hear that!
Try this:

void makebold(HWND hwnd)
{
	HFONT hFontB,hFont = (HFONT)SendMessage(hwnd,WM_GETFONT,0,0);
	LOGFONT lf;
	GetObject(hFont, sizeof(LOGFONT), &lf);
	lf.lfWeight = FW_BOLD;
	hFontB = CreateFontIndirect(&lf);
	SendMessage(hwnd,WM_SETFONT,(int)hFontB,1);
}


The argument to pass is the m_hWnd from your CEdit control.
 
Share this answer
 
v2
Comments
Mr. Tomay 25-Feb-11 6:04am    
I don't understand the basic principles of SendMessage :(
[no name] 25-Feb-11 8:32am    
Sending f.e. a WM_SETFONT message has the same effect as calling SetFont() from your CEdit class, just written differently.
If your goal is to underline the text of a CEdit control, my function does exactly that.
Mr. Tomay 26-Feb-11 8:46am    
ah! I didn't understand one thing. What do you mean of 1 as fourth argument in the function SendMessage(hwnd,WM_SETFONT,(int)hFontB,1); ?
[no name] 26-Feb-11 8:51am    
The fourth argument is a boolean, if it's 1, you force the control to redraw itself using the new font. Else it won't.
SendMessage sends a message (2nd arg) to a window (1st arg), with parameters wParam and lParam (3rd and 4th argument). If you look up the message on MSDN, you can see the meaning of the wParam and lParam.
Mr. Tomay 26-Feb-11 9:47am    
I got it now ;) from this link http://msdn.microsoft.com/en-en/library/ms632642.aspx
Thanks for the clarification ;)
I understand now that your code should work too, because the void CWnd::SetFont(CFont* pFont, BOOL bRedraw = TRUE); will invoke the LRESULT CWnd::SendMessage(WM_SETFONT, (WPARAM)(HFONT)pFont->GetSafeHandle(), bRedraw);

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