Winform自定义控件详解(未完....)

 

1. Winform概念

1.1. 定义

MSND上,微软对Winform的定义:FormsWinforms应用程序的基本单元,它实质上是一块空白的面板,开发人员通过使用控件去创建用户界面,并编写代码去操作数据。.Net中对WinfowsForm编程的称谓即为Winform

1.2. 与WPF的区别

WinFormWPF(Windows Presentation Foundation)的最大区别在于,WinForm是基于原来使用C++编写的Win32 API,使用GDI+去渲染控件,主要消耗CPU

WPF则是直接使用DirectX函数,直接使用显卡去渲染控件,性能较Winform为高。

2. Winform功能

2.1. 构建丰富的,交互式用户界面(Building Rich, Interactive User Interfaces)

l 事件驱动的Event Handlers界面;

l 丰富的控件集,去模仿高端的程序,如Office

l 通过UserControl组合控件;

l 界面布局支持,如FlowLayoutPanelSplitLayoutPanelTableLayoutPanel

l 绘图的支持,在System.Drawing命名空间中;

2.2. 显示和操作数据(Displaying and Manipulating Data)

l DataGridViewTreeView等组件去显示多种来源的数据,如:DatabaseXML FileWebService

l 使用BindingSource连接网络数据源;

l 可以简直创建数据源绑定型控件(Data-bound controls);

l 界面最后状态的记忆(Application-Setting),可以将此状态存储成XML文件并在运行时自动读取回内存;

2.3. 将应用程序部署到客户端计算机(Deploying Applications to Client Computers)

l Deploying an application by using ClickOnce

l Updating a ClickOnce deployment

l Managing security with ClickOnce

2.4. 其他控件的功能(Other Controls and Features)

l Printing the contents of a form

l Globalizing a Windows Forms application

l Learn more about Windows Forms security

3. 常用程序集

命名空间

描述

System.Windows

Provides several important Windows Presentation Foundation (WPF) base element classes, various classes that support the WPF property system and event logic, and other types that are more broadly consumed by the WPF core and framework.

System.Windows.Controls

Provides classes to create elements, known as controls, that enable a user to interact with an application. The control classes are at the core of the user's experience with any application because they allow a user to view, select, or enter data or other information. 

System.Windows.Data

Contains classes used for binding properties to data sources, data source provider classes, and data-specific implementations of collections and views.

System.Windows.Forms

The System.Windows.Forms namespace contains classes for creating Windows-based applications that take full advantage of the rich user interface features available in the Microsoft Windows operating system.

System.Windows.Forms.Design

The System.Windows.Forms.Design namespace contains classes that support design-time configuration and behavior for Windows Forms components. These classes consist of designer classes that provide support for Windows Forms components, a set of design-time services; UITypeEditor classes for configuring certain types of properties, and classes for importing ActiveX controls.

System.Windows.Forms.Layout

The System.Windows.Forms.Layout namespace contains classes for implementing layout behaviors in your form or control.

 

4. FormControlComponent

4.1. Component

Component是最底层的类,它描述一个在UI组件的最基本特性,如:是否启用事件、是否在设计期、释放资源等,Component并不在界面直接显示,只描述控件所需的结构、数据等内容,如:DataGridView.ColumnToolStrip.ToolStripItem等。

4.2. Control

Control是所有控件的基类,它继承了Component,描述一个在UI中显示的控件,在Component的基础上添加了位置、大小、文字背景等属性;

4.2.1. Handle属性

前文已经提及过,Winform是基于Win32API编写,所有控件实例都对应一个C++句柄,如果没有创建此句柄,哪怕newControl实例,控件依旧不会在界面显示。

4.2.2. CreateParam属性

创建控件时所需要的参数,例如:是否启用二级缓存、控件是否允许透明、是否允许重绘等,具体参数可以参考MSDN

4.2.3. WndProcref Message m)方法

这是Control最重要的方法,用于接收Windows系统消息,包括:鼠标点击、键盘点击、控件绘制、定时任务等消息。由Contorl基类将消息封装后调用对应的OnXxxx方法。

4.2.4. 

 

4.2.5. 几个重要的On方法

以下方法均为虚方法,子类可以重写这些方法定制控件。

Ø OnPaint:与其它的架构如SwingWPF等不同,Control没有使用MVC模式,这是其一直以来比较被诟病的地方,Control的内容与其显示都在Control中,由Control.OnPaint方法去进行控件渲染;

Ø OnClick:鼠标点击

Ø OnCreateControl:创建底层句柄

Ø OnKeyXxxxxx:几个鼠标事件

Ø OnLayout:控件布局

Ø OnSizeChange:大小变化

4.3. Form

一个Windows窗体,是Control类的子类,也是一个控件,包含窗口特性,如:右上角按钮、确定按钮、取消按钮等;

5. 组合控件、定制控件

想要实现自定义使控件,有两种方式:

(一)使用UserControl,将已有控件组合出新的控件;

(二)继承Control,通过实现其OnXxxx方法去做出新的控件;

我比较喜欢将第一种方式称为“组合控件”,第二种方式为“定制”控件,因为个人认为这两种试有着本质的区别,前者使用现有控件,将之放在容器中,好处是开发较为简直,坏处是增加Form类中的层次,而且处理不好会带来一些性能问题;而从Control继承的话,实现难度较大,但可以完全控制整个控件,性能会较UserControl好;

由于作者比较讨厌UserControl(不知道为什么),所以下文只对如果定制控件作介绍,并展示如何一步步创建出第一个定制控件。

5.1. 继承Control

5.2. 实现OnPaint方法

5.2.1. Graphic绘图类

 

5.2.2. 使用各种Draw方法进行图形绘制

 

5.2.3. Translate坐标变换

 

5.3. 自定义事件

5.3.1. 由WndProc直接生成的事件

 

5.3.2. 由其它事件转变的事件

 

5.4. 更多的特性

 

参考资料:

1、MSDN

2、百度

转载于:https://www.cnblogs.com/yihome/p/winform_control.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值