添加一个按钮size(385,263),运行的到的按钮size却跟设置的不一样!不知道是我错了还是什么问题?求大神帮忙看看。 下面附上代码 ,代码中鼠标坐标为控件右下角坐标 添
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
label1.Text = string.Format("鼠标屏幕坐标系({0},{1})", Cursor.Position.X, Cursor.Position.Y);
label2.Text = string.Format("鼠标窗体坐标系({0},{1})", e.X ,e.Y );
label3.Text = string.Format("按钮Width({0})", this.button1.Right - this.button1.Left);
label4.Text = string.Format("按钮Heigth({0})", this.button1.Bottom - this.button1.Top);
}
}
测试了你的代码没问题,和size一样
你没设置button的anchor为top,bottom,left,right吧,这个改变窗体大小会缩放控件
你可以先用界面的一些属性修改 或者使用代码在内部进行修改
WPF中的控件是跟着分辨率变化而变化的,如果你想要固定大小和固定位置的按钮,则必须设置按钮的相对位置。和绝对的大小。
出现你这种情况是因为你按钮的大小也设置了离边距的大小,当窗口变大时,则按钮也跟着相应的变化或缩小
private void setControls(float newx, float newy, Control cons)
{
foreach (Control con in cons.Controls)
{
string[] mytag = con.Tag.ToString().Split(new char[] { ':' });
float a = Convert.ToSingle(mytag[0]) * newx;
con.Width = (int)a;
a = Convert.ToSingle(mytag[1]) * newy;
con.Height = (int)(a);
a = Convert.ToSingle(mytag[2]) * newx;
con.Left = (int)(a);
a = Convert.ToSingle(mytag[3]) * newy;
con.Top = (int)(a);
Single currentSize = Convert.ToSingle(mytag[4]) * newy;
con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
if (con.Controls.Count > 0)
{
setControls(newx, newy, con);
}
}
}
看下你系统的DPI设置,我的就是笔记本的DPI高,台式机的DPI低,同一个程序在不同系统DPI下运行时控件大小会不一样
size(385,263)这个值指的是控件的大小把,不是他的位置,设置他的位置可以用Margin这个属性,一楼说的anchor属性是指按钮距离上下左右的距离,如选择了top,就是指离界面上边框的距离是保持不变的。
应该是按钮字体大小和窗体字体大小不一致的问题