Go language Windows Programming (2)-First window Program

Source: Internet
Author: User

In the previous article, we successfully created a pop-up message window through winapi, using the MessageBox (...) interface. Today, we officially started writing window programs. If you are familiar with Windows programs in C language, you will find that the programs behind them are very similar to those in C language, actually, the Go syntax is used to call windows APIs. The basic structure is similar. After talking about this, let's start with the code:

As shown above, the first step is to import some packages that need to be used.

Github.com/lxn/go-winapi is an open-source package that encapsulates windows APIs. As in the previous article, originwndproc declares that a window handle will be used later.

The following is a constant that declares the window height and width. The following _ text methods are used to convert string-type data to unsigned 16-bit integer pointers, which need to be called when writing text to a window.

 

Let's look at the rest:

The above is a window message processing program. It is mainly used to receive window messages and process them according to the Message type. If it is not clear, it doesn't matter. Currently, you only need to know that it is a method for processing window messages, the specific mechanism will be discussed later.

 

Finally, write the main function:

 

 

Func main (){
VaR message msg
VaR hwnd
VaR wproc uintptr
Hwnd = createdomainwex (
Ws_ex_clientedge,
_ Text ("edit "),
_ Text ("Hello Gui "),
Ws_overlappedwindow,
(Getsystemmetrics (sm_cxscreen)-winwidth)> 1,
(Getsystemmetrics (sm_cyscreen)-winheight)> 1,
Winwidth,
Winheight,
0,
0,
Getmodulehandle (NiL ),
Unsafe. pointer (NiL ))
Wproc = syscall. newcallback (wndproc)
Originwndproc = hwnd (setwindowlong (hwnd, gwl_wndproc, int32 (wproc )))
Showwindow (hwnd, sw_show)
For {
If getmessage (& message, 0, 0) = 0 {break}
Translatemessage (& message)
Dispatchmessage (& message)
}
OS. Exit (INT (message. wparam ))
}

 

The createmediawex method creates a window. For details about the parameters of the method, see the Windows API. After the window is created, a window handle hwnd is returned, and showwindow is called to pass the hwnd handle to it. The window is displayed successfully.

The last for loop is to receive window messages in the message queue cyclically and send them to the previous message processing service. Of course, the actual process is not that simple. At present, we mainly implement functions first. Recently, I have been busy with many projects and people are getting increasingly lazy. So I wrote some simple articles. Don't throw bricks ...... In the future, we will gradually fill in the specific knowledge details. Of course, if you can't wait, you can study it on your own. In fact, it is the same as the windows program design of C. Is it exciting? Want to see Windows in go language? So hurry up! Final running result:

 

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.