簡體   English   中英

Picturebox不在窗體調整大小的中心

[英]Picturebox not Centering on Form Resize

我正在使用C#Windows窗體,但無法重新定位Picturebox圖像的中心。

我要做的是查看顯示器的分辨率,將表格設置為全屏,然后將圖片框設置為表格中的全屏。

在設計器中Windowstate設置為“最大化”,在Designer中將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我確實嘗試過您發布的代碼,但是在我的情況下它不起作用,出於測試目的,我將以較小的形式重試。 我發現奇怪的是,由於某些原因,它在消息框后仍然有效。

謝謝安迪

您應將pictureBox1位置相對於其托管的Form (即,父控件/表單)居中。

我不確定為什么要將圖像相對於PrimaryScreen居中

使pictureBox居中的更好方法是訂閱Form.Resize事件並動態調整pixtureBox1位置。

private void Form1_Load(object sender, EventArgs e)
{
    this.Resize += Form1_Resize;
}

private void Form1_Resize(object sender, EventArgs e)
{
    pictureBox1.Left = (this.Width - pictureBox1.Width)/2;
    pictureBox1.Top = (this.Height - pictureBox1.Height)/2;
}

如我所見,您正在設置圖片框的大小=表單的大小。 就您而言,最好設置

pictureBox1.Dock = DockStyle.Fill;

並忘記設置大小/適當調整大小:)

我更喜歡C ++ CLI,但是下一個示例應該很清楚。

    Void MyForm::SetFullScreen(Boolean value)
    {
        if (value == FullScreen)
            return;

        if (value)
        {
            // DO NOT REORDER!!!
            this->LastWindowState = WindowState;                
            this->WindowState = FormWindowState::Normal;
            this->LastWindowBounds = Bounds;
            this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::None;                              
            this->Bounds = Screen::PrimaryScreen->Bounds;
        }
        else
        {
            // DO NOT REORDER!!!
            this->WindowState = LastWindowState;
            this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::Sizable;
            this->Bounds = LastWindowBounds;                    
        }

         Int32 NewX = (pictureBox1.Parent.ClientSize.Width / 2) - (pictureBox1.Width / 2);
         Int32 NewY = (pictureBox1.Parent.ClientSize.Height / 2) - (pictureBox1.Height / 2);
         pictureBox1.Location = new Point(NewX, NewY);
         pictureBox1.Anchor = AnchorStyles.None; 

        // finally save the actual state
        FullScreen = value;
    }

我終於找到了問題所在

我想念碼頭的風格。 如果我將其插入

pictureBox1.Location = new Point((ClientSize.Width / 2) - (pictureBox1.Width / 2), (ClientSize.Height / 2) - (pictureBox1.Height / 2));

pictureBox1.Dock = DockStyle.Top;

似乎每次都能完美運行。

感謝所有為我看過這個的人。

安迪

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM