Graphics 使用一点点注意
Form_Load 事件下绘制的结果会被 paint 刷新掉.也就等于没有绘制一样.
Graphics g = this .CreateGraphics(); g.DrawRectangle( new Pen(Brushes.Blue, 2), new Rectangle(0, 0, ClientRectangle.Width / 2, ClientRectangle.Height / 2)); g.DrawRectangle( new Pen(Color.Red, 2), new Rectangle(0, ClientRectangle.Height / 2, ClientRectangle.Width / 2, ClientRectangle.Height / 2)); g.DrawRectangle( new Pen(Color.Yellow, 2), new Rectangle(ClientRectangle.Width / 2, 0, ClientRectangle.Width / 2, ClientRectangle.Height / 2)); g.DrawRectangle( new Pen(Brushes.Green, 2), new Rectangle(ClientRectangle.Width / 2, ClientRectangle.Height / 2, ClientRectangle.Width / 2, ClientRectangle.Height / 2)); |
这段代码可以放在很多事件里执行.除了LOAD.
此外,今天使用的背景控件是 tableLayoutPanel1 但它没有触发 Enter 事件.最终用 tableLayoutPanel1_MouseEnter 事件解决.
全部代码如下
using System; using System.Drawing; using System.Windows.Forms; namespace SimpleCustorApplication { public class Form1 : Form { private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel(); this.SuspendLayout(); // // tableLayoutPanel1 // this.tableLayoutPanel1.ColumnCount = 2; this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F)); this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F)); this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill; this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0); this.tableLayoutPanel1.Name = "tableLayoutPanel1"; this.tableLayoutPanel1.RowCount = 2; this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F)); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F)); this.tableLayoutPanel1.Size = new System.Drawing.Size(292, 273); this.tableLayoutPanel1.TabIndex = 0; this.tableLayoutPanel1.Paint += new System.Windows.Forms.PaintEventHandler(this.tableLayoutPanel1_Paint); this.tableLayoutPanel1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.tableLayoutPanel1_MouseMove); this.tableLayoutPanel1.MouseEnter += new System.EventHandler(this.tableLayoutPanel1_MouseEnter); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.tableLayoutPanel1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion public Form1() { InitializeComponent(); } private void tableLayoutPanel1_MouseMove(object sender, MouseEventArgs e) { Point p = new Point(e.X, e.Y); if (new Rectangle(0, 0, ClientRectangle.Width / 2, ClientRectangle.Height / 2).Contains(p)) this.Cursor = Cursors.Cross; else if (new Rectangle(0, ClientRectangle.Height / 2, ClientRectangle.Width / 2, ClientRectangle.Height / 2).Contains(p)) this.Cursor = Cursors.Hand; else if (new Rectangle(ClientRectangle.Width / 2, 0, ClientRectangle.Width / 2, ClientRectangle.Height / 2).Contains(p)) this.Cursor = Cursors.VSplit; else if (new Rectangle(ClientRectangle.Width / 2, ClientRectangle.Height / 2, ClientRectangle.Width / 2, ClientRectangle.Height / 2).Contains(p)) this.Cursor = Cursors.UpArrow; else if (new Rectangle(0, 0, ClientRectangle.Width / 2, ClientRectangle.Height / 2).Contains(p)) this.Cursor = Cursors.Default; } private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(Brushes.Yellow, new Rectangle(0, 0, ClientRectangle.Width / 2, ClientRectangle.Height / 2)); g.FillRectangle(Brushes.Green, new Rectangle(0, ClientRectangle.Height / 2, ClientRectangle.Width / 2, ClientRectangle.Height / 2)); g.FillRectangle(Brushes.Red, new Rectangle(ClientRectangle.Width / 2, 0, ClientRectangle.Width / 2, ClientRectangle.Height / 2)); g.FillRectangle(Brushes.Blue, new Rectangle(ClientRectangle.Width / 2, ClientRectangle.Height / 2, ClientRectangle.Width / 2, ClientRectangle.Height / 2)); } private void tableLayoutPanel1_MouseEnter(object sender, EventArgs e) { Graphics g = tableLayoutPanel1.CreateGraphics(); g.DrawRectangle(new Pen(Brushes.Blue, 2), new Rectangle(0, 0, ClientRectangle.Width / 2, ClientRectangle.Height / 2)); g.DrawRectangle(new Pen(Color.Red, 2), new Rectangle(0, ClientRectangle.Height / 2, ClientRectangle.Width / 2, ClientRectangle.Height / 2)); g.DrawRectangle(new Pen(Color.Yellow, 2), new Rectangle(ClientRectangle.Width / 2, 0, ClientRectangle.Width / 2, ClientRectangle.Height / 2)); g.DrawRectangle(new Pen(Brushes.Green, 2), new Rectangle(ClientRectangle.Width / 2, ClientRectangle.Height / 2, ClientRectangle.Width / 2, ClientRectangle.Height / 2)); } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!