我正在处理 C# windows 窗体,但无法重新居中 Picturebox 图像。
我要做的是查看显示器的分辨率并将表单设置为全屏,然后将图片框设置为表单内的全屏。
Windowsstate 在设计器中设置为最大化,FormBorderstyle 在设计器中设置为无。
void Monitorresolution()
{
// grabs the resolution of the monitor
Screen screen = Screen.PrimaryScreen;
screenWidth = screen.Bounds.Width;
screenHeight = screen.Bounds.Height;
//MessageBox.Show("height = " + screenWidth + "/n" + "Width = " + screenHeight);
// grabs the resolution of the monitor
// sets the size of the window of Pictureviewer
this.ClientSize = new Size(screenWidth, screenHeight);
// sets the size of the window of Pictureviewer
// sets the size of the picturebox
pictureBox1.Size = new Size(screenWidth, screenHeight);
// sets the size of the picturebox
// sets the size of the image inside picturebox
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
// sets the size of the image inside picturebox
// center the image
pictureBox1.Location = new Point((pictureBox1.Parent.ClientSize.Width / 2) - (pictureBox1.Width / 2), (pictureBox1.Parent.ClientSize.Height / 2) - (pictureBox1.Height / 2));
// center the image
// anchors the picturebox
pictureBox1.Anchor = AnchorStyles.None;
// MessageBox.Show("WTF");
pictureBox1.Refresh();
}
除非我删除任何消息框语句的注释,否则图像将偏离中心。打开消息框后,图像完美居中。
为什么会发生这种情况,我该怎么做才能解决它。
谢谢安迪
更新
我正在展示更多代码,以便更好地了解我在做什么以及为什么 pictureBox1.Dock = DockStyle.Fill; 在这种情况下对我不起作用。
我对代码进行了一些重新排序,并将调整大小的代码放在了 form_load 代码中。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Image_Viewer
{
public partial class imageViewer : Form
{
System.Drawing.Point mouseDown;
int newWidth = 0;
int newHeight = 0;
int newX = 0;
int newY = 0;
int screenWidth;
int screenHeight;
public imageViewer()
{
InitializeComponent();
var frm2 = new exitform();
frm2.FormClosed += (o, e) => this.Close();
frm2.Show();
}
private void imageViewer_FormClosed(object sender, FormClosedEventArgs e)
{
}
private void imageViewer_Load(object sender, EventArgs e)
{
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = "Open Image";
dlg.Filter = "JPG|*.jpg;*.jpeg|PNG|*.png|TIFF|*.tif;*.tiff|BMP|*.bmp|GIF|*.gif|" + "All Graphics Types|*.bmp;*.jpg;*.jpeg;*.png;*.tif;*.tiff";
if (dlg.ShowDialog() == DialogResult.OK)
{
using (var fs = new System.IO.FileStream(dlg.FileName, System.IO.FileMode.Open))
{
var bmp = new Bitmap(fs);
pictureBox1.Image = bmp;
Invalidate();
}
}
}
// grabs the resolution of the monitor
Screen screen = Screen.PrimaryScreen;
screenWidth = screen.Bounds.Width;
screenHeight = screen.Bounds.Height;
// MessageBox.Show("height = " + screenHeight + "\n" + "Width = " + screenWidth);
// grabs the resolution of the monitor
// sets the size of the window of Pictureviewer
this.ClientSize = new Size(screenWidth, screenHeight);
// sets the size of the window of Pictureviewer
pictureBox1.Size = new Size(screenWidth, screenHeight);
pictureBox1.Location = new Point((ClientSize.Width / 2) - (pictureBox1.Width / 2), (ClientSize.Height / 2) - (pictureBox1.Height / 2));
// MessageBox.Show("When this message box pops, everything seems to work, no idea why though");
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
MouseEventArgs mouse = e as MouseEventArgs;
if (mouse.Button == MouseButtons.Left)
{
mouseDown = mouse.Location;
pictureBox1.Dock = DockStyle.None;
}
else if (mouse.Button == MouseButtons.Right)
{
// Do something else, not important in this example
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
MouseEventArgs mouse = e as MouseEventArgs;
if (mouse.Button == MouseButtons.Left)
{
// Pan functions
Point mousePosNow = mouse.Location;
int deltaX = mousePosNow.X - mouseDown.X;
int deltaY = mousePosNow.Y - mouseDown.Y;
int newX = pictureBox1.Location.X + deltaX;
int newY = pictureBox1.Location.Y + deltaY;
pictureBox1.Location = new Point(newX, newY);
}
else if (mouse.Button == MouseButtons.Right)
{
this.Close();
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
MouseEventArgs mouse = e as MouseEventArgs;
if (mouse.Button == MouseButtons.Left)
{
Point mousePosNow = mouse.Location;
int deltaX = mousePosNow.X - mouseDown.X;
int deltaY = mousePosNow.Y - mouseDown.Y;
int newX = pictureBox1.Location.X + deltaX;
int newY = pictureBox1.Location.Y + deltaY;
pictureBox1.Location = new Point(newX, newY);
}
}
protected override void OnMouseWheel(MouseEventArgs e)//zoom function
{
if (e.Delta > 0)
{
newWidth = pictureBox1.Size.Width + (pictureBox1.Size.Width / 10);
newHeight = pictureBox1.Size.Height + (pictureBox1.Size.Height / 10);
newX = pictureBox1.Location.X - ((pictureBox1.Size.Width / 10) / 2);
newY = pictureBox1.Location.Y - ((pictureBox1.Size.Height / 10) / 2);
}
else if (e.Delta < 0)
{
newWidth = pictureBox1.Size.Width - (pictureBox1.Size.Width / 10);
newHeight = pictureBox1.Size.Height - (pictureBox1.Size.Height / 10);
newX = pictureBox1.Location.X + ((pictureBox1.Size.Width / 10) / 2);
newY = pictureBox1.Location.Y + ((pictureBox1.Size.Height / 10) / 2);
}
pictureBox1.Size = new Size(newWidth, newHeight);
pictureBox1.Location = new Point(newX, newY);
}
}
}
表格 2 使用以下代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Image_Viewer
{
public partial class exitform : Form
{
private const int CP_NOCLOSE_BUTTON = 0x200;
public exitform()
{
InitializeComponent();
}
protected override CreateParams CreateParams
{
get
{
CreateParams myCp = base.CreateParams;
myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON;
return myCp;
}
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
我只显示表单 2 的代码只是为了显示退出按钮。我将它显示在表单 1 的顶部,并从两者中删除关闭按钮。
AppDeveloper 我确实尝试了您发布的代码,但是在我的情况下它不起作用,我将以较小的形式重试以进行测试。我觉得奇怪的是它出于某种原因在消息框之后起作用。
谢谢安迪