zoukankan      html  css  js  c++  java
  • C# 之控件重绘(二)

    本篇是接着上一篇写的,上一篇只是对按钮的前景色,背景色等做了一些改变,由于按钮的一个重要功能是本篇将涉及到鼠标事件,包括按下、抬起、经过、离开等事件。

    修改上一篇的MyButton.cs文件即可:

       1:  using System;
       2:  using System.Windows;
       3:  using System.Windows.Forms;
       4:  using System.Drawing;
       5:   
       6:  namespace WindowsFormsApplication1
       7:  {
       8:       class MyButton:System.Windows.Forms.Button
       9:      {
      10:          private bool mouseDown = false;
      11:          private bool mouseHover = false;
      12:   
      13:          public MyButton()
      14:          {
      15:              SetStyle(ControlStyles.UserPaint, true);
      16:              MouseDown += new MouseEventHandler(OnMouseDown);
      17:              MouseUp += new MouseEventHandler(OnMouseUp);
      18:              MouseEnter += new EventHandler(OnMouseEnter);
      19:              MouseLeave += new EventHandler(OnMouseLeave);
      20:              Height = 23;
      21:              Width = 75;
      22:          }
      23:          private void OnMouseDown(object sender, MouseEventArgs e)
      24:          {
      25:              mouseDown = true;
      26:          }
      27:          private void OnMouseUp(object sender, MouseEventArgs e)
      28:          {
      29:              mouseDown = false;
      30:              OnPaint(new PaintEventArgs(CreateGraphics(), ClientRectangle));
      31:          }
      32:          private void OnMouseEnter(object sender, EventArgs e)
      33:          {
      34:              mouseHover = true;
      35:              OnPaint(new PaintEventArgs(CreateGraphics(), ClientRectangle));
      36:          }
      37:          private void OnMouseLeave(object sender, EventArgs e)
      38:          {
      39:              mouseHover = false;
      40:              OnPaint(new PaintEventArgs(CreateGraphics(), ClientRectangle));
      41:          }
      42:          protected override void OnPaint(PaintEventArgs pevent)
      43:          {
      44:              pevent.Graphics.FillRectangle(new SolidBrush(Parent.BackColor),
      45:                  pevent.ClipRectangle);
      46:              if (Enabled == false)
      47:              {
      48:                  DrawDisableButton(pevent.Graphics);
      49:              }
      50:              else if (mouseDown)
      51:              {
      52:                  DrawMouseDownButton(pevent.Graphics);
      53:              }
      54:              else if (mouseHover)
      55:              {
      56:                  DrawMouseHoverButton(pevent.Graphics);
      57:              }
      58:              else if (Focused)
      59:              {
      60:                  DrawContainFocusButton(pevent.Graphics);
      61:              }
      62:              else
      63:              {
      64:                  DrawNormalButton(pevent.Graphics);
      65:              }
      66:                  WriteText(pevent.Graphics);
      67:              //base.OnPaint(pevent);
      68:          }
      69:   
      70:          private void DrawBorder(Graphics g, int state)
      71:          {
      72:              if (state == 1)//draw normal style border
      73:              {
      74:                  Pen p = new Pen(SystemColors.ControlLightLight, 2);
      75:                  g.DrawLine(p, 1, 1, 1, Height - 2);
      76:                  g.DrawLine(p, 1, 1, Width - 2, 1);
      77:                  g.DrawLine(p, Width - 1, 2, Width - 1, Height - 2);
      78:                  g.DrawLine(p, 2, Height - 1, Width - 2, Height - 1);
      79:              }
      80:              else if (state == 2)//draw hover style border
      81:              {
      82:                  Pen p = new Pen(Color.Yellow, 2);
      83:                  g.DrawLine(p, 1, 1, 1, Height - 2);
      84:                  g.DrawLine(p, 1, 1, Width - 2, 1);
      85:                  g.DrawLine(p, Width - 1, 2, Width - 1, Height - 2);
      86:                  g.DrawLine(p, 2, Height - 1, Width - 2, Height - 1);
      87:              }
      88:              else if (state == 3)//draw pressed style border
      89:              {
      90:                  Pen p = new Pen(SystemColors.ControlDark, 2);
      91:                  g.DrawLine(p, 1, 1, 1, Height - 2);
      92:                  g.DrawLine(p, 1, 1, Width - 2, 1);
      93:                  g.DrawLine(p, Width - 1, 2, Width - 1, Height - 2);
      94:                  g.DrawLine(p, 2, Height - 1, Width - 2, Height - 1);
      95:              }
      96:              else if (state == 4)//draw disabled style border
      97:              {
      98:                  Pen p = new Pen(SystemColors.ControlLight, 2);
      99:                  g.DrawLine(p, 1, 1, 1, Height - 2);
     100:                  g.DrawLine(p, 1, 1, Width - 2, 1);
     101:                  g.DrawLine(p, Width - 1, 2, Width - 1, Height - 2);
     102:                  g.DrawLine(p, 2, Height - 1, Width - 2, Height - 1);
     103:              }
     104:              else if (state == 5)//draw default style border
     105:              {
     106:                  Pen p = new Pen(Color.SkyBlue, 2);
     107:                  g.DrawLine(p, 1, 1, 1, Height - 2);
     108:                  g.DrawLine(p, 1, 1, Width - 2, 1);
     109:                  g.DrawLine(p, Width - 1, 2, Width - 1, Height - 2);
     110:                  g.DrawLine(p, 2, Height - 1, Width - 2, Height - 1);
     111:              }
     112:              if (state == 4)//draw disable style border
     113:              {
     114:                  Pen p = new Pen(Color.FromArgb(161, 161, 146), 1);
     115:                  g.DrawLine(p, 0, 2, 0, Height - 3);
     116:                  g.DrawLine(p, 2, 0, Width - 3, 0);
     117:                  g.DrawLine(p, Width - 1, 2, Width - 1, Height - 3);
     118:                  g.DrawLine(p, 2, Height - 1, Width - 3, Height - 1);
     119:                  g.DrawLine(p, 0, 2, 2, 0);
     120:                  g.DrawLine(p, 0, Height - 3, 2, Height - 1);
     121:                  g.DrawLine(p, Width - 3, 0, Width - 1, 2);
     122:                  g.DrawLine(p, Width - 3, Height - 1, Width - 1, Height - 3);
     123:              }
     124:              else//draw normal style border
     125:              {
     126:                  g.DrawLine(Pens.Black, 0, 2, 0, Height - 3);
     127:                  g.DrawLine(Pens.Black, 2, 0, Width - 3, 0);
     128:                  g.DrawLine(Pens.Black, Width - 1, 2, Width - 1, Height - 3);
     129:                  g.DrawLine(Pens.Black, 2, Height - 1, Width - 3, Height - 1);
     130:                  g.DrawLine(Pens.Black, 0, 2, 2, 0);
     131:                  g.DrawLine(Pens.Black, 0, Height - 3, 2, Height - 1);
     132:                  g.DrawLine(Pens.Black, Width - 3, 0, Width - 1, 2);
     133:                  g.DrawLine(Pens.Black, Width - 3, Height - 1,
     134:                      Width - 1, Height - 3);
     135:              }
     136:          }
     137:   
     138:          private void DrawNormalButton(Graphics g)
     139:          {
     140:              DrawBorder(g, 1);
     141:              PaintBack(g, SystemColors.ControlLightLight);
     142:          }
     143:   
     144:          private void DrawMouseHoverButton(Graphics g)
     145:          {
     146:              DrawBorder(g, 2);
     147:              PaintBack(g, SystemColors.ControlLightLight);
     148:          }
     149:   
     150:          private void DrawMouseDownButton(Graphics g)
     151:          {
     152:              DrawBorder(g, 3);
     153:              PaintBack(g, SystemColors.ControlLight);
     154:          }
     155:   
     156:          private void DrawDisableButton(Graphics g)
     157:          {
     158:              DrawBorder(g, 4);
     159:              PaintBack(g, SystemColors.ControlLight);
     160:          }
     161:   
     162:          private void DrawContainFocusButton(Graphics g)
     163:          {
     164:              DrawBorder(g, 5);
     165:              PaintBack(g, SystemColors.ControlLightLight);
     166:          }
     167:   
     168:          private void PaintBack(Graphics g, Color c)
     169:          {
     170:              g.FillRectangle(new SolidBrush(c), 3, 3, Width - 6, Height-6);
     171:          }
     172:   
     173:          private void WriteText(Graphics g)
     174:          {
     175:              int x = 0, y = 0;
     176:              Size s = g.MeasureString(Text, Font).ToSize();
     177:              x = (Width - s.Width) / 2;
     178:              y = (Height - s.Height) / 2;
     179:              if (Enabled)
     180:                  g.DrawString(Text, Font, Brushes.Black, x, y);
     181:              else
     182:                  g.DrawString(Text, Font, Brushes.Gray, x, y);
     183:          }
     184:      }
     185:  }

    窗体代码不变。

    image

  • 相关阅读:
    CS224n, lec 10, NMT & Seq2Seq Attn
    CS231n笔记 Lecture 11, Detection and Segmentation
    CS231n笔记 Lecture 10, Recurrent Neural Networks
    CS231n笔记 Lecture 9, CNN Architectures
    CS231n笔记 Lecture 8, Deep Learning Software
    CS231n笔记 Lecture 7, Training Neural Networks, Part 2
    pytorch坑点排雷
    Sorry, Ubuntu 17.10 has experienced an internal error
    VSCode配置python插件
    tmux配置与使用
  • 原文地址:https://www.cnblogs.com/humaoxiao/p/2378696.html
Copyright © 2011-2022 走看看