github.com/jmigpin/editor@v1.6.0/driver/windriver/winapi.go (about)

     1  //go:build windows
     2  
     3  package windriver
     4  
     5  import (
     6  	"image"
     7  	"image/color"
     8  	"log"
     9  
    10  	"golang.org/x/sys/windows"
    11  )
    12  
    13  //----------
    14  
    15  const (
    16  	_CW_USEDEFAULT = 0x80000000 - 0x100000000
    17  
    18  	_CS_VREDRAW = 0x0001 // redraw on width adjust
    19  	_CS_HREDRAW = 0x0002 // redraw on height adjust
    20  
    21  	_LR_DEFAULTCOLOR = 0x00000000
    22  	_LR_DEFAULTSIZE  = 0x00000040
    23  	_LR_SHARED       = 0x00008000
    24  
    25  	_IMAGE_BITMAP = 0
    26  	_IMAGE_ICON   = 1
    27  	_IMAGE_CURSOR = 2
    28  
    29  	_IDC_ARROW       = 32512
    30  	_IDC_IBEAM       = 32513
    31  	_IDC_WAIT        = 32514 // hourglass
    32  	_IDC_UPARROW     = 32516
    33  	_IDC_HAND        = 32649
    34  	_IDC_SIZEALL     = 32646
    35  	_IDC_SIZENS      = 32645
    36  	_IDC_SIZENWSE    = 32642
    37  	_IDC_SIZEWE      = 32644
    38  	_IDC_CROSS       = 32515 // crosshair
    39  	_IDC_NO          = 32648 // slashed circle
    40  	_IDC_APPSTARTING = 32650 // standard arrow with hourglass
    41  
    42  	_COLOR_WINDOW  = 5
    43  	_COLOR_BTNFACE = 15
    44  
    45  	//_IDI_APPLICATION = 32512
    46  
    47  	_SW_SHOWDEFAULT = 10
    48  	_SW_HIDE        = 0
    49  	_SW_SHOW        = 5
    50  
    51  	// redraw window
    52  	_RDW_INTERNALPAINT = 2
    53  	_RDW_UPDATENOW     = 256
    54  
    55  	// bitblt
    56  	_SRCCOPY     = 0x00CC0020
    57  	_NOT_SRCCOPY = 0x00330008
    58  
    59  	_ERROR_INVALID_PARAMETER = 0x57 // 87
    60  	_ERROR_NOT_ENOUGH_MEMORY = 0x8
    61  
    62  	_BI_RGB         = 0
    63  	_DIB_RGB_COLORS = 0
    64  
    65  	// related to wm_setcursor/wm_nchittest
    66  	_HTCLIENT = 1 // In a client area.
    67  
    68  	// clipboard format
    69  	_CF_UNICODETEXT = 13
    70  
    71  	// globalalloc()
    72  	_GMEM_MOVEABLE = 2
    73  
    74  	// error related
    75  	// https://docs.microsoft.com/en-us/windows/win32/seccrypto/common-hresult-values
    76  	_S_OK          = 0x0
    77  	_E_OUTOFMEMORY = 0x8007000e
    78  	_E_NOINTERFACE = 0x80004002 // No such interface supported
    79  	_E_POINTER     = 0x80004003 // 	Pointer that is not valid
    80  )
    81  
    82  const (
    83  	_MK_SHIFT    = 0x0004 // The SHIFT key is down.
    84  	_MK_CONTROL  = 0x0008 // The CTRL key is down.
    85  	_MK_LBUTTON  = 0x0001 // The left mouse button is down.
    86  	_MK_MBUTTON  = 0x0010 // The middle mouse button is down.
    87  	_MK_RBUTTON  = 0x0002 // The right mouse button is down.
    88  	_MK_XBUTTON1 = 0x0020 // The first X button is down.
    89  	_MK_XBUTTON2 = 0x0040 // The second X button is down.
    90  )
    91  
    92  const (
    93  	_VK_SHIFT   = 0x10
    94  	_VK_CONTROL = 0x11
    95  	_VK_MENU    = 0x12 // alt
    96  	//_VK_LMENU   = 0xa4 // alt-gr?
    97  	//_VK_RMENU   = 0xa5 // alt-gr?
    98  	_VK_CAPITAL = 0x14 // caps-lock
    99  
   100  	_VK_LBUTTON  = 0x01 // mouse left button
   101  	_VK_RBUTTON  = 0x02 // right mouse button
   102  	_VK_MBUTTON  = 0x04 // middle mouse button
   103  	_VK_XBUTTON1 = 0x05
   104  	_VK_XBUTTON2 = 0x06
   105  )
   106  
   107  // https://docs.microsoft.com/en-us/windows/win32/menurc/wm-syscommand
   108  const (
   109  	_SC_CLOSE        = 0xF060 // Closes the window.
   110  	_SC_CONTEXTHELP  = 0xF180
   111  	_SC_DEFAULT      = 0xF160
   112  	_SC_HOTKEY       = 0xF150
   113  	_SC_HSCROLL      = 0xF080     // Scrolls horizontally.
   114  	_SCF_ISSECURE    = 0x00000001 // screen saver is secure.
   115  	_SC_KEYMENU      = 0xF100
   116  	_SC_MAXIMIZE     = 0xF030 // Maximizes the window.
   117  	_SC_MINIMIZE     = 0xF020 // Minimizes the window.
   118  	_SC_MONITORPOWER = 0xF170 // need to check lparam
   119  	_SC_MOUSEMENU    = 0xF090
   120  	_SC_MOVE         = 0xF010 // Moves the window.
   121  	_SC_NEXTWINDOW   = 0xF040 // Moves to the next window.
   122  	_SC_PREVWINDOW   = 0xF050 // Moves to the previous window.
   123  	_SC_RESTORE      = 0xF120 // Restores the window pos/size
   124  	_SC_SCREENSAVE   = 0xF140 // Executes the screen saver
   125  	_SC_SIZE         = 0xF000 // Sizes the window.
   126  	_SC_TASKLIST     = 0xF130 // Activates the Start menu.
   127  	_SC_VSCROLL      = 0xF070 // Scrolls vertically.
   128  )
   129  
   130  // https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-mapvirtualkeyw
   131  const (
   132  	_MAPVK_VK_TO_VSC    = 0
   133  	_MAPVK_VSC_TO_VK    = 1
   134  	_MAPVK_VK_TO_CHAR   = 2
   135  	_MAPVK_VSC_TO_VK_EX = 3
   136  )
   137  
   138  const (
   139  	_WS_VISIBLE = 0x10000000
   140  	_WS_BORDER  = 0x00800000
   141  	_WS_SIZEBOX = 0x00040000 // same as thickframe
   142  
   143  	_WS_OVERLAPPED       = 0x00000000
   144  	_WS_MAXIMIZEBOX      = 0x00010000
   145  	_WS_MINIMIZEBOX      = 0x00020000
   146  	_WS_THICKFRAME       = 0x00040000
   147  	_WS_SYSMENU          = 0x00080000
   148  	_WS_CAPTION          = 0x00C00000
   149  	_WS_OVERLAPPEDWINDOW = _WS_OVERLAPPED |
   150  		_WS_CAPTION |
   151  		_WS_SYSMENU |
   152  		_WS_THICKFRAME |
   153  		_WS_MINIMIZEBOX | _WS_MAXIMIZEBOX
   154  )
   155  
   156  type _wm uint32
   157  
   158  const (
   159  	_WM_NULL            _wm = 0x00
   160  	_WM_CREATE          _wm = 0x01
   161  	_WM_DESTROY         _wm = 0x02
   162  	_WM_MOVE            _wm = 0x03
   163  	_WM_SIZE            _wm = 0x05
   164  	_WM_ACTIVATE        _wm = 0x06
   165  	_WM_SETFOCUS        _wm = 0x07
   166  	_WM_KILLFOCUS       _wm = 0x08
   167  	_WM_ENABLE          _wm = 0x0A
   168  	_WM_SETREDRAW       _wm = 0x0B
   169  	_WM_SETTEXT         _wm = 0x0C
   170  	_WM_GETTEXT         _wm = 0x0D
   171  	_WM_GETTEXTLENGTH   _wm = 0x0E
   172  	_WM_PAINT           _wm = 0x0F
   173  	_WM_CLOSE           _wm = 0x10
   174  	_WM_QUERYENDSESSION _wm = 0x11
   175  	_WM_QUIT            _wm = 0x12
   176  	_WM_QUERYOPEN       _wm = 0x13
   177  	_WM_ERASEBKGND      _wm = 0x14
   178  	_WM_SYSCOLORCHANGE  _wm = 0x15
   179  	_WM_ENDSESSION      _wm = 0x16
   180  	_WM_SYSTEMERROR     _wm = 0x17
   181  	_WM_SHOWWINDOW      _wm = 0x18
   182  	_WM_CTLCOLOR        _wm = 0x19
   183  	_WM_WININICHANGE    _wm = 0x1A
   184  	_WM_SETTINGCHANGE   _wm = 0x1A
   185  	_WM_DEVMODECHANGE   _wm = 0x1B
   186  	_WM_ACTIVATEAPP     _wm = 0x1C
   187  	_WM_FONTCHANGE      _wm = 0x1D
   188  	_WM_TIMECHANGE      _wm = 0x1E
   189  	_WM_CANCELMODE      _wm = 0x1F
   190  	_WM_SETCURSOR       _wm = 0x20
   191  	_WM_MOUSEACTIVATE   _wm = 0x21
   192  	_WM_CHILDACTIVATE   _wm = 0x22
   193  	_WM_QUEUESYNC       _wm = 0x23
   194  	_WM_GETMINMAXINFO   _wm = 0x24
   195  	_WM_PAINTICON       _wm = 0x26
   196  	_WM_ICONERASEBKGND  _wm = 0x27
   197  	_WM_NEXTDLGCTL      _wm = 0x28
   198  	_WM_SPOOLERSTATUS   _wm = 0x2A
   199  	_WM_DRAWITEM        _wm = 0x2B
   200  	_WM_MEASUREITEM     _wm = 0x2C
   201  	_WM_DELETEITEM      _wm = 0x2D
   202  	_WM_VKEYTOITEM      _wm = 0x2E
   203  	_WM_CHARTOITEM      _wm = 0x2F
   204  
   205  	_WM_SETFONT                _wm = 0x30
   206  	_WM_GETFONT                _wm = 0x31
   207  	_WM_SETHOTKEY              _wm = 0x32
   208  	_WM_GETHOTKEY              _wm = 0x33
   209  	_WM_QUERYDRAGICON          _wm = 0x37
   210  	_WM_COMPAREITEM            _wm = 0x39
   211  	_WM_COMPACTING             _wm = 0x41
   212  	_WM_WINDOWPOSCHANGING      _wm = 0x46
   213  	_WM_WINDOWPOSCHANGED       _wm = 0x47
   214  	_WM_POWER                  _wm = 0x48
   215  	_WM_COPYDATA               _wm = 0x4A
   216  	_WM_CANCELJOURNAL          _wm = 0x4B
   217  	_WM_NOTIFY                 _wm = 0x4E
   218  	_WM_INPUTLANGCHANGEREQUEST _wm = 0x50
   219  	_WM_INPUTLANGCHANGE        _wm = 0x51
   220  	_WM_TCARD                  _wm = 0x52
   221  	_WM_HELP                   _wm = 0x53
   222  	_WM_USERCHANGED            _wm = 0x54
   223  	_WM_NOTIFYFORMAT           _wm = 0x55
   224  	_WM_CONTEXTMENU            _wm = 0x7B
   225  	_WM_STYLECHANGING          _wm = 0x7C
   226  	_WM_STYLECHANGED           _wm = 0x7D
   227  	_WM_DISPLAYCHANGE          _wm = 0x7E
   228  	_WM_GETICON                _wm = 0x7F
   229  	_WM_SETICON                _wm = 0x80
   230  
   231  	_WM_NCCREATE        _wm = 0x81
   232  	_WM_NCDESTROY       _wm = 0x82
   233  	_WM_NCCALCSIZE      _wm = 0x83
   234  	_WM_NCHITTEST       _wm = 0x84
   235  	_WM_NCPAINT         _wm = 0x85
   236  	_WM_NCACTIVATE      _wm = 0x86
   237  	_WM_GETDLGCODE      _wm = 0x87
   238  	_WM_NCMOUSEMOVE     _wm = 0xA0
   239  	_WM_NCLBUTTONDOWN   _wm = 0xA1
   240  	_WM_NCLBUTTONUP     _wm = 0xA2
   241  	_WM_NCLBUTTONDBLCLK _wm = 0xA3
   242  	_WM_NCRBUTTONDOWN   _wm = 0xA4
   243  	_WM_NCRBUTTONUP     _wm = 0xA5
   244  	_WM_NCRBUTTONDBLCLK _wm = 0xA6
   245  	_WM_NCMBUTTONDOWN   _wm = 0xA7
   246  	_WM_NCMBUTTONUP     _wm = 0xA8
   247  	_WM_NCMBUTTONDBLCLK _wm = 0xA9
   248  
   249  	//_WM_KEYFIRST    _wm = 0x100
   250  	_WM_KEYDOWN     _wm = 0x100
   251  	_WM_KEYUP       _wm = 0x101
   252  	_WM_CHAR        _wm = 0x102
   253  	_WM_DEADCHAR    _wm = 0x103
   254  	_WM_SYSKEYDOWN  _wm = 0x104
   255  	_WM_SYSKEYUP    _wm = 0x105
   256  	_WM_SYSCHAR     _wm = 0x106
   257  	_WM_SYSDEADCHAR _wm = 0x107
   258  	_WM_KEYLAST     _wm = 0x108
   259  
   260  	_WM_IME_STARTCOMPOSITION _wm = 0x10D
   261  	_WM_IME_ENDCOMPOSITION   _wm = 0x10E
   262  	_WM_IME_COMPOSITION      _wm = 0x10F
   263  	_WM_IME_KEYLAST          _wm = 0x10F
   264  
   265  	_WM_INITDIALOG    _wm = 0x110
   266  	_WM_COMMAND       _wm = 0x111
   267  	_WM_SYSCOMMAND    _wm = 0x112
   268  	_WM_TIMER         _wm = 0x113
   269  	_WM_HSCROLL       _wm = 0x114
   270  	_WM_VSCROLL       _wm = 0x115
   271  	_WM_INITMENU      _wm = 0x116
   272  	_WM_INITMENUPOPUP _wm = 0x117
   273  	_WM_MENUSELECT    _wm = 0x11F
   274  	_WM_MENUCHAR      _wm = 0x120
   275  	_WM_ENTERIDLE     _wm = 0x121
   276  
   277  	_WM_CTLCOLORMSGBOX    _wm = 0x132
   278  	_WM_CTLCOLOREDIT      _wm = 0x133
   279  	_WM_CTLCOLORLISTBOX   _wm = 0x134
   280  	_WM_CTLCOLORBTN       _wm = 0x135
   281  	_WM_CTLCOLORDLG       _wm = 0x136
   282  	_WM_CTLCOLORSCROLLBAR _wm = 0x137
   283  	_WM_CTLCOLORSTATIC    _wm = 0x138
   284  
   285  	//_WM_MOUSEFIRST    _wm = 0x200
   286  	_WM_MOUSEMOVE     _wm = 0x200
   287  	_WM_LBUTTONDOWN   _wm = 0x201
   288  	_WM_LBUTTONUP     _wm = 0x202
   289  	_WM_LBUTTONDBLCLK _wm = 0x203
   290  	_WM_RBUTTONDOWN   _wm = 0x204
   291  	_WM_RBUTTONUP     _wm = 0x205
   292  	_WM_RBUTTONDBLCLK _wm = 0x206
   293  	_WM_MBUTTONDOWN   _wm = 0x207
   294  	_WM_MBUTTONUP     _wm = 0x208
   295  	_WM_MBUTTONDBLCLK _wm = 0x209
   296  	_WM_MOUSEWHEEL    _wm = 0x20A
   297  	_WM_MOUSEHWHEEL   _wm = 0x20E
   298  
   299  	_WM_PARENTNOTIFY   _wm = 0x210
   300  	_WM_ENTERMENULOOP  _wm = 0x211
   301  	_WM_EXITMENULOOP   _wm = 0x212
   302  	_WM_NEXTMENU       _wm = 0x213
   303  	_WM_SIZING         _wm = 0x214
   304  	_WM_CAPTURECHANGED _wm = 0x215
   305  	_WM_MOVING         _wm = 0x216
   306  	_WM_POWERBROADCAST _wm = 0x218
   307  	_WM_DEVICECHANGE   _wm = 0x219
   308  
   309  	_WM_MDICREATE      _wm = 0x220
   310  	_WM_MDIDESTROY     _wm = 0x221
   311  	_WM_MDIACTIVATE    _wm = 0x222
   312  	_WM_MDIRESTORE     _wm = 0x223
   313  	_WM_MDINEXT        _wm = 0x224
   314  	_WM_MDIMAXIMIZE    _wm = 0x225
   315  	_WM_MDITILE        _wm = 0x226
   316  	_WM_MDICASCADE     _wm = 0x227
   317  	_WM_MDIICONARRANGE _wm = 0x228
   318  	_WM_MDIGETACTIVE   _wm = 0x229
   319  	_WM_MDISETMENU     _wm = 0x230
   320  	_WM_ENTERSIZEMOVE  _wm = 0x231
   321  	_WM_EXITSIZEMOVE   _wm = 0x232
   322  	_WM_DROPFILES      _wm = 0x233
   323  	_WM_MDIREFRESHMENU _wm = 0x234
   324  
   325  	_WM_IME_SETCONTEXT      _wm = 0x281
   326  	_WM_IME_NOTIFY          _wm = 0x282
   327  	_WM_IME_CONTROL         _wm = 0x283
   328  	_WM_IME_COMPOSITIONFULL _wm = 0x284
   329  	_WM_IME_SELECT          _wm = 0x285
   330  	_WM_IME_CHAR            _wm = 0x286
   331  	_WM_IME_KEYDOWN         _wm = 0x290
   332  	_WM_IME_KEYUP           _wm = 0x291
   333  
   334  	_WM_MOUSEHOVER   _wm = 0x2A1
   335  	_WM_NCMOUSELEAVE _wm = 0x2A2
   336  	_WM_MOUSELEAVE   _wm = 0x2A3
   337  
   338  	_WM_CUT   _wm = 0x300
   339  	_WM_COPY  _wm = 0x301
   340  	_WM_PASTE _wm = 0x302
   341  	_WM_CLEAR _wm = 0x303
   342  	_WM_UNDO  _wm = 0x304
   343  
   344  	_WM_RENDERFORMAT      _wm = 0x305
   345  	_WM_RENDERALLFORMATS  _wm = 0x306
   346  	_WM_DESTROYCLIPBOARD  _wm = 0x307
   347  	_WM_DRAWCLIPBOARD     _wm = 0x308
   348  	_WM_PAINTCLIPBOARD    _wm = 0x309
   349  	_WM_VSCROLLCLIPBOARD  _wm = 0x30A
   350  	_WM_SIZECLIPBOARD     _wm = 0x30B
   351  	_WM_ASKCBFORMATNAME   _wm = 0x30C
   352  	_WM_CHANGECBCHAIN     _wm = 0x30D
   353  	_WM_HSCROLLCLIPBOARD  _wm = 0x30E
   354  	_WM_QUERYNEWPALETTE   _wm = 0x30F
   355  	_WM_PALETTEISCHANGING _wm = 0x310
   356  	_WM_PALETTECHANGED    _wm = 0x311
   357  
   358  	_WM_HOTKEY      _wm = 0x312
   359  	_WM_PRINT       _wm = 0x317
   360  	_WM_PRINTCLIENT _wm = 0x318
   361  
   362  	_WM_HANDHELDFIRST  _wm = 0x358
   363  	_WM_HANDHELDLAST   _wm = 0x35F
   364  	_WM_PENWINFIRST    _wm = 0x380
   365  	_WM_PENWINLAST     _wm = 0x38F
   366  	_WM_COALESCE_FIRST _wm = 0x390
   367  	_WM_COALESCE_LAST  _wm = 0x39F
   368  	_WM_DDE_FIRST      _wm = 0x3E0
   369  	_WM_DDE_INITIATE   _wm = 0x3E0
   370  	_WM_DDE_TERMINATE  _wm = 0x3E1
   371  	_WM_DDE_ADVISE     _wm = 0x3E2
   372  	_WM_DDE_UNADVISE   _wm = 0x3E3
   373  	_WM_DDE_ACK        _wm = 0x3E4
   374  	_WM_DDE_DATA       _wm = 0x3E5
   375  	_WM_DDE_REQUEST    _wm = 0x3E6
   376  	_WM_DDE_POKE       _wm = 0x3E7
   377  	_WM_DDE_EXECUTE    _wm = 0x3E8
   378  	_WM_DDE_LAST       _wm = 0x3E8
   379  
   380  	_WM_USER _wm = 0x400
   381  	_WM_APP  _wm = 0x8000
   382  
   383  	//	_WM_QUERYNEWPALETTE       _wm = 0x030F
   384  	//	_WM_DWMNCRENDERINGCHANGED _wm = 0x031F
   385  
   386  )
   387  
   388  //----------
   389  //----------
   390  //----------
   391  
   392  type _WndClassExW struct {
   393  	CbSize        uint32
   394  	Style         uint32
   395  	LpfnWndProc   uintptr
   396  	CbClsExtra    int32
   397  	CbWndExtra    int32
   398  	HInstance     windows.Handle
   399  	HIcon         windows.Handle
   400  	HCursor       windows.Handle
   401  	HbrBackground windows.Handle
   402  	LpszMenuName  *uint16
   403  	LpszClassName *uint16
   404  	HIconSm       windows.Handle
   405  }
   406  
   407  type _Msg struct {
   408  	HWnd     windows.Handle
   409  	Msg      uint32
   410  	WParam   uintptr
   411  	LParam   uintptr
   412  	Time     uint32
   413  	Pt       _Point
   414  	LPrivate uint32
   415  }
   416  
   417  type _CreateStructW struct {
   418  	LpCreateParams uintptr
   419  	HInstance      windows.Handle
   420  	HMenu          windows.Handle
   421  	HWnd           windows.Handle
   422  	CY             int32 // h
   423  	CX             int32 // w
   424  	Y              int32
   425  	X              int32
   426  	Style          int32
   427  	LpszName       *uint16
   428  	LpszClass      *uint16
   429  	DwExStyle      uint32
   430  }
   431  
   432  type _WindowPos struct {
   433  	HWndInsertAfter uintptr
   434  	HWnd            uintptr
   435  	X               int32
   436  	Y               int32
   437  	CX              int32 // h
   438  	CY              int32 // w
   439  	Flags           uint32
   440  }
   441  
   442  type _MinMaxInfo struct {
   443  	PtReserved     _Point
   444  	PtMaxSize      _Point
   445  	PtMaxPosition  _Point
   446  	PtMinTrackSize _Point
   447  	PtMaxTrackSize _Point
   448  }
   449  
   450  type _Paint struct {
   451  	Hdc         windows.Handle
   452  	FErase      bool
   453  	RcPaint     _Rect
   454  	FRestore    bool
   455  	FIncUpdate  bool
   456  	RgbReserved [32]byte
   457  }
   458  
   459  type _Bitmap struct {
   460  	BmType       int32
   461  	BmWidth      int32
   462  	BmHeight     int32
   463  	BmWidthBytes int32
   464  	BmPlanes     uint16
   465  	BmBitsPixel  uint16
   466  	BmBits       uintptr
   467  }
   468  
   469  type _BitmapInfo struct {
   470  	BmiHeader _BitmapInfoHeader
   471  	BmColors  [1]_RgbQuad
   472  }
   473  
   474  type _BitmapInfoHeader struct {
   475  	BiSize          uint32
   476  	BiWidth         int32
   477  	BiHeight        int32
   478  	BiPlanes        uint16
   479  	BiBitCount      uint16
   480  	BiCompression   uint32
   481  	BiSizeImage     uint32
   482  	BiXPelsPerMeter int32
   483  	BiYPelsPerMeter int32
   484  	BiClrUsed       uint32
   485  	BiClrImportant  uint32
   486  }
   487  
   488  type _RgbQuad struct {
   489  	Blue     byte
   490  	Green    byte
   491  	Red      byte
   492  	Reserved byte
   493  }
   494  
   495  //----------
   496  
   497  type _Point struct {
   498  	X, Y int32
   499  }
   500  
   501  func (p *_Point) ToImagePoint() image.Point {
   502  	return image.Point{X: int(p.X), Y: int(p.Y)}
   503  }
   504  
   505  //----------
   506  
   507  type _Rect struct {
   508  	left, top, right, bottom int32
   509  }
   510  
   511  func RectFromImageRectangle(r image.Rectangle) _Rect {
   512  	return _Rect{
   513  		left:   int32(r.Min.X),
   514  		right:  int32(r.Max.X),
   515  		top:    int32(r.Min.Y),
   516  		bottom: int32(r.Max.Y),
   517  	}
   518  }
   519  
   520  func (r *_Rect) ToImageRectangle() image.Rectangle {
   521  	return image.Rect(int(r.left), int(r.top), int(r.right), int(r.bottom))
   522  }
   523  
   524  //----------
   525  
   526  type _ColorRef uint32 // hex form: 0x00bbggrr
   527  
   528  func ColorRefFromImageColor(c color.Color) _ColorRef {
   529  	if c2, ok := c.(color.RGBA); ok {
   530  		return rgbToColorRef(c2.R, c2.G, c2.B)
   531  	}
   532  	r, g, b, _ := c.RGBA()
   533  	return rgbToColorRef(byte(r>>8), byte(g>>8), byte(b>>8))
   534  }
   535  
   536  func rgbToColorRef(r, g, b byte) _ColorRef {
   537  	return _ColorRef(r) | _ColorRef(g)<<8 | _ColorRef(b)<<16
   538  }
   539  
   540  //----------
   541  
   542  func unpackLowHigh(v uint32) (int, int) {
   543  	low := uint16(v)
   544  	high := uint16(v >> 16)
   545  	return int(low), int(high)
   546  }
   547  func packLowHigh(l, h uint16) uint32 {
   548  	return (uint32(h) << 16) | uint32(l)
   549  }
   550  
   551  func UTF16PtrFromString(s string) *uint16 {
   552  	ptr, err := windows.UTF16PtrFromString(s)
   553  	if err != nil {
   554  		log.Printf("error: windows UTF16PtrFromString: %v", err)
   555  	}
   556  	return ptr
   557  }
   558  
   559  //----------
   560  
   561  // NOTES
   562  // int -> int32
   563  // uint -> uint32
   564  // lpcstr -> *uint8(?)
   565  // lpcwstr -> *uint16 // string of 16-bit unicode characters
   566  // lpwstr -> *uint16 // string of 16-bit unicode characters
   567  // short -> uint16
   568  // word -> uint16
   569  // dword -> uint32
   570  // long -> int32 // apparently not 64(?)
   571  
   572  //sys _GetModuleHandleW(name *uint16) (modH windows.Handle, err error) = kernel32.GetModuleHandleW
   573  //sys _GlobalLock(h windows.Handle) (ptr uintptr, err error) = kernel32.GlobalLock
   574  //sys _GlobalUnlock(h windows.Handle) (ok bool) = kernel32.GlobalUnlock
   575  //sys _GlobalAlloc(uFlags uint32, dwBytes uintptr) (h windows.Handle, err error) = kernel32.GlobalAlloc
   576  //sys _GetConsoleWindow() (cH windows.Handle) = kernel32.GetConsoleWindow
   577  //sys _GetCurrentProcessId() (pid uint32)  = kernel32.GetCurrentProcessId
   578  
   579  //sys _LoadImageW(hInstance windows.Handle, name uintptr, typ uint32, cx int32, cy int32, fuLoad uint32) (imgH windows.Handle, err error) = user32.LoadImageW
   580  //sys _LoadCursorW(hInstance windows.Handle, name uint32) (cursorH windows.Handle, err error) = user32.LoadCursorW
   581  //sys _RegisterClassExW(wcx *_WndClassExW) (atom uint16, err error) = user32.RegisterClassExW
   582  //sys _CreateWindowExW(dwExStyle uint32,lpClassName *uint16, lpWindowName *uint16, dwStyle int32, x int32, y int32, nWidth int32, nHeight int32, hWndParent windows.Handle, hMenu windows.Handle, hInstance windows.Handle, lpParam uintptr) (wndH windows.Handle, err error) = user32.CreateWindowExW
   583  //sys _PostMessageW(hwnd windows.Handle, msg uint32, wParam uintptr, lParam uintptr) (ok bool) =user32.PostMessageW
   584  //sys _GetMessageW(msg *_Msg, hwnd windows.Handle, msgFilterMin uint32, msgFilterMax uint32) (res int32, err error) [failretval==-1] = user32.GetMessageW
   585  //sys _TranslateAccelerator(hwnd windows.Handle, hAccTable windows.Handle, msg *_Msg) (ok bool) = user32.TranslateAccelerator
   586  //sys _TranslateMessage(msg *_Msg) (translated bool) = user32.TranslateMessage
   587  //sys _DispatchMessageW(msg *_Msg) (res int32) = user32.DispatchMessageW
   588  //sys _DefWindowProcW(hwnd windows.Handle, msg uint32, wparam uintptr, lparam uintptr) (ret uintptr) = user32.DefWindowProcW
   589  //sys _GetWindowRect(hwnd windows.Handle, r *_Rect) (ok bool) = user32.GetWindowRect
   590  //sys _SetCursor(cursorH windows.Handle) (prevCursorH windows.Handle) = user32.SetCursor
   591  //sys _DestroyWindow(hwnd windows.Handle) (ok bool) = user32.DestroyWindow
   592  //sys _PostQuitMessage(exitCode int32) = user32.PostQuitMessage
   593  //sys _GetCursorPos(p *_Point) (ok bool) = user32.GetCursorPos
   594  //sys _ValidateRect(hwnd windows.Handle, r *_Rect) (ok bool) = user32.ValidateRect
   595  //sys _InvalidateRect(hwnd windows.Handle, r *_Rect, erase bool) (ok bool) = user32.InvalidateRect
   596  //sys _BeginPaint(hwnd windows.Handle, paint *_Paint) (dcH windows.Handle, err error) = user32.BeginPaint
   597  //sys _EndPaint(hwnd windows.Handle, paint *_Paint) (ok bool) = user32.EndPaint
   598  //sys _UpdateWindow(hwnd windows.Handle) (ok bool) = user32.UpdateWindow
   599  //sys _RedrawWindow(hwnd windows.Handle, r *_Rect, region windows.Handle, flags uint) (ok bool) = user32.RedrawWindow
   600  //sys _ShowWindow(hwnd windows.Handle, nCmdShow int) (ok bool) = user32.ShowWindow
   601  //sys _ShowWindowAsync(hwnd windows.Handle, nCmdShow int) (ok bool) = user32.ShowWindowAsync
   602  //sys _GetDC(hwnd windows.Handle) (dcH windows.Handle, err error) = user32.GetDC
   603  //sys _ReleaseDC(hwnd windows.Handle, dc windows.Handle) (ok bool) = user32.ReleaseDC
   604  //sys _MapVirtualKeyW(uCode uint32, uMapType uint32) (code uint32) = user32.MapVirtualKeyW
   605  //sys _ToUnicode(wVirtKey uint32, wScanCode uint32, lpKeyState *[256]byte, pwszBuff *uint16, cchBuff int32, wFlags uint32) (code int32) = user32.ToUnicode
   606  //sys _GetKeyboardState(state *[256]byte) (ok bool) = user32.GetKeyboardState
   607  //sys _GetKeyState(vkey int32) (state uint16) = user32.GetKeyState
   608  //sys _SetCursorPos(x int32, y int32) (ok bool) = user32.SetCursorPos
   609  //sys _MapWindowPoints(hwndFrom windows.Handle, hwndTo windows.Handle, lpPoints *_Point, cPoints uint32) (res int32) = user32.MapWindowPoints
   610  //sys _ClientToScreen(hwnd windows.Handle, lpPoint *_Point) (ok bool) = user32.ClientToScreen
   611  //sys _OpenClipboard(hWndNewOwner windows.Handle) (ok bool) = user32.OpenClipboard
   612  //sys _CloseClipboard() (ok bool) = user32.CloseClipboard
   613  //sys _SetClipboardData(uFormat uint32, h windows.Handle) (dataH windows.Handle, err error) = user32.SetClipboardData
   614  //sys _GetClipboardData(uFormat uint32) (dataH windows.Handle, err error) = user32.GetClipboardData
   615  //sys _EmptyClipboard() (ok bool) = user32.EmptyClipboard
   616  //sys _GetWindowThreadProcessId(hwnd windows.Handle, pid *uint32) (threadId uint32) = user32.GetWindowThreadProcessId
   617  //sys _SetWindowTextW(hwnd windows.Handle, lpString *uint16) (res bool) = user32.SetWindowTextW
   618  
   619  //sys _SelectObject(hdc windows.Handle, obj windows.Handle) (prevObjH windows.Handle, err error) = gdi32.SelectObject
   620  //sys _CreateBitmap(w int32, h int32, planes uint32, bitCount uint32, bits uintptr) (bmH windows.Handle, err error) = gdi32.CreateBitmap
   621  //sys _CreateCompatibleBitmap(hdc windows.Handle, w int32, h int32) (bmH windows.Handle, err error) = gdi32.CreateCompatibleBitmap
   622  //sys _DeleteObject(obj windows.Handle) (ok bool) = gdi32.DeleteObject
   623  //sys _CreateCompatibleDC(hdc windows.Handle) (dcH windows.Handle, err error)  = gdi32.CreateCompatibleDC
   624  //sys _DeleteDC(dc windows.Handle) (ok bool) = gdi32.DeleteDC
   625  //sys _BitBlt(hdc windows.Handle, x int32, y int32, w int32, h int32, hdcSrc windows.Handle, x2 int32, y2 int32, rOp uint32) (ok bool) = gdi32.BitBlt
   626  // colorSet should be ColorRef(uint32), but the docs say it can return -1!
   627  //sys _SetPixel(hdc windows.Handle, x int, y int, c _ColorRef) (colorSet int32, err error) [failretval==-1] = gdi32.SetPixel
   628  //sys _CreateBitmapIndirect(bm *_Bitmap) (bmH windows.Handle, err error) = gdi32.CreateBitmapIndirect
   629  //sys _GetObject(h windows.Handle, c int32, v uintptr) (n int) = gdi32.GetObject
   630  //sys	_CreateDIBSection(dc windows.Handle, bmi *_BitmapInfo, usage uint32, bits **byte, section windows.Handle, offset uint32) (bmH windows.Handle, err error) = gdi32.CreateDIBSection
   631  
   632  //sys _DragAcceptFiles(hwnd windows.Handle, fAccept bool) = shell32.DragAcceptFiles
   633  //sys _DragQueryPoint(hDrop uintptr, ppt *_Point) (res bool) = shell32.DragQueryPoint
   634  //sys _DragQueryFileW(hDrop uintptr, iFile uint32, lpszFile *uint16, cch uint32)(res uint32) = shell32.DragQueryFileW
   635  //sys _DragFinish(hDrop uintptr) = shell32.DragFinish
   636  
   637  // NOT USED
   638  ////sys _OleInitialize(pvReserved uintptr) (hRes uintptr) = ole32.OleInitialize
   639  ////sys _RegisterDragDrop(hwnd windows.Handle, pDropTarget **_IDropTarget) (resH uintptr) = ole32.RegisterDragDrop
   640  ////sys _CoLockObjectExternal(pUnk uintptr, fLock bool, fLastUnlockReleases bool) (hres uintptr) = ole32.CoLockObjectExternal