获取代码如下
this.ClientRectangle.Width,
this.ClientRectangle.Height
例程详细代码
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 WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen pen = new Pen(Color.FromArgb(255, 0, 0), 30);
pen.Color = Color.Red;
//g.DrawLine(pen, 10, 10, this.Width - 150, this. Height - 150);
//g.DrawRectangle
//int windowBorder = (this.Width - this.ClientRectangle.Width) / 2;
//Rectangle rf = new Rectangle(10, 10, this.Width-50, this.Height - 50);
Rectangle rf = new Rectangle(0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height);
for ( int i = 0; i < 80; i+=10 )
{
//pen.Color = Color.FromArgb(i*5, 0, 0);
g.DrawRectangle(pen, RectREF(rf, (float)i));
}
//g.DrawRectangle(pen, RectREF(rf, 10f));
pen.Color = Color.Blue;
g.DrawRectangle(pen, rf);
}
private Rectangle RectREF(Rectangle InRect, float per)
{
//RectangleF OutRet;
//var Left = InRect.Left + (InRect.Width - InRect.Left) * per/100f;
//var Top = InRect.Top + (InRect.Height - InRect.Top) * per / 100f;
//var Width = InRect.Width * (1-per / 100f)-Left;
//var Height = InRect.Height * (1-per / 100f)-Top;
var Left = InRect.Left + (InRect.Width) * per / 100f;
var Top = InRect.Top + (InRect.Height) * per / 100f;
var Width = InRect.Width * (1 - 2 * per / 100f);// - Left;
var Height = InRect.Height * (1 - 2 * per / 100f);// -Top;
return new Rectangle((int)Left, (int)Top, (int)Width, (int)Height);
}
private void Form1_Load(object sender, EventArgs e)
{
//处理闪烁问题
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
}
}
}
运行结果如下图
参考链接
(25条消息) C# winform 获取标题栏,状态栏,菜单栏的高度_huangwenhua5000的博客-CSDN博客_c# 句柄 标题栏高度
特此记录
anlog
2022年12月24日