Changing fonts in an edit control

S

Scott

Consider the follow code snippet. Why doesn't it work?

// Initialize a CFont object with the characteristics given

// in a LOGFONT structure.

LOGFONT lf;

memset(&lf, 0, sizeof(LOGFONT)); // clear out structure

lf.lfHeight = 10; // request a 10-pixel-height font

strcpy(lf.lfFaceName, "Courier"); // request a face name "Courier"

HFONT hfont = ::CreateFontIndirect(&lf); // create the font

// Convert the HFONT to CFont*.

CFont* pfont = CFont::FromHandle(hfont);

//try to apply font to this window

pMainFrame->SetFont(pfont);

// The main window has been initialized, so show and update it.

pMainFrame->ShowWindow(m_nCmdShow);

pMainFrame->UpdateWindow();

::DeleteObject(hfont);

Obviously, I'm trying to set the font for a window to Courier, but I'm
trying that as a workaround to a more difficult problem. Here's another code
snippet.

void CVoiceView::Locate(int & col, int & row)

{

CEdit& EditCtrl = GetEditCtrl();

row = EditCtrl.LineFromChar(-1) + 1 ;

col = LOWORD(EditCtrl.CharFromPos(GetCaretPos())) ;

co1l = col - EditCtrl.LineIndex(row-1)+1 ;

if (!EditCtrl.IsWindowEnabled())

{

row=-1;

col=-1;

}

}

Yes, fairly standard stuff meant to return the current column and row of the
cursor (well, actually the caret) in the edit control. The hitch is, of
course, that it doesn't always work as expected. With proportional fonts,
"i," "l," and "j" will frequently cause the column count to be one less than
what it should be. The row count is always right. This is the problem I
really need to solve, going to a fixed font was just a workaround.
 
J

Jeff Partch [MVP]

Scott said:
Consider the follow code snippet. Why doesn't it work?

// Initialize a CFont object with the characteristics given

// in a LOGFONT structure.

LOGFONT lf;

memset(&lf, 0, sizeof(LOGFONT)); // clear out structure

lf.lfHeight = 10; // request a 10-pixel-height font

strcpy(lf.lfFaceName, "Courier"); // request a face name "Courier"

HFONT hfont = ::CreateFontIndirect(&lf); // create the font

// Convert the HFONT to CFont*.

CFont* pfont = CFont::FromHandle(hfont);

//try to apply font to this window

pMainFrame->SetFont(pfont);

// The main window has been initialized, so show and update it.

pMainFrame->ShowWindow(m_nCmdShow);

pMainFrame->UpdateWindow();

::DeleteObject(hfont);

Obviously, I'm trying to set the font for a window to Courier...

The relationship of your pMainFrame and the edit control in the subject is
unclear. I doubt that your pMainFrame is the edit control. I doubt that your
pMainFrame even handles WM_SETFONT. I doubt that it is ever appropriate to
DeleteObject the HFONT while WM_SETFONT target window is expected to use it.
 
S

Scott

The relationship of your pMainFrame and the edit control in the subject is
unclear. I doubt that your pMainFrame is the edit control. I doubt that
your pMainFrame even handles WM_SETFONT. I doubt that it is ever
appropriate to DeleteObject the HFONT while WM_SETFONT target window is
expected to use it.
Fair enough questions. I was trying to not post the entire routine, but I
see that more of its context is needed.

// Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views.
CMultiDocTemplate* pDocTemplate;
pDocTemplate = new CMultiDocTemplate(
IDR_VOICETYPE,
RUNTIME_CLASS(CVoiceDoc),
RUNTIME_CLASS(CVoiceChildWnd), // standard MDI child frame
RUNTIME_CLASS(CVoiceView));
AddDocTemplate(pDocTemplate);
// create main MDI Frame window
CMainFrame* pMainFrame = new CMainFrame;
if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
return FALSE;
m_pMainWnd = pMainFrame;

Notice that last line? I have the understanding that fonts apply to all
objects derived from CWnd. We are looking at chunks of
CMainApp::InitInstance. The last line above says to me that the main frame
in this app is also the main window from which MDI children will derive. I
did not write the original code, I just need to modify it so that child
windows inherit a nice fixed-width font.
 
J

Jeff Partch [MVP]

Scott said:
// Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views.
CMultiDocTemplate* pDocTemplate;
pDocTemplate = new CMultiDocTemplate(
IDR_VOICETYPE,
RUNTIME_CLASS(CVoiceDoc),
RUNTIME_CLASS(CVoiceChildWnd), // standard MDI child frame
RUNTIME_CLASS(CVoiceView));
AddDocTemplate(pDocTemplate);
// create main MDI Frame window
CMainFrame* pMainFrame = new CMainFrame;
if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
return FALSE;
m_pMainWnd = pMainFrame;

Notice that last line?
Yes.

I have the understanding that fonts apply to all objects derived from
CWnd.

What does that mean?
We are looking at chunks of CMainApp::InitInstance.
Okay.

The last line above says to me that the main frame in this app is also the
main window from which MDI children will derive.

I don't see it that way. Could you elaborate on your vision?
I did not write the original code, I just need to modify it so that child
windows inherit a nice fixed-width font.

Inherit it from where?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top