WinForm控件开发总结(一)------开篇

简介:
  我本人不是专业的控件开发人员,只是在平常的工作中,需要自己开发一些控件。在自己开发 WinForm 控件的时候,没有太多可以借鉴的资料,只能盯着 MSDN 使劲看,还好总算有些收获。现在我会把这些经验陆陆续续的总结出来,写成一系列方章,希望对看到的朋友有所帮助。今天我来开个头。
      其实开发 WinForm 控件并不是很复杂, .NET 为我们提供了丰富的底层支持。如果你有 MFC或者API图形界面 的开发经验,那么学会 WinForm 控件可能只需要很短的时间 就够了。
      自己开发的 WinForm 控件通常有三种类型:复合控件( Composite Controls ),扩展控件( Extended Controls ),自定义控件( Custom Controls )。    
      复合控件:将现有的各种控件组合起来,形成一个新的控件,将集中控件的功能集中起来。
      扩展控件:在现有控件的控件的基础上派生出一个新的控件,为原有控件增加新的功能或者修改原有控件的控能。
      自定义控件:直接从 System.Windows.Forms.Control 类派生出来。 Control 类提供控件所需要的所有基本功能,包括键盘和鼠标的事件处理。自定义控件是最灵活最强大的方法,但是对开发者的要求也比较高,你必须为 Control 类的 OnPaint 事件写代码,你也可以重写 Control 类的 WndProc 方法,处理更底层的 Windows 消息,所以你应该了解 GDI +和 Windows API 。    
      本系列文章主要介绍自定义控件的开发方法。
      控件(可视化的)的基本特征:
      1.       可视化。
      2.       可以与用户进行交互,比如通过键盘和鼠标。
      3.       暴露出一组属性和方法供开发人员使用。
      4.       暴露出一组事件供开发人员使用。
      5.       控件属性的可持久化。
      6.       可发布和可重用。
      这些特征是我自己总结出来,不一定准确,或者还有遗漏,但是基本上概括了控件的主要方面。
      接下来我们做一个简单的控件来增强一下感性认识。首先启动VS2005创建一个ClassLibrary工程,命名为CustomControlSampleVS会自动为我们创建一个solution与这个工程同名,然后删掉自动生成的Class1.cs文件,最后在Solution explorer里右键点击CustomControlSample工程选择Add->Classes…添加一个新类,将文件的名称命名为FirstControl。下边是代码:
      
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
using  System.Windows.Forms;
None.gif
using  System.ComponentModel;
None.gif
using  System.Drawing;
None.gif
None.gif
namespace  CustomControlSample
ExpandedBlockStart.gif
{
InBlock.gif    
public class FirstControl : Control
ExpandedSubBlockStart.gif    
{
InBlock.gif
InBlock.gif        
public FirstControl()
ExpandedSubBlockStart.gif        
{
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// ContentAlignment is an enumeration defined in the System.Drawing
InBlock.gif        
// namespace that specifies the alignment of content on a drawing 
InBlock.gif        
// surface.
InBlock.gif
        private ContentAlignment alignmentValue = ContentAlignment.MiddleLeft;
InBlock.gif
InBlock.gif        [
InBlock.gif        Category(
"Alignment"),
InBlock.gif        Description(
"Specifies the alignment of text.")
InBlock.gif        ]
InBlock.gif        
public ContentAlignment TextAlignment
ExpandedSubBlockStart.gif        
{
InBlock.gif
InBlock.gif            
get
ExpandedSubBlockStart.gif            
{
InBlock.gif                
return alignmentValue;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gif            
{
InBlock.gif                alignmentValue 
= value;
InBlock.gif
InBlock.gif                
// The Invalidate method invokes the OnPaint method described 
InBlock.gif                
// in step 3.
InBlock.gif
                Invalidate();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
protected override void OnPaint(PaintEventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
base.OnPaint(e);
InBlock.gif            StringFormat style 
= new StringFormat();
InBlock.gif            style.Alignment 
= StringAlignment.Near;
InBlock.gif            
switch (alignmentValue)
ExpandedSubBlockStart.gif            
{
InBlock.gif                
case ContentAlignment.MiddleLeft:
InBlock.gif                    style.Alignment 
= StringAlignment.Near;
InBlock.gif                    
break;
InBlock.gif                
case ContentAlignment.MiddleRight:
InBlock.gif                    style.Alignment 
= StringAlignment.Far;
InBlock.gif                    
break;
InBlock.gif                
case ContentAlignment.MiddleCenter:
InBlock.gif                    style.Alignment 
= StringAlignment.Center;
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
// Call the DrawString method of the System.Drawing class to write   
InBlock.gif            
// text. Text and ClientRectangle are properties inherited from
InBlock.gif            
// Control.
InBlock.gif
            e.Graphics.DrawString(
InBlock.gif                Text,
InBlock.gif                Font,
InBlock.gif                
new SolidBrush(ForeColor),
InBlock.gif                ClientRectangle, style);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

   晚了,今天写到这里,下一篇文章介绍怎样使用我们写好的控件。







本文转自纶巾客博客园博客,原文链接:http://www.cnblogs.com/guanjinke/archive/2006/12/04/582084.html,如需转载请自行联系原作者
目录
相关文章
|
7天前
|
C# UED 定位技术
WPF控件大全:初学者必读,掌握控件使用技巧,让你的应用程序更上一层楼!
【8月更文挑战第31天】在WPF应用程序开发中,控件是实现用户界面交互的关键元素。WPF提供了丰富的控件库,包括基础控件(如`Button`、`TextBox`)、布局控件(如`StackPanel`、`Grid`)、数据绑定控件(如`ListBox`、`DataGrid`)等。本文将介绍这些控件的基本分类及使用技巧,并通过示例代码展示如何在项目中应用。合理选择控件并利用布局控件和数据绑定功能,可以提升用户体验和程序性能。
19 0
|
C# Windows
wpf怎么使用WindowsFormsHost(即winform控件)
原文:wpf怎么使用WindowsFormsHost(即winform控件) 使用方法:   1、首先,我们需要向项目中的引用(reference)中添加两个动态库dll,一个是.
5287 0
|
12月前
|
Java C# 索引
C#之 十九 使用WinForm控件
C#之 十九 使用WinForm控件
179 0
|
存储 算法 数据可视化
应用C#设计winform的一些心得
近期,因工作需要,应用C#设计了一个winform界面,主要是用来实现人员的量化积分管理,类似于很多单位的绩效考核管理系统那种。坦言之,这其实只是个人第二次涉猎winform窗体应用的设计(上一次还要追溯6-7年前的院校时期),上手还是比较慢的,前后大概花了10天时间。因为最后功能上还算比较满意,特写此文以作总结,记录当下。
587 0
应用C#设计winform的一些心得
|
Windows
艾伟:WinForm控件开发总结(一)------开篇
我本人不是专业的控件开发人员,只是在平常的工作中,需要自己开发一些控件。在自己开发WinForm控件的时候,没有太多可以借鉴的资料,只能盯着MSDN使劲看,还好总算有些收获。现在我会把这些经验陆陆续续的总结出来,写成一系列方章,希望对看到的朋友有所帮助。
978 0
|
程序员
WinForm中使用DDE技术(含源码)
提起DDE技术,相信很多人不知道是啥东东,尤其是90后的程序员们。不过,有时候这个东西还是有用处的,用一句话可以总结:实现Winform程序间的通信。比如:两个Winform程序A和B需要实现通信,用DDE技术可以实现。
1373 0