


| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using System.Reflection; namespace DZB { public partial class ImageButton : Form { private IList private IButton capturedButton; private Font windowFont = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Regular); private string CurrentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase); private Bitmap bg, title; public ImageButton() { InitializeComponent(); } private void ImageButton_Load(object sender, EventArgs e) { title = new Bitmap(CurrentPath + @"Resourcestitle.bmp"); bg = new Bitmap(CurrentPath + @"Resourcesmainbg.bmp"); btnlist = new List btnlist.Add(new IButton(this, CurrentPath + @"Resources", 100, 100, "button.bmp", "按钮1", Color.White, Color.Blue, Command.cmd1)); btnlist.Add(new IButton(this, CurrentPath + @"Resources", 100, 200, "button.bmp", "按钮2", Color.White, Color.Blue, Command.cmd1)); btnlist.Add(new IButton(this, CurrentPath + @"Resources", 100, 300, "button.bmp", "按钮3", Color.White, Color.Blue, Command.cmd2)); btnlist.Add(new IButton(this, CurrentPath + @"Resources", 100, 400, "button.bmp", "按钮4", Color.White, Color.Blue, Command.cmd2)); btnlist.Add(new IButton(this, CurrentPath + @"Resources", 400, 100, "button.bmp", "按钮5", Color.White, Color.Blue, Command.cmd2)); btnlist.Add(new IButton(this, CurrentPath + @"Resources", 400, 200, "button.bmp", "按钮6", Color.White, Color.Blue, Command.cmd2)); btnlist.Add(new IButton(this, CurrentPath + @"Resources", 400, 300, "button.bmp", "按钮7", Color.White, Color.Blue, Command.cmd2)); btnlist.Add(new IButton(this, CurrentPath + @"Resources", 400, 400, "button.bmp", "按钮8", Color.White, Color.Blue, Command.cmd3)); } private void ImageButton_Paint(object sender, PaintEventArgs e) { // Buttons Graphics graphics; graphics = e.Graphics; graphics.DrawImage(title, 0, 0); graphics.DrawImage(bg, 0, 63); // Edit line foreach (IButton btn in btnlist) { btn.Render(graphics); } } protected override void OnPaintBackground(PaintEventArgs paintArgs) { //gr.DrawImage(img, 0, 0); base.OnPaintBackground(paintArgs); } ~ImageButton() { windowFont.Dispose(); } private void ImageButton_MouseDown(object sender, MouseEventArgs e) { foreach (IButton btn in btnlist) { if (btn.IsHit(e.X, e.Y)) { btn.IsSelected = true; capturedButton = btn; break; } } } private void ImageButton_MouseMove(object sender, MouseEventArgs e) { if (capturedButton != null) { capturedButton.IsSelected = capturedButton.IsHit(e.X, e.Y); } } private void ImageButton_MouseUp(object sender, MouseEventArgs e) { if (capturedButton != null) { if (capturedButton.IsHit(e.X, e.Y)) DoCommand(capturedButton.Cmd); try { capturedButton.IsSelected = false; capturedButton = null; } catch { } } } /// /// 执行指令 /// /// private void DoCommand(Command command) { switch (command) { case Command.cmd1://0 // this.Close(); break; case Command.cmd2://1 MessageBox.Show("cmd2"); break; case Command.cmd3://2 this.Close(); break; case Command.cmd4://3 { Line nl = new Line(); nl.Show(); } break; default: break; } } } } |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | //====================================================================== // // Copyright (C) 2008-2009 PCODE TEAM. All rights reserved. // // CLR Version: 2.0.50727.1433 // NameSpace: DZB FileName: IButton.Cs // // Created by Jy at 2009/9/10 22:07:34 // Email:jy@cjlu.edu.cn // //====================================================================== using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using System.Drawing; using System.Drawing.Imaging; namespace DZB { public class IButton { private Control MainForm; private Font sFont = new Font(FontFamily.GenericSansSerif,16, FontStyle.Bold); private Font sFont_ = new Font(FontFamily.GenericSansSerif, 16, FontStyle.Bold); private bool IsSelectedValue; private Command ButtonCommand; private Rectangle selfRec; private string _caption; private float _cx, _cy;//画标题的位置 private Bitmap btnbg, btnbg_; private string _CurrentPath; Color transpColor = Color.FromArgb(255, 0, 255);//资源文件的透明色 ImageAttributes imageAttr; Color _ncolor, _fcolor; Graphics g, g_; /// /// 初始化 /// /// 容器名称 /// 当前程序路径 /// 按钮位置X /// 按钮位置Y /// 按钮图片名称 /// 标题 /// 正常颜色 /// 激活颜色 /// 指令 public IButton(Control form,string CurrentPath,int x,int y,string bgfilename,string caption,Color nColor,Color fColor, Command cmd) { MainForm = form; _ncolor = nColor; _fcolor = fColor; ButtonCommand = cmd; _caption = caption; _CurrentPath = CurrentPath; string bg = CurrentPath + bgfilename; string bg_ = CurrentPath +System.IO.Path.GetFileNameWithoutExtension(bg) + "_.bmp"; btnbg = new Bitmap(bg); selfRec = new Rectangle(x, y, btnbg.Width,btnbg.Height); g = Graphics.FromImage(btnbg); _cx = (219 - g.MeasureString(_caption, sFont).Width) / 2; _cy = (48 - g.MeasureString(_caption, sFont).Height) / 2; btnbg_=new Bitmap(bg_); g_ = Graphics.FromImage(btnbg_); imageAttr = new ImageAttributes(); imageAttr.SetColorKey(transpColor, transpColor); } public void Render(Graphics graphics) { if (IsSelectedValue) {//点下的状态 g_.DrawString(_caption, sFont_, new SolidBrush(_fcolor), _cx,_cy); graphics.DrawImage(btnbg_, selfRec, 0, 0, selfRec.Width, selfRec.Height, GraphicsUnit.Pixel, imageAttr); } else { g.DrawString(_caption, sFont, new SolidBrush(_ncolor),_cx, _cy); graphics.DrawImage(btnbg, selfRec, 0, 0, selfRec.Width, selfRec.Height, GraphicsUnit.Pixel, imageAttr); } } private static Color GetTransparentColor(Image image) { return ((Bitmap)image).GetPixel(image.Width - 1, image.Height - 1); } public bool IsHit(int x,int y) { return (x >= selfRec.X && x < selfRec.X +selfRec.Width && y >= selfRec.Y && y public bool IsSelected { get { return IsSelectedValue; } set { Graphics graphics; if (value != IsSelectedValue) { IsSelectedValue = value; // Redraw right away graphics = MainForm.CreateGraphics(); this.Render(graphics); graphics.Dispose(); } } } public Command Cmd { get { return (ButtonCommand); } } } } |



更多回帖