Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Requires AutoHotkey 2.0
- ; 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
- /*
- #Requires AutoHotkey 2.1-alpha.8
- class Point
- {
- X: i32, Y: i32
- }
- MonitorFromCursor()
- {
- DllCall("GetCursorPos", "Ptr", cursorPosition_pointStruct := Point())
- MsgBox(DllCall("MonitorFromPoint", Point, cursorPosition_pointStruct, "UInt", 0))
- }
- */
- ; The best way to get the monitor is this: MonitorFromCursor() => (DllCall("GetCursorPos", "UInt64*", &cursorPosition := 0), DllCall("MonitorFromPoint", "Int64", cursorPosition, "UInt", 0))
- ; And this is also possible with alpha structs:
- /*
- #Requires AutoHotkey 2.1-alpha.9
- class Point
- {
- X: i32, Y: i32
- }
- class MSLLHOOKSTRUCT
- {
- Pt: Point, MouseData: i32, Flags: u32, Time: u32, DwExtraInfo: uptr
- }
- ; And then inside the PointerDeviceProc callback fn (https://learn.microsoft.com/en-us/windows/win32/winmsg/lowlevelmouseproc)
- eventInfo := StructFromPtr(MSLLHOOKSTRUCT, lParam)
- Tooltip(eventInfo.Pt.X)
- */
- F1::
- {
- ; "Int" is 4 bytes, so both axes together is 8 bytes total
- ; Can use the pointStruct as "Ptr" argument in DllCall (no need to use * symbol in the type nor the & symbol in the value):
- pointStruct := Buffer(8)
- NumPut("Int", xValueToAdd := 123, pointStruct, 0)
- NumPut("Int", yValueToAdd := 456, pointStruct, 4)
- ; 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"):
- DllCall("GetCursorPos", "Ptr", cursorPosition_pointStruct := Buffer(8))
- currentCursorX := NumGet(cursorPosition_pointStruct, 0, "Int")
- currentCursorY := NumGet(cursorPosition_pointStruct, 4, "Int")
- MsgBox(currentCursorX " " currentCursorY)
- ; 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):
- MouseGetPos(¤tCursorX, ¤tCursorY)
- NumPut("Int", currentCursorX, "Int", currentCursorY, cursorPosition_pointStruct := Buffer(8))
- MsgBox(DllCall("MonitorFromPoint", "Int64", NumGet(cursorPosition_pointStruct, "Int64"), "UInt", 0))
- }
- ; For information about more complex structs, research this function:
- F2::DetermineIsWindowFullscreen(WinExist("A"))
- ; 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)
- DetermineIsWindowFullscreen(winId)
- {
- WinGetClientPos(&specifiedWindowLeftX, &specifiedWindowTopY, &specifiedWindowWidth, &specifiedWindowHeight, winId)
- specifiedWindowRightX := specifiedWindowLeftX + specifiedWindowWidth
- specifiedWindowBottomY := specifiedWindowTopY + specifiedWindowHeight
- ; The MONITORINFO struct consists of DWORD cbSize (4 bytes), RECT rcMonitor (16 bytes), RECT rcWork (16 bytes) and DWORD dwFlags (4 bytes).
- ; 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)
- ; GetMonitorInfo requires you to set the cbSize member of the structure to sizeof(MONITORINFO) before calling the GetMonitorInfo function, hence NumPut being used
- NumPut("UInt", 40, monitorInfo := Buffer(40))
- DllCall("GetMonitorInfo", "Ptr", DllCall("MonitorFromWindow", "UInt", WinExist("A"), "UInt", MONITOR_DEFAULTTOPRIMARY := 1), "Ptr", monitorInfo)
- ; 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.
- ; 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
- specifiedWindowMonitorLeftX := NumGet(monitorInfo, 4, "Int")
- specifiedWindowMonitorTopY := NumGet(monitorInfo, 8, "Int")
- specifiedWindowMonitorRightX := NumGet(monitorInfo, 12, "Int")
- specifiedWindowMonitorBottomY := NumGet(monitorInfo, 16, "Int")
- if (specifiedWindowLeftX <= specifiedWindowMonitorLeftX && specifiedWindowTopY <= specifiedWindowMonitorTopY && specifiedWindowRightX >= specifiedWindowMonitorRightX && specifiedWindowBottomY >= specifiedWindowMonitorBottomY)
- Tooltip("fullscreen`n" specifiedWindowLeftX " " specifiedWindowTopY " " specifiedWindowRightX " " specifiedWindowBottomY "`n" specifiedWindowMonitorLeftX " " specifiedWindowMonitorTopY " " specifiedWindowMonitorRightX " " specifiedWindowMonitorBottomY)
- else
- Tooltip("normal`n" specifiedWindowLeftX " " specifiedWindowTopY " " specifiedWindowRightX " " specifiedWindowBottomY "`n" specifiedWindowMonitorLeftX " " specifiedWindowMonitorTopY " " specifiedWindowMonitorRightX " " specifiedWindowMonitorBottomY)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement