Skip to content

Instantly share code, notes, and snippets.

@ggaaooppeenngg
Created May 6, 2014 02:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ggaaooppeenngg/8cff70ae9e7a7aa32d7c to your computer and use it in GitHub Desktop.
Save ggaaooppeenngg/8cff70ae9e7a7aa32d7c to your computer and use it in GitHub Desktop.
go调用dll打印彩色字体
package main
import(
"syscall"
"fmt"
)
const(
//标准输出宏
STD_OUTPUT_HANDLE = uint32(-11 & 0xFFFFFFFF)
)
var(
err error
kernel32,_ = syscall.LoadLibrary("kernel32.dll")
//设置console属性
setConsoleTextAttribute ,_ = syscall.GetProcAddress(kernel32,"SetConsoleTextAttribute")
//获取标准输入输出的函数
getStdHandle ,_ = syscall.GetProcAddress(kernel32,"GetStdHandle")
//标准输出
hCon uintptr
)
func init(){
//nargs 代表参数个数
var nargs uintptr = 1
//参数需要全部转成uinptr
hCon,_,_ = syscall.Syscall(uintptr(getStdHandle),nargs,uintptr(STD_OUTPUT_HANDLE),0,0)
}
func SetConsoleTextAttribute(hConsoleOutput uintptr, wAttributes uint32) bool{
var nargs uintptr = 2
ret,_,_:=syscall.Syscall(setConsoleTextAttribute,nargs,hConsoleOutput,uintptr(wAttributes),0)
return ret!=0
}
func main(){
defer syscall.FreeLibrary(kernel32)
SetConsoleTextAttribute(hCon,0x001f)
fmt.Println("Hello world!")
//恢复默认值
SetConsoleTextAttribute(hCon,0x0007)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment