Click here to Skip to main content
15,881,803 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.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)


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