Click here to Skip to main content
13,349,997 members (48,912 online)
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 7 Jun 2006

WYSIWYG Color and Font CommonDialogs

, 7 Jun 2006
Rate this:
Please Sign up or sign in to vote.
Enhancing user pleasure, or how to get the current user choice from a running dialog.


The Color and Font Common Dialogs use a small sample window to show the current user choice. If your application draws fast enough, why not display the resulting color/font directly in the application? This way, the specified part can be seen in relation to the overall appearance. This submission uses derived ColorDialog/FontDialog classes, that signal the current user choice while the dialog is still running. To evaluate this improved WYSIWYG functionality, try the provided demo.


To catch a change in the user's choice, the sample window is monitored by a NativeWindow class, which is created on the WM_INITDIALOG message in the overridden HookProc method. Whenever the WM_GETTEXT message is received, a click on the hidden Apply button is simulated. As the .NET FontDialog exposes an Apply event with a virtual OnApply method, the Font and Color properties now hold the actual choice. To filter out unwanted events, the new values are compared to the last signaled values. On a real change, the FontChanged event is raised, indicating whether the font and/or font color have changed. Surprisingly, this works only with a hidden Apply button (ShowApply == false), but this would be useless anyway with our improved WYSIWYG functionality.


Here, the WM_PAINT message is trapped, but the dialog is then notified by means of an interface defined method. There is no built-in way to get the actual color value, so we ask the RGB editboxes using GetDlgItemInt. After filtering out the ordinary repainting of the sample window, we fire the ColorChanged event with the new value. The dialog must be run in FullOpen mode, otherwise the sample window is not created.

Since we already use a derived dialog, the CustomColors property is shadowed to provide the Color type instead of the int array that the base class exposes. Furthermore, a boolean ShouldSerializeCustomColors property indicates whether the user changed any custom color(s).

Using the code

Setting the form's BackColor using an anonymous method is done as shown below:

ocColorDialog dlg = new ocColorDialog();
dlg.Color = this.BackColor;

dlg.ColorChanged = delegate(object sender, ColorChangedEventArgs e)
    { this.BackColor = e.CurrentColor; };
if (dlg.ShowDialog() == DialogResult.Cancel)
    this.BackColor = dlg.Color;

Setting the UseCancelEvent property simplifies the coding for the case of the dialog cancellation:

dlg.UseCancelEvent = true;

Points of Interest

Will I ever see this technique applied in a (commercial) application?


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionNeed to set the initial position of the ColorDialog Pin
Tim8w8-May-08 14:18
memberTim8w8-May-08 14:18 
GeneralPoints of Interest Pin
Softilog26-Oct-06 10:08
memberSoftilog26-Oct-06 10:08 
Nice example!

This type of functionality is used in Office 2007 with fonts, colors, styles etc.

JokeRe: Points of Interest Pin
OrlandoCurioso28-Oct-06 3:38
memberOrlandoCurioso28-Oct-06 3:38 
QuestionScreenshots? Pin
Mark II14-Jun-06 0:01
memberMark II14-Jun-06 0:01 
AnswerRe: Screenshots? Pin
OrlandoCurioso15-Jun-06 7:38
memberOrlandoCurioso15-Jun-06 7:38 
GeneralNot a valid project file Pin
Rahul.M.Rao12-Jun-06 16:33
memberRahul.M.Rao12-Jun-06 16:33 
GeneralRe: Not a valid project file Pin
OrlandoCurioso15-Jun-06 7:36
memberOrlandoCurioso15-Jun-06 7:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 7 Jun 2006
Article Copyright 2006 by OrlandoCurioso
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid