Click here to Skip to main content
15,885,039 members
Articles / Desktop Programming / Win32

Hacking FontDialog

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
23 Aug 2008CPOL2 min read 36.8K   753   13  
How to hide UI elements of a FontDialog (e.g., Font Size).
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DrawTools;
using System.Runtime.InteropServices;

namespace FontDialogHack
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnBest_Click(object sender, EventArgs e)
        {
            FontDialogEx dialog = new FontDialogEx();
            //dialog.FontDialog.Font = new Font("Arial", 12f, FontStyle.Regular);
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                Font resFont = dialog.FontDialog.Font;
            }
        }

        private void btnFindWindow_Click(object sender, EventArgs e)
        {
            Timer dialog = new Timer();
            dialog.Tick += new EventHandler(dialog_Tick1);
            dialog.Interval = 500;
            dialog.Enabled = true;

            FontDialog fd = new FontDialog();
            fd.ShowDialog();
        }

        private void btnActiveWindow_Click(object sender, EventArgs e)
        {
            Timer dialog = new Timer();
            dialog.Tick += new EventHandler(dialog_Tick2);
            dialog.Interval = 500;
            dialog.Enabled = true;

            FontDialog fd = new FontDialog();
            fd.ShowDialog();
        }

        [DllImport("user32.dll")]
        static extern IntPtr GetActiveWindow();

        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        void dialog_Tick1(object sender, EventArgs e)
        {
            IntPtr dialogHandle = IntPtr.Zero;
            string dialogName = "Font"; 
            //Note that window title may be different if non-English Windows is used, for example "Bet�t�pus" in Hungarian

            dialogHandle = FindWindow("#32770", dialogName);
            while (dialogHandle == IntPtr.Zero) //this cycle can run forever if window cannot be found!!
            {
                dialogHandle = FindWindow("#32770", dialogName);
            }
            Win32.ShowWindow(Win32.GetDlgItem(dialogHandle, 1090), Win32.SW_HIDE);
            Win32.ShowWindow(Win32.GetDlgItem(dialogHandle, 1138), Win32.SW_HIDE);

            ((Timer)sender).Dispose();
        }

        void dialog_Tick2(object sender, EventArgs e)
        {
            IntPtr dialogHandle = GetActiveWindow(); //ok, but if active window is changed in the 500msec time...
            Win32.ShowWindow(Win32.GetDlgItem(dialogHandle, 1090), Win32.SW_HIDE);
            Win32.ShowWindow(Win32.GetDlgItem(dialogHandle, 1138), Win32.SW_HIDE);

            ((Timer)sender).Dispose();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Hungary Hungary
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions