Thread: Font in a CEdit control

  1. #1
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644

    Font in a CEdit control

    I'm using MFC, and a CEdit control. My problem right now is changing the font. I've asked a few friends for help, and that didn't work out to well (thanks though), and so I thought I'd come here. I've also tried the examples and reading what MSDN says, but that also has failed. Here's the current code I'm using:

    Code:
    #include "stdafx.h"
    #include "NoteX.h"
    #include "NoteXDoc.h"
    #include "NoteXView.h"
    
    CNoteXView *pView;
    
    #include "MainFrm.h"
    
    //.....
    
    void CMainFrame::OnChangeFont() 
    {
    	CFontDialog cfd;
    	CFont   cf;
    	LOGFONT lf;
    
    	if(cfd.DoModal() == IDOK){
    		//cf.CreateFontIndirect(&lf);
    		cfd.GetCurrentFont(&lf);
    		cf.CreateFontIndirect(&lf);
    		pView = (CNoteXView*)GetActiveView();
    		pView->SendMessage(WM_SETFONT, (WPARAM)&cf, 0);
    	}
    }
    Does anyone know why this isn't working?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    There are a couple things wrong:
    First, the WM_SETFONT message expects a font handle (or HFONT) in wParam - you are passing in the address of a local stack variable.
    Second, cf is a local stack variable and any resources allocted within cf.CreateFontIndirect(&lf); are released when cf's destructor is called.

    Once you ensure cf persists as long as it needs to, you can use CFont's HFONT cast operator to get the HFONT handle.

    gg

  3. #3
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Quote Originally Posted by Codeplug
    There are a couple things wrong:
    First, the WM_SETFONT message expects a font handle (or HFONT) in wParam - you are passing in the address of a local stack variable.
    Second, cf is a local stack variable and any resources allocted within cf.CreateFontIndirect(&lf); are released when cf's destructor is called.

    Once you ensure cf persists as long as it needs to, you can use CFont's HFONT cast operator to get the HFONT handle.

    gg
    Thank you, but it's still not working. I'm just gonna scrap the project for now.

  4. #4
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Thanks to a buddy of mine, I got it working finally.

    Here's the code if anyone wants it:

    PHP Code:
    void CMainFrame::OnFont() 
    {
        
    // TODO: Add your command handler code here
        
    CNoteXView *pView = (CNoteXView*)GetActiveView();

        
    ASSERT(pView != NULL);

        if(
    dlg.DoModal() == IDOK){
            
    dlg.GetCurrentFont(&lf);

            
    font.CreateFontIndirect(&lf);
            
    pView->SetFont(&font);
        }


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  2. problem with my font manager
    By hannibar in forum C Programming
    Replies: 1
    Last Post: 03-07-2006, 08:03 AM
  3. char copy
    By variable in forum C Programming
    Replies: 8
    Last Post: 02-06-2005, 10:18 PM
  4. font & font color in edit control
    By XRIC++ in forum Windows Programming
    Replies: 2
    Last Post: 10-27-2003, 09:07 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM