Interface Development (I)

Source: Internet
Author: User

Interface Development (I)

Author: zhjp11 Source: Xiao Space

Interface development overview

Each software must have its own software interface. for software development, the software interface is not necessarily the most important, but also very important. If a software can be done well on the interface, attracting the customer's attention, the software will be half successful.

Software from all walks of life now adds their own skin colors, showing different features. For example, QQ, MSN, Foxmail, etc. These software have modified the interfaces of their own software and made their interfaces perfect and beautiful. It feels comfortable to use. I am also a software engineer, so I plan to make my software as beautiful as those software products, so I am looking for a solution for my software interface on the Internet. Of course there are many. Below are several examples:

1. Use skin components (IrisSkin2.dll)

This is a set of software interfaces developed by Dongri software company. IrisSkin is the easiest interface enhancement. NET (WinForm) package developed for Microsoft Visual Studio. NET. It automatically adds skin replacement support for your application, and does not need to change your designed Form or add a line of code! You no longer need to spend a lot of time making your applications more beautiful. Of course, he is charged, but there are many cracked versions on the Internet. Of course, I also downloaded a set, which is quite convenient to use.

: Irisskin.rar

Ii. Custom Development

This is more difficult. You need to know a lot about Windows development. One of the two methods is to use custom skin forms, buttons, and other spaces to inherit. net Framework provides a majority of the controls you use, and then extends the WndProc method or some other methods, which is the most common method in the city. In addition, the IrisSkin method is used to Hook the underlying class NativeWindow. The main example is as follows:

1. Su Fei's blog:

Http://www.cnblogs.com/sufei/archive/2010/03/13/1685236.html

2. Skin in CsharpWin

Http://www.csharpwin.com/csharpresource/2992.shtml

I have benefited a lot from both of them.

Summary of the above skin development, I learned that the so-called skin development is nothing more than the use of pictures, colors and other content to repaint the form or overwrite, that is, the Paint and NCPaint operations in WndProc are reloaded. For Winform, a form is divided into two parts: Non-Client Area and Client Area, for example:

To put it simply, draw the Client Area and Non-Client Area. In Form, the Paint and NCPaint of WndProc are the same.

3. My implementation method.

My implementation method mainly studies the two programs of Su Fei and CshaorpWindow, as well as the decompilation source code of IrisSkin. I think the implementation method is better than the IrisSkin method, the implementation of IrisSkin is to use a simple NativeWindow class locally, which is a registration that can implement simple forms and other functions, that is, Hook. I followed this method and wrote a simple program. Basically, the IrisSkin content can be implemented in the same way as that of IrisSkin. The Component of SkinEngine is added to the form, so that the form displays the Office2007 style, as shown in:

The Label on the form can be displayed as the skin color, but the color in the dialog box has not been processed. It also requires a lot of processing, including skin design and the design of various controls in the form. I will write relevant content in subsequent topics.

Source code: SkinEngines2010-03-17.rar

(1) --- Hook all forms

In the previous article, I gave an overview of interface development and some experiences and basic principles on the development interface. From this article, I started to explain all the processes of interface development, the development interface process is explained step by step. This article mainly describes Hook programming and Hook all forms.

Hook. For most programmers, this word is no stranger. For Windows systems, Message transmission runs through the entire system. Message is an integer, which has a corresponding meaning. In winuser. h of C ++, we can see many common messages. The Hook and Message are inseparable. The Chinese explanation of the Hook is "Hook", which means to monitor the delivery of messages in the system, that is, before the Message is delivered to the final Message processing, processes specific messages.

For Hook, there are three major API functions under development, all of which are stored in the User32.dll file. These three functions are:

