Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Hacking FontDialog

, 23 Aug 2008
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.IO;
using System.Text;
using System.Data;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace DrawTools
{
    public partial class FontDialogEx : UserControl
    {
        private const SetWindowPosFlags UFLAGSHIDE =
            SetWindowPosFlags.SWP_NOACTIVATE |
            SetWindowPosFlags.SWP_NOOWNERZORDER |
            SetWindowPosFlags.SWP_NOMOVE |
            SetWindowPosFlags.SWP_NOSIZE |
            SetWindowPosFlags.SWP_HIDEWINDOW;

        public FontDialogEx()
        {
            InitializeComponent();
        }

        public FontDialog FontDialog
        {
            get { return dlgFont; }
        }

        public DialogResult ShowDialog()
        {
            return ShowDialog(null);
        }

        public DialogResult ShowDialog(IWin32Window owner)
        {
            DialogResult returnDialogResult = DialogResult.Cancel;
            DummyForm form = new DummyForm(this);
            form.Show(owner);
            Win32.SetWindowPos(form.Handle, IntPtr.Zero, 0, 0, 0, 0, UFLAGSHIDE);
            form.WatchForActivate = true;
            try
            {
                returnDialogResult = dlgFont.ShowDialog(form);
            }
            // Sometimes if you open a animated .gif on the preview and the Form is closed, .Net class throw an exception
            // Lets ignore this exception and keep closing the form.
            catch (Exception) { }
            form.Close();
            return returnDialogResult;
        }

        private class FontDialogNative : NativeWindow, IDisposable
        {
            private IntPtr mFontDialogHandle;
            private BaseDialogNative mBaseDialogNative;
            private FontDialogEx mSourceControl;

            public FontDialogNative(IntPtr handle, FontDialogEx sourceControl)
            {
                mFontDialogHandle = handle;
                mSourceControl = sourceControl;
                AssignHandle(mFontDialogHandle);

                //hide unneeded components
                Win32.ShowWindow(Win32.GetDlgItem(mFontDialogHandle, 1090), Win32.SW_HIDE);
                Win32.ShowWindow(Win32.GetDlgItem(mFontDialogHandle, 1138), Win32.SW_HIDE);
            }

            public void Dispose()
            {
                ReleaseHandle();
                if (mBaseDialogNative != null)
                {
                    mBaseDialogNative.Dispose();
                }
            }

            private void PopulateWindowsHandlers()
            {
                Win32.EnumChildWindows(mFontDialogHandle, new Win32.EnumWindowsCallBack(OpenFileDialogEnumWindowCallBack), 0);
            }

            private bool OpenFileDialogEnumWindowCallBack(IntPtr hwnd, int lParam)
            {
                StringBuilder className = new StringBuilder(256);
                Win32.GetClassName(hwnd, className, className.Capacity);

                // Dialog Window
                if (className.ToString().StartsWith("#32770"))
                {
                    mBaseDialogNative = new BaseDialogNative(hwnd);
                    return true;
                }
                return true;
            }
        }

        private class BaseDialogNative : NativeWindow, IDisposable
        {
            private IntPtr mHandle;

            public BaseDialogNative(IntPtr handle)
            {
                mHandle = handle;
                AssignHandle(handle);
            }

            public void Dispose()
            {
                ReleaseHandle();
            }
        }

        private class DummyForm : Form
        {
            private FontDialogNative mNativeDialog = null;
            private FontDialogEx mFileDialogEx = null;
            private bool mWatchForActivate = false;
            private IntPtr mFontDialogHandle = IntPtr.Zero;

            public DummyForm(FontDialogEx fileDialogEx)
            {
                mFileDialogEx = fileDialogEx;
                this.Text = "";
                this.StartPosition = FormStartPosition.Manual;
                this.Location = new Point(-32000, -32000);
                this.ShowInTaskbar = false;
            }

            public bool WatchForActivate
            {
                get { return mWatchForActivate; }
                set { mWatchForActivate = value; }
            }

            protected override void OnClosing(CancelEventArgs e)
            {
                if (mNativeDialog != null)
                    mNativeDialog.Dispose();
                base.OnClosing(e);
            }

            protected override void WndProc(ref Message m)
            {
                if (mWatchForActivate && m.Msg == (int)Win32.WM_ACTIVATE)
                {
                    mWatchForActivate = false;
                    mFontDialogHandle = m.LParam;
                    mNativeDialog = new FontDialogNative(m.LParam, mFileDialogEx);
                }
                base.WndProc(ref m);
            }
        }
    }
}

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 23 Aug 2008
Article Copyright 2008 by legcsabi
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid