Create | control use the. NET SDK Creation control in C #
Author: Norm Almond
Download sample Works-8 Kb
Introduced
In this tutorial, I will use the. NET architecture to create a simple clock control example, this control is a display of the current time clock,
I will instruct the reader to implement the second hand and display the hour number.
The article highlights the key point of creating this control, and the reader can refer to the code. The quickest way to create a control is to copy it from here.
Shell Control Sample Code:
Rename the Hellowordlcontrol file under the directory to mycontrol.
Helloworldcontrol.cs-> Mycontrol.cs
HELLOWORLDCONTROL.SRC-> MYCONTROL.SRC
Change the Helloworldcontrol in the following file to MyControl:
Hostapp.cs
Makefile
Open 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 that the basic framework code has been set up, we can test it by running Mycontrol.exe.
Now we can start writing our control.
We need to add some namespaces that are about to be used, namespace include the classes we have in control:
Using 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 some C # extension features that allow you to invoke Windows operating system features, and I can't find a similar
To get 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);
Now we declare some of the member variables that will be used during the run of the object.
Private Colorm_colorhands;private Colorm_colorface;
Private Boolm_bactivateclock;
Private System.Timers.Timer M_timer;
Note here that you want to introduce a keyword before declaring any variable, rather than being able to define it with a variable like C + +.
Defines a constructor.
Similar to Java, methods can be written in-house, although the future needs to be modified frequently, but the modification becomes 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 the attributes, which include a new feature: the attribute tag, which will provide Run-time library information for the 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 parentheses [] defines a specific property, and the Get and set functions are also available outside of the object.
To modify the color of the clock pointer, you can do this:
Someobj. Handcolor = color.red;
This sentence implicitly invokes the set function.
Overloaded base class functions
protected override void OnPaint (PaintEventArgs pe) {
Let-base class draw its stuff
Base. OnPaint (PE);
Draw code here ...
}
Note the keyword used to overload the base class function override
This code calls the base class function OnPaint (base. OnPaint (PE); )
Other valuable things in the code are that the object is built on the heap and does not require a delete operation like C + +. The rubbish in the Nwgs
The collection feature will reclaim objects that are assigned with new.
For example:
{
// ... Some Code
SolidBrush brush = new SolidBrush (color.white)
Scope ends ... no delete operator needed for brush
}
Another attribute of C # changes the value of the variable 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, which defines that the variable will be changed when the function is entered.
We can define this:
protected void Calculatepoint (Point pstart, out Point Pend,
int NPOs, BOOL bflag, Rectangle RC)
Mycontrol.exe has been built, another way to test control is to run WinDes.exe, and then build a new C # Win32form,
Select Edit/add under the Library menu and select Mycontrol.dll
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.