golang windows 程序

发布时间:2024-12-23 03:41:06

Windows是目前全球使用最广泛的操作系统之一,而golang(Go语言)作为性能优异的编程语言,自然也支持在Windows平台开发应用程序。本文将探讨在golang下如何开发Windows程序。

搭建开发环境

在开始之前,我们需要先搭建好golang的开发环境。首先,从官网(https://golang.org)下载并安装golang的Windows版本。安装完成后,我们可以通过运行go version命令验证是否安装成功。

创建一个简单的Windows程序

现在,我们来创建一个简单的Windows程序。我们可以通过以下代码创建一个基本的窗口:

package main

import (
	"fmt"
	"syscall"
	"unsafe"
)

type (
	HANDLE syscall.Handle
	HWND   HANDLE
	HDC    HANDLE
	HBRUSH HANDLE
)

const (
	CS_HREDRAW = 0x0002
	CS_VREDRAW = 0x0001
	WM_CREATE  = 0x0001
	WM_PAINT   = 0x000f
	COLOR_BTNFACE = 15
)

var (
	hInstance     syscall.Handle
	className     []uint16
	mainWindow    HWND
	background    HBRUSH
	defaultWndProcPtr uintptr
)

func MainWindowProc(hWnd HWND, uMsg uint32, wParam uintptr, lParam uintptr) uintptr {
	switch uMsg {
	case WM_CREATE:
		background = CreateSolidBrush(COLOR_BTNFACE)
		return 0
	case WM_PAINT:
		var ps PAINTSTRUCT
		hdc := BeginPaint(hWnd, &ps)
		Rectangle(hdc, 20, 20, 200, 100, background)
		EndPaint(hWnd, &ps)
		return 0
	default:
		return CallWindowProc(defaultWndProcPtr, hWnd, uMsg, wParam, lParam)
	}
}

func NewMainWindow() {
	instance, _ := syscall.GetModuleHandle(nil)
	hInstance = syscall.Handle(instance)

	className = syscall.StringToUTF16Ptr("GolangWin32WindowClass")
	defaultWndProcPtr = uintptr(GetProcAddress(GetModuleHandle(syscall.StringToUTF16Ptr("user32.dll")), "DefWindowProcW"))

	style := CS_HREDRAW | CS_VREDRAW
	var wc WNDCLASSEX
	wc.CbSize = uint32(unsafe.Sizeof(wc))
	wc.LpfnWndProc = syscall.NewCallback(MainWindowProc)
	wc.HInstance = hInstance
	wc.LpszClassName = className
	wc.Style = style
	RegisterClassEx(&wc)

	mainWindow, _ = CreateWindow(className, syscall.StringToUTF16Ptr("Golang Win32 Window"), WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT, 400, 300, 0, 0, hInstance, nil)
	if mainWindow == 0 {
		fmt.Println("Failed to create window")
		return
	}

	ShowWindow(mainWindow, SW_SHOWDEFAULT)
	UpdateWindow(mainWindow)

	var msg MSG
	for {
		if GetMessage(&msg, 0, 0, 0) == 0 {
			break
		}
		TranslateMessage(&msg)
		DispatchMessage(&msg)
	}
}

func main() {
	defer syscall.FreeLibrary(syscall.Handle(hInstance))
	NewMainWindow()
}

// 下面是对应Windows API函数的定义,可以通过golang.org/x/sys/windows包更方便地调用:
var (
	procCreateWindow       = user32.NewProc("CreateWindowExW")
	procRegisterClassEx    = user32.NewProc("RegisterClassExW")
	procGetMessage         = user32.NewProc("GetMessageW")
	procTranslateMessage   = user32.NewProc("TranslateMessage")
	procDispatchMessage    = user32.NewProc("DispatchMessageW")
	procShowWindow         = user32.NewProc("ShowWindow")
	procUpdateWindow       = user32.NewProc("UpdateWindow")
	procBeginPaint         = user32.NewProc("BeginPaint")
	procEndPaint           = user32.NewProc("EndPaint")
	procRectangle          = gdi32.NewProc("Rectangle")
	procCreateSolidBrush   = gdi32.NewProc("CreateSolidBrush")
	procCallWindowProc     = user32.NewProc("CallWindowProcW")
	procGetProcAddress    = kernel32.NewProc("GetProcAddress")
	procGetModuleHandle    = kernel32.NewProc("GetModuleHandleW")
)

type (
	WNDPROC uintptr
	HINSTANCE HANDLE
	LRESULT  uintptr
	LPARAM   uintptr
	WPARAM   uintptr
	DWORD    uint32
	WNDCLASSEX struct {
		CbSize        uint32
		Style         uint32
		LpfnWndProc   WNDPROC
		CbClsExtra    int32
		CbWndExtra    int32
		HInstance     HINSTANCE
		HIcon         HANDLE
		HCursor       HANDLE
		HbrBackground HANDLE
		LpszMenuName  *uint16
		LpszClassName *uint16
		HIconSm       HANDLE
	}
	MSG struct {
		HWnd    HWND
		Message uint32
		WParam  uintptr
		LParam  uintptr
		Time    DWORD
		Pt      POINT
	}
	POINT struct {
		X, Y int32
	}
	PAINTSTRUCT struct {
		Hdc         HDC
		FErase      int32
		RcPaint     RECT
		FRestore    int32
		FIncUpdate  int32
		rgbReserved [32]byte
	}
	RECT struct {
		Left, Top, Right, Bottom int32
	}
	COLORREF uint32
)

编译和运行程序

在代码中,我们通过调用Windows API函数来创建窗口、处理消息以及绘制图形。为了方便调用这些函数,我们使用了golang.org/x/sys/windows包。使用以下命令编译程序:

go build -ldflags="-H windowsgui" main.go

编译完成后,会生成一个可执行文件。双击运行该可执行文件即可看到我们创建的窗口。

总结

本文介绍了如何在golang下开发Windows程序,我们了解了搭建开发环境、创建简单的Windows程序以及编译和运行程序的方法。虽然使用golang开发Windows程序相较于其他语言来说可能稍显复杂,但对于熟悉golang的开发者来说,通过调用Windows API函数来实现自定义的窗口和功能并不是一件困难的事情。希望本文对你有所帮助,感谢阅读!

相关推荐