Improved program appearance by setting the correct font, colors, and other styles.

This commit is contained in:
Pietro Gagliardi 2014-02-10 20:48:08 -05:00
parent bcc8751c8b
commit 07c791331c
4 changed files with 118 additions and 2 deletions

View File

@ -12,6 +12,7 @@ import (
var (
user32 = syscall.NewLazyDLL("user32.dll")
kernel32 = syscall.NewLazyDLL("kernel32.dll")
gdi32 = syscall.NewLazyDLL("gdi32.dll")
)
type HANDLE uintptr
@ -21,6 +22,8 @@ type HMENU HANDLE
const (
NULL = 0
FALSE = 0 // from windef.h
TRUE = 1 // from windef.h
)
type ATOM uint16

22
main.go
View File

@ -106,6 +106,14 @@ func wndProc(hwnd HWND, msg uint32, wParam WPARAM, lParam LPARAM) LRESULT {
panic("unreachable")
}
func setFontAll(hwnd HWND, lParam LPARAM) (cont bool) {
_, err := SendMessage(hwnd, WM_SETFONT, WPARAM(lParam), LPARAM(TRUE))
if err != nil {
fatalf("error setting window font: %v", err)
}
return true
}
const className = "mainwin"
func main() {
@ -119,6 +127,10 @@ func main() {
if err != nil {
fatalf("error getting WinMain nCmdShow: %v", err)
}
font, err := getStandardWindowFont()
if err != nil {
fatalf("error getting standard window font: %v", err)
}
icon, err := LoadIcon_ResourceID(NULL, IDI_APPLICATION)
if err != nil {
@ -135,7 +147,7 @@ func main() {
HInstance: hInstance,
HIcon: icon,
HCursor: cursor,
HbrBackground: HBRUSH(COLOR_WINDOW + 1),
HbrBackground: HBRUSH(COLOR_BTNFACE + 1),
}
_, err = RegisterClass(wc)
if err != nil {
@ -143,7 +155,7 @@ func main() {
}
hwnd, err := CreateWindowEx(
WS_EX_OVERLAPPEDWINDOW,
0,
className, "Main Window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 320, 240,
@ -249,6 +261,12 @@ func main() {
fatalf("error creating checkbox: %v", err)
}
setFontAll(hwnd, LPARAM(font))
err = EnumChildWindows(hwnd, setFontAll, LPARAM(font))
if err != nil {
fatalf("error setting font on controls: %v", err)
}
_, err = ShowWindow(hwnd, nCmdShow)
if err != nil {
fatalf("error showing window: %v", err)

73
stdfont.go Normal file
View File

@ -0,0 +1,73 @@
// 10 february 2014
package main
import (
// "syscall"
"unsafe"
)
const (
SPI_GETNONCLIENTMETRICS = 0x0029
LF_FACESIZE = 32 // from wingdi.h
)
type LOGFONT struct {
lfHeight int32
lfWidth int32
lfEscapement int32
lfOrientation int32
lfWeight int32
lfItalic byte
lfUnderline byte
lfStrikeOut byte
lfCharSet byte
lfOutPrecision byte
lfClipPrecision byte
lfQuality byte
lfPitchAndFamily byte
lfFaceName [LF_FACESIZE]uint16
}
type NONCLIENTMETRICS struct {
cbSize uint32
iBorderWidth int
iScrollWidth int
iScrollHeight int
iCaptionWidth int
iCaptionHeight int
lfCaptionFont LOGFONT
iSmCaptionWidth int
iSmCaptionHeight int
lfSmCaptionFont LOGFONT
iMenuWidth int
iMenuHeight int
lfMenuFont LOGFONT
lfStatusFont LOGFONT
lfMessageFont LOGFONT
}
var (
systemParametersInfo = user32.NewProc("SystemParametersInfoW")
createFontIndirect = gdi32.NewProc("CreateFontIndirectW")
)
// TODO adorn errors with which step failed?
func getStandardWindowFont() (hfont HANDLE, err error) {
var ncm NONCLIENTMETRICS
ncm.cbSize = uint32(unsafe.Sizeof(ncm))
r1, _, err := systemParametersInfo.Call(
uintptr(SPI_GETNONCLIENTMETRICS),
uintptr(unsafe.Sizeof(ncm)),
uintptr(unsafe.Pointer(&ncm)),
0)
if r1 == 0 { // failure
return NULL, err
}
// TODO does this specify an error?
r1, _, err = createFontIndirect.Call(uintptr(unsafe.Pointer(&ncm.lfMessageFont)))
if r1 == 0 { // failure
return NULL, err
}
return HANDLE(r1), nil
}

View File

@ -134,6 +134,7 @@ const (
var (
createWindowEx = user32.NewProc("CreateWindowExW")
destroyWindow = user32.NewProc("DestroyWindow")
enumChildWindows = user32.NewProc("EnumChildWindows")
showWindow = user32.NewProc("ShowWindow")
)
@ -166,6 +167,27 @@ func DestroyWindow(hWnd HWND) (err error) {
return nil
}
type WNDENUMPROC func(hwnd HWND, lParam LPARAM) (cont bool)
type _WNDENUMPROC func(hwnd HWND, lParam LPARAM) int
func enumChildProc(p WNDENUMPROC) _WNDENUMPROC {
return func(hwnd HWND, lParam LPARAM) int {
if p(hwnd, lParam) {
return TRUE
}
return FALSE
}
}
// TODO figure out how to handle errors
func EnumChildWindows(hWndParent HWND, lpEnumFunc WNDENUMPROC, lParam LPARAM) (err error) {
enumChildWindows.Call(
uintptr(hWndParent),
syscall.NewCallback(enumChildProc(lpEnumFunc)),
uintptr(lParam))
return nil
}
// TODO figure out how to handle errors
func ShowWindow(hWnd HWND, nCmdShow int) (previouslyVisible bool, err error) {
r1, _, _ := showWindow.Call(