![]() |
Desktop Development »
Dialogs and Windows »
Dialogs
Intermediate
License: The Code Project Open License (CPOL)
XFontDialog - Customizing CFontDialog Part I: Adding Font FiltersBy Hans DietrichXFontDialog demonstrates how to customize CFontDialog by removing unwanted controls |
VC6, VC8.0Win2K, WinXP, Vista, MFC, VS2005, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
But I had found out two things in my research into CFontDialog: first, you could use your own dialog template, based on the template provided in Font.Dlg; and second, there was a hook proc you could supply in your own CFontDialog-derived class.
After a few days playing with CFontDialog, I knew it would be possible to customize the font dialog to look the way we wanted. Mostly this involved editing the template used for the standard font dialog, and moving the unwanted controls outside the dialog:
I also had to create new controls for sample text, because CFontDialog insists on reverting to text "AaBbYyZz" when you select new font in fonts combo.
To jump ahead a bit, here is final dialog:
This is more compact than the standard font dialog, but looks enough like it that our users won't have to learn a completely different UI. At the same time, it removes the elements that might be frustrating to try to select (with no apparent effect, since they're not used in our apps). Plus, it gave us an opportunity to add some features that permitted better integration with our apps.
| Removal of all controls except font list and size list | |
| Font filters to select only specific groups of fonts to display | |
| Visual indication of monospaced fonts | |
| New APIs for monospaced and symbol fonts | |
| API to set caption of font dialog | |
| API to set sample text of font dialog |
lpfnHook member that allows you to
set your hook proc, and
the lpTemplateName and hInstance
members, which must be set in order to load the customized template.
What MSDN doesn't tell you is where to find the default template.
For VS 6.0, I found it in
C:\Program Files\Microsoft Visual Studio\VC98\Include\Font.Dlg.
For Visual Studio 8 and 9, it's located in the Platform SDK's
Include directory.
After I found it, next step was to set up a separate XFontDialog.rc file,
and cut and paste the template from Font.Dlg. To set up XFontDialog.rc,
I used the techniques I describe in my article
XDialogImport - How to share dialogs between projects.
IDD_XFONTDIALOG,
the Class Wizard will treat IDD_XFONTDIALOG as a string.
The advantage of this is that there is no possibility of conflicts
with any other dialog resource ids in your project.
Now I can use Visual Studio Resource Editor to configure controls like I wanted - moving unwanted controls to the side and marking them not visible. I also make sample control bigger, and add static control to top of font list - so for monospaced fonts, you will see MONOSPACED displayed:
I then use Class Wizard to generate new CXFontDialog class by double-clicking on template in Resource Editor:
I select CFontDialog as base class, and enter CXFontDialog for new class name:
And finally I am able to use Class Wizard for the new class:
You will notice that all symbolic names in Font.Dlg have been
converted to numeric IDs. This doesn't matter, since these IDs must
not change anyway or CFontDialog would not work.
(I assign names to these IDs inside XFontDialog.cpp
to make it easier to work with.)
I have added
two handler functions to CXFontDialog: OnCtlColor()
and OnShowMonoSpaced(), which is called from within the hook proc
when user selects a monospaced font (this is where the MONOSPACED
gets displayed). I also add DoModal() function,
which is where dialog template and hook function are specified.
The key function is the hook proc, where WM_INITDIALOG
is caught. This is where font filters (if any) are applied.
To integrate CXFontDialog into your app, you first need
to add following files to your project:
You also need to add XFontDialog.rc to project rc file - go to
View | Resource Includes... and in the bottom listbox,
scroll down to the end. Insert #include "XFontDialog.rc" right before the
#endif:
Next, include header file XFontDialog.h in
appropriate project files.
Now you are ready to start using CXFontDialog.
CXFontDialog:
bIsMonoSpaced = (tm.tmPitchAndFamily & TMPF_FIXED_PITCH) == 0;
but I discovered that some monospaced fonts (such as the excellent
Pragmata font
by Fabrizio Schiavi) did not mark the font in this way.
So I resorted to the simpler, but more reliable method of comparing the
widths of the '!' and 'W' characters. If they're the same,
that means the font is monospaced.
This software is released into the public domain. You are free to use it in
any way you like, except that you may not sell this source code. If you modify
it or extend it, please to consider posting new code here for everyone to share.
This software is provided "as is" with no expressed or implied warranty.
I accept no liability for any damage or loss of business that this software
may cause.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 7 Oct 2008 Editor: Smitha Vijayan |
Copyright 2003 by Hans Dietrich Everything else Copyright © CodeProject, 1999-2009 Web10 | Advertise on the Code Project |