m.qayyum
m.qayyum

Reputation: 411

Placing and sticking control around a rectangle

I'm developing a small snipping tool, which works but I just can't get my head around how I place two toolstrip controls around selected snip region (rectangle). I refresh the toolstrip location on mouse-up event, which handles the movements for both toolstrips like this.

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

This is how it looks at start. enter image description here

And this is how it looks when i resize and move that rectangle enter image description here


Debug

Verticle one is toolstrip1 and horizontal is 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


Debug 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

Upvotes: 3

Views: 314

Answers (2)

Marc
Marc

Reputation: 19

Are you basing the location of the toolstrips on the size or the coordinates of your rectangle object? (rcSelect). The bug is most likely to do with the mishmash of coordinates and sizes being used as delimiters for how your objects are moved around on the display. I have two recommendations:

  1. Your object rcSelect could be re-instantiated once the user releases their click to redraw your ToolStrips in the correct location. The downside to this method would be that your toolstrips would no longer 'follow' your rectangle but instead be redrawn once the user has made their selection. If not to re-instantiate I would recommend a process that redraws toolstrips on user mouse release.
  2. Use a corner of the rectangle as a constant anchor point. Your toolstrip would follow rcSelect based on its co-ordinates alone. To avoid being drawn off screen you could modify which corner is being used as the anchor. This changes your options of movement to four set places.

You said that you run the 're-draw' process on mouse up, but it appears that you have set up your X and Y equations wrong. Notice that your vertical toolstrip appears to maintain the correct X co-ordinate but can not correct it's Y. However your horizontal toolstrip mutually presents the same error but reversed.

Take a look at your equation and let us know if you fix anything :)

Upvotes: 0

LarsTech
LarsTech

Reputation: 81610

I don't know which ToolStrip is vertical or horizontal, but you can try it this way:

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

Upvotes: 1

Related Questions