簡體   English   中英

如何創建自定義屬性,例如在C#中的按鈕上添加圖像?

[英]How to create a custom property like adding image on a button in c#?

我正在使用自定義UI編輯器。 編輯器允許我們將背景圖像放置在按鈕上。 但是,與VS編輯器不同,我們無法放置前景圖像。

要在屬性欄中添加屬性,我們只需添加以下代碼。

        [Browsable(true), Category("Custom controls"), Description("Background Image of Control")]
        //[EditorAttribute(typeof(UIFileNameEditor), typeof(System.Drawing.Design.UITypeEditor))]
        //public UIBitmap UIBackImage
        public UIImageRes UIBackImage
        {
            get { return backImage;  }
            set { backImage = value; Invalidate(); }
        }

現在,要添加背景圖像,我們有以下代碼

        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.TextRenderingHint =
            System.Drawing.Text.TextRenderingHint.AntiAlias;
            e.Graphics.InterpolationMode =
                System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
            e.Graphics.PixelOffsetMode =
                System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
            e.Graphics.SmoothingMode =
                System.Drawing.Drawing2D.SmoothingMode.HighQuality;


            GraphicsUnit units = GraphicsUnit.Pixel;
            if (this.Is9Slice == UISlice.no && this.UIBackImage.UIImagePath != null  && this.UIBackImage.UIImagePath !="" )
            {

                Bitmap BgI = new Bitmap(this.backImage.UIImagePath);
                Rectangle dest = new Rectangle(0, 0, this.Width, this.Height);
                Rectangle src = Rectangle.Empty;
                if (backImage.UIUVRectangle.Width * backImage.UIUVRectangle.Height != 0)
                {
                    src = backImage.UIUVRectangle;
                }
                else
                {
                    src= new Rectangle(0, 0, BgI.Width, BgI.Height);
                }
                e.Graphics.DrawImage(BgI, dest, src, units);
                return;
            }
            if (this.UIBackImage.UIImagePath != null  && this.UIBackImage.UIImagePath != "")
            {
                Bitmap BgImage = new Bitmap(this.backImage.UIImagePath);
                Size BgI = new Size(UIBackImage.UIUVRectangle.Width, UIBackImage.UIUVRectangle.Height);
                Point Offset = new Point(UIBackImage.UIUVRectangle.X, UIBackImage.UIUVRectangle.Y);


                Rectangle dTopLeft, dTopCenter, dTopRight,   //Destination Rectangles
                          dMidLeft, dMidCenter, dMidRight,
                          dBottomLeft, dBottomCenter, dBottomRight;

                Rectangle sTopLeft, sTopCenter, sTopRight,   //Source Rectangles
                          sMidLeft, sMidCenter, sMidRight,
                          sBottomLeft, sBottomCenter, sBottomRight;

                int side_control;
                int side = side_control = (int)(BgI.Width < BgI.Height ? BgI.Width * 0.2f: BgI.Height * 0.2f);


                if (2 * side > this.Size.Width || 2 * side > this.Size.Height)
                    side_control = (int)(this.Width < this.Height ? this.Width / 2 : this.Height / 2);

                if (designScaleFactor != 1.0f)
                    side_control *= (int)designScaleFactor;


                dTopLeft = dTopCenter = dTopRight = dMidLeft = dMidCenter = dMidRight = dBottomLeft = dBottomCenter = dBottomRight = new Rectangle();
                sTopLeft = sTopCenter = sTopRight = sMidLeft = sMidCenter = sMidRight = sBottomLeft = sBottomCenter = sBottomRight = new Rectangle();

                /*  Top Left */
                dTopLeft.Location = new Point(0, 0);
                dTopLeft.Width = dTopLeft.Height = side_control;
                sTopLeft.Location = new Point(0 + Offset.X, 0 + Offset.Y ); // Just add the offset here for reflecting in every other rectangle
                sTopLeft.Width = sTopLeft.Height = side;

                /* Top Center */
                dTopCenter.Location = new Point(dTopLeft.Right, dTopLeft.Top);
                dTopCenter.Width = this.Width - 2 * side_control;
                dTopCenter.Height = dTopLeft.Height;
                sTopCenter.Location = new Point(sTopLeft.Right, sTopLeft.Top);
                sTopCenter.Width = BgI.Width - 2 * side;
                sTopCenter.Height = sTopLeft.Height;

                /* Top Right */
                dTopRight.Location = new Point(dTopCenter.Right, dTopCenter.Top);
                dTopRight.Width = dTopRight.Height = side_control;
                sTopRight.Location = new Point(sTopCenter.Right, sTopCenter.Top);
                sTopRight.Width = sTopRight.Height = side;

                /* Middle Top */
                dMidLeft.Location = new Point(dTopLeft.Left, dTopLeft.Bottom);
                dMidLeft.Width = dTopLeft.Width;
                dMidLeft.Height = this.Height - 2 * side_control;
                sMidLeft.Location = new Point(sTopLeft.Left , sTopLeft.Bottom );
                sMidLeft.Width = sTopLeft.Width;
                sMidLeft.Height = BgI.Height - 2 * side;

                /* Middle Center */
                dMidCenter.Location = new Point(dMidLeft.Right, dMidLeft.Top);
                dMidCenter.Width = this.Size.Width - 2 * side_control;
                dMidCenter.Height = this.Size.Height - 2 * side_control;
                sMidCenter.Location = new Point(sMidLeft.Right , sMidLeft.Top);
                sMidCenter.Width = BgI.Width - 2 * side;
                sMidCenter.Height = BgI.Height - 2 * side;

                /* Middle Right */
                dMidRight.Location = new Point(dMidCenter.Right, dMidCenter.Top);
                dMidRight.Width = side_control;
                dMidRight.Height = this.Size.Height - 2 * side_control;
                sMidRight.Location = new Point(sMidCenter.Right , sMidCenter.Top );
                sMidRight.Width = side;
                sMidRight.Height = BgI.Height - 2 * side;

                /* Bottom Left*/
                dBottomLeft.Location = new Point(dMidLeft.Left, dMidLeft.Bottom);
                dBottomLeft.Width = dBottomLeft.Height = side_control;
                sBottomLeft.Location = new Point(sMidLeft.Left , sMidLeft.Bottom );
                sBottomLeft.Width = sBottomLeft.Height = side;

                /* Bottom Center */
                dBottomCenter.Location = new Point(dBottomLeft.Right, dBottomLeft.Top);
                dBottomCenter.Width = this.Width - 2 * side_control;
                dBottomCenter.Height = side_control;
                sBottomCenter.Location = new Point(sBottomLeft.Right , sBottomLeft.Top );
                sBottomCenter.Width = BgI.Width - 2 * side;
                sBottomCenter.Height = side;

                /* Bottom Right */
                dBottomRight.Location = new Point(dBottomCenter.Right, dBottomCenter.Top);
                dBottomRight.Width = dBottomRight.Height = side_control;
                sBottomRight.Location = new Point(sBottomCenter.Right , sBottomCenter.Top );
                sBottomRight.Width = sBottomRight.Height = side;

                Image BG = (Image)BgImage;

                e.Graphics.DrawImage(BG, dTopLeft, sTopLeft, units);
                e.Graphics.DrawImage(BG, dTopCenter, sTopCenter, units);
                e.Graphics.DrawImage(BG, dTopRight, sTopRight, units);

                e.Graphics.DrawImage(BG, dMidLeft, sMidLeft, units);
                e.Graphics.DrawImage(BG, dMidCenter, sMidCenter, units);
                e.Graphics.DrawImage(BG, dMidRight, sMidRight, units);

                e.Graphics.DrawImage(BG, dBottomLeft, sBottomLeft, units);
                e.Graphics.DrawImage(BG, dBottomCenter, sBottomCenter, units);
                e.Graphics.DrawImage(BG, dBottomRight, sBottomRight, units);

            }

        }

