如何使用"CreateParams"设置自定义控件的最小大小



我正在尝试以最小尺寸制作一个可拖动可调整大小的面板。我已经将CreateParams用于调整大小,现在Minimumsize 属性不起作用。

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

我尝试过限制自定义控件 (c# .net( 的可调整大小的尺寸,但无法使其工作。

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消息并在动态调整控件大小时重写最小大小:

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);
}
}

只需设置 MinimumSize 属性:

这里的这个类是我的一个 Nuget 包的一部分。我在构造函数中添加了这个 MinimumSize 只是为了向您展示,我通常没有在代码中:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DataJuggler.Win.Controls.Objects
{
#region class PanelExtender : Panel
/// <summary>
/// This class inherits from Panel; this is intended to stop the flickering on panels
/// </summary>
public class PanelExtender : Panel
{
#region Constructor
/// <summary>
/// Create a new instance of a PanelExtender object
/// </summary>
public PanelExtender()
{
// Set Style to stop flickering
this.SetStyle(System.Windows.Forms.ControlStyles.UserPaint | System.Windows.Forms.ControlStyles.AllPaintingInWmPaint | System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer, true);
// Set the minimum size
this.MinimumSize = new System.Drawing.Size(400, 800);
}
#endregion
#region Properties
#region CreateParams
/// <summary>
/// This property here is what did the trick to reduce the flickering.
/// I also needed to make all of the controls Double Buffered, but
/// this was the final touch. It is a little slow when you switch tabs
/// but that is because the repainting is finishing before control is
/// returned.
/// </summary>
protected override CreateParams CreateParams
{
get
{
// initial value
CreateParams cp = base.CreateParams;
// Apparently this interrupts Paint to finish before showing
cp.ExStyle |= 0x02000000;
// return value
return cp;
}
}
#endregion
#endregion
}
#endregion
}

我不确定您需要调整大小,请调整以适应您的情况。

如果你想要它:

Nuget: DataJuggler.Win.Controls

来源:https://github.com/DataJuggler/DataJuggler.Win.Controls

完整的工作代码:感谢帮助

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
this.MinimumSize = new System.Drawing.Size(200, 200);
}



// 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);
}
}



// Make the Minumum Size - Work
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);
}


}
}

最新更新