public class DropDownButton : System.Windows.Forms.Control
{
private System.ComponentModel.Container components = null; private bool isHover = false;
private bool isPressLeft = false;
private bool isPressRight = false; public event EventHandler ClickEvent; public Menu.MenuItemCollection MenuItems { get; set; } public string Caption { get; set; } public DropDownButton()
{
InitializeComponent(); this.RefreshButtonsRects(); m_comboMenu = new ContextMenu();
MenuItems = new Menu.MenuItemCollection(m_comboMenu); this.SizeChanged += new EventHandler(DropDownButton_SizeChanged);
this.MouseUp += new MouseEventHandler(DropDownButton_MouseUp);
} protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
components.Dispose();
}
base.Dispose(disposing);
} protected override void OnMouseHover(EventArgs e)
{
this.isHover = true;
this.Invalidate();
base.OnMouseHover(e);
} protected override void OnMouseLeave(EventArgs e)
{
this.isHover = false;
this.Invalidate();
base.OnMouseLeave(e);
} protected override void OnMouseDown(MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
if (m_buttonRect.Contains(e.Location))
{
isPressLeft = true;
}
if (m_comboButtonRect.Contains(e.Location))
{
isPressRight = true;
}
this.Invalidate();
}
base.OnMouseDown(e);
} protected override void OnMouseUp(MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
isPressLeft = false;
isPressRight = false;
this.Invalidate();
}
base.OnMouseUp(e);
}
private void InitializeComponent()
{ } private const int COMBOBUTTON_WIDTH = 20;
private Rectangle m_buttonRect;
private Rectangle m_comboButtonRect;
private ContextMenu m_comboMenu; protected override void OnPaint(PaintEventArgs pe)
{
System.Windows.Forms.VisualStyles.PushButtonState stateL = System.Windows.Forms.VisualStyles.PushButtonState.Normal;
System.Windows.Forms.VisualStyles.PushButtonState stateR = System.Windows.Forms.VisualStyles.PushButtonState.Normal; if (isHover)
{
stateL = System.Windows.Forms.VisualStyles.PushButtonState.Hot;
stateR = System.Windows.Forms.VisualStyles.PushButtonState.Hot;
} if (isPressLeft)
{
stateL = System.Windows.Forms.VisualStyles.PushButtonState.Pressed;
} if (isPressRight)
{
stateR = System.Windows.Forms.VisualStyles.PushButtonState.Pressed;
} this.CreateGraphics().DrawRectangle(new Pen(SystemBrushes.Control), this.ClientRectangle); ButtonRenderer.DrawButton(this.CreateGraphics(),
m_buttonRect, Caption,
new Font(this.Font, FontStyle.Regular), false,
stateL); ButtonRenderer.DrawButton(this.CreateGraphics(),
m_comboButtonRect, "v",
new Font(this.Font, FontStyle.Regular), false,
stateR); base.OnPaint(pe);
} private void DropDownButton_SizeChanged(object sender, EventArgs e)
{
this.RefreshButtonsRects();
this.Invalidate();
} private void RefreshButtonsRects()
{
m_buttonRect = new Rectangle(
new Point(0, 0),
new Size(this.Width - COMBOBUTTON_WIDTH + 2, this.Height)
);
m_comboButtonRect = new Rectangle(
new Point(this.Width - COMBOBUTTON_WIDTH, 0),
new Size(COMBOBUTTON_WIDTH, this.Height)
);
} private void DropDownButton_MouseUp(object sender, MouseEventArgs e)
{
Point clickedPoint = new Point(e.X, e.Y); if (m_comboButtonRect.Contains(clickedPoint))
{
OnComboButtonClicked();
}
else
{
OnButtonClicked(e);
}
} private void OnButtonClicked(MouseEventArgs e)
{
if (this.ClickEvent != null)
{
ClickEvent(this, e);
}
} private void OnComboButtonClicked()
{
Point contextMenuPoint = new Point(m_comboButtonRect.Y, m_comboButtonRect.Height);
//m_comboMenu.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
m_comboMenu.Show(this, contextMenuPoint);
}
}

  

