c# - 在矩形周围放置和粘贴控件

标签 c# winforms graphics

我正在开发一个小型剪切工具,它可以工作,但我无法理解如何在选定的剪切区域(矩形)周围放置两个工具条控件。我在鼠标弹起事件上刷新工具条位置,它会像这样处理两个工具条的移动。

//The two toolstrips that I have
//rcSelect is the selected rectangle

toolStrip1.Visible = true;
toolStrip2.Visible = true;

//Tool Strip One
int toolstripY = (rcSelect.Y + rcSelect.Height) - toolStrip1.Height;
int toolstripX = rcSelect.Width + rcSelect.X + 5;

if (toolstripX + toolStrip1.Width >= this.Width)
{
    toolstripX = rcSelect.X - toolStrip1.Width - 5;
}

if (rcSelect.Width + toolStrip1.Width >= this.Width)
{
    toolstripX = rcSelect.Width - toolStrip1.Width;
}

if (toolstripY + toolStrip1.Height >= this.Height)
{
    toolstripY = rcSelect.Y;
}

if (rcSelect.Height + toolStrip1.Height >= this.Height)
{
    toolstripY = rcSelect.Height;
}

if (toolStrip1.Location.Y + toolStrip1.Height >= this.Height)
{
    toolstripY = rcSelect.Y - toolStrip1.Height - 5;
}


//Tool Strip Two

int toolstrip2Y = rcSelect.Y + rcSelect.Height + 5;
int toolstrip2X = rcSelect.X + (rcSelect.Width - toolStrip2.Width);


if (toolstrip2Y + toolStrip2.Height >= this.Height)
{
    toolstrip2Y = rcSelect.Y - toolStrip2.Height - 5;
}

if (rcSelect.Height + toolStrip2.Height >= this.Height)
{
    toolstrip2Y = rcSelect.Height - toolStrip2.Height;
}

if (toolstrip2X + toolStrip2.Width >= this.Width)
{
    toolstrip2X = rcSelect.X;
}

if (rcSelect.Width + toolStrip2.Width >= this.Width)
{
    toolstrip2X = rcSelect.Width;
}

if (toolStrip2.Location.X + toolStrip2.Width >= this.Width)
{
    toolstrip2X = rcSelect.X - toolStrip2.Width - 5;
}


//Try to Insure visbility yet again

if (toolStrip1.Location.Y + toolStrip1.Height >= this.Height)
{
    int offsetY = (toolStrip1.Location.Y + toolStrip1.Height) - this.Height;
    toolstripY = toolStrip1.Location.Y - offsetY;
}


if (toolStrip2.Location.X + toolStrip2.Width >= this.Width)
{
    int offsetX = (toolStrip2.Location.X + toolStrip2.Width) - this.Width;
    toolstrip2X = toolStrip2.Location.X - offsetX;
}


if (toolstrip2X < 0)
{
    toolstrip2X = 0;
}

if (toolstrip2Y < 0)
{
    toolstrip2Y = 0;
}

if (toolstripX < 0)
{
    toolstripX = 0;
}

if (toolstripY < 0)
{
    toolstripY = 0;
}


toolStrip1.Location = new Point(toolstripX, toolstripY);
toolStrip2.Location = new Point(toolstrip2X, toolstrip2Y);

这是开始时的样子。 enter image description here

这就是我调整大小并移动该矩形时的样子 enter image description here


调试

垂直一是toolstrip1,水平是toolstrip2

Strip1: X 1185 Y 236
Strip2: X 894 Y 629
Strip1: X 1504 Y 626
Strip2: X 1213 Y 582
Strip1: X 1412 Y 30
Strip2: X 1121 Y 423
Strip1: X 1362 Y 375
Strip2: X 1407 Y 768
Strip1: X 1224 Y 230
Strip2: X 933 Y 623
Strip1: X 1424 Y 626
Strip2: X 1133 Y 582
Strip1: X 835 Y 443
Strip2: X 544 Y 836
Strip1: X 1475 Y 245
Strip2: X 1184 Y 638
Strip1: X 828 Y 265
Strip2: X 537 Y 658
Strip1: X 1038 Y 278
Strip2: X 747 Y 671
Strip1: X 1422 Y 576
Strip2: X 1131 Y 969
Strip1: X 1786 Y 614
Strip2: X 1495 Y 570
Strip1: X 1638 Y 120
Strip2: X 1347 Y 513
Strip1: X 1780 Y 311
Strip2: X 1489 Y 704
Strip1: X 1780 Y 311
Strip2: X 1489 Y 704
Strip1: X 1862 Y 356
Strip2: X 1907 Y 749
Strip1: X 1557 Y 308
Strip2: X 1634 Y 701
Strip1: X 1008 Y 347
Strip2: X 1634 Y 740
Strip1: X 1095 Y 384
Strip2: X 1634 Y 777
Strip1: X 1133 Y 113
Strip2: X 1634 Y 506
Strip1: X 1112 Y 0
Strip2: X 1634 Y 230
Strip1: X 1183 Y 700
Strip2: X 1634 Y 656
Strip1: X 1216 Y 692
Strip2: X 1634 Y 880
Strip1: X 1168 Y 692
Strip2: X 1634 Y 401
Strip1: X 1163 Y 692
Strip2: X 1634 Y 792
Strip1: X 1034 Y 692
Strip2: X 1634 Y 719
Strip1: X 1170 Y 692
Strip2: X 1634 Y 669
Strip1: X 1209 Y 692
Strip2: X 1634 Y 679

