Write Windows desktop app with Go-use form

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

A few months ago saw the news of go, after reading the introductory PPT, there is a feeling, this is my imagination of the language. The grammar is simple, the writing feels very good, the design everywhere reveals concise.

It's handy to start using it instead of Python to write some common gadgets. A few months later, increasingly inseparable from, and then in other languages, always feel missing something.

I love writing desktop apps, I've been using C + + for a long time, but I really don't like its complicated design, and I don't have a UI library that fits my mind. At this point there is a thought, with go to write a set of UI library. Very bold, very risky, even very two. A lot of people have warned that the UI library is a huge piece of stuff that can't be done by a single person. I also often struggle, but still did not resist this impulse, I did!

From the zero-based packaging Win32 API, the workload is very large, but fortunately already have a lot of predecessors already in C + + has done the same thing, so it is also smooth.

Well, if you don't say more, look at the code first! Is that the mule is horse-drawn out to sneak.

The first step is to install Gform, which is the UI library I encapsulated. Confirm that you are using the latest weekly version of Go, and then open the command line to run the following two lines.

Go get Github.com/allendang/gform

Go Install Github.com/allendang/gform

Done, because the GO command will automatically detect the library dependencies, so will also incidentally put W32, I encapsulated the Win32 API library, installed well.

OK, 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 came out, the application volume 706kb, no dependent DLL, with UPX compression after 219kb, very good, I am satisfied.

Now to add a button, click on it to pop up a "Hello World" message box.

Add the application to the "W32" package first

Import (
"Github.com/allendang/gform"
"Github.com/allendang/w32"
)

Then add a button

Btnok: = Gform. Newpushbutton (MainForm)
Btnok.setcaption ("Point Me")
Btnok.setpos (100, 10)
Btnok.onlbup (). Attach (Btnok_onlbup)

Add an event handler function

Func Btnok_onlbup (Arg *gform. EventArg) {
Gform. MsgBox (Arg. Sender (), "message", "Hello World", W32. Mb_ok|w32. Mb_iconinformation)
}

Get! The complete code after the 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 ("Point Me")
Btnok.setpos (100, 10)
Btnok.onlbup (). Attach (Btnok_onlbup)

MainForm. Show ()

Gform. Runmainloop ()
}

Run, how about?

Is it simple enough? Well, I admit, not simply, the purely coded way to create UI is very tormenting, especially for control positioning. Don't worry, Gform also support another way to create UI, using resource files, in fact, familiar with Win32 GUI system friends should be familiar with this way.

In addition, you may notice that the button style is not normal, and does not apply the control style of the Windows system, which will be mentioned in the next article.

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.