Files
lserp_cs_6.0/插件库/Lskj.Main/Control/BackgroundPanel.cs
T
cyf 8c8b515664 SVN r1224
SVN-Revision: r1224
2026-06-22 03:11:46 +00:00

49 lines
1.6 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Lskj.Main.Control
{
public partial class BackgroundPanel :Panel
{
private Image _originalBackgroundImage;
// 公开属性:设置/获取背景图片
public Image BackgroundImageCustom
{
get => _originalBackgroundImage;
set
{
_originalBackgroundImage = value;
Invalidate(); // 设置图片后立即重绘
}
}
public BackgroundPanel()
{
// 关键设置:关闭Panel默认背景,启用双缓冲减少闪烁
this.BackgroundImage = null;
this.DoubleBuffered = true;
this.SetStyle(ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.OptimizedDoubleBuffer, true);
}
// 重写OnPaint方法,手动绘制背景图
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// 图片为空时直接返回
if (_originalBackgroundImage == null) return;
// 1. 计算等比缩放比例(以Panel的ClientSize为基准)
float scaleX = (float)this.ClientSize.Width / _originalBackgroundImage.Width;
float scaleY = (float)this.ClientSize.Height / _originalBackgroundImage.Height;
float scale = Math.Min(scaleX, sc