解決したいこと
・下記の画像の通り、メニューバーを作成したのですが画面遷移をする際に、遷移後のFormが開くことによって生じる画面のちらつきをなくしたいです。
ちらつきをなくしたいのですが、なるべくコードは、基底クラスでメニューバー生成処理を記述して、そのメニューバーを派生クラスで利用する形にしたいです。
自分で試してみたこととして、DoubleBufferedを有効にするためにコンストラクタに、「this.DoubleBuffered = true;」や「SetStyle(ControlStyles.OptimizedDoubleBuffer, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true);」を追加してみたのですが、画面のちらつきはなくなりませんでした。何か良い方法があればご教授いただけると幸いです。
コードは下記の通りとなります。
c#
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 WindowsFormsApp2 { // 基底Formクラス public partial class FormBase : Form { private Label menuberLbl; // メニューバー表示に利用するラベル public static int LBL_CNT = 4; // メニューバーで表示するラベルの数 private Panel menuPnl; // メニューバー生成時にラベルを張り付けるためのパネル public FormBase() { InitializeComponent(); // 全画面表示 this.WindowState = FormWindowState.Maximized; // メニューバー作成処理 // パネル生成処理 menuPnl = new Panel(); menuPnl.Location = new Point(90,50); menuPnl.Size = new Size(240, 30); menuPnl.BackColor = Color.FromArgb(255, 255, 255); this.Controls.Add(menuPnl); // ラベル生成処理 List<Label> lblList = new List<Label>(); for (int i = 0; i < LBL_CNT; i++) { menuberLbl = new Label(); menuberLbl.Name = "lbl" + (i+1); menuberLbl.Font = new Font("MS UI Gothic", float.Parse("9.75"), FontStyle.Bold); menuberLbl.TextAlign = ContentAlignment.MiddleLeft; menuberLbl.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; menuberLbl.AutoSize = false; menuberLbl.Size = new Size(60, 30); menuberLbl.Location = new Point(1+(60 * i), 1); menuberLbl.BackColor = Color.FromArgb(255, 255, 255); menuberLbl.ForeColor = Color.FromArgb(0, 0, 0); menuberLbl.Text = "メニュー" + (i + 1); menuberLbl.Click += new EventHandler(sideLbl_Click); lblList.Add(menuberLbl); } foreach (Label lbl in lblList) { menuPnl.Controls.Add(lbl); menuberLbl.BringToFront(); } } // 画面遷移 private void sideLbl_Click(object sender, EventArgs e) { if (((Label)sender).Name == "lbl1") { Form1 f1 = new Form1(); f1.Show(); this.Visible = false; } else if (((Label)sender).Name == "lbl2") { Form1 f1 = new Form1(); f1.Show(); this.Visible = false; } else if (((Label)sender).Name == "lbl3") { Form1 f1 = new Form1(); f1.Show(); this.Visible = false; } else if (((Label)sender).Name == "lbl4") { Form1 f1 = new Form1(); f1.Show(); this.Visible = false; } } } }
c#
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 WindowsFormsApp2 { public partial class Form1 : FormBase { public Form1() { InitializeComponent(); } } }
お手数をおかけしますがどうぞよろしくお願いいたします。
このコードだとラベルをクリックするたびに Form1 がどんどん生成されて、画面を閉じても終了できないんですが、その辺はどう考えてますか?
返信が遅くなってしまい申し訳ございません。
重要なご指摘ありがとうございます。
ご指摘の通り、画面を閉じても終了しない点については考慮が欠けていました。
自分でも調べてみて下記のように画面遷移のコードを変更してみました。
下記のコードで考慮が足りない場合は、ご指摘いただけると幸いです。
if (((Label)sender).Name == "lbl1")
{
Form1 f1 = new Form1();
f1.Owner = this;
f1.Show();
this.Hide();
f1.Owner.Show();
f1.Close();
}
それだと f1 のインスタンスを立ち上げて、一瞬表示した後閉じてるので意味がありません。
Runメソッドの引数を変更するようにコードを修正してみようと思います。
まだ回答がついていません
会員登録して回答してみよう