enter image description here


调试 2

Strip1: X 1513 Y 465
Strip2: X 1222 Y 858
rcSelect Loc: X 1048 Y 479
rcSelect Siz: W 460 H 374
Strip1: X 1710 Y 278
Strip2: X 1419 Y 671
rcSelect Loc: X 1245 Y 292
rcSelect Siz: W 460 H 374
Strip1: X 1710 Y 278
Strip2: X 1419 Y 671
rcSelect Loc: X 1245 Y 292
rcSelect Siz: W 460 H 374
Strip1: X 1685 Y 288
Strip2: X 1394 Y 681
rcSelect Loc: X 1204 Y 280
rcSelect Siz: W 476 H 396
Strip1: X 1634 Y 376
Strip2: X 1679 Y 769
rcSelect Loc: X 1679 Y 368
rcSelect Siz: W 476 H 396
Strip1: X 1258 Y 332
Strip2: X 1634 Y 725
rcSelect Loc: X 777 Y 324
rcSelect Siz: W 476 H 396
Strip1: X 1479 Y 0
Strip2: X 1634 Y 144
rcSelect Loc: X 998 Y -257
rcSelect Siz: W 476 H 396
Strip1: X 1567 Y 427
Strip2: X 1634 Y 820
rcSelect Loc: X 1086 Y 419
rcSelect Siz: W 476 H 396
Strip1: X 1496 Y 647
Strip2: X 1634 Y 1040
rcSelect Loc: X 1015 Y 639
rcSelect Siz: W 476 H 396
Strip1: X 1524 Y 822
Strip2: X 1634 Y 778
rcSelect Loc: X 1043 Y 822
rcSelect Siz: W 476 H 396
Strip1: X 1419 Y 692
Strip2: X 1634 Y 557
rcSelect Loc: X 938 Y 156
rcSelect Siz: W 476 H 396
Strip1: X 1169 Y 692
Strip2: X 1634 Y 144
rcSelect Loc: X 688 Y -257
rcSelect Siz: W 476 H 396
Strip1: X 1318 Y 692
Strip2: X 1634 Y 290
rcSelect Loc: X 837 Y -111
rcSelect Siz: W 476 H 396
Strip1: X 1304 Y 692
Strip2: X 1634 Y 449
rcSelect Loc: X 823 Y 48
rcSelect Siz: W 476 H 396
Strip1: X 1326 Y 692
Strip2: X 1634 Y 581
rcSelect Loc: X 845 Y 180
rcSelect Siz: W 476 H 396
Strip1: X 1305 Y 692
Strip2: X 1634 Y 508
rcSelect Loc: X 824 Y 107
rcSelect Siz: W 476 H 396
Strip1: X 1302 Y 692
Strip2: X 1634 Y 471
rcSelect Loc: X 821 Y 70
rcSelect Siz: W 476 H 396

enter image description here

最佳答案

我不知道哪个ToolStrip是垂直的还是水平的,不过你可以这样试试:

protected override void OnMouseDown(MouseEventArgs e) {
  base.OnMouseDown(e);
  rcSelect = new Rectangle(e.Location, Size.Empty);
}

protected override void OnMouseUp(MouseEventArgs e) {
  base.OnMouseUp(e);
  int rcLeft = Math.Min(e.X, rcSelect.X);
  int rcTop = Math.Min(e.Y, rcSelect.Y);
  int rcWidth = Math.Max(e.X, rcSelect.Left) - Math.Min(e.X, rcSelect.Left);
  int rcHeight = Math.Max(e.Y, rcSelect.Top) - Math.Min(e.Y, rcSelect.Top);
  rcSelect = new Rectangle(new Point(rcLeft, rcTop), new Size(rcWidth, rcHeight));

  toolStrip1.Location = new Point(
    Math.Max(0, 
      Math.Min(this.ClientSize.Width - toolStrip1.Width,
               rcSelect.Right - toolStrip1.Width)
    ),
    rcSelect.Bottom > this.ClientSize.Height ?
      rcSelect.Top - toolStrip1.Height :
      rcSelect.Bottom
  );

  toolStrip2.Location = new Point(
    rcSelect.Right > this.ClientSize.Width ?
      rcSelect.Left - toolStrip2.Width :
      rcSelect.Right,
    Math.Max(0, 
      Math.Min(this.ClientSize.Height - toolStrip2.Height,
               rcSelect.Bottom - toolStrip2.Height)
    )
  );
}

关于c# - 在矩形周围放置和粘贴控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59584644/

相关文章:

c# - 在没有点网框架的情况下安装 C# 应用程序

c# - 将 .NET 3.5 程序反编译为 C#,导出问题

c# - 上传的文件未显示在项目解决方案 (Mvc5) 中

c# - "mysqli_fetch_array"是否有等效的 C# 函数?

python - 为什么4个按钮不显示?

c# - 使用Soap Headers处理错误

c# - Properties.Settings.Default.Save();不在用户机器上保存

c# - GZip header 中的魔数(Magic Number)不正确。确保您传递的是 GZip 流

r - 嵌套 mfrow 和布局

java - 如何减少 java.awt.font 中的字母间距?