简单的DropDownButton(Winform)的更多相关文章

  1. 简单说下 Winform 的分页快速开发框架必须要实现的几个功能之一

    简单说下 Winform 的分页快速开发框架必须要实现的几个功能之一 分页非为前端分页  和 后端分页,前端分页只有适用于B/S,B/S的呈现速度远远不如C/S,而C/S则没有这个问题,所以分页必然是 ...

  2. WCF简单实例--用Winform启动和引用

    以订票为例简单应用wcf程序,需要的朋友可以参考下 本篇转自百度文档,自己试过,确实可以用. 以订票为例简单应用wcf 新建一个wcf服务应用程序 在IService1.cs定义服务契约 namesp ...

  3. Task(TPL)简单的实现Winform(WPF)异步

    很多时候,我们要实现Winform异步操作,你可以用传统的方法,但个人感觉代码不好理解,而且使用真有点不舒服.也可以用Task来实现,Task(.net4.0新添加的对象)其实就是对线程池线程的一个封 ...

  4. 简单的c#winform象棋游戏(附带源码)

    算法源自网络(网络源码连接:http://www.mycodes.net/161/6659.htm)   整体思路:用二维数组构建棋盘每一个数组元素封装为一个picturebox附带若干属性(例如:棋 ...

  5. C# 简单反射实现winform左侧树形导航,右侧切换内容

    先看看效果: 核心代码: using System; using System.Collections.Generic; using System.ComponentModel; using Syst ...

  6. winform 与 html 交互 简单案例

    本文主要简单的记录winform如何与html文件中的信息如何进行交互,即在winform中加载html界面,从而可以进行相互调用. 1.新建一个winform项目,若要在winform中加载html ...

  7. 简单的winform编辑器

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  8. C# 简单实现直线方程,抛物线方程

    本例子是简单的在WinForm程序中实现在坐标系中绘制直线方程,抛物线方程,点.重新学习解析几何方面的知识.仅供学习分享使用,如有不足之处,还请指正. 涉及知识点: 直线方程的表达方式:一般表达式Ax ...

  9. 封装简单的API——微信小程序

    前几天自己琢磨微信小程序的基本开发,里边用到的技术包括WebAPI,也就是方法的封装. 当然也可以用ASP.NET MVC WCF来写接口.更简单应该就是 WinForm 简单易部署. 这里用的是 2 ...

随机推荐

  1. EasyUI-validatebox 自定义validType验证

    根据项目要求,对于一些信息的验证也越来越特殊,当你发现EasyUI-validatebox提供的常用的validType验证类型已经满足不了你时,你可以选择自己自定义新的validType来完成信息验 ...

  2. ComboboxColumn取值——Winform中DataGridView中某一列使用下拉框

    ComboboxColumn的用法网上很多,绑定数据源都很简单,这里我遇到的是.不绑定数据源,即所有comobox的绑定都是固定的几个数据: 可以看到没有绑定任何数据源, ,在后台cs中取到下拉框的值 ...

  3. Virtualbox安装USB2.0/3.0

    系统:Ubuntu16.04 软件:Virtualbox5.1 1.打开Virtualbox,不启动虚拟系统. 2.点击设置->USB->启动usb2.0. 3.若发现不能启用,则到官网下 ...

  4. JAVA方法03之动手动脑问题解决

    动手动脑1.当JAVA里定义的函数中去掉static后,怎么办?(如下程序,将square()函数的static去掉) public class SquareIntTest { public stat ...

  5. JAVA语法02之课程问题解决

    (一)示例程序+运行结果: ①EnumTest.java public class EnumTest { public static void main(String[] args) { Size s ...

  6. 我需要在Web上完成一个图片上传的功能(+2)

    增加一个页面,用于判断传参是否正确. @{     //判断是否具备会员参数     if (UrlData.Count > 0)     {         Session["Arg ...

  7. Win8开虚拟wifi ‘无法启动承载网络 组或资源的状态不是执行请求操作的正确状态“

    第一步,首先我们点开开始按钮菜单,要右键以“管理员身份”打开CMD“命令提示符”并键入或者复制(粘贴)命令:netsh wlan show drivers 查看本机无线网卡是否支持此项Wifi热点共享 ...

  8. 日期控件,layui

    <link rel="stylesheet" href="<%=path%>/layui/css/layui.css" type=" ...

  9. 复制物料(参考的MMCC想法)

    MMCC这个事务码没用过,也是才听到的,都是业务搞起来的...然后感觉这个东西有点意思,就搞搞咯 网上找的一篇文章,自己修改的.改了默认收货工厂为创建时的工厂,因为这边一直报收货工厂必输...不管是不 ...

  10. MacOS长按无效问题

    defaults write -g ApplePressAndHoldEnabled -bool FALSE 注销并重新登录系统使其更改生效. 如果需要恢复长按键盘可以重音字符或非英文字符的功能,请打 ...