C# 围绕矩形放置和粘贴控件

C# 围绕矩形放置和粘贴控件,c#,winforms,graphics,C#,Winforms,Graphics,我正在开发一个小的剪贴工具,它可以工作,但我就是不知道如何在选定的剪贴区域(矩形)周围放置两个toolstrip控件。我在鼠标向上事件中刷新toolstrip位置,它像这样处理两个toolstrip的移动 //The two toolstrips that I have //rcSelect is the selected rectangle toolStrip1.Visible = true; toolStrip2.Visible = true; //Tool Strip One int

我正在开发一个小的剪贴工具,它可以工作,但我就是不知道如何在选定的剪贴区域(矩形)周围放置两个toolstrip控件。我在鼠标向上事件中刷新toolstrip位置,它像这样处理两个toolstrip的移动

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


调试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

我不知道哪个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)
    )
  );
}

工具条的位置是基于矩形对象的大小还是坐标?(
rcSelect
)。这个错误最有可能与坐标和大小的混乱有关,这些坐标和大小被用作分隔符,用于确定对象在显示器上的移动方式。 我有两项建议:

  • 一旦用户在正确位置释放单击以重新绘制工具条,您的对象
    rcSelect
    就可以重新实例化。这种方法的缺点是,工具条不再“跟随”矩形,而是在用户做出选择后重新绘制。如果不重新实例化,我建议在用户鼠标释放时重新绘制toolstrips
  • 使用矩形的一个角点作为常量定位点。您的toolstrip将仅基于其坐标跟随
    rcSelect
    。为了避免脱离屏幕,您可以修改将哪个角用作锚点。这会将移动选项更改为四个设置位置
  • 您说您在鼠标悬停时运行“重新绘制”过程,但似乎您的X和Y方程设置错误。请注意,您的垂直工具条似乎保持正确的
    X
    坐标,但无法纠正它的
    Y
    。但是,您的水平toolstrip相互呈现相同的错误,但相反


    请查看您的等式,并告知我们您是否解决了任何问题:)

    第一幅图像和第二幅图像中rcSelect的位置和大小是多少?对不起,不确定位置,但矩形上方提到了大小。不确定?看来这非常重要。显示确定rcSelect值的代码。请检查调试2否,我们需要查看设置rcSelect位置和维度的代码。谢谢,我切换了工具名称,如下所示。但我的代码也会根据snip所在的位置更改striptool的位置。例如,当snip位于屏幕右侧和右下角时,@m.qayyum会更新代码以适应屏幕外的问题。主要思想就在这里。
    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)
        )
      );
    }