如何使用“ CreateParams”设置自定义控件的最小大小 [英] How to set minimum Size of Custom Control with "CreateParams"

查看:183
本文介绍了如何使用“ CreateParams”设置自定义控件的最小大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个可拖动可调整大小的面板,且尺寸最小。我使用了 CreateParams 来调整大小,现在 Minimum size属性不起作用。

I am trying to make a draggable, resizable panel with a minimum size. I have used CreateParams for the Resize and now the Minimum size property doesn't work.

我的问题是在这种情况下如何设置最小大小?

我已尝试限制自定义控件的可调整大小(c#.net ),但无法正常工作。

I have tried Limit resizable dimensions of a custom control (c# .net) but can't get it to work.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Move_Resize_Controls
{
    class MyPanel: Panel
    {
        // For Moving Panel "Drag the Titlebar and move the panel"
        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HT_CAPTION = 0x2;

        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd,
                         int Msg, int wParam, int lParam);
        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();

        // Constructor
        public MyPanel()
        {
            typeof(MyPanel).InvokeMember("DoubleBuffered", BindingFlags.SetProperty | BindingFlags.Instance | BindingFlags.NonPublic, null, this, new object[] { true }); // Double buffer MyPanel
            TitleBar(); // TitleBar
        }

        // Resize function for the panel - "Resizable Panel"
        protected override CreateParams CreateParams
        {
            get
            {
                var cp = base.CreateParams;
                cp.Style |= (int)0x00040000L;  // Turn on WS_BORDER + WS_THICKFRAME
                //cp.Style |= (int)0x00C00000L;  // Move
                return cp;
            }
        }

        // The Title Bar
        private void TitleBar()
        {
            Panel titleBar = new Panel();
            titleBar.BackColor = Color.Black;
            titleBar.Size = new Size(this.Size.Width, 20);
            titleBar.Dock = DockStyle.Top;
            this.Controls.Add(titleBar);

            titleBar.MouseDown  += new System.Windows.Forms.MouseEventHandler(this.MouseDownTitleBar); // Mouse Down - Event
            typeof(Panel).InvokeMember("DoubleBuffered", BindingFlags.SetProperty | BindingFlags.Instance | BindingFlags.NonPublic, null, titleBar, new object[] { true }); // Double Buffered 
        }

        // Move Panel
        private void MouseDownTitleBar(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
        }
    }
}


推荐答案

MinimumSize()属性设置为所需的控件(通常通过IDE或通过代码),然后添加代码捕获 WM_GETMINMAXINFO 消息并在控件动态调整大小时覆盖最小尺寸:

Set the MinimumSize() property to want you want for your control (as normal through the IDE or through code), then add the code below to your control to trap the WM_GETMINMAXINFO message and override the minimum size as the control is dynamically resized:

class MyPanel: Panel
{

    public const int WM_GETMINMAXINFO = 0x24;

    public struct POINTAPI
    {
        public Int32 X;
        public Int32 Y;
    }

    public struct MINMAXINFO
    {
        public POINTAPI ptReserved;
        public POINTAPI ptMaxSize;
        public POINTAPI ptMaxPosition;
        public POINTAPI ptMinTrackSize;
        public POINTAPI ptMaxTrackSize;
    }

    protected override void WndProc(ref Message m)
    {
        switch (m.Msg)
        {
            case WM_GETMINMAXINFO:
                MINMAXINFO mmi = (MINMAXINFO)System.Runtime.InteropServices.Marshal.PtrToStructure(m.LParam, typeof(MINMAXINFO));
                mmi.ptMinTrackSize.X = this.MinimumSize.Width;
                mmi.ptMinTrackSize.Y = this.MinimumSize.Height;
                System.Runtime.InteropServices.Marshal.StructureToPtr(mmi, m.LParam, true);
                break;
        }
        base.WndProc(ref m);
    }

}

这篇关于如何使用“ CreateParams”设置自定义控件的最小大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