简单的DropDownButton(Winform)
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)的更多相关文章
- 简单说下 Winform 的分页快速开发框架必须要实现的几个功能之一
简单说下 Winform 的分页快速开发框架必须要实现的几个功能之一 分页非为前端分页 和 后端分页,前端分页只有适用于B/S,B/S的呈现速度远远不如C/S,而C/S则没有这个问题,所以分页必然是 ...
- WCF简单实例--用Winform启动和引用
以订票为例简单应用wcf程序,需要的朋友可以参考下 本篇转自百度文档,自己试过,确实可以用. 以订票为例简单应用wcf 新建一个wcf服务应用程序 在IService1.cs定义服务契约 namesp ...
- Task(TPL)简单的实现Winform(WPF)异步
很多时候,我们要实现Winform异步操作,你可以用传统的方法,但个人感觉代码不好理解,而且使用真有点不舒服.也可以用Task来实现,Task(.net4.0新添加的对象)其实就是对线程池线程的一个封 ...
- 简单的c#winform象棋游戏(附带源码)
算法源自网络(网络源码连接:http://www.mycodes.net/161/6659.htm) 整体思路:用二维数组构建棋盘每一个数组元素封装为一个picturebox附带若干属性(例如:棋 ...
- C# 简单反射实现winform左侧树形导航,右侧切换内容
先看看效果: 核心代码: using System; using System.Collections.Generic; using System.ComponentModel; using Syst ...
- winform 与 html 交互 简单案例
本文主要简单的记录winform如何与html文件中的信息如何进行交互,即在winform中加载html界面,从而可以进行相互调用. 1.新建一个winform项目,若要在winform中加载html ...
- 简单的winform编辑器
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- C# 简单实现直线方程,抛物线方程
本例子是简单的在WinForm程序中实现在坐标系中绘制直线方程,抛物线方程,点.重新学习解析几何方面的知识.仅供学习分享使用,如有不足之处,还请指正. 涉及知识点: 直线方程的表达方式:一般表达式Ax ...
- 封装简单的API——微信小程序
前几天自己琢磨微信小程序的基本开发,里边用到的技术包括WebAPI,也就是方法的封装. 当然也可以用ASP.NET MVC WCF来写接口.更简单应该就是 WinForm 简单易部署. 这里用的是 2 ...
随机推荐
- 【解决方法】magento paypal快速结账 不跳转
magento paypal Express Checkout(快速结账) 页面不跳转到Paypal的解放方法 我使用的magento 1.9.0.1 版本的,Paypal 快速结账都已经设置完毕,但 ...
- 运行Shell脚本的几种方式解析
1 给脚本加上执行权限chmod u+x a.sh, 而后就可以直接用全路径来执行脚本了,比如当前文件夹下用./a.sh, 如果脚本所在目录在PATH环境变量之中, 则直接用a.sh即可 2 sh/b ...
- Nodejs的安装及配置
1.从Nodejs官网下载安装包,进行安装,是否安装成功,可以从cmd npm去检查,出现如下界面,就表示安装成功 2.打开Webstorm进行配置,(如果安装前打开webstorm了,需要重启web ...
- 使用自定义标签模拟jstl的<c:for each>标签
一.自定义标签的基本编写 下面编写一个自定义标签,它可以输出当前的时间. 1.编写标签类 类可以通过继承SimpleTagSupport类实现一个标签类编写.父类为我们提供了一些编写自定义标签的快捷的 ...
- 【crawler】log4j:WARN No appenders could be found for logger (dao.hsqlmanager).
This Short introduction to log4j guide is a little bit old but still valid. That guide will give you ...
- 使用automake等命令自动生成Makefile文件 (转载)
使用automake等命令自动生成Makefile文件 Linux下编程时,为了方便编译,往往使用Makefile文件自动完成编译,但是Makefile文件本身的书写十分复杂,规则很多.好在Lin ...
- CSS3--transform
transform:向元素应用2D或3D转换,该属性允许我们对属性旋转,缩放,移动,或倾斜. transfrom:none:定义不进行转换 transfrom:matrix(n,n,n,n,n,n); ...
- 2015项目timeline
1. app签到 http://h5.a.rongyi.com/html/app/sign/index.html (pc无效果.app端 20160105) 2.圣诞活动--砍价 http://h5 ...
- iedriverserver使用报错
在win7下面使用IEdriverserver报错: AttributeError: 'Service' object has no attribute 'process' 1,下载最新的ie ...
- Hadoop集群配置过程中需要注意的问题
环境:VMwareWorkstation8.0 和 Ubuntu14.04 1. 使用三台虚拟机搭建Hadoop集群 2. 一定要在每台机器上配置ssh免密码登录 3. 由于需要给/etc/hosts ...