0

プログラミングを学ぼうとしていて、多少実験しています。解決策を見つけることができなかった小さな問題に遭遇しました。プログラムで作成されたボタンと、プログラムで新しいフォームを作成するクリック イベントがあります。フォームには、いくつかの作成されたラベルとカスタム境界線があります。同じカスタム境界線がメイン フォームで使用され、自動サイズがオンになっていて、サイズが変更される動的に作成されたコンテンツでも機能します。しかし、このプログラムで作成されたフォームでは、境界線は左側と上部にしか表示されず、答えを見つけることも、実験によって解決することもできませんでした.

十分なコードの量はわかりませんが、問題を示す簡単なプログラムを作成しました。これは、プログラムで作成された数百のボタンと、myForm フォームで使用されるラベルを埋めるいくつかの情報の配列を使用する私のプログラムのコードから単純化されたものであり、配列とボタンの作成プロセスを削除したため、いくつかの点が奇妙に見えるかもしれません。

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace StackoverflowSample
{
public partial class Form1 : Form
{
    private Button myButton;

    /// <summary>
    /// Borrowed code snippet for custom border: http://stackoverflow.com/q/5092216. Would have preferred no to do dllimport yet, but will let it pass this time.
    /// </summary>
    /// <param name="nLeftRect"></param>
    /// <param name="nTopRect"></param>
    /// <param name="nRightRect"></param>
    /// <param name="nBottomRect"></param>
    /// <param name="nWidthEllipse"></param>
    /// <param name="nHeightEllipse"></param>
    /// <returns></returns>
    [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
    private static extern IntPtr CreateRoundRectRgn
    (
        int nLeftRect, // x-coordinate of upper-left corner
        int nTopRect, // y-coordinate of upper-left corner
        int nRightRect, // x-coordinate of lower-right corner
        int nBottomRect, // y-coordinate of lower-right corner
        int nWidthEllipse, // height of ellipse
        int nHeightEllipse // width of ellipse
     );

    public Form1()
    {
        InitializeComponent();

        this.BackColor = Color.White;
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

        Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, 0, 0, 0, 0)); // Since our form is dynamically created and/or filled and auto sized, set starting values here to 0.
        Region.MakeInfinite(); // This one was my own, to solve size problems with my dynamically created forms auto size, further reading required for other ways to do it.
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Font font = new System.Drawing.Font("Meiryo UI", 16.0f);

        myButton = new Button();
        myButton.Name = "tstButton";
        myButton.Size = new Size(50, 50);
        myButton.Location = new Point(10, 10);
        myButton.Font = font;
        myButton.Text = "Test Button";
        myButton.Click += (sender2, e2) => { ButtonClick(sender, e); };

        this.Controls.Add(myButton);
    }

    private void ButtonClick(object sender, EventArgs e)
    {
        Font font = new System.Drawing.Font("Meiryo UI", 16.0f);

        Button myButton = (sender as Button);

        Form myForm = new Form();
        Label row1Label = new Label();
        Label row2Label = new Label();
        Label row3Label = new Label();
        Label row4Label = new Label();
        Label row5Label = new Label();

        row2Label.AutoSize = true;
        row2Label.Text = "Row 1: ";
        row2Label.Font = font;
        row2Label.Location = new Point(5, 5);

        row1Label.AutoSize = true;
        row1Label.Text = "Row 2: ";
        row1Label.Font = font;
        row1Label.Location = new Point(5, 35);

        row3Label.AutoSize = true;
        row3Label.Text = "Row 3: ";
        row3Label.Font = font;
        row3Label.Location = new Point(5, 65);

        row4Label.AutoSize = true;
        row4Label.Text = "Row 4: ";
        row4Label.Font = font;
        row4Label.Location = new Point(5, 95);

        row5Label.AutoSize = true;
        row5Label.Text = "Row 5: ";
        row5Label.Font = font;
        row5Label.Location = new Point(5, 125);

        myForm.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
        myForm.AutoSize = true;
        myForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

        myForm.Controls.Add(row1Label);
        myForm.Controls.Add(row2Label);
        myForm.Controls.Add(row3Label);
        myForm.Controls.Add(row4Label);
        myForm.Controls.Add(row5Label);

        myForm.Font = font;
        myForm.BackColor = Color.White;
        myForm.Padding = new System.Windows.Forms.Padding(10, 10, 10, 10);
        myForm.Show();
        myForm.Paint += (sender4, e4) => { Form1_Paint(sender4, e4); }; // Hijacking the borrowed code for custom borders again for the info boxes.
        myForm.Location = new Point(Cursor.Position.X + 25, Cursor.Position.Y - 100); // Create info box a little to the right and up from the cursors position.
        myForm.LostFocus += (sender3, e3) => { CloseForm(sender3, e3, myForm); }; // If the info box loses focus, for example by clicking another button, close that box (form)
        myForm.MouseEnter += (sender3, e3) => { CloseForm(sender3, e3, myForm); }; // Also, if the mouse enters the box, also close, so we can show the buttons under it.
    }

    /// <summary>
    /// Event for closing my "custom" info boxes. Runs at LostFocus or MouseEnter events. So, clicking another button or entering the info box will close it.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <param name="myForm">Gets the relevant form that the event will run on. Solved my problem of targeting dynamically created forms from outside its scope.</param>
    private void CloseForm(object sender, EventArgs e, Form myForm)
    {
        myForm.Close();
    }

    /// <summary>
    /// Paint event. Holds some borrowed code for getting custom border. Works so far, but further reading required.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        // Borrowed code snippet for custom border: http://stackoverflow.com/q/5092216
        ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle,
            Color.LightBlue, 5, ButtonBorderStyle.Solid,
            Color.LightBlue, 5, ButtonBorderStyle.Solid,
            Color.LightBlue, 5, ButtonBorderStyle.Solid,
            Color.LightBlue, 5, ButtonBorderStyle.Solid);
    }
}
}

