按钮和矩形大小与预期不符 [英] button and rectangle size are not as expected

查看:73
本文介绍了按钮和矩形大小与预期不符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Rectangle tmprect,然后用它绘制了一个矩形和一个按钮.
他们两个都有相同的大小.但实际上按钮的输出显示的尺寸比矩形小得多.我不知道为什么?有人可以给我提示吗?
这里的单位对他们来说是否不同?

//绘制一个矩形
graphics.DrawRectangle(tmppen,tmprect.X,tmprect.Y,tmprect.Width,tmprect.Height);

//添加一个按钮
Button btn = new Button();
btn.Location =新点(tmprect.X,tmprect.Y);
btn.Size = new Size(tmprect.Width,tmprect.Height);
this.Controls.Add(btn);

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

推荐答案

我认为这可能与获取图形对象的方式有关.使用paint事件参数中的图形对象与使用this.CreateGraphics具有不同的结果.如果不为应用程序启用视觉样式,则结果是相同的.

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.

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



       }


这篇关于按钮和矩形大小与预期不符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