簡體   English   中英

什么是,如何通過繪制矩形確定這種奇怪異常的原因?

[英]What is, and how do I determine the cause of this strange exception with drawing a Rectangle?

我正在嘗試在自定義控件的OnPaint方法中繪制一個Rectangle。 並不是我不知道如何,只是我這次試圖正確地做到這一點(而不是每次調用OnPaint時都創建一個新的Pen)。

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

namespace SomeNamespace
{
    public partial class Window : Form
    {
        #region Designer Properties
        [Browsable(true), DisplayName("FormBorderColor"), Category("Appearance")]
        public Color FormBorderColor { get; set; }

        [Browsable(true), DisplayName("FormBorderThickness"), Category("Appearance")]
        public float FormBorderThickness { get; set; }

        #endregion

        private Pen formBorderPen;
        private Brush formBorderBrush;
        private Rectangle formBorderRectangle;

        public Window()
        {
            InitializeComponent();

            this.DoubleBuffered = true;

            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);

            this.SetStyle(ControlStyles.ContainerControl, true);
            this.SetStyle(ControlStyles.Selectable, true);

            // Initialize Border properties
            formBorderBrush = new SolidBrush(FormBorderColor);
            formBorderPen = new Pen(formBorderBrush, FormBorderThickness);
            formBorderRectangle = new Rectangle(0, 0, this.Width - (int)FormBorderThickness, this.Height - (int)FormBorderThickness);
        }

        protected override void OnPaint(PaintEventArgs paint)
        {
            switch(this.FormBorderStyle)
            {
                // If this FormBorderStyle is set to None, we can either
                // draw our own custom border, or no border at all.
                case FormBorderStyle.None:
                    // Draw Form Border if necessary.
                    Console.WriteLine(FormBorderColor);
                    Console.WriteLine(FormBorderThickness);

                    Console.WriteLine(formBorderBrush);
                    Console.WriteLine(formBorderPen);
                    Console.WriteLine(formBorderRectangle);

                    if(FormBorderThickness >= 1)
                    {
                        Console.WriteLine("Hooray?");
                        paint.Graphics.DrawRectangle(formBorderPen, formBorderRectangle);
                    }
                    else
                    {
                        Console.WriteLine("Can't draw border.");
                    }
                    break;
                default:
                    break;
            }

            base.OnPaint(paint);
        }
    }
}

...但是,我遇到了一堆異常,我相信這是因為在期望設置某些值時未設置某些值。 但是我不知道還要在哪里設置這些值。

詳細說明拋出的異常

我將在這里采取黑暗的手段,並假設由於在構造函數中設置了這些對象的值而引發了這些異常,但是,我不知道在其他地方還要設置這些值。 從我在MSDN上看到的一切來看,我認為我在正確的位置設置了值。


CustomEndCap異常:

“ formBorderPen.CustomEndCap”引發了“ System.ArgumentException”類型的異常

CustomStartCap異常:

'formBorderPen.CustomStartCap'引發了類型'System.ArgumentException'的異常

DashPattern異常:

“ formBorderPen.DashPattern”引發了“ System.OutOfMemoryException”類型的異常

我不明白的是,為什么我會收到這些例外? 我什至沒有玩Caps或DashPatterns。 當我手動設置它們時,為什么筆和畫筆為空?


對於FormBorderThickness,您的筆大小可能為零,這將導致您看到的某些問題。

另外,如果您引用諸如Width或Height之類的屬性,則不應使用表單的構造函數,因為這些尺寸尚未完全確定。 您應該為此使用OnLoad覆蓋。

我個人將只創建這些筆並在Paint事件中將它們丟棄,將它們放在局部范圍內。 那個矩形也一樣。

您說對象的值是空的,但是您執行的Console.WriteLine顯示它們確實具有值..並且您顯示的異常似乎不是應用程序中的異常。 這些異常位於Visual Studio檢查器中,因為它試圖顯示對象的所有成員的值。某些成員可能尚未初始化或處於無效狀態,因此Visual Studio遇到異常。 。,但這對您的應用程序沒有影響,因為您沒有使用這些屬性。

暫無
暫無

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

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