לבינתיים השלמתי את המלאכה בwinform אולי יהיה שימושי למישהו

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ResizableTextBox
{
class SizeableUserControl : UserControl
{
private const int grab = 16;
public SizeableUserControl()
{
this.ResizeRedraw = true;
InitializeComponent();
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
const int WM_NCHITTEST = 0x84;
const int HT_LEFT = 10;
const int HT_RIGHT = 11;
const int HT_TOP = 12;
const int HT_TOPLEFT = 13;
const int HT_TOPRIGHT = 14;
const int HT_BOTTOM = 15;
const int HT_BOTTOMLEFT = 16;
const int HT_BOTTOMRIGHT = 17;
if (m.Msg == WM_NCHITTEST)
{
var pos = this.PointToClient(new Point(m.LParam.ToInt32()));
if (pos.X <= grab && pos.Y <= grab)
m.Result = new IntPtr(HT_TOPLEFT);
else if (pos.X >= this.ClientSize.Width - grab && pos.Y <= grab)
m.Result = new IntPtr(HT_TOPRIGHT);
else if (pos.X <= grab && pos.Y >= this.ClientSize.Height - grab)
m.Result = new IntPtr(HT_BOTTOMLEFT);
else if (pos.X >= this.ClientSize.Width - grab && pos.Y >= this.ClientSize.Height - grab)
m.Result = new IntPtr(HT_BOTTOMRIGHT);
else if (pos.X <= grab)
m.Result = new IntPtr(HT_LEFT);
else if (pos.X >= this.ClientSize.Width - grab)
m.Result = new IntPtr(HT_RIGHT);
else if (pos.Y <= grab)
m.Result = new IntPtr(HT_TOP);
else if (pos.Y >= this.ClientSize.Height - grab)
m.Result = new IntPtr(HT_BOTTOM);
}
}
private void InitializeComponent()
{
this.SuspendLayout();
this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.Name = "SizeableUserControl";
this.Size = new System.Drawing.Size(100, 100);
Panel titleBar = new Panel();
titleBar.Dock = DockStyle.Top;
titleBar.BackColor = Color.White;
titleBar.Height = 20;
Controls.Add(titleBar);
titleBar.MouseDown += TitleBar_MouseDown;
titleBar.MouseMove += TitleBar_MouseMove;
titleBar.MouseUp += TitleBar_MouseUp;
// Minimize button
Button minimizeButton = new Button();
minimizeButton.Text = "─";
minimizeButton.TextAlign = ContentAlignment.MiddleCenter;
minimizeButton.Width = 20;
minimizeButton.Height = 20;
minimizeButton.Dock = DockStyle.Right;
minimizeButton.FlatStyle = FlatStyle.Flat;
minimizeButton.FlatAppearance.BorderSize = 0;
titleBar.Controls.Add(minimizeButton);
minimizeButton.Click += (sender, e) =>
{
if (minimizeButton.Text == "─")
{
minimizeButton.Text = "+";
this.Height = 20;
this.Width = 60;
}
else
{
minimizeButton.Text = "─";
this.Height = 100;
this.Width = 100;
}
};
// Maximize button
Button maximizeButton = new Button();
maximizeButton.Text = "⬜"; // Larger square Unicode character
maximizeButton.TextAlign = ContentAlignment.MiddleCenter;
maximizeButton.Width = 20;
maximizeButton.Height = 20;
maximizeButton.Dock = DockStyle.Right;
maximizeButton.FlatStyle = FlatStyle.Flat;
maximizeButton.FlatAppearance.BorderSize = 0;
// Store the original DockStyle
DockStyle originalDockStyle = DockStyle.None;
maximizeButton.Click += (sender, e) =>
{
if (this.Dock == DockStyle.None)
{
// Maximize the form
originalDockStyle = this.Dock;
this.Dock = DockStyle.Fill;
maximizeButton.Text = "❐"; // Change text to indicate restore
}
else
{
// Restore the original DockStyle
this.Dock = originalDockStyle;
maximizeButton.Text = "⬜"; // Change text to indicate maximize
}
};
titleBar.Controls.Add(maximizeButton);
// X button
Button xButton = new Button();
xButton.Text = "X";
xButton.Dock = DockStyle.Right;
xButton.Width = 20;
xButton.Height = 20;
xButton.FlatStyle = FlatStyle.Flat;
xButton.FlatAppearance.BorderSize = 0;
xButton.Click += (sender, e) => this.Dispose();
titleBar.Controls.Add(xButton);
this.ResumeLayout(false);
}
bool isMouseDown = false;
private Point mouseDownLocation;
private void TitleBar_MouseUp(object sender, MouseEventArgs e)
{
isMouseDown = false;
}
private void TitleBar_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseDown)
{
if (isMouseDown)
{
// Calculate the new position of the form based on the mouse movement
this.Location = new Point(
(this.Location.X - mouseDownLocation.X) + e.X,
(this.Location.Y - mouseDownLocation.Y) + e.Y);
this.Update(); // Forces the form to repaint for a smoother visual experience
}
}
}
private void TitleBar_MouseDown(object sender, MouseEventArgs e)
{
isMouseDown = true;
mouseDownLocation = e.Location;
}
}
}



