Advertisement
Lorenzo501

Buffer, NumPut & NumGet (with point struct).ahk

Mar 12th, 2024 (edited)
1,775
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Requires AutoHotkey 2.0
  2. ; These are just examples, you usually don't need to use Buffer/NumPut/NumGet at all. Here's a much better alternative (example below): https://www.autohotkey.com/docs/alpha/Structs.htm#classdef
  3. /*
  4. #Requires AutoHotkey 2.1-alpha.8
  5. class Point
  6. {
  7.     X: i32, Y: i32
  8. }
  9. MonitorFromCursor()
  10. {
  11.     DllCall("GetCursorPos", "Ptr", cursorPosition_pointStruct := Point())
  12.     MsgBox(DllCall("MonitorFromPoint", Point, cursorPosition_pointStruct, "UInt", 0))
  13. }
  14. */
  15. ; The best way to get the monitor is this: MonitorFromCursor() => (DllCall("GetCursorPos", "UInt64*", &cursorPosition := 0), DllCall("MonitorFromPoint", "Int64", cursorPosition, "UInt", 0))
  16. ; And this is also possible with alpha structs:
  17. /*
  18. #Requires AutoHotkey 2.1-alpha.9
  19. class Point
  20. {
  21.     X: i32, Y: i32
  22. }
  23. class MSLLHOOKSTRUCT
  24. {
  25.     Pt: Point, MouseData: i32, Flags: u32, Time: u32, DwExtraInfo: uptr
  26. }
  27. ; And then inside the PointerDeviceProc callback fn (https://learn.microsoft.com/en-us/windows/win32/winmsg/lowlevelmouseproc)
  28. eventInfo := StructFromPtr(MSLLHOOKSTRUCT, lParam)
  29. Tooltip(eventInfo.Pt.X)
  30. */
  31.  
  32. F1::
  33. {
  34.     ; "Int" is 4 bytes, so both axes together is 8 bytes total
  35.  
  36.     ; Can use the pointStruct as "Ptr" argument in DllCall (no need to use * symbol in the type nor the & symbol in the value):
  37.     pointStruct := Buffer(8)
  38.     NumPut("Int", xValueToAdd := 123, pointStruct, 0)
  39.     NumPut("Int", yValueToAdd := 456, pointStruct, 4)
  40.  
  41.     ; To retrieve the x and y values from a point struct of which its "Ptr" was returned by a DllCall (or from the buffer I made above simply use "NumGet"):
  42.     DllCall("GetCursorPos", "Ptr", cursorPosition_pointStruct := Buffer(8))
  43.     currentCursorX := NumGet(cursorPosition_pointStruct, 0, "Int")
  44.     currentCursorY := NumGet(cursorPosition_pointStruct, 4, "Int")
  45.     MsgBox(currentCursorX " " currentCursorY)
  46.  
  47.     ; You can also add both axes to (new) cursorPosition_pointStruct at once, and then for example convert from type "Buffer" to "Int64" (to use in DllCall):
  48.     MouseGetPos(&currentCursorX, &currentCursorY)
  49.     NumPut("Int", currentCursorX, "Int", currentCursorY, cursorPosition_pointStruct := Buffer(8))
  50.     MsgBox(DllCall("MonitorFromPoint", "Int64", NumGet(cursorPosition_pointStruct, "Int64"), "UInt", 0))
  51. }
  52.  
  53. ; For information about more complex structs, research this function:
  54. F2::DetermineIsWindowFullscreen(WinExist("A"))
  55.  
  56. ; When you need to determine whether a non-active window is fullscreen, you can use this (or use the alpha features mentioned at the top w/ "cbSize: i32 := ObjGetDataSize(this)", so you don't need Buffer, NumPut & NumGet, nor knowing how much bytes the struct is)
  57. DetermineIsWindowFullscreen(winId)
  58. {
  59.     WinGetClientPos(&specifiedWindowLeftX, &specifiedWindowTopY, &specifiedWindowWidth, &specifiedWindowHeight, winId)
  60.     specifiedWindowRightX := specifiedWindowLeftX + specifiedWindowWidth
  61.     specifiedWindowBottomY := specifiedWindowTopY + specifiedWindowHeight
  62.  
  63.     ; The MONITORINFO struct consists of DWORD cbSize (4 bytes), RECT rcMonitor (16 bytes), RECT rcWork (16 bytes) and DWORD dwFlags (4 bytes).
  64.     ; Which is 40 bytes in total. DWORD (aka UInt) is 4 bytes and RECT is 16 bytes (a struct consisting of 4 LONG variables [C++ type], aka Int which is 4 bytes, so 4*4=16)
  65.     ; GetMonitorInfo requires you to set the cbSize member of the structure to sizeof(MONITORINFO) before calling the GetMonitorInfo function, hence NumPut being used
  66.     NumPut("UInt", 40, monitorInfo := Buffer(40))
  67.     DllCall("GetMonitorInfo", "Ptr", DllCall("MonitorFromWindow", "UInt", WinExist("A"), "UInt", MONITOR_DEFAULTTOPRIMARY := 1), "Ptr", monitorInfo)
  68.  
  69.     ; The first 4 bytes (DWORD cbSize) aren't relevant for the situation, the next 16 bytes (RECT rcMonitor) are necessary so it starts after the 4 bytes that were skipped.
  70.     ; Then there are 20 more bytes (RECT rcWork and DWORD dwFlags, in that order) which also aren't relevant for the situation, so they won't be used either
  71.     specifiedWindowMonitorLeftX := NumGet(monitorInfo, 4, "Int")
  72.     specifiedWindowMonitorTopY := NumGet(monitorInfo, 8, "Int")
  73.     specifiedWindowMonitorRightX := NumGet(monitorInfo, 12, "Int")
  74.     specifiedWindowMonitorBottomY := NumGet(monitorInfo, 16, "Int")
  75.  
  76. if (specifiedWindowLeftX <= specifiedWindowMonitorLeftX && specifiedWindowTopY <= specifiedWindowMonitorTopY && specifiedWindowRightX >= specifiedWindowMonitorRightX && specifiedWindowBottomY >= specifiedWindowMonitorBottomY)
  77.     Tooltip("fullscreen`n" specifiedWindowLeftX " " specifiedWindowTopY " " specifiedWindowRightX " " specifiedWindowBottomY "`n" specifiedWindowMonitorLeftX " " specifiedWindowMonitorTopY " " specifiedWindowMonitorRightX " " specifiedWindowMonitorBottomY)
  78. else
  79.     Tooltip("normal`n" specifiedWindowLeftX " " specifiedWindowTopY " " specifiedWindowRightX " " specifiedWindowBottomY "`n" specifiedWindowMonitorLeftX " " specifiedWindowMonitorTopY " " specifiedWindowMonitorRightX " " specifiedWindowMonitorBottomY)
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement