如何使按钮的位置和大小随图片框大小而变化 [英] How do I make location and size of button change with picture box size

查看:132
本文介绍了如何使按钮的位置和大小随图片框大小而变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我有一个窗口形式的图片框,我在图片框上放了很多按钮。我已经完成了通过滑动鼠标中键来改变图片大小,但我不知道如何让所有按钮一起改变带有图片框,包括按钮的位置和大小。谁能帮助我。



我尝试过:



我试图解决这个问题。 vs.net2013中的presend代码如下:



Now I have a picturebox in a window form,and I placed many buttons on the picturebox.I have accomplished of changing the picture size by sliding the Mouse middle button,but I don't know how to make all buttons change together with picturebox,including button's location and size. Who can give me a helping hand.

What I have tried:

I tried to solve the problem. The presend codes in vs.net2013 are as follows:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ImageScale
{
    public partial class Form1 : Form
    {
        public Form1() {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            this.pictureBox1.BorderStyle = BorderStyle.FixedSingle;
            this.pictureBox1.BackColor = Color.DarkGray;
            this.pictureBox1.MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel);
          
        }

        Bitmap m_bmp;             
        Point m_ptCanvas;          
        Point m_ptCanvasBuf;        
        Point m_ptBmp;              
        float m_nScale = 1.0F;     

        Point m_ptMouseDown;        

        string m_strMousePt;        

        private void Form1_Load(object sender, EventArgs e) {
         
            m_bmp = Properties.Resources.QQ20170722225306;
           
            m_ptCanvas = new Point(pictureBox1.Width / 2, pictureBox1.Height / 2);
            m_ptBmp = new Point(-(m_bmp.Width / 2), -(m_bmp.Height / 2));
           
        }
       
       
        }
        
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

        }
        private void pictureBox1_Paint(object sender, PaintEventArgs e) {
            Graphics g = e.Graphics;
            g.TranslateTransform(m_ptCanvas.X, m_ptCanvas.Y);      
            g.ScaleTransform(m_nScale, m_nScale);                   
            g.DrawImage(m_bmp, m_ptBmp);                          
           
            g.ResetTransform();                                     

        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e) {
            if (e.Button == MouseButtons.Middle) {     
                m_ptMouseDown = e.Location;
                m_ptCanvasBuf = m_ptCanvas;
            }
            pictureBox1.Focus();
        }
     
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e) {
            if (e.Button == MouseButtons.Middle) {     
              
                m_ptCanvas = (Point)((Size)m_ptCanvasBuf + ((Size)e.Location - (Size)m_ptMouseDown));
                pictureBox1.Invalidate();
            }
           
        }
       
        private void pictureBox1_MouseWheel(object sender, MouseEventArgs e) {
            if (m_nScale <= 0.3 && e.Delta <= 0) return;       
            if (m_nScale >= 4.9 && e.Delta >= 0) return;       
          
            SizeF szSub = (Size)m_ptCanvas - (Size)e.Location;
            
            float tempX = szSub.Width / m_nScale;          
            float tempY = szSub.Height / m_nScale;         
          
            m_ptCanvas.X -= (int)(szSub.Width - tempX);     
            m_ptCanvas.Y -= (int)(szSub.Height - tempY);    
                               
            szSub.Width = tempX;
            szSub.Height = tempY;
            m_nScale += e.Delta > 0 ? 0.2F : -0.2F;
            m_ptCanvas.X += (int)(szSub.Width * m_nScale - szSub.Width);
            m_ptCanvas.Y += (int)(szSub.Height * m_nScale - szSub.Height);
            pictureBox1.Invalidate();
          
        }

推荐答案

使用Anchor和Dock属性管理WinForm控件 - TechRepublic [ ^ ]


这篇关于如何使按钮的位置和大小随图片框大小而变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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