向winforms tabcontrol添加自定义控件时出现的问题

向winforms tabcontrol添加自定义控件时出现的问题,winforms,runtime,tabcontrol,zooming,pan,Winforms,Runtime,Tabcontrol,Zooming,Pan,我在运行时动态地将可缩放的自定义图片框控件添加到tabcontrol.tabpage时遇到问题。我试了很多,想知道你们这些聪明的家伙是否能给我这样的穷家伙一些建议。。。这里有一些代码 using Microsoft.VisualBasic; using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Diagnostics; using Syst

我在运行时动态地将可缩放的自定义图片框控件添加到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
}

最后一点是。。。pictureBox实现也运行良好,因此我知道我正在使用intDwg类的反序列化方法从磁盘中很好地提取图像。任何想法都将不胜感激!提前谢谢

更新:

通过将backgroundimage设置为位图而不是picBox,我获得了加载图片的控件。。。。令人沮丧的。。。。但我设置图像的方式似乎没有正确定位。。。正在努力改进并解决这个问题。。。任何提示和技巧都会非常棒!谢谢

更新:

屏幕截图。。。正如您可以看到的那样,选项卡页面已正确加载,并且“我的收藏”中的每个位图都有一个选项卡页面,但是自定义zoomPanPicBox控件似乎不希望显示!见下文:

啊。。。。似乎我没有代表来张贴照片。。。好吧,那

更新再次得到它感谢所有丢失设置的大小,正如你提到的使用以下:picBox.SetBounds(0,0,300,300)


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

另外,不要使用计数器:

    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);

你需要给你的“picbox”一个尺寸()!你是说picbox而不是picbox。。。正如我所决定的,我宁愿使用自定义控件实现,而不是windows PictureBox实现。PictureBox实现目前运行良好。。。我正在尝试实现zoomPanPicBox…我的字面意思是“picBox”,因为这是变量的名称,类型为zoomPanPicBox。你没有给它一个尺码。如果你能显示一个屏幕截图,说明它是如何工作的,这会很有帮助……据我所知,ZoomPanPicBox没有大小的成员变量。。。。它继承了一个可滚动控件,我找到了一种获取size.width和size.height的方法。。。但是还没有找到一种方法来设置它们。。。让我拍一张屏幕快照。。。谢谢你的帮助:DTHANKS Idle_Mind我想代表+,但我的是 TabPage myTabPage = new TabPage(title); myTabPage.Controls.Add(picBox); tabControl1.TabPages.Add(myTabPage);