From d742a8cbae4e0c24ff33993329b503a8960f1fad Mon Sep 17 00:00:00 2001 From: whitehack Date: Thu, 13 Jul 2023 14:33:51 +0800 Subject: [PATCH] remove CGO require in code. --- dialog.go | 1 + examples/dynamic-library/dll/dll.go | 1 - wingui.go | 85 +++++++++++------------------ 3 files changed, 32 insertions(+), 55 deletions(-) diff --git a/dialog.go b/dialog.go index 34277b3..f6a1abf 100644 --- a/dialog.go +++ b/dialog.go @@ -86,6 +86,7 @@ func NewModalDialog(idd uintptr, parent win.HWND, dialogConfig *DialogConfig, cb } dlg := &Dialog{ items: make(map[win.HWND]Widget), + iddMap: make(map[uintptr]Widget), config: dialogConfig, cb: cb, } diff --git a/examples/dynamic-library/dll/dll.go b/examples/dynamic-library/dll/dll.go index 6ca5998..4d169f9 100644 --- a/examples/dynamic-library/dll/dll.go +++ b/examples/dynamic-library/dll/dll.go @@ -1,6 +1,5 @@ package main -import "C" import ( "log" diff --git a/wingui.go b/wingui.go index b5276e9..ed2fbff 100644 --- a/wingui.go +++ b/wingui.go @@ -1,7 +1,7 @@ -/** +/* +* wingui Golang GUI library - # Usage ### Simple usage: @@ -16,71 +16,36 @@ or `windres -i ui/ui.rc -O coff -o ui.syso` - main.go ```go package main import "github.com/whtiehack/wingui" -func main() { - dlg, _ := wingui.NewDialog(101, 0, nil) - dlg.SetIcon(105) - btnok, _ := wingui.BindNewButton(1002, dlg) - btncancel, _ := wingui.BindNewButton(1003, dlg) - btnok.OnClicked = func() { - dlg.Close() + func main() { + dlg, _ := wingui.NewDialog(101, 0, nil) + dlg.SetIcon(105) + btnok, _ := wingui.BindNewButton(1002, dlg) + btncancel, _ := wingui.BindNewButton(1003, dlg) + btnok.OnClicked = func() { + dlg.Close() + } + btncancel.OnClicked = btnok.OnClicked + dlg.Show() + // This invoke is optional. + wingui.SetCurrentDialog(dlg.Handle()) + wingui.MessageLoop() } - btncancel.OnClicked = btnok.OnClicked - dlg.Show() - // This invoke is optional. - wingui.SetCurrentDialog(dlg.Handle()) - wingui.MessageLoop() -} - ``` - run: `go run .` Don't use `go run main.go`, because golang can't load x.syso files. - - - */ package wingui -/* -#include - -HWND dlg; - -// Message Loop -void MessageLoop(){ - MSG msg; - while (GetMessage(&msg, NULL, 0, 0)) - { - if(dlg){ - if(!IsDialogMessage(dlg, &msg)) - { - TranslateMessage(&msg); - DispatchMessage(&msg); - } - }else{ - TranslateMessage(&msg); - DispatchMessage(&msg); - } - } - -} - -void SetCurrentDialog(long long int h){ - dlg = (HWND)h; -} -*/ -import "C" import ( "github.com/lxn/win" "log" @@ -95,7 +60,7 @@ func init() { InitHInstance("") } -//InitHInstance init hInstance,used by Dialog APIs. +// InitHInstance init hInstance,used by Dialog APIs. func InitHInstance(lpModuleName string) { var name *uint16 if lpModuleName != "" { @@ -105,13 +70,25 @@ func InitHInstance(lpModuleName string) { log.Println("hInstance", hInstance) } +var dlg win.HWND + // MessageLoop start windows message loop. func MessageLoop() { - C.MessageLoop() + // message loop + var msg win.MSG + for win.GetMessage(&msg, 0, 0, 0) > 0 { + if dlg > 0 { + if win.IsDialogMessage(dlg, &msg) { + continue + } + } + win.TranslateMessage(&msg) + win.DispatchMessage(&msg) + } } // SetCurrentDialog make sure Message Loop could process dialog msg correct,such as Tabstop msg. -//This is a optional method. +// This is a optional method. func SetCurrentDialog(h win.HWND) { - C.SetCurrentDialog(C.longlong(uintptr(h))) + dlg = h }