当前位置:编程学习 > C#/ASP.NET >>

在C#中使用.NET SDK创建控制

下载示例工程 - 8 Kb

介绍

在这篇教程中,我将使用.NET架构创建一个简单的时钟控制示例,这个控制是一个显示当前时间的时钟,
我将指导读者实现秒针并显示钟点数。
文章加亮处是创建这个控制的关键点,读者可以参考其中的代码。创建一个控制的最快的方法是从这里拷
贝一个控制示例代码:

..Program FilesNGWSSDKSamplesQuickStartwinformssamplesCsWritingControlshelloworldcontrol

将目录拷贝到 MyControl目录

..Program FilesNGWSSDKSamplesQuickStartwinformssamplesCsWritingControlsMyControl

将目录下的Hellowordlcontrol文件重命名为myControl.

  • Helloworldcontrol.cs -> mycontrol.cs
  • Helloworldcontrol.src -> mycontrol.src

将下列文件中的helloworldcontrol改为myControl:

  • Hostapp.cs
  • Makefile

打开控制台窗口输入 NMAKE ALL. 将建立下列两个文件:

  • MyControl.exe – The application that hosts the control
  • MyControl.DLL – The actual control.

现在基本的框架代码已经建立好了,我们可以通过运行mycontrol.exe来测试。

现在我们可以开始编写我们的控制.

  1. 我们需要添加一些即将使用的 namespaces, namespace包含了我们在控制中所涉及到的类 :

    using System.ComponentModel;// Needed for control support
    
    using System.Timers; // Needed to support timer
    
    using System.Runtime.InteropServices;// Needed for StructLayout attribute 
    
    

  2. 下一步是包含一些允许调用WINDOWS操作系统功能的C#扩展特性,我无法找到一个类似
    获得系统时间的函数,所以我作了如下定义:

    // Definition of WINAPI SYSTEMTIME structure 
    
    [StructLayout(LayoutKind.Sequential)]
    
    public class SystemTime {
    
     public ushort wYear;
    
     public ushort wMonth;
    
     public ushort wDayOfWeek;
    
     public ushort wDay;
    
     public ushort wHour;
    
     public ushort wMinute;
    
     public ushort wSecond;
    
     public ushort wMilliseconds;
    
    }
    
    
    
    // Definition of WINAPI GetLocalTime function[DllImport("Kernel32.dll")]
    
    public static extern void GetLocalTime(SystemTime st);
    
    
  3. 现在我们申明一些在对象运行期间将被使用的成员变量。
    private Colorm_colorHands;private Colorm_colorFace;
    
    private boolm_bActivateClock;
    
    private System.Timers.Timer m_timer;
    
    

    这里要注意的是,要在声明任何变量之前引入关键字,而不是像C++那样可以跟变量一起定义。

  4. 定义构造函数.

    跟Java类似,方法可以在内部编写,将来虽然需要经常修改,但修改变得简单易行了。

    public MyControl(){
    
     m_colorHands = Color.White;
    
     m_colorFace = Color.Blue;
    
     SetStyle(ControlStyles.Opaque, false);
    
     SetStyle(ControlStyles.ResizeRedraw, true);
    
    }
  5. 下一步是定义一些属性,这里包含了一个新的功能:属性标记,他将为其它子系统提供运行时库信息。

    [
    
    Category("Clock"),
    
    Description("Hands color for Clock"),
    
    DefaultValue(0xFFFFFF),
    
    ]
    
    public Color HandsColor {
    
     get {
    
      return m_colorHands;
    
     }
    
    
    
     set {
    
      m_colorHands = value;
    
      Invalidate();
    
      Update(); 
    
     }
    
    }

    括弧[ ]中的代码定义了特定的属性, get 和 set 函数对于对象外面也是可用的,
    要想修改时钟指针的颜色,你可以这样做:

    someobj.HandColor = Color.Red;

    此句隐含调用了set函数。

  6. 重载基类函数

    protected override void OnPaint(PaintEventArgs pe) {
    
     // Let base class draw its stuff first
    
     base.OnPaint(pe);
    
    
    
     // Draw code here...
    
    }

    请注意用来重载基类函数的关键字 override

    这段代码调用了基类函数 OnPaint (base.OnPaint(pe); )

在代码中其它有价值的地方是:对象建立在堆上,且不需要象C++中进行delete操作. NWGS 中的垃圾
收集功能将会对用NEW分配的对象进行回收。

例如:

{

 // ... Some code

 SolidBrush brush = new SolidBrush(Color.White)



 // Scope ends... no delete operator needed for brush

}
C#的另一个特性时在调用函数时更改变量的取值。

请看如下代码:

CalculatePoint(ptStart, out ptEnd,(st.wHour*5)+(st.wMinute/12), false, rc);

请注意 out 参数,这样定义当进入函数后变量将被更改。

我们可以这样定义:

protected void CalculatePoint(Point pStart, out Point pEnd, 

                              int nPos, bool bFlag, Rectangle rc)

Mycontrol.exe 已经建好了,另一种测试控制的方法是运行 WinDes.exe, 然后建立一个新的 C# Win32Form,
选择Library 菜单下Edit/Add 并且选择 mycontrol.dll

补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,