Skip Navigation LinksHome > Categories > Code from a Category

How to Change settings on Font Dialog Box

User Name: codelecturer
Name: Mike Chauhan
Contact Me:
Home Page:
6 years of total IT experience including programming, application development, System Maintenance . 3 years of Experienced and expertise in .Net Framework VB, Visual Basic.Net, ASP.Net 2.0, 3.5 and AD... [More]
Viewed Times: 1184
Add Date: 10/17/2012
In this article we will learn about to change settings on Font Dialog box
It is a common problem that we want to use Common Dialogs (like the Font dialog), but we do not want to allow users to select all the properties offered by the dialogs. This article shows how we can do that, with the font dialog as an example.

I definitely needed a font dialog without the font size property, for a drawing application which manages font size automatically, so we do not want users to be able to alter it. There are two options to select from:

If we have the handle of the window to be hacked, the whole problem is very easy. A few native function calls should be added, like:

Each UI component of a Common Dialog has a fixed ID. Spy++ can help us identify them. In the case of Font Size, these are 1090 and 1138; so, we can hide the unwanted components by calling:

Finding the dialog handle
The key problem is identifying the dialogHandle. The sample shows three possible solutions, two of which are popular but have their own problems.

FindWindow: Let’s find the font dialog after it is shown, using a FindWindow() native call. Before showing the dialog, a timer is started, which finishes if the dialog is found. This approach has the following problems:

What is the right interval of the timer? If a large amount of time is selected, users can see the unwanted UI elements which later disappear.
FindWindow takes the title of the window as an input parameter. If Windows isn’t English, the title of the Font dialog isn’t "Font".
If it cannot find the right dialog, it will continue running till eternity.

GetActiveWindow: Let us suppose that the active window is our Font dialog; again, start a timer before showing it, and hope that the font dialog will be the active window when the timer first ticks. Again, the right timer interval is unknown, and if by any chance our Font dialog is not the topmost, we send commands to another application. Best solution: let’s take the approach described in this article: Customizing OpenFileDialog in .NET.

I removed all the unneeded code from it to get a simple solution to our problem. The FontDialogEx class is to be used instead of the FontDialog. Its ShowDialog method creates a dummy, invisible form named DummyForm. Invisibility is achieved by setting a flag named UFLAGSHIDE. DummyForm waits until it gets activated, this is to be found in its overwritten WndProc(ref Message m) function.

This is the place where we can find the handle of the Font dialog. FontDialogNative is created after the right handle is found, so our unneeded UI elements can be hidden in the constructor of FontDialogNative.

Happy Programming!!!

Post a Comment

Name: (Optional)
Email: (Optional, you can get an email if somebody replys your comments)*
Email me if somebody respons my comment below:
Enter Text
as Below:
(case insensitive, if hard to read, click the "get a new one" button)
* Your email address will not be shared with any third parties for any reason.
** Maximum 1000 charactors.