From: Glyn on


"RB" wrote:

> I have created the below code that actually works and compiles with
> zero errors and zero warnings. I would like criticism and input on
> it from all angles, First off is there a better way to do this. Does
> my code have problems etc. (granted I have left some error checking
> out to keep it brief)
> This all started when I wanted to change the font type and size in my
> FormView Listbox. I always try to keep everything within MFC since I
> want to concentrate on my stuff and not windows stuff. But everywhere
> I looked on the net for changing a font in a resource control had stuff
> setting the Owner Draw style to yes and writing an owner draw function
> of DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct ) which I eventually
> did learn to create and get working BUT in the process I ran across some
> things that made me think I did not have to write the owner draw function
> and I could set the font with supplied MFC control functions.
> And therein became this code. I have a standard MFC appwizard app with
> the generated view class having the CFormView as the base class
> Then I created a Listbox in the resource editor (on top of the FormView)
> with the following styles Selection = None and
> OwnerDraw = No (since I no longer need DrawItem)
> Vertical scroll and No integral height are checked, & no extended styles.
> Then in my FormView class header file I put the New font declaration of
> ----------header file-----------------------------------
> class CTry_2View : public CFormView
> {
> protected: // create from serialization only
> CTry_2View();
> DECLARE_DYNCREATE(CTry_2View)
> public:
> //{{AFX_DATA(CTry_2View)
> enum { IDD = IDD_TRY_2_FORM };
> CListBox m_FormListBoxObj;
> //}}AFX_DATA
>
> // Attributes
> public:
> CTry_2Doc* GetDocument();
> //// >>>>>HERE IS FUTURE NEW FONT FOR MY LISTBOX <<<<<<<////////
> CFont NewFont; // Construct instance of CFont class for new font.
>
> // Operations
> public:
> etc etc ....
> --------------cpp file------------------------------------
> Then in the destructor of the cpp file I put the DeleteObject
> CTry_2View::CTry_2View()
> : CFormView(CTry_2View::IDD)
> {
> //{{AFX_DATA_INIT(CTry_2View)
> // NOTE: the ClassWizard will add member initialization here
> //}}AFX_DATA_INIT
> // TODO: add construction code here
> }
>
> CTry_2View::~CTry_2View() // add destructor any code here
> { ////>>>>> HERE IS MY CLEAN UP OF FONT <<<<<<<<///////
> NewFont.DeleteObject(); // this CFont NewFont obj is declared in this
> // view class's header file.
> }
> ----------NOW further down in this same cpp file I put a button handler
> that changes the listbox font when clicked. The button was also created
> in the resource editor and the skeleton handler was added through the
> class wizard. I basically came up with all this code my own by experimenting
> with what I could find in the help files and seeing what intellisense would
> offer me on each item so I expect some criticism on my novice level creation.
> And are there still reasons I should go with the owner draw scenario as
> opposed to this way?
>
> //-------------Begin OnChangeFontClick function------------//
> void CTry_2View::OnChangeFontClick()
> {
> // Create instance of CClientDC class to this ( CWnd* )
> CClientDC dc(this);
>
> //create CFont Ptr to current font obj of listbox
> //The m_FormListBoxObj is a ClassWizard created listbox variable
> //of category Control and type CListBox
> CFont *pOldListBoxFont = m_FormListBoxObj.GetFont();
>
> // Declare old LOGFONT to save just in case I needed it later
> LOGFONT OldFontLogStruct;
> LOGFONT* pOldFontLogStruct = &OldFontLogStruct; //create ptr to it.
>
> // fill OldFontLogStuct with current font data
> pOldListBoxFont->GetLogFont(pOldFontLogStruct);
>
> // Declare a New LOGFONT struct variable
> LOGFONT NewFontLogStruct;
> LOGFONT* pNewFontLogStruct = &NewFontLogStruct; //create ptr to it.
>
> // copy the old stuff to new
> *pNewFontLogStruct = *pOldFontLogStruct;
>
> // CFont NewFont is declared in header file and destructed
> //(DeleteObject) in cpp file of the generated View class.
> CFont *pNewFont = &NewFont; // create ptr to it.
>
> // set the values we are concerned with in new font
> NewFontLogStruct.lfPitchAndFamily = (FF_MODERN || FIXED_PITCH);
>
> // lfHeight is negative int so convert abs.
> NewFontLogStruct.lfHeight = ( abs( pOldFontLogStruct->lfHeight ) + 4);
>
> // Courier New is a fixed space font
> strcpy(NewFontLogStruct.lfFaceName, "Courier New");
>
> // Create the new font with updated NewFontLogStruct
> BOOL FontCreation = NewFont.CreateFontIndirect(&NewFontLogStruct);
>
> if (FontCreation == 0)
> {
> MessageBox("Font creation failed!", "Error", MB_OK | MB_ICONEXCLAMATION);
> }
> else
> { // Now set the font to the listbox control in my formview class
> m_FormListBoxObj.SetFont(pNewFont, TRUE);
> }
> } // end of function
>
>
> .
>

Don't know if you've been told about this but you have (logical operator)..

NewFontLogStruct.lfPitchAndFamily = (FF_MODERN || FIXED_PITCH);

which should be

NewFontLogStruct.lfPitchAndFamily = (FF_MODERN | FIXED_PITCH);

From: RB on
Yes I have been told that, but thanks for the reply anyhow

"Glyn" <Glyn(a)discussions.microsoft.com> wrote in message news:9E84085D-0764-48A2-999E-530E89B6E6B6(a)microsoft.com...
>
>
> "RB" wrote:
>
>> I have created the below code that actually works and compiles with
>> zero errors and zero warnings. I would like criticism and input on
>> it from all angles, First off is there a better way to do this. Does
>> my code have problems etc. (granted I have left some error checking
>> out to keep it brief)
>> This all started when I wanted to change the font type and size in my
>> FormView Listbox. I always try to keep everything within MFC since I
>> want to concentrate on my stuff and not windows stuff. But everywhere
>> I looked on the net for changing a font in a resource control had stuff
>> setting the Owner Draw style to yes and writing an owner draw function
>> of DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct ) which I eventually
>> did learn to create and get working BUT in the process I ran across some
>> things that made me think I did not have to write the owner draw function
>> and I could set the font with supplied MFC control functions.
>> And therein became this code. I have a standard MFC appwizard app with
>> the generated view class having the CFormView as the base class
>> Then I created a Listbox in the resource editor (on top of the FormView)
>> with the following styles Selection = None and
>> OwnerDraw = No (since I no longer need DrawItem)
>> Vertical scroll and No integral height are checked, & no extended styles.
>> Then in my FormView class header file I put the New font declaration of
>> ----------header file-----------------------------------
>> class CTry_2View : public CFormView
>> {
>> protected: // create from serialization only
>> CTry_2View();
>> DECLARE_DYNCREATE(CTry_2View)
>> public:
>> //{{AFX_DATA(CTry_2View)
>> enum { IDD = IDD_TRY_2_FORM };
>> CListBox m_FormListBoxObj;
>> //}}AFX_DATA
>>
>> // Attributes
>> public:
>> CTry_2Doc* GetDocument();
>> //// >>>>>HERE IS FUTURE NEW FONT FOR MY LISTBOX <<<<<<<////////
>> CFont NewFont; // Construct instance of CFont class for new font.
>>
>> // Operations
>> public:
>> etc etc ....
>> --------------cpp file------------------------------------
>> Then in the destructor of the cpp file I put the DeleteObject
>> CTry_2View::CTry_2View()
>> : CFormView(CTry_2View::IDD)
>> {
>> //{{AFX_DATA_INIT(CTry_2View)
>> // NOTE: the ClassWizard will add member initialization here
>> //}}AFX_DATA_INIT
>> // TODO: add construction code here
>> }
>>
>> CTry_2View::~CTry_2View() // add destructor any code here
>> { ////>>>>> HERE IS MY CLEAN UP OF FONT <<<<<<<<///////
>> NewFont.DeleteObject(); // this CFont NewFont obj is declared in this
>> // view class's header file.
>> }
>> ----------NOW further down in this same cpp file I put a button handler
>> that changes the listbox font when clicked. The button was also created
>> in the resource editor and the skeleton handler was added through the
>> class wizard. I basically came up with all this code my own by experimenting
>> with what I could find in the help files and seeing what intellisense would
>> offer me on each item so I expect some criticism on my novice level creation.
>> And are there still reasons I should go with the owner draw scenario as
>> opposed to this way?
>>
>> //-------------Begin OnChangeFontClick function------------//
>> void CTry_2View::OnChangeFontClick()
>> {
>> // Create instance of CClientDC class to this ( CWnd* )
>> CClientDC dc(this);
>>
>> //create CFont Ptr to current font obj of listbox
>> //The m_FormListBoxObj is a ClassWizard created listbox variable
>> //of category Control and type CListBox
>> CFont *pOldListBoxFont = m_FormListBoxObj.GetFont();
>>
>> // Declare old LOGFONT to save just in case I needed it later
>> LOGFONT OldFontLogStruct;
>> LOGFONT* pOldFontLogStruct = &OldFontLogStruct; //create ptr to it.
>>
>> // fill OldFontLogStuct with current font data
>> pOldListBoxFont->GetLogFont(pOldFontLogStruct);
>>
>> // Declare a New LOGFONT struct variable
>> LOGFONT NewFontLogStruct;
>> LOGFONT* pNewFontLogStruct = &NewFontLogStruct; //create ptr to it.
>>
>> // copy the old stuff to new
>> *pNewFontLogStruct = *pOldFontLogStruct;
>>
>> // CFont NewFont is declared in header file and destructed
>> //(DeleteObject) in cpp file of the generated View class.
>> CFont *pNewFont = &NewFont; // create ptr to it.
>>
>> // set the values we are concerned with in new font
>> NewFontLogStruct.lfPitchAndFamily = (FF_MODERN || FIXED_PITCH);
>>
>> // lfHeight is negative int so convert abs.
>> NewFontLogStruct.lfHeight = ( abs( pOldFontLogStruct->lfHeight ) + 4);
>>
>> // Courier New is a fixed space font
>> strcpy(NewFontLogStruct.lfFaceName, "Courier New");
>>
>> // Create the new font with updated NewFontLogStruct
>> BOOL FontCreation = NewFont.CreateFontIndirect(&NewFontLogStruct);
>>
>> if (FontCreation == 0)
>> {
>> MessageBox("Font creation failed!", "Error", MB_OK | MB_ICONEXCLAMATION);
>> }
>> else
>> { // Now set the font to the listbox control in my formview class
>> m_FormListBoxObj.SetFont(pNewFont, TRUE);
>> }
>> } // end of function
>>
>>
>> .
>>
>
> Don't know if you've been told about this but you have (logical operator)..
>
> NewFontLogStruct.lfPitchAndFamily = (FF_MODERN || FIXED_PITCH);
>
> which should be
>
> NewFontLogStruct.lfPitchAndFamily = (FF_MODERN | FIXED_PITCH);
>