0

I have two Windows forms in a project: a normal rectangular one called frm1 and a shaped windows form called frm2. In frm1, there is a button named themeBtn which controls the visibility of frm2. Now I want to make frm2 always show itself at the same position(below the themeBtn) of the frm1(frm1 can be moved but it's size is fixed)). How can make it?

Below is the code of the shape for frm2:

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
    System.Drawing.Drawing2D.GraphicsPath shape = new System.Drawing.Drawing2D.GraphicsPath();
    Point point1 = new Point(0, 12);
    Point point2 = new Point(133, 12);
    Point point3 = new Point(141, 0);
    Point point4 = new Point(149, 12);
    Point point5 = new Point(282, 12);
    Point point6 = new Point(282, 238);
    Point point7 = new Point(0, 238);
    Point[] curvePoints =
             {
         point1,
         point2,
         point3,
         point4,
         point5,
         point6,
         point7
     };

    shape.AddPolygon(curvePoints);
    this.Region = new System.Drawing.Region(shape);
}

I tried the following code in vain:

   protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            Point p = new Point();
            p = Properties.Settings.Default.themeBtnLocation;
            System.Drawing.Drawing2D.GraphicsPath shape = new System.Drawing.Drawing2D.GraphicsPath();
            Point point1 = new Point(0, p.Y+12);
            Point point2 = new Point(p.X-8, p.Y+12);
            Point point3 = new Point(p.X, p.Y);
            Point point4 = new Point(p.X+8, p.Y+12);
            Point point5 = new Point(2*p.X, p.Y+12);
            Point point6 = new Point(2*p.X, s.Height);//height of frm2
            Point point7 = new Point(0, s.Height);
            Point[] curvePoints =
                     {
                 point1,
                 point2,
                 point3,
                 point4,
                 point5,
                 point6,
                 point7
             };

            shape.AddPolygon(curvePoints);
            this.Region = new System.Drawing.Region(shape);
        }

What I try to make is thmem select in lots of software.See the picture below

enter image description here

When I click the theme button, a new custom shape was showed exactly below the button regardless the main form is moving or not.)

  • 1
    That code is horribly wrong, never do anything in OnPaint that requires the window to be repainted. Like setting the Region property. Doing this will cause the UI thread to burn 100% core. It belongs in the constructor, perhaps in OnLoad or OnResize if you want to make the shape fit the window. The Location and ClientSize properties still matter, you have to make it big enough so the shape doesn't get clipped. And you move it in the right place by setting Location. – Hans Passant Apr 30 '17 at 08:41
  • Thanks. I have edited my question to make a little bit clearer. Do you mean I should set the location of frm2 in LocationChanged events of frm1? – Alan Ackart Apr 30 '17 at 08:56
  • 1
    Yes. Do note that the ToolTip class has the OwnerDraw property. And already looks like this without OwnerDraw. – Hans Passant Apr 30 '17 at 08:59
  • Thanks. I want to show a new custom shaped form not a few sentence when I click the button, so the UI is very different. Maybe my English is not so good to description it clearly to you. I have upload the ui design to github [link](https://github.com/alanackart/Temp/blob/master/Capture.JPG), Can you check it? Basically, I want to make the same thing.(When I click the theme button, a new custom shape was showed exactly below the button regardless the main form is moving or not.). Again, thank for your patients to answer my questions. – Alan Ackart Apr 30 '17 at 09:37

1 Answers1

0

I have solve the issue in the following code:

frm8.Location = themeButton.PointToScreen(new Point(Point.Empty.X-(int)(0.680*frm8.Size.Width), Point.Empty.Y+24));

see reference

The above code is better than the following code by which the form would change its position a little bit in accordance with the exact position being clicked.

this.Cursor = new Cursor(Cursor.Current.Handle);
frm8.Location = new Point(Cursor.Position.X - (int)(0.726 * frm8.Size.Width),
   Cursor.Position.Y+15); 
Community
  • 1
  • 1