Click here to Skip to main content
12,072,536 members (61,433 online)
Click here to Skip to main content

Stats

21.3K views
546 downloads
11 bookmarked
Posted

Hacking FontDialog

, 23 Aug 2008 CPOL
How to hide UI elements of a FontDialog (e.g., Font Size).
FontDialogHack.zip
FontDialogHack
FontDialogHack
bin
Debug
FontDialogHack.exe
FontDialogHack.vshost.exe
Properties
Settings.settings
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)

Share

About the Author

legcsabi
Hungary Hungary
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.160208.1 | Last Updated 23 Aug 2008
Article Copyright 2008 by legcsabi
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid