github.com/secoba/wails/v2@v2.6.4/internal/frontend/desktop/windows/winc/app.go (about)

     1  //go:build windows
     2  
     3  /*
     4   * Copyright (C) 2019 The Winc Authors. All Rights Reserved.
     5   * Copyright (C) 2010-2013 Allen Dang. All Rights Reserved.
     6   */
     7  package winc
     8  
     9  import (
    10  	"runtime"
    11  	"unsafe"
    12  
    13  	"github.com/secoba/wails/v2/internal/frontend/desktop/windows/winc/w32"
    14  )
    15  
    16  var (
    17  	// resource compilation tool assigns app.ico ID of 3
    18  	// rsrc -manifest app.manifest -ico app.ico -o rsrc.syso
    19  	AppIconID = 3
    20  )
    21  
    22  func init() {
    23  	runtime.LockOSThread()
    24  
    25  	gAppInstance = w32.GetModuleHandle("")
    26  	if gAppInstance == 0 {
    27  		panic("Error occurred in App.Init")
    28  	}
    29  
    30  	// Initialize the common controls
    31  	var initCtrls w32.INITCOMMONCONTROLSEX
    32  	initCtrls.DwSize = uint32(unsafe.Sizeof(initCtrls))
    33  	initCtrls.DwICC =
    34  		w32.ICC_LISTVIEW_CLASSES | w32.ICC_PROGRESS_CLASS | w32.ICC_TAB_CLASSES |
    35  			w32.ICC_TREEVIEW_CLASSES | w32.ICC_BAR_CLASSES
    36  
    37  	w32.InitCommonControlsEx(&initCtrls)
    38  }
    39  
    40  // SetAppIconID sets recource icon ID for the apps windows.
    41  func SetAppIcon(appIconID int) {
    42  	AppIconID = appIconID
    43  }
    44  
    45  func GetAppInstance() w32.HINSTANCE {
    46  	return gAppInstance
    47  }
    48  
    49  func PreTranslateMessage(msg *w32.MSG) bool {
    50  	// This functions is called by the MessageLoop. It processes the
    51  	// keyboard accelerator keys and calls Controller.PreTranslateMessage for
    52  	// keyboard and mouse events.
    53  
    54  	processed := false
    55  
    56  	if (msg.Message >= w32.WM_KEYFIRST && msg.Message <= w32.WM_KEYLAST) ||
    57  		(msg.Message >= w32.WM_MOUSEFIRST && msg.Message <= w32.WM_MOUSELAST) {
    58  
    59  		if msg.Hwnd != 0 {
    60  			if controller := GetMsgHandler(msg.Hwnd); controller != nil {
    61  				// Search the chain of parents for pretranslated messages.
    62  				for p := controller; p != nil; p = p.Parent() {
    63  
    64  					if processed = p.PreTranslateMessage(msg); processed {
    65  						break
    66  					}
    67  				}
    68  			}
    69  		}
    70  	}
    71  
    72  	return processed
    73  }
    74  
    75  // RunMainLoop processes messages in main application loop.
    76  func RunMainLoop() int {
    77  	m := (*w32.MSG)(unsafe.Pointer(w32.GlobalAlloc(0, uint32(unsafe.Sizeof(w32.MSG{})))))
    78  	defer w32.GlobalFree(w32.HGLOBAL(unsafe.Pointer(m)))
    79  
    80  	for w32.GetMessage(m, 0, 0, 0) != 0 {
    81  
    82  		if !PreTranslateMessage(m) {
    83  			w32.TranslateMessage(m)
    84  			w32.DispatchMessage(m)
    85  		}
    86  	}
    87  
    88  	w32.GdiplusShutdown()
    89  	return int(m.WParam)
    90  }
    91  
    92  // PostMessages processes recent messages. Sometimes helpful for instant window refresh.
    93  func PostMessages() {
    94  	m := (*w32.MSG)(unsafe.Pointer(w32.GlobalAlloc(0, uint32(unsafe.Sizeof(w32.MSG{})))))
    95  	defer w32.GlobalFree(w32.HGLOBAL(unsafe.Pointer(m)))
    96  
    97  	for i := 0; i < 10; i++ {
    98  		if w32.GetMessage(m, 0, 0, 0) != 0 {
    99  			if !PreTranslateMessage(m) {
   100  				w32.TranslateMessage(m)
   101  				w32.DispatchMessage(m)
   102  			}
   103  		}
   104  	}
   105  }
   106  
   107  func Exit() {
   108  	w32.PostQuitMessage(0)
   109  }