上个星期看了卢彦关于<<使用Visual C#制作可伸缩个性化窗体>>的一文,于是准备试一试,写一个winform小程序.(注:以前只写asp.net)
代码如下
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Resources;
using System.Reflection ;
namespace WindowsApp
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
// private Image Bottom_Left;
// private Image Bottom_Right;
// private Image Bottom_Middle;
private Image Top_Middle;
private Image Top_Left;
private Image Top_Right;
private void LoadFormImage()
{
string curpath =Application.StartupPath.ToString();
this.Top_Left=Image.FromFile(curpath+"\\"+"LT.jpg");
this.Top_Middle=Image.FromFile(curpath+"\\"+"MT.jpg");
this.Top_Right=Image.FromFile(curpath+"\\"+"RT.jpg");
}
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);
LoadFormImage();
Graphics g = e.Graphics;
this.DrawTop_Left(g);
this.DrowTop_Middle(g);
this.DrowTop_Right(g);
}
private void DrawTop_Left(Graphics g)
{
Brush brush = new TextureBrush(Top_Left, new Rectangle(0, 0,Top_Left.Width, Top_Left.Height));
g.FillRectangle(brush, 0, 0, Top_Left.Width,Top_Left.Height );
}
private void DrowTop_Right(Graphics g)
{
Brush brush = new TextureBrush(Top_Right, new Rectangle(0, 0, Top_Right.Width, Top_Right.Height));
g.FillRectangle(brush, this.Width-Top_Right.Width, 0, this.Width,Top_Right.Height );
}
private void DrowTop_Middle(Graphics g)
{
Brush brush = new TextureBrush(Top_Middle, new Rectangle(0, 0, Top_Middle.Width , Top_Middle.Height));
g.FillRectangle(brush, this.Top_Left.Width, 0,this.Width- Top_Right.Width-Top_Left.Width,Top_Middle.Height );
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(325, 360);
this.ControlBox = false;
this.MaximizeBox = false;
this.Name = "Form1";
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}
当我将窗体默认size设为333, 368这个时绘出情况是这样的:就会发现DrowTop_Right绘制不正确.
当将窗体再调整大一些或小些的情况下又绘制正确..
![eer1.bmp]()
size:333, 368时出现的现象:其它情况也会出这样的情况
![eer2.bmp]()
这是所有用到几个图片:
![LT.jpg]()
![MT.jpg]()
![RT.jpg]()
代码如下
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Resources;
using System.Reflection ;
namespace WindowsApp
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
// private Image Bottom_Left;
// private Image Bottom_Right;
// private Image Bottom_Middle;
private Image Top_Middle;
private Image Top_Left;
private Image Top_Right;
private void LoadFormImage()
{
string curpath =Application.StartupPath.ToString();
this.Top_Left=Image.FromFile(curpath+"\\"+"LT.jpg");
this.Top_Middle=Image.FromFile(curpath+"\\"+"MT.jpg");
this.Top_Right=Image.FromFile(curpath+"\\"+"RT.jpg");
}
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);
LoadFormImage();
Graphics g = e.Graphics;
this.DrawTop_Left(g);
this.DrowTop_Middle(g);
this.DrowTop_Right(g);
}
private void DrawTop_Left(Graphics g)
{
Brush brush = new TextureBrush(Top_Left, new Rectangle(0, 0,Top_Left.Width, Top_Left.Height));
g.FillRectangle(brush, 0, 0, Top_Left.Width,Top_Left.Height );
}
private void DrowTop_Right(Graphics g)
{
Brush brush = new TextureBrush(Top_Right, new Rectangle(0, 0, Top_Right.Width, Top_Right.Height));
g.FillRectangle(brush, this.Width-Top_Right.Width, 0, this.Width,Top_Right.Height );
}
private void DrowTop_Middle(Graphics g)
{
Brush brush = new TextureBrush(Top_Middle, new Rectangle(0, 0, Top_Middle.Width , Top_Middle.Height));
g.FillRectangle(brush, this.Top_Left.Width, 0,this.Width- Top_Right.Width-Top_Left.Width,Top_Middle.Height );
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(325, 360);
this.ControlBox = false;
this.MaximizeBox = false;
this.Name = "Form1";
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}
当我将窗体默认size设为333, 368这个时绘出情况是这样的:就会发现DrowTop_Right绘制不正确.
当将窗体再调整大一些或小些的情况下又绘制正确..

size:333, 368时出现的现象:其它情况也会出这样的情况

这是所有用到几个图片:
浙公网安备 33010602011771号