J'essaie de faire un J'ai essayé limite redimensionnable dimensions d'un contrôle personnalisé (C # .NET) mais ne peut pas le faire fonctionner. P> Creatreparams code> pour le redimensionnement et maintenant la propriété de taille minimale code> minimum code> ne fonctionne pas.
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);
}
}
}
}
3 Réponses :
Il suffit de définir la propriété minimumSize:
Cette classe ici fait partie de l'un de mes packages de Nuge. J'ai ajouté ce minimumisé dans le constructeur juste pour vous montrer, je n'ai généralement pas que dans le code: p> Je ne suis pas sûr du redimensionnement dont vous avez besoin, ajuster à Convient à votre situation. P> Si vous le souhaitez: P> Nuget: datajuggler.win.controls p> Source: https://github.com/datajuggler/datajuggler.win.controls p> p>
Définir la propriété minimumisize () code> Pour que vous souhaitiez votre contrôle (comme normal via l'IDE ou via le code), ajoutez le code ci-dessous à votre contrôle pour piéger le wm_getminmaxinfo message et remplacer la taille minimale car le contrôle est redimensionné de manière dynamique: < Pré> xxx pré> p>
le code de travail complet: strong>
Thnks pour l'aide 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);
}
}
}