Hook
1 /// <summary>
2 // SetWindowsHookEx
3 /// </summary>
4 /// <param name = "idHook"> </param>
5 /// <param name = "lpfn"> </param>
6 /// <param name = "hMod"> </param>
7 // <param name = "dwThreadId"> </param>
8 /// <returns> </returns>
9 [DllImport ("User32.dll", CharSet = CharSet. Auto)]
10 public static extern IntPtr SetWindowsHookEx (int idHook, HookProc lpfn, int hMod, int dwThreadId );
11
12 /// <summary>
13 // CallNextHookEx
14 /// </summary>
15 /// <param name = "hhk"> </param>
16 /// <param name = "nCode"> </param>
17 /// <param name = "wParam"> </param>
18 /// <param name = "lParam"> </param>
19 /// <returns> </returns>
20 [DllImport ("User32.dll", CharSet = CharSet. Auto)]
21 public static extern IntPtr CallNextHookEx (IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam );
22
23 /// <summary>
24 // UnhookWindowsHookEx
25 /// </summary>
26 /// <param name = "hhk"> </param>
27 /// <returns> </returns>
28 [DllImport ("User32.dll", CharSet = CharSet. Auto)]
29 public static extern bool UnhookWindowsHookEx (IntPtr hhk );

The three methods are respectively add a Hook, release the Hook, and execute the next Hook. You do not need to introduce the parameters. There is a lot of information on the Internet.

The next step is to intercept messages in Windows Forms.

I created a project named SkinEngines and added these three methods to NativeMethod. In SetWindowsHookEx, a parameter is required to be the name of an aspect. in C #, the delegate is used for implementation. Therefore, a dedicated place for saving the delegate is also created and placed in the Delegates. cs file.

HookProc

/// <Summary>
/// HookProc -- HookProc
/// </Summary>
/// <Param name = "nCode"> </param>
/// <Param name = "wParam"> </param>
/// <Param name = "lParam"> </param>
/// <Returns> </returns>
Public delegate IntPtr HookProc (int nCode, IntPtr wParam, IntPtr lParam );

Create a Component named SkinEngine. Added some operations to the SkinEngine constructor to Hook the form. The Code is as follows:

1 /// <summary>
2 // SkinEngine -- Skin All Form, Dialog, Control
3 /// </summary>
4 public partial class SkinEngine: Component
5 {
6 # region Field
7 /// <summary>
8 // CBTHook -- Hook WH_CBT
9 /// </summary>
10 private static HookProc _ cbtHook;
11
12 /// <summary>
13 // Hook
14 /// </summary>
15 private static IntPtr Hook;
16
17 /// <summary>
18 // Current SkinEngine
19 /// </summary>
20 internal static SkinEngine Engine;
21
22 /// <summary>
23 // Skinned handled
24 /// </summary>
25 internal static ArrayList SkinHandleList = new ArrayList ();
26 # endregion
27
28 # region Constructor
29 /// <summary>
30 /// Constructor
31 /// </summary>
32 public SkinEngine ()
33 {
34 InitializeComponent ();
35 // Internal Constructor
36 this. InternalConstructor ();
37}
38
39 /// <summary>
40 // Constructor With container
41 /// </summary>
42 // <param name = "container"> </param>
43 public SkinEngine (IContainer container)
44 {
45 container. Add (this );
46
47 InitializeComponent ();
48
49 // Internal Constructor
50 this. InternalConstructor ();
51}
52 # endregion
53
54 # region InternalConstructor
55 /// <summary>
56 // the Internal Constructor to Create Hook and Get List of all control to skin.
57 /// </summary>
58 private void InternalConstructor ()
59 {
60 // IsDesignMode = false
61 if (! IsDesignMode)
62 {
63 // Check Engine
64 if (Engine = null)
65 {
66 // Set Engine
67 Engine = this;
68
69 // Hook Process
70 if (Hook = IntPtr. Zero)
71 {
72 _ cbtHook = new HookProc (SkinEngine. FnHookProc );
73 Hook = NativeMethod. SetWindowsHookEx (5, _ cbtHook, 0, AppDomain. GetCurrentThreadId ());
74 Application. ApplicationExit + = new EventHandler (Application_ApplicationExit );
75}
76}
77}
78}
79 # endregion
80
81 # region FnHookProc
82 // <summary>
83 // FnHookProc
84 /// </summary>
85 // <param name = "nCode"> </param>
86 // <param name = "wParam"> </param>
87 // <param name = "lParam"> </param>
88 /// <returns> </returns>
89 private static unsafe IntPtr FnHookProc (int nCode, IntPtr wParam, IntPtr lParam)
90 {
91 if (Engine! = Null)
92 {
93 switch (nCode)
94 {
95 case 5:
96 // Get Skin Control
97 Control control = Control. FromHandle (wParam );
98 // Control is null it Can be Dialog
99 if (control = null)
100 {
101 StringBuilder builder = new StringBuilder (260 );
102 NativeMethod. GetClassName (wParam, builder, 260 );
103 // #32770 is Dialog
104 if (builder. Length = 6 & builder. ToString () = "#32770 ")
105 {
106 // Add to SkinHandleList
107 SkinHandleList. Add (wParam );
108 // Print
109 Debug. WriteLine (builder. ToString ());
110}
111 break;
112}
113 if (! SkinHandleList. Contains (wParam) & (control is Form ))
114 {
115 // Add to SkinHandleList
116 SkinHandleList. Add (wParam );
117 // Print all control's Name
118 Debug. WriteLine (control. Name );
119}
120 break;
Default 121:
122 break;
123}
124}
125 return NativeMethod. CallNextHookEx (Hook, nCode, wParam, lParam );
126}
127 # endregion
128
129 # region Application_ApplicationExit
130 /// <summary>
131 // Application_ApplicationExit
132 /// </summary>
133 /// <param name = "sender"> </param>
134 /// <param name = "e"> </param>
135 private void Application_ApplicationExit (object sender, EventArgs e)
136 {
137 Engine. Dispose (false );
138}
139 # endregion
140
141 # region Property
142 /// <summary>
143 // Gets or sets a value indicating whether we are currently in design mode.
144 /// </summary>
145 /// <value>
146 /// <c> true </c> if this we are in design mode; otherwise, <c> false </c>.
147 /// </value>
148 internal static bool IsDesignMode {get; set ;}
149 # endregion
150}

