I saw the Go message a few months ago. After reading the introductory PPT, I felt like this is the language I imagined. The syntax is simple, the writing is superb, and the design is concise everywhere.
Then I began to use it to write some common gadgets instead of python, Which is handy. A few months later, it became increasingly indispensable. When I used other languages, I always felt that something was missing.
I like writing desktop applications. I have been using C ++ for a long time, but I really don't like its complicated design, and I have not found a friendly UI library. At this time, I came up with a thought, using Go to write a set of UI libraries. Very bold, adventurous, or even second-class. Many people have warned that the huge UI library cannot be accomplished by one person. I often struggle, but I still don't hold back this impulse. I started it!
It takes a lot of work to encapsulate win32 APIs from scratch, but many predecessors have already done the same thing in C ++, so it is still smooth.
Well, let's look at the code first! It's a donkey, it's a horse.
Step 1: first install gform, which is the encapsulated UI library. Confirm that the latest Weekly version of Go is used, and then open the command line to run the following two lines.
Go get github.com/AllenDang/gform
Go install github.com/AllenDang/gform
This is done because the go command will automatically detect library dependencies, so w32 and my encapsulated win32 api libraries will also be installed properly.
Now you can create an empty form.
Package main
Import (
"Github.com/AllenDang/gform"
)
Func main (){
Gform. Init ()
Mainform: = gform. NewForm (nil)
Mainform. SetSize (300,200)
Mainform. Center ()
Mainform. Show ()
Gform. RunMainLoop ()
}
Compile with the following command line
Go tool 8g app. go
Go tool 8l-s-Hwindowsgui-o app.exe app.8
Run app.exe. The form is displayed. The application size is kb and there is no dependent dll. It is 219kb after upx compression, which is good. I'm satisfied.
Now, add a button. Click it to bring up a "Hello world" message box.
Add the application for the "w32" Package first
Import (
"Github.com/AllenDang/gform"
"Github.com/AllenDang/w32"
)
Then add button
BtnOk: = gform. NewPushButton (mainform)
BtnOk. SetCaption ("Click me ")
BtnOk. SetPos (100, 10)
BtnOk. OnLBUp (). Attach (btnOK_OnLBUp)
Add event processing functions
Func btnOK_OnLBUp (arg * gform. EventArg ){
Gform. MsgBox (arg. Sender (), "message", "Hello world", w32.MB _ OK | w32.MB _ ICONINFORMATION)
}
Done! The complete code after modification is as follows:
Package main
Import (
"Github.com/AllenDang/gform"
"Github.com/AllenDang/w32"
)
Func btnOK_OnLBUp (arg * gform. EventArg ){
Gform. MsgBox (arg. Sender (), "message", "Hello world", w32.MB _ OK | w32.MB _ ICONINFORMATION)
}
Func main (){
Gform. Init ()
Mainform: = gform. NewForm (nil)
Mainform. SetSize (300,200)
Mainform. Center ()
BtnOk: = gform. NewPushButton (mainform)
BtnOk. SetCaption ("Click me ")
BtnOk. SetPos (100, 10)
BtnOk. OnLBUp (). Attach (btnOK_OnLBUp)
Mainform. Show ()
Gform. RunMainLoop ()
}
How about running?
Is it simple enough? Well, I admit that it is not simple. It is difficult to create a UI in a pure encoding mode, especially for control positioning. Don't worry, gform also supports another way to create a UI and use resource files. In fact, friends who are familiar with the win32 gui system should be familiar with this method.
In addition, you may also notice that the button style is not normal and does not apply the control style of the windows system. The solution will be mentioned in the next article.