Use Go to write Windows desktop applications-use the Form Designer

Source: Internet
Author: User
Tags constant definition response code

At present, there is no mature design tool for writing Windows applications using Go. I am used to the powerful IDE of C #. I will feel that I will return to the beginning of liberation. In addition, the current design idea of gform requires users to be very familiar with win32 APIs, otherwise it will be very uncomfortable to use. This is also where I have been trying to improve the solution. After all, a class library is intended to be easy to use, but my mind full of OO ideas has not fully adapted to the Go design method, in the past, many self-written ideas used Go may cause problems, or even if it is set up, it may feel incompatible with the language. The most obvious thing is that after two weeks of hard work, I suddenly found that the simulation class was inherited, day! At that time, I felt that I really needed to stop. Otherwise, what I made according to the current inertia would be a mess.

My thoughts on the gform framework are still proceeding. After figuring out how the Go design pattern is, I believe it will be reconstructed.

Now, go to the topic and use the Form Designer as the interface. The principle is the old method of MFC. design the dialog box in the resource file and attach it at runtime.

Open ResEdit, add a dialog box, and place various controls on it. Is an actual example of my gadgets.

Set a meaningful ID for each control. For example, in the text box on the right of "filter", the ID is "IDC_FILTER ".

After saving, you will find that ResEdit automatically generates a header file with the same name as the resource file, as shown below.

Open this header file and find that the value of the Control ID is simply defined, as shown below.

Now we create a new go file called app. go, which copies all the constant definitions in the header file into the constant definition of Go, for example:

Const (
IDD_DIALOG1 = 104
IDC_CLOSE = 1006
)

As you can see, the ID of the dialog box is IDD_DIALOG1. Now we can use gform. NewDialogFromResId to read this dialog box.

Func main (){
Gform. Init ()

Mainform: = gform. NewDialogFromResId (nil, IDD_DIALOG1)
Mainform. Show ()

Gform. RunMainLoop ()
}

Run the command to see how it works? See the form. Now let's try the corresponding Button event, use gform. AttachPushButton, and then the corresponding OnLBUp (On Left Button Up is when the Left mouse Button pops Up ).

BtnClose = gform. AttachPushButton (mainform, IDC_CLOSE)
BtnClose. OnLBUp (). Attach (btnClose_OnLBUp)

 

Func btnClose_OnLBUp (arg * gform. EventArg ){

// Event response code is written here

}

The Event Response Function in gform really misses the delegate in C #. Each event can Attach multiple event processing functions, which are called by a single thread in turn. The event response function has only one parameter. Let's take a look at the definition of gform. EventArg.

Type EventArg struct {
Sender Controller
Data interface {}
}

Sender, as its name implies, represents the control that inspires events. The event information is included in data. For example, for mouse button events, data is of the MouseEventData type and is defined as follows.

Type MouseEventData struct {
X, Y int
Button int
Wheel int
}

Other events have their own event data, which will not be listed here. Go interface {} is quite useful. It is safe and efficient to add its static type check.

 

This completes. Currently, gform supports Label, Button, Combobox, Edit, ListView, and ProgressBar (because only these controls are used in the gadgets I wrote ). Later, more widgets will be supported.

In the next article, I will talk about how to customize controls. First, I would like to show you the small software interface that is being developed recently. It is far from complete, but I am quite satisfied with it. Haha

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.