From cfcac03df76e9ccfe81f3b8f70126ddcf2463637 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Mon, 24 Mar 2014 17:01:33 -0400 Subject: [PATCH] Added GDI+ to the package's Windows version: DLL loaded, initialized, and shut down. It will be used for drawing to Areas because using GDI itself is more complex than it needs to be. --- common_windows.go | 1 + init_windows.go | 29 +++++++++++++++++++++++++++++ uitask_windows.go | 1 + 3 files changed, 31 insertions(+) diff --git a/common_windows.go b/common_windows.go index eaa7d34..ee09650 100644 --- a/common_windows.go +++ b/common_windows.go @@ -12,6 +12,7 @@ var ( kernel32 = syscall.NewLazyDLL("kernel32.dll") gdi32 = syscall.NewLazyDLL("gdi32.dll") comctl32 = syscall.NewLazyDLL("comctl32.dll") + gdiplus = syscall.NewLazyDLL("gdiplus.dll") ) type _HANDLE uintptr diff --git a/init_windows.go b/init_windows.go index 9ccd5df..bfc7649 100644 --- a/init_windows.go +++ b/init_windows.go @@ -11,6 +11,7 @@ import ( var ( hInstance _HANDLE nCmdShow int + gdiplusToken uintptr ) // TODO is this trick documented in MSDN? @@ -56,6 +57,26 @@ func getWinMainnCmdShow() { } } +func initGDIPlus() (err error) { + var gdiplusInit struct { + GdiplusVersion uint32 + DebugEventCallback uintptr + SuppressBackgroundThread int32 // originally BOOL + SuppressExternalCodecs int32 // originally BOOL + } + + gdiplusInit.GdiplusVersion = 1 // required + // TODO suppress external codecs? + r1, _, err := gdiplus.NewProc("GdiplusStartup").Call( + uintptr(unsafe.Pointer(&gdiplusToken)), + uintptr(unsafe.Pointer(&gdiplusInit)), + uintptr(0)) // we use the GDI+ thread so no need for output info + if r1 != 0 { // failure + return fmt.Errorf("error initializing GDI+ (GDI+ error code %d; windows last error %v)", r1, err) + } + return nil +} + func doWindowsInit() (err error) { err = getWinMainhInstance() if err != nil { @@ -74,6 +95,14 @@ func doWindowsInit() (err error) { if err != nil { return fmt.Errorf("error initializing Common Controls (comctl32.dll): %v", err) } + err = initGDIPlus() + if err != nil { + return fmt.Errorf("error initializing GDI+ (gdiplus.dll): %v", err) + } // TODO others return nil // all ready to go } + +func doWindowsQuitStuff() { + gdiplus.NewProc("GdiplusShutdown").Call(gdiplusToken) // returns void according to MSDN +} diff --git a/uitask_windows.go b/uitask_windows.go index 58d5a6b..7a4db36 100644 --- a/uitask_windows.go +++ b/uitask_windows.go @@ -84,6 +84,7 @@ func ui(main func()) error { } } + doWindowsQuitStuff() return nil }