Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a Rectangle tmprect,then with it, I have drawn a rectangle as well as a button.
both of them have a same size. but actually the output of the button shows much smaller size than rectangle. I do not know why? Anyone can give me a hint?
whether here the unit are different for them?

//draw a rectangle
graphics.DrawRectangle(tmppen, tmprect.X, tmprect.Y, tmprect.Width, tmprect.Height);

//add a button
Button btn = new Button();
btn.Location = new Point(tmprect.X, tmprect.Y);
btn.Size = new Size(tmprect.Width, tmprect.Height);
this.Controls.Add(btn);
Posted
Comments
[no name] 9-Jul-12 16:48pm    
I suspect that the difference in size is due to the size of the width of the pen that you are using to draw the rectangle.
Seraph_summer 9-Jul-12 17:07pm    
no, pen's width is independent on rect size.
[no name] 9-Jul-12 17:17pm    
Define "much smaller size than rectangle".... In my test code, the button was exactly smaller than the rectangle by the width of the pen. So you are using a pen to draw the rectangle are you not?
Seraph_summer 9-Jul-12 17:22pm    
I do not understand what you mean, you mean if I change the widht of the pen, then rectangle's size will be changed? no, I do not think so, the pen width only change the line width and not the rectangle's width and height, right?
[no name] 9-Jul-12 17:28pm    
No and I never said that. The pen and rectangle are 2 different things.

1 solution

I think it might have something to do with how you are getting your graphics object. Using the graphics object from the paint eventargs has different results than using this.CreateGraphics. If you do NOT enable visual styles for the application then the results are the same.

C#
[STAThread]
static void Main()
{
    //Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}


just by enabling/disabling visual styles you can see a difference

private void Form1_Paint(object sender, PaintEventArgs e)
       {

           //second graphics option
           System.Drawing.Graphics graphics = this.CreateGraphics();


           System.Drawing.Pen tmppen = new Pen(new SolidBrush(Color.Red), 2);

           System.Drawing.Rectangle tmprect = new Rectangle(5, 5, 125, 25);
           System.Drawing.Rectangle tmprect2 = new Rectangle(150, 5, 125, 25);

           //draw a rectangle
           e.Graphics.PageUnit = GraphicsUnit.Display;

           e.Graphics.DrawRectangle(tmppen, tmprect.X, tmprect.Y, tmprect.Width, tmprect.Height);
           graphics.DrawRectangle(tmppen, tmprect2.X, tmprect2.Y, tmprect2.Width, tmprect2.Height);

           //add a button
           Button btn = new Button();
           btn.Location = new Point(tmprect.X, tmprect.Y);
           btn.Size = new Size(tmprect.Width, tmprect.Height);
           btn.Text = e.Graphics.PageUnit.ToString() + e.Graphics.PageScale.ToString() + " " + e.Graphics.DpiX + "x" + e.Graphics.DpiY;
           this.Controls.Add(btn);

           Button btn2 = new Button();
           btn2.Location = new Point(tmprect2.X, tmprect2.Y);
           btn2.Size = new Size(tmprect2.Width, tmprect2.Height);
           btn2.Text = graphics.PageUnit.ToString() + graphics.PageScale.ToString() + " " + graphics.DpiX + "x" + graphics.DpiY;
           this.Controls.Add(btn2);



       }
 
Share this answer
 
Comments
Seraph_summer 10-Jul-12 13:52pm    
thanks, actually, I found out the reason is the e.Graphics.PageUnit.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900