Use. net sdk creation control in C #

Source: Internet
Author: User

Download Sample project-8 Kb

Introduction

In this tutorial, I will use the. NET architecture to create a simple clock control example, which is a clock that displays the current time,
I will instruct the reader to set a second and display the number of minutes.
The highlights of this article are the key points for creating this control. You can refer to the code here. The fastest way to create a control is to copy it from here
Sample Code of a beian control:

..Program FilesNGWSSDKSamplesQuickStartwinformssamplesCsWritingControlshelloworldcontrol

Copy the directoryMyControlDirectory

..Program FilesNGWSSDKSamplesQuickStartwinformssamplesCsWritingControlsMyControl

Rename the Hellowordlcontrol file in the directory to myControl.

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

Change helloworldcontrol in the following files to myControl:

  • Hostapp. cs
  • Makefile

Open the console window and enter nmake all. The following two files will be created:

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

Now the basic framework code has been established. We can test it by running mycontrol.exe.

Now we can start to write our control.

  1. We need to add some namespaces to be used. The namespace contains the classes involved in our control:

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

  2. The next step is to include some C # extension features that allow calling WINDOWS operating system functions.
    Obtain the system time function, so I made the following definition:

    // 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. Now we declare some member variables that will be used during object running.
    private Colorm_colorHands;private Colorm_colorFace;private boolm_bActivateClock;private System.Timers.Timer m_timer;

    Note that keywords should be introduced before declaring any variables, rather than being defined with variables like C ++.

  4. Define the constructor.

    Similar to Java, the method can be written internally. Although it needs to be modified frequently in the future, it is easy to modify.

    public MyControl(){ m_colorHands = Color.White; m_colorFace = Color.Blue; SetStyle(ControlStyles.Opaque, false); SetStyle(ControlStyles.ResizeRedraw, true);}
  5. The next step is to define some attributes, including a new feature: attribute tag, which provides Runtime library information for other subsystems.

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

    The code in Arc [] defines specific attributes. get and set functions are also available outside the object,
    To modify the color of the clock pointer, you can do this:

    someobj.HandColor = Color.Red;

    This statement implicitly calls the set function.

  6. Overload base functions

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

    Pay attention to the keywords used to reload basic functions.Override

    This Code calls the basic functions.OnPaint(base.OnPaint(pe);)

Other valuable aspects in the Code are: the object is built on the stack and does not need to perform the delete operation like in C ++. The garbage in NWGS
The collection function recycles objects allocated with NEW.

For example:

{ // ... Some code SolidBrush brush = new SolidBrush(Color.White) // Scope ends... no delete operator needed for brush}
Another feature of C # changes the value of a variable when calling a function.

See the following code:

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

Note:OutParameter.

We can define it as follows:

protected void CalculatePoint(Point pStart, out Point pEnd,                               int nPos, bool bFlag, Rectangle rc)

Mycontrol.exe has been created. Another test control method is to run WinDes.exe and create a new C # Win32Form,
Select Edit/Add under the Library menu and select mycontrol. dll.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.