Use .NET SDK to create control in C #

zhaozj2021-02-08  227

Control of use to create .NET SDK in C #: Norm Almond

Download Sample Project - 8 KB

In this tutorial, I will use the .NET schema to create a simple clock control example, this control is a clock showing the current time, I will guide the reader to implement the second hand and display the number of minutes. Article Exaggeration is a key point to create this control, and readers can refer to the code. Creating the fastest way to control is to copy a control sample code from here: ../program files / ngwssdk / Samples / CS / WritingControls / HelloWorldControl copies directory to myControl directory ../program files / NGWSSDK / Samples / QuickStart / WinForms / Samples / CS / WritingControls / MyControl Rename the HelloWordLControl file in the directory to MyControl.

HelloWorldControl.cs -> MyControl.cs HelloWorldControl.src -> MyControl.SRC Change the HelloWorldControl in the following files to MyControl:

Hostapp.cs makefile opens the console window to 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 run MyControl.exe. Now we can start writing our control.

We need to add some upcoming namespaces, namespace contains the classes we involved in the control: use system.componentmodel; // NEeded for Control Support

Using system.timers; // needed to support Timer

Using system.Runtime.Interopservices; // Needed for structlayout Attribute

The next step is to include C # extension characteristics that allow calls for Windows operating system functions. I can't find a function similar to obtaining system time, 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 wheh;

Public ushort wminute;

Public Ushort WSecond;

Public ushort wmilliseconds;

}

// definition of WinApi getLocalTime Function [DLLIMPORT ("kernel32.dll")]]]]

Public Static Extern Void getLocalTime (SystemTime ST);

Now we have declared some members variables that will be used during the operation. Private color_colorhands; private color_colorface;

Private boolm_bactivateclock;

Private system.timers.timer m_timer;

It should be noted here that to be introduced into the keyword before declaring any variables, rather than the variable can be defined together like C . Define the constructor. Similar to Java, the method can be written internally, although it needs to be modified in the future, but modifications become easy. Public myControl () {m_colorhands = color.white;

m_colorface = color.blue;

SetStyle (ControlStyles.opaque, false);

SetStyle (ControlStyles.ResizeRedRAW, TRUE);

} The next step is to define some properties, which contains a new feature: property tag, which will provide runtime library information for other subsystems. [

Category ("Clock"),

Description ("Hands Color for Clock",

DefaultValue (0xfffff),

]

Public Color Handscolor {

Get {

Return M_ColorHands;

}

SET {

m_colorhands = value;

Invalidate ();

Update ();

}

} The code in parential arc [] defines a specific property, the GET and SET functions are available outside of the object. To modify the color of the clock pointer, you can do this: someobj.handcolor = color.red; this sentence is implied Set function. Overload base class function protected override void onpaint (painteventargs pe) {

// Let base Class Draw ITS Stuff First

Base.onpaint (PE);

// Draw Code Here ...

} Please note that the keyword used to overrunate the base class Override is called the base class function onpaint (Base.onPaint (PE);) other valuable places in the code: the object is built on the heap, and It is necessary to perform DELETE operations in C . The garbage collection feature in NWGS will reclaim objects allocated with the New. E.g:{

// ... Some Code

Solidbrush Brush = New Solidbrush (Color.White)

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

} Another feature of C # When the value of the variable is changed when the function is called. Please see the following code: CalculatePoint (ptstart, out ptend, (st.whour * 5) (st.wminute / 12), false, rc); Note the OUT parameter, this definition When the entry function will be changed. We can define this: protected void cagculatepoint (Point Pstart, Out Point Pend,

INT NPOS, BOOL BFLAG, Rectangle RC) MyControl.exe has been built, another test control method is to run Windes.exe, then create a new C # Win32Form, select the 2 / add / add of the library menu and select MyControl.dll

转载请注明原文地址:https://www.9cbs.com/read-1565.html

New Post(0)