golang development-windows platform solves the problem of users accidentally selecting the console window and causing the program to pause.

Go compilation command review
go build -o tool.exe will compile the main file in the current directory into the tool.exe executable file
:: Interfaceless compilation: go build -ldflags="-w -s -H windowsgui" -o tool .exe
:: Normal build (-ldflags="-w -s" to remove debugging information) go build -ldflags="-w -s" -o tool.exe

Problem description:
The windows tools developed by golang are packaged into exe executable files, and the console is selected to display real-time printing during compilation (that is, there is an interface to compile). However, the user will inadvertently select the console printing window with the cursor, causing the service to pause and the user cursor to In the console window area, press the Enter key to continue running.

Original code example :

package main

import (
	"fmt"
	"time"
)


func main() {
    
    
	// 在控制台输出一些测试信息
	for i := 1; i <= 10; i++ {
    
    
		fmt.Printf("第 %d 行\n", i)
		time.Sleep(time.Second * 2)
	}

	// 编译成.exe文件命令: go build tool.go
}

Problem symptoms :
Insert image description here

Insert image description here

Solution :
To solve the problem of the user accidentally selecting the console window and causing the program to pause, you can use the SetConsoleMode function under the Windows console to set the console input mode and disable the mouse selection function of the console window.
On the Windows platform, you can use the golang.org/x/sys/windows package to call the Win32 API. This package provides a set of Go interfaces called by the Windows system.

go get golang.org/x/sys/windows

Here is sample code:

package main

import (
	"fmt"
	"golang.org/x/sys/windows"
	"time"
)

const ENABLE_QUICK_EDIT_MODE = 0x0040

// ForbiddenConsoleEdit 禁止控制台窗口的鼠标选择功能
func ForbiddenConsoleEdit() error {
    
    
	hConsole, err := windows.GetStdHandle(windows.STD_INPUT_HANDLE)
	if err == nil {
    
    
		mode := uint32(0)
		err = windows.GetConsoleMode(hConsole, &mode)
		if err == nil {
    
    
			mode &^= ENABLE_QUICK_EDIT_MODE
			if err := windows.SetConsoleMode(hConsole, mode); err != nil {
    
    
				return err
			} else {
    
    
				return nil
			}
		} else {
    
    
			return err
		}
	} else {
    
    
		return err
	}
}

func main() {
    
    
	// 禁止控制台窗口的鼠标选择功能(鼠标点击控制器或选择控制台打印的内容,会暂停程序运行,鼠标移开程序才能继续运行)
	if err := ForbiddenConsoleEdit(); err != nil {
    
    
		fmt.Println("设置失败", err.Error())
	} else {
    
    
		fmt.Println("设置成功")
	}

	// 在控制台输出一些测试信息
	for i := 1; i <= 10; i++ {
    
    
		fmt.Printf("第 %d 行\n", i)
		time.Sleep(time.Second * 2)
	}

	// 编译成.exe文件命令: go build tool.go
}

Explanation:
In the sample code, we first use the syscall.Open function to open the Windows console device file "CONIN$" and obtain its handle hConsole. Then, obtain the input mode of the console through the GetConsoleMode function, remove the fast editing mode of the console (that is, the function of double-clicking the mouse to select text) through bit operations, and finally update the console input mode through the SetConsoleMode function.

In this way, the user cannot use the mouse to select text in the console window while the program is running, thereby avoiding the problem of inadvertently causing the program to pause.

Insert image description here

Guess you like

Origin blog.csdn.net/qq_38923792/article/details/130811387