自定义控件不是绘制 Windows 窗体按钮



我有一个自定义控件,我正在尝试显示一个System.Windows.Forms.Button。

下面是"绘制时"事件。这确实会被调用,并且消息框显示正确的值。位置是1,1。大小为 75,23,可见为真。

public partial class CustomControl : Control
{
    public CustomControl()
    {
        InitializeComponent();
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics graphics = e.Graphics;
        Rectangle rect = e.ClipRectangle;
        // Other code here.
        OptionsBtn.Refresh(); // I tried Invalidate and Update. Neither of them worked.
        //MessageBox.Show(OptionsBtn.Location.ToString() + "n" + OptionsBtn.Size.ToString() + "n" + OptionsBtn.Visible.ToString());
    } 
}

下面是组件的初始化。

partial class CustomControl
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }
    #region Component Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.OptionsBtn = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // OptionsBtn
        // 
        this.OptionsBtn.BackColor = System.Drawing.Color.Blue;
        this.OptionsBtn.ForeColor = System.Drawing.Color.Red;
        this.OptionsBtn.Location = new System.Drawing.Point(1, 1);
        this.OptionsBtn.Name = "Options";
        this.OptionsBtn.Size = new System.Drawing.Size(75, 23);
        this.OptionsBtn.TabIndex = 0;
        this.OptionsBtn.Text = "Options";
        this.OptionsBtn.UseVisualStyleBackColor = false;
        this.ResumeLayout(false);
    }
    #endregion
    private System.Windows.Forms.Button OptionsBtn;
}

代码的主要问题是,尽管您创建了OptionBtn按钮,但从未将其添加到CustomControl中。

private void InitializeComponent()
{
    this.OptionsBtn = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // OptionsBtn
    // 
    this.OptionsBtn.BackColor = System.Drawing.Color.Blue;
    this.OptionsBtn.ForeColor = System.Drawing.Color.Red;
    this.OptionsBtn.Location = new System.Drawing.Point(1, 1);
    this.OptionsBtn.Name = "Options";
    this.OptionsBtn.Size = new System.Drawing.Size(75, 23);
    this.OptionsBtn.TabIndex = 0;
    this.OptionsBtn.Text = "Options";
    this.OptionsBtn.UseVisualStyleBackColor = false;
    this.Controls.Add(this.OptionsBtn);
    this.ResumeLayout(false);
}

我添加了这一行:

this.Controls.Add(this.OptionsBtn);

只有这样,按钮才属于CustomControl

然后,您必须将CustomControl添加到某些Form。类似的东西(假设你也想在代码中加入这一位):

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        CustomControl cc = new CustomControl();
        cc.Location = new Point(100, 100);
        cc.Size = new Size(300, 300);
        this.Controls.Add(cc);
    }
}

最新更新