私はプログラミングの初心者なので、悪いコーディングを非難するのではなく、ある程度寛容になり、正しい方法を示してください。

敬具、

マティアス

4

2 に答える 2

3

問題は、両方のフォームに同じイベント ハンドラーを使用していることです。その方法を続けたい場合はClientRectangle、Pain イベント ハンドラが呼び出されたフォームに応じて のサイズを変更する必要があります (現在は常にClientSizeメイン フォームの を使用しています)。Form1_Paintイベント ハンドラのコードを次のように変更します。

private void Form1_Paint(object sender, PaintEventArgs e)
{
        Form frm = (Form)sender;
        ControlPaint.DrawBorder(e.Graphics, frm.ClientRectangle,
        Color.LightBlue, 5, ButtonBorderStyle.Solid,
        Color.LightBlue, 5, ButtonBorderStyle.Solid,
        Color.LightBlue, 5, ButtonBorderStyle.Solid,
        Color.LightBlue, 5, ButtonBorderStyle.Solid);
}

以前のコードでClientRectangleは、子フォームの再描画の場合にメイン フォームの を渡していました。コードを変更すると、DrawBorderメソッドはClientRectangle呼び出されたフォームに適したものを取得します。

于 2013-03-23T11:29:17.777 に答える
0

コントロールによってオーバーライドされる「ClientRectangle」を使用しているため、左/右/上/下のコントロールドックがある場合、davidovicのソリューションには副作用があります。

境界線を再描画する通常の解決策は 2 つあります。

  1. フォームの下に別のフォームを配置しますが、フォームの破棄の問題が発生し、メモリ不足になる可能性があります

  2. 非クライアント領域のウィンドウ メッセージをキャッチし、境界線を再描画します。しかし、これは非常に注意が必要であり、多くの問題を引き起こす可能性があります。たとえば、フォームが最小化されている場合、フォームのサイズが変更されている場合、一部のフォーム領域が非表示になっている場合、このイベントを処理する必要があります。

方法 2 を使用する場合は、Lizard.dll を参照することをお勧めします。これにはまだバグがありますが、他の種類のライブラリよりも安定しています。

于 2015-09-17T04:50:07.203 に答える