### Install Windigo Go Package Source: https://github.com/rodrigocfd/windigo/blob/master/README.md This snippet provides the command to install the Windigo library. It uses the standard Go package manager to fetch the latest version of the `github.com/rodrigocfd/windigo` module. ```Go go get -u github.com/rodrigocfd/windigo ``` -------------------------------- ### Create a Basic GUI Window with Button in Go Source: https://github.com/rodrigocfd/windigo/blob/master/README.md This comprehensive example demonstrates building a simple GUI window using Windigo. It includes creating a main window, adding static text, an editable text field, and a button, then handles the button click event to display a message box with user input. The `runtime.LockOSThread()` call is crucial for Windows GUI applications. ```Go package main import ( "fmt" "runtime" "github.com/rodrigocfd/windigo/ui" "github.com/rodrigocfd/windigo/win/co" ) func main() { runtime.LockOSThread() // important: Windows GUI is single-threaded myWindow := NewMyWindow() // instantiate myWindow.wnd.RunAsMain() // ...and run } // This struct represents our main window. type MyWindow struct { wnd *ui.Main lblName *ui.Static txtName *ui.Edit btnShow *ui.Button } // Creates a new instance of our main window. func NewMyWindow() *MyWindow { wnd := ui.NewMain( // create the main window ui.OptsMain(). Title("Hello you"). Size(ui.Dpi(340, 80)). ClassIconId(101), // ID of icon resource, see resources folder ) lblName := ui.NewStatic( // create the child controls wnd, ui.OptsStatic(). Text("Your name"). Position(ui.Dpi(10, 22)), ) txtName := ui.NewEdit( wnd, ui.OptsEdit(). Position(ui.Dpi(80, 20)). Width(ui.DpiX(150)), ) btnShow := ui.NewButton( wnd, ui.OptsButton(). Text("&Show"). Position(ui.Dpi(240, 19)), ) me := &MyWindow{wnd, lblName, txtName, btnShow} me.events() return me } func (me *MyWindow) events() { me.btnShow.On().BnClicked(func() { msg := fmt.Sprintf("Hello, %s!", me.txtName.Text()) me.wnd.Hwnd().MessageBox(msg, "Saying hello", co.MB_ICONINFORMATION) }) } ``` ```Go go build -ldflags "-s -w -H=windowsgui" ``` -------------------------------- ### Automate Excel with COM IDispatch and VARIANT in Go Source: https://github.com/rodrigocfd/windigo/blob/master/README.md This example demonstrates COM Automation using the IDispatch interface to interact with Microsoft Excel. It shows how to create an Excel application instance, open a workbook, save a copy of it, and then close the workbook, all programmatically. The `win.OleReleaser` is used for COM object cleanup. ```Go package main import ( "github.com/rodrigocfd/windigo/win" "github.com/rodrigocfd/windigo/win/co" ) func main() { win.CoInitializeEx(co.COINIT_APARTMENTTHREADED | co.COINIT_DISABLE_OLE1DDE) defer win.CoUninitialize() rel := win.NewOleReleaser() defer rel.Release() clsId, _ := win.CLSIDFromProgID("Excel.Application") var excel *win.IDispatch win.CoCreateInstance(rel, clsId, nil, co.CLSCTX_LOCAL_SERVER, &excel) books, _ := excel.InvokeGetIDispatch(rel, "Workbooks") file, _ := books.InvokeMethodIDispatch(rel, "Open", "C:\\Temp\\foo.xlsx") file.InvokeMethod(rel, "SaveAs", "C:\\Temp\\foo copy.xlsx") file.InvokeMethod(rel, "Close") } ``` -------------------------------- ### Capture Screenshot using GDI and Save to BMP in Go Source: https://github.com/rodrigocfd/windigo/blob/master/README.md This example demonstrates how to capture a screenshot of the entire screen using Windows GDI functions and save it as a BMP file. It involves creating compatible device contexts and bitmaps, performing a BitBlt operation, and then serializing the bitmap data into a file. ```Go package main import ( "unsafe" "github.com/rodrigocfd/windigo/win" "github.com/rodrigocfd/windigo/win/co" ) func main() { cxScreen := win.GetSystemMetrics(co.SM_CXSCREEN) cyScreen := win.GetSystemMetrics(co.SM_CYSCREEN) hdcScreen, _ := win.HWND(0).GetDC() defer win.HWND(0).ReleaseDC(hdcScreen) hBmp, _ := hdcScreen.CreateCompatibleBitmap(uint(cxScreen), uint(cyScreen)) defer hBmp.DeleteObject() hdcMem, _ := hdcScreen.CreateCompatibleDC() defer hdcMem.DeleteDC() hBmpOld, _ := hdcMem.SelectObjectBmp(hBmp) defer hdcMem.SelectObjectBmp(hBmpOld) hdcMem.BitBlt( win.POINT{X: 0, Y: 0}, win.SIZE{Cx: cxScreen, Cy: cyScreen}, hdcScreen, win.POINT{X: 0, Y: 0}, co.ROP_SRCCOPY, ) bi := win.BITMAPINFO{ BmiHeader: win.BITMAPINFOHEADER{ BiWidth: cxScreen, BiHeight: cyScreen, BiPlanes: 1, BiBitCount: 32, BiCompression: co.BI_RGB, }, } bi.BmiHeader.SetBiSize() bmpObj, _ := hBmp.GetObject() bmpSize := bmpObj.CalcBitmapSize(bi.BmiHeader.BiBitCount) rawMem, _ := win.GlobalAlloc(co.GMEM_FIXED|co.GMEM_ZEROINIT, bmpSize) defer rawMem.GlobalFree() bmpSlice, _ := rawMem.GlobalLockSlice() defer rawMem.GlobalUnlock() hdcScreen.GetDIBits(hBmp, 0, uint(cyScreen), bmpSlice, &bi, co.DIB_RGB_COLORS) var bfh win.BITMAPFILEHEADER bfh.SetBfType() bfh.SetBfOffBits(uint32(unsafe.Sizeof(bfh) + unsafe.Sizeof(bi.BmiHeader))) bfh.SetBfSize(bfh.BfOffBits() + uint32(bmpSize)) fo, _ := win.FileOpen("C:\\Temp\\screenshot.bmp", co.FOPEN_RW_OPEN_OR_CREATE) defer fo.Close() fo.Write(bfh.Serialize()) fo.Write(bi.BmiHeader.Serialize()) fo.Write(bmpSlice) } ``` -------------------------------- ### Read and Enumerate Windows Registry Values in Go Source: https://github.com/rodrigocfd/windigo/blob/master/README.md This example illustrates how to interact with the Windows Registry using Windigo. It covers opening a specific registry key, reading a single named value, and then iterating through all values within that key, demonstrating how to extract and print different data types like strings and unsigned 32-bit integers. ```Go package main import ( "github.com/rodrigocfd/windigo/win" "github.com/rodrigocfd/windigo/win/co" ) func main() { // Open a registry key hKey, _ := win.HKEY_CURRENT_USER.RegOpenKeyEx( "Control Panel\\Mouse", co.REG_OPTION_NONE, co.KEY_READ) // open key as read-only defer hKey.RegCloseKey() // Read a single value from this key regVal, _ := hKey.RegQueryValueEx("Beep") // data can be string, uint32, etc. if strVal, ok := regVal.Sz(); ok { // try to extract a string value println("Beep is", strVal) } // Enumerate all values under this key allValues, _ := hKey.RegEnumValue() for _, value := range allValues { regVal, _ := hKey.RegQueryValueEx(value) if strVal, ok := regVal.Sz(); ok { // does it contain a string? println("Value str", value, strVal) } else if intVal, ok := regVal.Dword(); ok { // does it contain an uint32? println("Value int", value, intVal) } else { println("Value other", value, regVal.Type()) } } } ``` -------------------------------- ### Manage COM Objects with OleReleaser for File Open Dialog in Go Source: https://github.com/rodrigocfd/windigo/blob/master/README.md This example illustrates the usage of Component Object Model (COM) objects in Go with the Windigo library. It specifically shows how to initialize COM, create an IFileOpenDialog instance, set its options and file types, and display the native Open File dialog. The `win.OleReleaser` is used to manage the lifetime of COM objects, ensuring proper cleanup. ```Go package main import ( "github.com/rodrigocfd/windigo/win" "github.com/rodrigocfd/windigo/win/co" ) func main() { runtime.LockOSThread() // important: Windows GUI is single-threaded win.CoInitializeEx(co.COINIT_APARTMENTTHREADED | co.COINIT_DISABLE_OLE1DDE) defer win.CoUninitialize() releaser := win.NewOleReleaser() // will release all COM objects created here defer releaser.Release() var fod *win.IFileOpenDialog win.CoCreateInstance( releaser, co.CLSID_FileOpenDialog, nil, co.CLSCTX_INPROC_SERVER, &fod, ) defOpts, _ := fod.GetOptions() fod.SetOptions(defOpts | co.FOS_FORCEFILESYSTEM | co.FOS_FILEMUSTEXIST, ) fod.SetFileTypes([]win.COMDLG_FILTERSPEC{ {Name: "Text files", Spec: "*.txt"}, {Name: "All files", Spec: "*.*"}, }) fod.SetFileTypeIndex(1) if ok, _ := fod.Show(win.HWND(0)); ok { // in real applications, pass the parent HWND item, _ := fod.GetResult(releaser) fileName, _ := item.GetDisplayName(co.SIGDN_FILESYSPATH) println(fileName) } } ``` -------------------------------- ### List Running Windows Processes in Go Source: https://github.com/rodrigocfd/windigo/blob/master/README.md This snippet shows how to obtain a snapshot of running processes on a Windows system using Windigo's `CreateToolhelp32Snapshot` function. It then iterates through the process list, printing each process's ID and executable file name, providing a basic way to enumerate active applications. ```Go package main import ( "github.com/rodrigocfd/windigo/win" "github.com/rodrigocfd/windigo/win/co" ) func main() { hSnap, _ := win.CreateToolhelp32Snapshot(co.TH32CS_SNAPPROCESS, 0) defer hSnap.CloseHandle() processes, _ := hSnap.EnumProcesses() for _, nfo := range processes { println("PID:", nfo.Th32ProcessID, "name:", nfo.SzExeFile()) } println(len(processes), "found") } ``` -------------------------------- ### Windigo Internal Package Dependency Flowchart Source: https://github.com/rodrigocfd/windigo/blob/master/README.md This Mermaid flowchart visualizes the internal dependencies between the core packages of the Windigo library. It shows how high-level UI components depend on lower-level Win32 bindings, and how these bindings rely on internal utility and string management modules. ```mermaid flowchart BT internal/utl([internal/utl]) --> win/co ui --> win win --> internal/dll([internal/dll]) win --> internal/utl win --> win/wstr ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.