执行Invalidate()方法后,DrawItem事件没发生
下面的代码是一个完整的代码:大家可以建个FORM程序,用下面的代码全部替换/*类名:myphoto
*功能:查看图片
*创建人:欲望无限
*创建日期:2008-09-02
*修改人:
*修改日期:
* */
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace myphoto
{
public partial class Form1 : Form
{
//定义变量
private MainMenu MainMenu1;
//File菜单
private MenuItem MenuItem1;
private MenuItem MenuLoad;
private MenuItem MenuItem2;
private MenuItem MenuExit;
//Edite菜单
private MenuItem MenuEdite;
private MenuItem MenuMode;
private MenuItem MenuAutoSize;
private MenuItem MenuCenterImage;
private MenuItem MenuStretchImage;
private MenuItem MenuZoom;
private PictureBox PictureBox1;
//上下文菜单
private ContextMenu ContextMenu1;
//状态栏面版
private StatusBarPanel PanelText;
private StatusBarPanel PanelImage;
private StatusBarPanel PanelPercent;
//其它控件声明
private StatusBar StatusBar1;
private PictureBoxSizeMode[] ModeArray ={ PictureBoxSizeMode.AutoSize,
PictureBoxSizeMode.CenterImage, PictureBoxSizeMode.Normal, PictureBoxSizeMode.StretchImage,
PictureBoxSizeMode.Zoom };
private int selectmode = 5;
//初始化
private void load()
{
Form1_Load();//窗体初始化
PictureBox1 = new PictureBox();
PictureBox1.Dock = DockStyle.Fill;
PictureBox1.BorderStyle = BorderStyle.Fixed3D;
Status_Bar();//状态栏代码
this.Controls.AddRange(new Control[] { this.StatusBar1, this.PictureBox1 });
}
private void Context()
{
//MainMenu1
MainMenu1 = new MainMenu();
this.Menu = MainMenu1;
//
//ContextMenu1
ContextMenu1 = new ContextMenu();
//
//MenuItem1
MenuItem1 = new MenuItem();
MenuItem1.Index = 0;
MenuItem1.Text = "&File";
//
//MenuEdite
MenuItem1 = new MenuItem();
MenuItem1.Index = 0;
MenuItem1.Text = "&File";
//
//MenuLoad
MenuLoad = new MenuItem();
MenuLoad.Index = 0;
MenuLoad.Shortcut = Shortcut.CtrlL;
MenuLoad.Text = "&Load";
MenuLoad.Click += new EventHandler(MenuLoad_Click);
//
//MenuItem2
MenuItem2 = new MenuItem();
MenuItem2.Index = 1;
MenuItem2.Text = "-";
//
//MenuExit
MenuExit = new MenuItem();
MenuExit.Index = 2;
MenuExit.Text = "&Exit";
//
//MenuEdite
MenuEdite = new MenuItem();
MenuEdite.Index = 1;
MenuEdite.Text = "&Edite";
//
//MenuMode
MenuMode = new MenuItem();
MenuMode.Index = 0;
MenuMode.Text = "&Mode";
MenuMode.Popup += new EventHandler(MenuMode_Popup);
//
//MenuAutoSize
MenuAutoSize = new MenuItem();
MenuAutoSize.Index = 0;
MenuAutoSize.Text = "&AutoSize";
MenuAutoSize.Click += new EventHandler(Mode_Click);
//
//MenuCenterImage
MenuCenterImage = new MenuItem();
MenuCenterImage.Index = 1;
MenuCenterImage.Text = "&CenterImage";
MenuCenterImage.Click += new EventHandler(Mode_Click);
//
//MenuStretchImage
MenuStretchImage = new MenuItem();
MenuStretchImage.Index = 2;
MenuStretchImage.Text = "&StretchImage";
MenuStretchImage.Click += new EventHandler(Mode_Click);
//
//MenuZoom
MenuZoom = new MenuItem();
MenuZoom.Index = 3;
MenuZoom.Text = "&Zoom";
MenuZoom.Click+=new EventHandler(Mode_Click);
//
MainMenu1.MenuItems.AddRange(new MenuItem[] { this.MenuItem1,this.MenuEdite});
MenuItem1.MenuItems.AddRange(new MenuItem[] { this.MenuLoad, this.MenuItem2,
this.MenuExit });
MenuEdite.MenuItems.AddRange(new MenuItem[] { this.MenuMode});
MenuMode.MenuItems.AddRange(new MenuItem[] {
this.MenuAutoSize,this.MenuCenterImage,this.MenuStretchImage,this.MenuZoom});
#region 上下文菜单
ContextMenu1 = new ContextMenu();
PictureBox1.ContextMenu = ContextMenu1;
foreach (MenuItem t in MenuEdite.MenuItems)
{
ContextMenu1.MenuItems.Add(t.Index, t.CloneMenu());
}
#endregion
}//菜单处理代码
private void StatusBar_Panel()
{
//PanelText
PanelText = new StatusBarPanel();
this.PanelText.BeginInit();
this.PanelText.AutoSize = StatusBarPanelAutoSize.Spring;
this.PanelText.BorderStyle = StatusBarPanelBorderStyle.Sunken;
this.PanelText.Text = "panelimage";
this.PanelText.ToolTipText = "图片路径";
this.PanelText.EndInit();
//
//PanelImage
PanelImage = new StatusBarPanel();
this.PanelImage.BeginInit();
this.PanelImage.AutoSize = StatusBarPanelAutoSize.Contents;
this.PanelImage.BorderStyle = StatusBarPanelBorderStyle.Sunken;
this.PanelImage.ToolTipText = "图片像素";
this.PanelImage.EndInit();
//
//PanelPercent
PanelPercent = new StatusBarPanel();
this.PanelPercent.BeginInit();
this.PanelPercent.AutoSize = StatusBarPanelAutoSize.Contents;
this.PanelPercent.BorderStyle = StatusBarPanelBorderStyle.Sunken;
this.PanelPercent.ToolTipText = "图片进度";
this.PanelPercent.EndInit();
//
StatusBar1.Panels.AddRange(new StatusBarPanel[] { PanelText, PanelImage, PanelPercent
});
}//状态栏面版处理代码
private void Form1_Load()
{
Version ver=new Version(Application.ProductVersion);
this.Text = string.Format("{0:#}({1:#}.{2:#})", Application.ProductName, ver.Major,
ver.Minor);
}
private void Status_Bar()
{
StatusBar1 = new StatusBar();
StatusBar1.DrawItem += new StatusBarDrawItemEventHandler(StatusBar1_DrawItem);
StatusBar1.Size = new Size(292, 22);
StatusBar1.Text = "Ready";
StatusBar1.TabIndex = 2;
}
void StatusBar1_DrawItem(object sender, StatusBarDrawItemEventArgs sbdevent)
{
if (sbdevent.Panel == PanelPercent)
{
int percent = 100;
if (PictureBox1.SizeMode != PictureBoxSizeMode.Zoom)
{
Rectangle dr = PictureBox1.ClientRectangle;
int imagewidth = PictureBox1.Image.Width;
int imageheiht = PictureBox1.Image.Height;
percent = 100 * Math.Min(dr.Width, imagewidth) * Math.Min(dr.Height,
imageheiht) / (imagewidth * imageheiht);
Rectangle fillrect = sbdevent.Bounds;
fillrect.Width = sbdevent.Bounds.Width * percent / 100;
sbdevent.Graphics.FillRectangle(Brushes.SlateGray, fillrect);
sbdevent.Graphics.DrawString(percent.ToString() + "%", sbdevent.Font,
Brushes.White, sbdevent.Bounds);
}
}
}
void MenuMode_Popup(object sender, EventArgs e)
{
if (sender is MenuItem)
{
bool ImageBool = (PictureBox1.Image != null);
Menu ParentMenu = (Menu)sender;
foreach (MenuItem mi in ParentMenu.MenuItems)
{
mi.Enabled = ImageBool;
mi.Checked = (selectmode == mi.Index);
}
}
}
void Mode_Click(object sender, EventArgs e)
{
if (sender is MenuItem)
{
MenuItem mi = (MenuItem)sender;
PictureBox1.SizeMode=ModeArray[mi.Index];
selectmode = mi.Index;
PictureBox1.Invalidate();
StatusBar1.Invalidate();//这里执行后,怎么没有执行StatusBar1_DrawItem事件
}
}
void MenuLoad_Click(object sender, EventArgs e)
{
OpenFileDialog openfile = new OpenFileDialog();
openfile.Filter = "All file(*.jpg)|*.jpg|All file|*.*";
openfile.Title = "load photo";
if (openfile.ShowDialog() == DialogResult.OK)
{
try
{
StatusBar1.ShowPanels = false;
StatusBar1.Text = string.Format("正在加载{0:#}图片",openfile.FileName);
PictureBox1.Image = new Bitmap(openfile.OpenFile());
PanelText.Text = string.Format("正在浏览{0:#}图片", openfile.FileName);
this.PanelImage.Text = string.Format("{0:#} X {1:#}",
PictureBox1.Image.Width, PictureBox1.Image.Height);
StatusBar1.ShowPanels = true;
}
catch(Exception err)
{
StatusBar1.Text = string.Format("错误:加载{0:#}图片", openfile.FileName);
MessageBox.Show("错误信息:\r\n"+err.Message,"错误提示");
}
}
}
public Form1()
{
InitializeComponent();
load();//初始化
Context();//主菜单代码
StatusBar_Panel();//状态栏面版处理代码
}
}
}
问题产生在上面代码的红字部分.StatusBar1.Invalidate();//这里执行后,怎么没有执行StatusBar1_DrawItem事件
如果要改的话,请问怎么改?????高手赐教