目前WinForm程序中,控件放置的太多会导致界面刷新太慢,所以想自己绘制按钮以加快界面刷新的速度
private void button1_Click(object sender, EventArgs e)
{
Graphics vGraphics = CreateGraphics();
ControlPaint.DrawButton(vGraphics, 0, 0, 75, 25, ButtonState.Normal);
vGraphics.Dispose();
}
上面这种方式绘制出的按钮不够理想,没有xp风格的漂亮
如何绘制XP系统风格一致的按钮呢?
注意按钮状态有普通、鼠标移入、鼠标按下、不可控制,要考虑按钮随主题不同而不同估计问题关键的是:如何取得当前xp风格按钮的图案?(我想过拷贝屏幕的方法虽然可行但很别扭)
至于那些判断按钮状态、鼠标区域、事件触发、绘制文字等等都不用照顾提供线索或路过均有分(不晓得是不是必须给viena 100分才能结贴)看到新功能,拿VMM测试一下先
http://www.codeproject.com/cs/miscctrl/MgXpImageButton.asp
http://www.codeproject.com/cs/miscctrl/JPEnhancedXPButton.asp接分
谢谢
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;namespace WindowsApplication1
{
public class Form1 : Form
{
public Form1()
{
InitializeComponent();
} /// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows Form Designer generated code /// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(460, 212);
this.Name = "Form1";
this.Text = "Form1";
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
this.ResumeLayout(false); } #endregion /// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
} private void Form1_Paint(object sender, PaintEventArgs e)
{
this.DrawXpButtonBackground(e.Graphics, new Rectangle(50, 50, 80, 23), ButtonState.Normal);
this.DrawXpButtonBackground(e.Graphics, new Rectangle(50, 75, 80, 23), ButtonState.Pushed);
this.DrawXpButtonBackground(e.Graphics, new Rectangle(50, 100, 80, 23), ButtonState.Checked);
this.DrawXpButtonBackground(e.Graphics, new Rectangle(50, 125, 80, 23), ButtonState.Inactive);
} private void DrawXpButtonBackground(Graphics g, Rectangle bounds, ButtonState state)
{
if (!VisualStyleRenderer.IsSupported)
{
ControlPaint.DrawButton(g, bounds, state);
return;
} VisualStyleRenderer render = null; if ((state & ButtonState.Inactive) == ButtonState.Inactive)
{
render = new VisualStyleRenderer(VisualStyleElement.Button.PushButton.Disabled);
render.DrawBackground(g, bounds);
return;
}
if ((state & ButtonState.Pushed) == ButtonState.Pushed)
{
render = new VisualStyleRenderer(VisualStyleElement.Button.PushButton.Pressed);
render.DrawBackground(g, bounds);
return;
}
if ((state & ButtonState.Checked) == ButtonState.Checked)
{
render = new VisualStyleRenderer(VisualStyleElement.Button.PushButton.Hot);
render.DrawBackground(g, bounds);
return;
} render = new VisualStyleRenderer(VisualStyleElement.Button.PushButton.Normal);
render.DrawBackground(g, bounds);
return;
}
}
}
■■■■■■■■■■■■■■■
■■■■■■■■■■■■■■■■■
■■■■■■■■■■■■■■■■■■■■■
■■■■■■■■■■■■■■■■■■■■■■
■■■■ ■■■■■■■■■■■■■■■■■■
■■■■ ■■■■■■■■■■■■■■■■■■
■■■ ■■■■■■■■■■■■■■■■■■■■
■■■■ ■■■■■■■■■■■■■■■■■■■■
■■■■ ■■■■■■■■■■■■■■■■■■■■■■
■■■■ ■■■■■■■■■■■■■■■■■■■■■■
■■■■ ■■■■■■■■■■■■■■■■■■■■■■■■
■■■■ ■■■■■■■■■■■■■■■■■■■■■■■■
■■■■ ■■■■■■■■■■■■■■■■■■■■■■■■
■■■■■ ■■■■■■■■■■■■■■■■■■■■■■■■■■
■■■■■ ■■■■■■■■■■■■■■■■■■■■■■■■■■
■■ ■■■■■ ■■■■■■■■■■■■■■■■■■■■■■■■■■■■
■■■■■■■ ■■■■■■■■■■■■■■■■■■■■■■■■■■■■
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
■■■■ ■■■■■■■■■■■■■■■■■■■■■■■■■■■
■■■■ ■■■■■■■■■■■■■■■■■■■■■■■■■■■
■■■■■ ■■■■■■■■■■■■■■■■■■■■■■■■■■■
■■■■ ■■■■■■■■■■■■■■■■■■■■■■■■■■
■■■■ ■■■■■■■■■■■■■■■■■■■■■■■■■■
■■■■ ■■■■■■■■■■■■■■■■■■■■■■■■
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
■■■■■■■■■■■■■■■■■■■■■■■■■■
■■■■■■■■■■■■■■■■■■■■■■■■
■■■■■■■■■■■■■■■■■■
感谢你提供的链接,多是重载控件的方法,还有一个需要图片资源,没有直接绘制系统风格的
再感谢大家关注这个问题