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.
My question is how to set the minimum size in this case?
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);
}
}
}
}