現在,我的問題是,還需要重寫或編寫哪些方法以獲取前景圖像?

您將需要一個前景圖像屬性。 因此,以您的背景圖片屬性為例,它可能看起來像這樣:

[Browsable(true), Category("Custom controls"), Description("Foreground Image of Control")]
public UIImageRes UIForeImage
{
    get { return foreImage;  }
    set { foreImage = value; Invalidate(); }
}

您需要將添加foreImage變量在相同的地方backImage (我猜這些都是私有類變量)。

然后,應在OnPaint方法中添加一些部分以繪制前景圖像。 同樣,您應該查看背景圖像的繪制方式,並執行類似的操作。 繪制事物的順序很重要,因此確保在背景繪制之后進行任何前景繪制(將前景事物繪制在頂部)非常重要。 還要注意,如果前景圖像是不透明的,並且與背景圖像大小相同,那么您將看不到背景圖像,因為您將在背景圖像上進行繪制。

據我所知,您似乎需要將以下部分添加到OnPaint方法中:

if (this.Is9Slice == UISlice.no && this.UIForeImage.UIImagePath != null  && this.UIForeImage.UIImagePath !="" )
{
    // Add foreground drawing code here
}

if (this.UIForeImage.UIImagePath != null  && this.UIForeImage.UIImagePath != "")
{
    // Add foreground drawing code here
}

這可能是重構OnPaint方法的好時機。 您可以為PaintBackgroundImage(Graphics g)PaintForegroundImage(Graphics g)創建單獨的方法。 或者,您甚至可以只使用一種可以繪制背景圖像和前景圖像的方法,例如PaintImage(Graphics g, UIImageRes image) 然后,您可以從OnPaint方法中調用這些方法。

暫無
暫無

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

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