Files
minigui-docs/programming-guide/MiniGUIProgGuidePart1Chapter05.md
2019-10-28 15:34:18 +08:00

5.6 KiB
Raw Blame History

Advanced Programming of Control

User-Defined Control

You can also register your own control class by calling RegisterWindowClass function, and create an instance of your control class. If the program will not use a certain user-defined control class any more, it should use UnregisterWindowClass function to unregister this user-defined control class. Please refer to “control class” of section 3.2.4 of this guide for the usage of the above two functions.

Subclassing of Control

Using the framework of a control class and control instance can not only improve the code reusability, but also extend the existing control class conveniently. For example, when you need to create an edit box which only allows inputting digits, you can realize by overriding the existing edit box control class rather than write a new control class. In MiniGUI, this technique is called subclassing or window derived. Methods of subclassing have three types:

  • The first one is to perform subclassing on the created control instance, and the result of subclassing only affects this control instance.
  • The second one is to perform subclassing on a certain control class, which will affect all the control instances created later of this control class.
  • The last one is to register a subclassing control class based on a certain control class, which will not affect the original control class. In Windows, this technique is also called super-subclassing.

In MiniGUI, the subclassing of a control is actually implemented by replacing the existing window procedure. The codes in List 1 create two subclassing edit boxes by subclassing: one only allows inputting digits, and the other only allows inputting letters:

List 1 Subclassing of control

Combined Use of Controls

We can also combine two different controls together to achieve a certain special effect. In fact, the predefined control class of the combo box is a typical one of combining controls. When we combine different controls, we can encapsulate and register the combined control to be a new control class, and can also use it directly without encapsulation.

To illustrate the method to combine controls more clearly, we can assume that we want to implement a time editor. This time editor displays the time in form of “08:05:30”, and we need further add a method to edit the time neatly according the users requirement. To meet this requirement, we combine the edit box and the spin box together which implement the following functions, respectively:

  • The edit box displays the time in form of “HH:MM:SS”.
  • When the input focus is in the edit box, the user can not edit the time directly, but must control the time value where the caret is with the arrow keys and PageDown and PageUp keys. So we must subclass this edit box to catch the key-pressed in it and perform the appropriate handling.
  • Place a spin box beside the edit box. The user can adjust the time element where the caret is to increase or decrease by clicking the spin box. To achieve this goal, we can use the function of the spin box, and set the handle of the target window to be the edit box.

Thus, the time editor can work normally. Partial codes of this program are listed in List 2, and please refer to timeeditor.c file of the sample program package of this guide for the complete source code. Fig 6.1 shows the running effect of the timeeditor.

List 2 Time Editor
Figure 1 Running effect of time editor

<< Foundation of Control Programming | Table of Contents | Menu >>

[Writing DRI Engine Driver for Your GPU]: /supplementary-docs/Writing-DRI-Engine-Driver-for-Your-GPU.md [Writing MiniGUI Apps for 64-bit Platforms]: /supplementary-docs/Writing-MiniGUI-Apps-for-64-bit-Platforms.md

[Building MiniGUI]: /user-manual/MiniGUIUserManualBuildingMiniGUI.md [Compile-time Configuration]: /user-manual/MiniGUIUserManualCompiletimeConfiguration.md [Runtime Configuration]: /user-manual/MiniGUIUserManualRuntimeConfiguration.md [Tools]: /user-manual/MiniGUIUserManualTools.md [Feature List]: /user-manual/MiniGUIUserManualFeatureList.md