Skip to content

Commit

Permalink
remove CGO require in code.
Browse files Browse the repository at this point in the history
  • Loading branch information
whtiehack committed Jul 13, 2023
1 parent f7e6512 commit d742a8c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 55 deletions.
1 change: 1 addition & 0 deletions dialog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
1 change: 0 additions & 1 deletion examples/dynamic-library/dll/dll.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package main

import "C"
import (
"log"

Expand Down
85 changes: 31 additions & 54 deletions wingui.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
/*
*
wingui Golang GUI library
# Usage
### Simple usage:
Expand All @@ -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 <windows.h>
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"
Expand All @@ -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 != "" {
Expand All @@ -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
}

0 comments on commit d742a8c

Please sign in to comment.