private void DrawUpDownButton() { bool mouseOver = false; bool mousePress = LeftKeyPressed(); bool mouseInUpButton = false; RECT rect = new RECT(); GetClientRect(base.Handle, ref rect); Rectangle clipRect = Rectangle.FromLTRB( rect.Top, rect.Left, rect.Right, rect.Bottom); Point cursorPoint = new Point(); GetCursorPos(ref cursorPoint); GetWindowRect(base.Handle, ref rect); mouseOver = PtInRect(ref rect, cursorPoint); cursorPoint.X -= rect.Left; cursorPoint.Y -= rect.Top; mouseInUpButton = cursorPoint.X < clipRect.Width / 2; using (Graphics g = Graphics.FromHwnd(base.Handle)) { UpDownButtonPaintEventArgs e = new UpDownButtonPaintEventArgs( g, clipRect, mouseOver, mousePress, mouseInUpButton); _owner.OnPaintUpDownButton(e); } }
/// <summary> /// 对左右键进行重绘 /// </summary> /// <param name="e">左右键的参数,继承painteventargs</param> protected virtual void OnPaintUpDownButton( UpDownButtonPaintEventArgs e) { Graphics g = e.Graphics; Rectangle rect = e.ClipRectangle;//程序分配给左右箭头的矩形 Color upButtonBaseColor = _baseColor; Color upButtonBorderColor = _borderColor; Color upButtonArrowColor = _arrowColor; Color downButtonBaseColor = _baseColor; Color downButtonBorderColor = _borderColor; Color downButtonArrowColor = _arrowColor; Rectangle upButtonRect = rect; Bitmap bmp = new Bitmap(rect.Width, rect.Height); Graphics gb = Graphics.FromImage(bmp); Rectangle bmpRect = new Rectangle(0, 0, rect.Width, rect.Height); upButtonRect.X = 1; upButtonRect.Y = 1; upButtonRect.Width = rect.Width / 2 - 4; upButtonRect.Height -= 2; Rectangle downButtonRect = rect; downButtonRect.X = upButtonRect.Right + 2; downButtonRect.Y = 1; downButtonRect.Width = rect.Width / 2 - 4; downButtonRect.Height -= 2; if (Enabled) { if (e.MouseOver) { if (e.MousePress)//按下键时 { if (e.MouseInUpButton) { upButtonBaseColor = GetColor(_baseColor, 0, -35, -24, -9); } else { downButtonBaseColor = GetColor(_baseColor, 0, -35, -24, -9); } } else//仅仅是鼠标over未按键时 { if (e.MouseInUpButton) { upButtonBaseColor = GetColor(_baseColor, 0, 35, 24, 9); } else { downButtonBaseColor = GetColor(_baseColor, 0, 35, 24, 9); } } } } else { upButtonBaseColor = SystemColors.Control; upButtonBorderColor = SystemColors.ControlDark; upButtonArrowColor = SystemColors.ControlDark; downButtonBaseColor = SystemColors.Control; downButtonBorderColor = SystemColors.ControlDark; downButtonArrowColor = SystemColors.ControlDark; } g.SmoothingMode = SmoothingMode.AntiAlias; gb.SmoothingMode = SmoothingMode.AntiAlias; Color backColor = Enabled ? _backColor : SystemColors.Control; using (SolidBrush brush = new SolidBrush(_backColor)) { rect.Inflate(1, 1); gb.FillRectangle(brush, bmpRect); } RenderButton( gb, upButtonRect, upButtonBaseColor, upButtonBorderColor, upButtonArrowColor, ArrowDirection.Left); RenderButton( gb, downButtonRect, downButtonBaseColor, downButtonBorderColor, downButtonArrowColor, ArrowDirection.Right); g.DrawImage(bmp, rect.X, rect.Y); bmp.Dispose(); gb.Dispose(); UpDownButtonPaintEventHandler handler = base.Events[EventPaintUpDownButton] as UpDownButtonPaintEventHandler; if (handler != null) { handler(this, e); } }