In this way, the entire form can be hooked. NativeMethod. SetWindowsHookEx sets the WM_CBT message, which is used to activate and display the Windows form. In this way, the form is recorded. Now, only the recorded form is displayed and the name of the form is written. The result is as follows:

This article will be written for the time being. The next article will Hook and draw the Form message.

Code: SkinEngines20100318.rar

(2) --- NativeWindow

NativeWindow is an underlying class provided by. net Framework. Microsoft's official explanation is: NativeWindow Provides a low-level encapsulation of a window handle and a window procedure. this is equivalent to not speaking, because NativeWindow does not clearly understand what it is, which means a low-level encapsulation of the form handle and form process. Later, I found a lot of materials to understand the meaning of the materials.

Source:

Http://www.diybl.com/course/4_webprogram/asp.net/netjs/2007921/72804.html

This article describes how to use NativeWindow to monitor WndProc messages. The content is long and the number of pages is large. after hard work and patience, we finally completed the example. The code is NativeWindowApplication.rar.

Http://www.codeproject.com/KB/dialog/OpenFileDialogEx.aspx

This article is from CodeProject. The code written by foreigners is relatively good. The OpenFileDialog subclass of Windows is displayed as a custom OpenFileDialog, and many events are added. Very admired.

After carefully reading these two articles, I finally learned about the role of NativeWindow. NativeWindow provides the underlying encapsulation and AssignHandle and RealeseHandle methods, it mainly hooks the forms that have registered the form handle, and submits the events of the forms to Windows for processing. I used Refector.net to view the AssignHandle method in NativeWindow and verified my statement. There is such a code in AssignHandle:

UserDefWindowProc = UnsafeNativeMethods. GetProcAddress (new HandleRef (null, UnsafeNativeMethods. GetModuleHandle ("user32.dll"), lpProcName );

For Win32 developers, we may all know the meaning of this Code, which is also a kind of Hook. But it is a more advanced Hook method. Here we will not go into detail. Now we only need to know that he can Hook the form we registered and load our code to the desired position.

Then I use NativeWindow to get all the events of all forms, and then output the Hook time. The result is as follows:

The download code is as follows: SkinEngines20100319.rar

Source:

Http://www.uml.org.cn/jmshj/201004074.asp

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.