トップ回答者
自作枠描画透明ユーザコントロール、サイズ大きくした際に、描画範囲の問題

質問
-
環境VS2017、C#
透明のユーザコントロールに枠を描画機能を実装しました。コントロール右下の摘みでコントロールのサイズを調整できるようにしました。が、Formデザイン時のサイズを超えると、超えた部分の描画が消えてしまいます。 原因が分からなくで、どうすればいいかは分かりません。教えていただきたいです。
Button1に、緑枠付き透明のコントロールを配置しています。青色の摘みで枠サイズを調整できます。
枠のサイズが、デザイン時コントロールのサイズを超えると、超えた部分を表示できなくなってしまいます。
プログラムコードです。
private Pen PenGreen = new Pen(Color.Lime, 2); private SolidBrush BrushBlue = new SolidBrush(Color.Blue); private SolidBrush BrushRed = new SolidBrush(Color.Red); private bool flg_Editabled = false; private bool flg_Move = false; private Graphics myGraphics; private bool flg_SizeChange = false; private Point m_lstPoint; //マウス位置1 private Point m_endPoint; //マウス位置2 private void TransFrame_Load(object sender, EventArgs e) { this.PenGreen.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom; PenGreen.DashPattern = new float[] { 5, 5 }; myGraphics = this.CreateGraphics(); } //枠描画 private void TransFrame_Paint(object sender, PaintEventArgs e) { Rectangle rect = new Rectangle(5, 5, this.Width - 10, this.Height - 10); myGraphics.DrawRectangle(PenGreen, rect); if (flg_Editabled) { if(flg_SizeChange) myGraphics.FillRectangle(BrushRed, this.Width - 10, this.Height - 10, 10, 10); else myGraphics.FillRectangle(BrushBlue, this.Width - 10, this.Height - 10, 10, 10); } } //マウスボタン押し private void TransFrame_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { if (e.X > (this.Width - 10) && e.Y > (this.Height - 10)) { if (this.flg_Editabled) { this.flg_SizeChange = true; //this.CallPaint(); this.Refresh(); } else { this.flg_Move = true; } } else { this.flg_Move = true; } this.m_lstPoint.X = e.X; this.m_lstPoint.Y = e.Y; } } //マウス移動 private void TransFrame_MouseMove(object sender, MouseEventArgs e) { if (this.flg_Editabled && this.flg_SizeChange) { this.m_endPoint.X = e.X - this.m_lstPoint.X; this.m_endPoint.Y = e.Y - this.m_lstPoint.Y; this.m_lstPoint.X = e.X; this.m_lstPoint.Y = e.Y; Size size = new Size(); size.Width = this.Width + this.m_endPoint.X; size.Height = this.Height + this.m_endPoint.Y; this.Size = size; this.Refresh(); } if (this.flg_Move) { Point Loc = new Point(); Loc.X = e.X - this.m_lstPoint.X + this.Location.X; Loc.Y = e.Y - this.m_lstPoint.Y + this.Location.Y; this.Location = Loc; this.Refresh(); } } //マウスボタン上げ private void TransFrame_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { if (this.flg_SizeChange) { this.flg_SizeChange = false; this.Parent.Focus(); this.Refresh(); } if (this.flg_Move) this.flg_Move = false; } } //マウスボタンダブルクリック private void TransFrame_MouseDoubleClick(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { this.flg_Editabled = !this.flg_Editabled; this.Refresh(); } }
cyo
回答
-
LoadでCreateGraphics()した時点のGraphics領域を使いまわしているせいです。
Paintイベントのe.Graphicsで描画しましょう。private void TransFrame_Load(object sender, EventArgs e) { this.PenGreen.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom; PenGreen.DashPattern = new float[] { 5, 5 }; //myGraphics = this.CreateGraphics(); } //枠描画 private void TransFrame_Paint(object sender, PaintEventArgs e) { Rectangle rect = new Rectangle(5, 5, this.Width - 10, this.Height - 10); e.Graphics.DrawRectangle(PenGreen, rect); if (flg_Editabled) { if (flg_SizeChange) e.Graphics.FillRectangle(BrushRed, this.Width - 10, this.Height - 10, 10, 10); else e.Graphics.FillRectangle(BrushBlue, this.Width - 10, this.Height - 10, 10, 10); } }
個別に明示されていない限りgekkaがフォーラムに投稿したコードにはフォーラム使用条件に基づき「MICROSOFT LIMITED PUBLIC LICENSE」が適用されます。(かなり自由に使ってOK!)
- 回答としてマーク AK46.5 2019年6月17日 4:51
すべての返信
-
LoadでCreateGraphics()した時点のGraphics領域を使いまわしているせいです。
Paintイベントのe.Graphicsで描画しましょう。private void TransFrame_Load(object sender, EventArgs e) { this.PenGreen.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom; PenGreen.DashPattern = new float[] { 5, 5 }; //myGraphics = this.CreateGraphics(); } //枠描画 private void TransFrame_Paint(object sender, PaintEventArgs e) { Rectangle rect = new Rectangle(5, 5, this.Width - 10, this.Height - 10); e.Graphics.DrawRectangle(PenGreen, rect); if (flg_Editabled) { if (flg_SizeChange) e.Graphics.FillRectangle(BrushRed, this.Width - 10, this.Height - 10, 10, 10); else e.Graphics.FillRectangle(BrushBlue, this.Width - 10, this.Height - 10, 10, 10); } }
個別に明示されていない限りgekkaがフォーラムに投稿したコードにはフォーラム使用条件に基づき「MICROSOFT LIMITED PUBLIC LICENSE」が適用されます。(かなり自由に使ってOK!)
- 回答としてマーク AK46.5 2019年6月17日 4:51