Guest User

http://stackoverflow.com/questions/12414669

a guest
Sep 13th, 2012
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.78 KB | None | 0 0
  1. /*
  2.   file: testwinapi.go
  3.  
  4.   go! winapi test using "go-winapi":
  5.     http://github.com/lxn/go-winapi
  6. */
  7.  
  8. // this package
  9. package main
  10.  
  11. // package imports
  12. import (
  13.     "fmt"
  14.     "github.com/lxn/go-winapi"
  15.     "os"
  16.     "syscall"
  17.     "unsafe"
  18. )
  19.  
  20. // variables
  21. var (
  22.     oldWndProc winapi.HWND
  23. )
  24.  
  25. // constants
  26. const (
  27.     winWidth       int32 = 300
  28.     winHeight      int32 = 200
  29.     SIZE_MAXIMIZED       = 2
  30. )
  31.  
  32. // string to int16(utf) helper function
  33. func _S(_srt string) *uint16 {
  34.     return syscall.StringToUTF16Ptr(_srt)
  35. }
  36.  
  37. // window procedure
  38. func WndProc(hwnd winapi.HWND, msg uint32, wparam uintptr, lparam uintptr) uintptr {
  39.     switch msg {
  40.     case winapi.WM_SIZE:
  41.         if wparam == SIZE_MAXIMIZED {
  42.             fmt.Println("Changed!")
  43.         }
  44.     }
  45.     // call original procedure
  46.     return winapi.CallWindowProc(uintptr(oldWndProc), hwnd, msg, wparam, lparam)
  47. }
  48.  
  49. // main
  50. func main() {
  51.     var message winapi.MSG
  52.     var hwnd winapi.HWND
  53.     var wproc uintptr
  54.  
  55.     // create an edit window
  56.     hwnd = winapi.CreateWindowEx(
  57.         winapi.WS_EX_CLIENTEDGE,
  58.         _S("EDIT"),
  59.         _S("test window"),
  60.         winapi.WS_OVERLAPPEDWINDOW,
  61.         (winapi.GetSystemMetrics(winapi.SM_CXSCREEN)-winWidth)>>1,
  62.         (winapi.GetSystemMetrics(winapi.SM_CYSCREEN)-winHeight)>>1,
  63.         winWidth,
  64.         winHeight,
  65.         0,
  66.         0,
  67.         winapi.GetModuleHandle(nil),
  68.         unsafe.Pointer(nil))
  69.  
  70.     // create callback, set procedure, show window
  71.     wproc = syscall.NewCallback(WndProc)
  72.     oldWndProc = winapi.HWND(winapi.SetWindowLong(hwnd, winapi.GWL_WNDPROC, int32(wproc)))
  73.     winapi.ShowWindow(hwnd, winapi.SW_SHOWDEFAULT)
  74.  
  75.     // dispatch message
  76.     for {
  77.         if winapi.GetMessage(&message, 0, 0, 0) == 0 {
  78.             break
  79.         }
  80.         winapi.TranslateMessage(&message)
  81.         winapi.DispatchMessage(&message)
  82.     }
  83.  
  84.     // print handle and exit
  85.     fmt.Println("hwnd =", hwnd)
  86.     os.Exit(int(message.WParam))
  87. }
Add Comment
Please, Sign In to add comment