C# 打开一个;ColorDialog";位于指定位置的对话框

C# 打开一个;ColorDialog";位于指定位置的对话框,c#,.net,winforms,dialog,positioning,C#,.net,Winforms,Dialog,Positioning,可能重复: 我必须在用户单击的按钮上方显示一个颜色对话框 目前我找不到如何指定此位置: 没有起始位置/位置属性() 构造函数只接受windows a参数并将其放在中间 我需要将它直接放在光标上方,指定一个X;Y 你知道如何做到这一点吗 谢谢大家! 你不能!事实上,这既不是ColorDialog的问题,也不是其他常见对话框的问题。如果您想显示一个模态对话框,您将直接使用或不使用“DialogBox.Show”方法,在这种情况下,操作系统决定位置的选择。 唯一的解决方案是使用无模式对话框和Dial

可能重复:

我必须在用户单击的按钮上方显示一个颜色对话框

目前我找不到如何指定此位置:

  • 没有
    起始位置
    /
    位置
    属性()
  • 构造函数只接受windows a参数并将其放在中间
  • 我需要将它直接放在光标上方,指定一个X;Y

    你知道如何做到这一点吗


    谢谢大家!

    你不能!事实上,这既不是ColorDialog的问题,也不是其他常见对话框的问题。如果您想显示一个模态对话框,您将直接使用或不使用“DialogBox.Show”方法,在这种情况下,操作系统决定位置的选择。
    唯一的解决方案是使用无模式对话框和DialogBox.Show方法,但您还需要重新创建整个对话框

    我终于找到了一种方法,不是最漂亮的东西,但它是有效的:

    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
    }
    
    公共类ColorDialogExtension:ColorDialog { #区域私人常数 //Windows消息常量 private const Int32 WM_INITDIALOG=0x0110; //uFlag常数 私人警察SWP_NOSIZE=0x0001; 私人建筑SWP_SHOWWINDOW=0x0040; 私人建筑SWP_NOZORDER=0x0004; 私人建筑UFLAGS=SWP|u NOSIZE | SWP|u NOZORDER | SWP|u SHOWWINDOW; #端区 #区域专用只读 //Windows句柄常量 私有静态只读IntPtr HWND_TOPMOST=新IntPtr(-1); 私有静态只读IntPtr HWND_NOTOPMOST=new IntPtr(-2); 私有静态只读IntPtr HWND_TOP=new IntPtr(0); 私有静态只读IntPtr HWND_BOTTOM=新IntPtr(1); #端区 #区域私人VAR //模块变量 私人互联网; 私人国际; 私有字符串_title=null; #端区 #区域私有静态方法导入 //WinAPI定义 /// ///设置窗口文本。 /// ///那辆车停了下来。 ///文本。 /// [DllImport(“user32.dll”,CharSet=CharSet.Auto)] 私有静态外部bool SetWindowText(IntPtr hWnd,字符串文本); /// ///设置窗口位置。 /// ///那辆车停了下来。 ///之后插入h WND。 ///x。 ///y。 ///国泰航空公司。 ///西。 ///美国国旗。 /// [DllImport(“user32.dll”,SetLastError=true)] [返回:Marshallas(UnmanagedType.Bool)] 私有静态外部bool SetWindowPos(IntPtr hWnd、IntPtr hwninsertafter、intx、inty、intcx、intcy、uint uFlags); #端区 #区域公共构造函数 /// ///初始化类的新实例。 /// ///X位置 ///Y位置 ///窗口的标题。如果设置为null(默认),则不会更改标题 公共ColorDialogExtension(int x,int y,字符串标题=null) { _x=x; _y=y; _头衔=头衔; } #端区 #区域保护覆盖方法 /// ///定义公共对话框挂钩过程,该过程被覆盖以向公共对话框添加特定功能。 /// ///对话框窗口的句柄。 ///正在接收的消息。 ///有关此消息的其他信息。 ///有关此消息的其他信息。 /// ///如果默认对话框过程处理消息,则为零值;如果默认对话框过程忽略消息,则为非零值。 /// 受保护的覆盖IntPtr HookProc(IntPtr hWnd、int msg、IntPtr wparam、IntPtr lparam) { //我们进行基本初始化 IntPtr hookProc=base.hookProc(hWnd、msg、wparam、lparam); //当我们初始化对话框时 if(msg==WM_INITDIALOG) { //我们改变标题 如果(!String.IsNullOrEmpty(_title)) { SetWindowText(hWnd,标题); } //我们移动位置 设置窗口位置(hWnd、hWnd_顶部、x、y、0、UFLAGS); } 返回hookProc; } #端区 }
    不同的对话框,但应该是相同的解决方案。@JonB:我不明白提供一个
    iwin32窗口的可能性如何帮助我将对话框定位在正确的位置?@J4N查看链接到codeproject文章的问题的答案@J4N-对话框是一个窗口。设置窗口的位置将产生您想要做的事情。