5

Possible Duplicate:
Setting the start position for OpenFileDialog/SaveFileDialog

I've to show a ColorDialog, just above the button where the user clicked.

Currently I can't find how to specify this location:

  1. There is no StartPosition/Location properties ( How can I control the location of a dialog when using ShowDialog to display it? )
  2. The constructor only take a windows a parameter and put this in the middle

I need to place it directly above my cursor, specifying a X;Y.

Any idea about how to achieve this?

Thank you!

Community
  • 1
  • 1
J4N
  • 19,480
  • 39
  • 187
  • 340
  • 1
    Different dialog, but should be the same solution. – Jon B Jan 09 '13 at 15:35
  • @JonB : I don't understand how the possibility of giving an `IWin32Window` will help me to position the dialog at the correct place? – J4N Jan 09 '13 at 15:41
  • @J4N See the answer in that question that links to a codeproject article. http://stackoverflow.com/a/1256524/860585 – Rotem Jan 09 '13 at 15:47
  • @J4N - The dialog is a window. Setting the position of the window will result in what you want to do. – Security Hound Jan 09 '13 at 17:24

2 Answers2

9

I finally found a way, not the prettiest thing, but it works:

public class ColorDialogExtension : ColorDialog
{
    #region private const
    //Windows Message Constants
    private const Int32 WM_INITDIALOG = 0x0110;

    //uFlag Constants
    private const uint SWP_NOSIZE = 0x0001;
    private const uint SWP_SHOWWINDOW = 0x0040;
    private const uint SWP_NOZORDER = 0x0004;
    private const uint UFLAGS = SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW;
    #endregion

    #region private readonly
    //Windows Handle Constants
    private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
    private static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
    private static readonly IntPtr HWND_TOP = new IntPtr(0);
    private static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
    #endregion

    #region private vars
    //Module vars
    private int _x;
    private int _y;
    private string _title = null;
    #endregion

    #region private static methods imports
    //WinAPI definitions

    /// <summary>
    /// Sets the window text.
    /// </summary>
    /// <param name="hWnd">The h WND.</param>
    /// <param name="text">The text.</param>
    /// <returns></returns>
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern bool SetWindowText(IntPtr hWnd, string text);

    /// <summary>
    /// Sets the window pos.
    /// </summary>
    /// <param name="hWnd">The h WND.</param>
    /// <param name="hWndInsertAfter">The h WND insert after.</param>
    /// <param name="x">The x.</param>
    /// <param name="y">The y.</param>
    /// <param name="cx">The cx.</param>
    /// <param name="cy">The cy.</param>
    /// <param name="uFlags">The u flags.</param>
    /// <returns></returns>
    [DllImport("user32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);
    #endregion

    #region public constructor
    /// <summary>
    /// Initializes a new instance of the <see cref="ColorDialogExtension"/> class.
    /// </summary>
    /// <param name="x">The X position</param>
    /// <param name="y">The Y position</param>
    /// <param name="title">The title of the windows. If set to null(by default), the title will not be changed</param>
    public ColorDialogExtension(int x, int y, String title = null)
    {
        _x = x;
        _y = y;
        _title = title;
    }
    #endregion

    #region protected override methods
    /// <summary>
    /// Defines the common dialog box hook procedure that is overridden to add specific functionality to a common dialog box.
    /// </summary>
    /// <param name="hWnd">The handle to the dialog box window.</param>
    /// <param name="msg">The message being received.</param>
    /// <param name="wparam">Additional information about the message.</param>
    /// <param name="lparam">Additional information about the message.</param>
    /// <returns>
    /// A zero value if the default dialog box procedure processes the message; a nonzero value if the default dialog box procedure ignores the message.
    /// </returns>
    protected override IntPtr HookProc(IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam)
    {
        //We do the base initialization
        IntPtr hookProc = base.HookProc(hWnd, msg, wparam, lparam);
        //When we init the dialog
        if (msg == WM_INITDIALOG)
        {
            //We change the title
            if (!String.IsNullOrEmpty(_title))
            {
                SetWindowText(hWnd, _title);
            }
            //We move the position
            SetWindowPos(hWnd, HWND_TOP, _x, _y, 0, 0, UFLAGS); 

        }
        return hookProc;
    }
    #endregion
}
J4N
  • 19,480
  • 39
  • 187
  • 340
-1

You can't ! In fact, it's neither the problem of a ColorDialog nor another Common Dialog. If you want to display a modal dialog bon, you will use directly or not the "DialogBox.Show" method, and in this case, the Operating System decides of the position. The only solution is to use a modeless Dialog Box, and to use the DialogBox.Show method, but you also need to re-create the whole dialogBox !

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Stephane Halimi
  • 408
  • 3
  • 5