0

我无法在运行时动态地将自定义可缩放和可平移图片框控件添加到 tabcontrol.tabpage。我已经尝试了很多,想知道你们中的任何聪明人是否会对像我这样的可怜的菜鸟有一些建议……这里有一些代码……

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace TAQTv4
{
    public class ZoomPanPicBox : ScrollableControl
    {
        private Image _image;
        //Double buffer the control
        public ZoomPanPicBox()
        {
            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);
            this.AutoScroll = true;
            this.Image = null;
            this.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            this.Zoom = 1f;
        }
        //New
        [Category("Appearance"), Description("The image to be displayed")]
        public Image Image
        {
            get { return _image; }
            set
            {
                _image = value;
                UpdateScaleFactor();
                Invalidate();
            }
        }
        private float _zoom = 1f;
        [Category("Appearance"), Description("The zoom factor. Less than 1 to reduce. More than 1 to magnify.")]
        public float Zoom
        {
            get { return _zoom; }
            set
            {
                if (value < 0 || value < 1E-05)
                {
                    value = 1E-05f;
                }
                _zoom = value;
                UpdateScaleFactor();
                Invalidate();
            }
        }
        private void UpdateScaleFactor()
        {
            if (_image == null)
            {
                this.AutoScrollMargin = this.Size;
            }
            else
            {
                this.AutoScrollMinSize = new Size(Convert.ToInt32(this._image.Width * _zoom + 0.5f), Convert.ToInt32(this._image.Height * _zoom + 0.5f));
            }
        }
        //UpdateScaleFactor
        private InterpolationMode _interpolationMode = InterpolationMode.High;
        [Category("Appearance"), Description("The interpolation mode used to smooth the drawing")]
        public InterpolationMode InterpolationMode
        {
            get { return _interpolationMode; }
            set { _interpolationMode = value; }
        }
        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
        }
        //OnPaintBackground
        protected override void OnPaint(PaintEventArgs e)
        {
            //if no image, don't bother. I tried check for IsNothing(_image) but this test wasn't detecting a no-image.
            if (_image == null)
            {
                base.OnPaintBackground(e);
                return;
            }
            //Added because the first test sometimes failed
            try
            {
                int H = _image.Height;
                //Throws an exception if image is nothing.
            }
            catch (Exception ex)
            {
                base.OnPaintBackground(e);
                return;
            }
            //Set up a zoom matrix
            Matrix mx = new Matrix(_zoom, 0, 0, _zoom, 0, 0);
            mx.Translate(this.AutoScrollPosition.X / _zoom, this.AutoScrollPosition.Y / _zoom);
            e.Graphics.Transform = mx;
            e.Graphics.InterpolationMode = _interpolationMode;
            e.Graphics.DrawImage(_image, new Rectangle(0, 0, this._image.Width, this._image.Height), 0, 0, _image.Width, _image.Height, GraphicsUnit.Pixel);
            base.OnPaint(e);
        }
        //OnPaint
    }
}
//ZoomPicBox

现在这在使用设计器时似乎工作正常......但是当尝试在运行时添加图像和控件时,选项卡实例化很好,但 zoomPicBox 控件什么也不做,所以看起来......这就是我使用它的方式...... .

public void loadImagesToTabControl()
{
    int i = 0;


    foreach (Bitmap bitmap in intDwg.getBitmaps())
    {
        //ToDo add pic boxes and tabs and bitmaps to tabcontrol1

        TAQTv4.ZoomPanPicBox picBox = new TAQTv4.ZoomPanPicBox();
        picBox.Image = bitmap;

        picBox.Anchor = AnchorStyles.Top;
        picBox.Anchor = AnchorStyles.Bottom;
        picBox.Anchor = AnchorStyles.Left;
        picBox.Anchor = AnchorStyles.Right;
        picBox.AutoScroll = true;
        picBox.CausesValidation = true;
        picBox.Visible = true;
        picBox.Zoom = 1;
        picBox.BackgroundImageLayout = ImageLayout.Tile;
        picBox.Location = new System.Drawing.Point(0, 0);
        picBox.TabStop = true;
        picBox.Enabled = true;
        picBox.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
        picBox.CreateControl();





        string title = "Pg " + (tabControl1.TabCount + 1).ToString();
        TabPage myTabPage = new TabPage(title);
        tabControl1.TabPages.Add(myTabPage);
        tabControl1.TabPages[i].Controls.Add(picBox);
        i++;



        /* Possible pictureBox Implementation...
        string title = "Pg " + (tabControl1.TabCount + 1).ToString();
        TabPage myTabPage = new TabPage(title);
        tabControl1.TabPages.Add(myTabPage);
        PictureBox picbox = new PictureBox();
        picbox.Anchor = AnchorStyles.Top;
        picbox.Anchor = AnchorStyles.Bottom;
        picbox.Anchor = AnchorStyles.Left;
        picbox.Anchor = AnchorStyles.Right;
        picbox.Image = bitmap;
        picbox.Height = 800;
        picbox.Width = 1300;
        tabControl1.TabPages[i].Controls.Add(picbox);                
        i++;
        */
    }
}

}

最后一点...... pictureBox 实现也很好,所以我知道我在我的 intDwg 类的反序列化方法中从磁盘中提取我的图像很好。任何想法将不胜感激!提前致谢!

更新:

我通过将 backgroundimage 设置为位图而不是 picBox.Image 来控制加载图片....令人沮丧....但似乎我设置图像的方式没有正确锚定...试图改善这一点现在就解决它......任何提示和技巧都会很棒!谢谢!

更新:

屏幕截图...您可以看到标签页正确加载,并且我的收藏中的每个位图都有一个,但自定义 zoomPanPicBox 控件似乎不想显示!见波纹管:

啊....似乎我没有代表发布照片.... ...好吧怎么样...

https://www.dropbox.com/s/ogj5jlcce831n3p/scrst.png?v=0mcns

...

再次更新,谢谢大家都没有像你提到的那样设置大小:picBox.SetBounds(0, 0, 300, 300);

:D:D:D:D:D:D:)

4

1 回答 1

0

另外,不要使用计数器:

    TabPage myTabPage = new TabPage(title);
    tabControl1.TabPages.Add(myTabPage);
    tabControl1.TabPages[i].Controls.Add(picBox);
    i++;

只需使用您的“myTabPage”参考:

    TabPage myTabPage = new TabPage(title);
    myTabPage.Controls.Add(picBox);
    tabControl1.TabPages.Add(myTabPage);
于 2013-05-30T04:34:45.133 回答