wwww

wwTiling

Jun 3rd, 2014
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. GoSub Initialise
  2.  
  3. ; Pixel Values
  4. MarginWidth := 8                 ; Margin (px)
  5. MarginWidthHalf := MarginWidth//2 ; Margin of edges (px)
  6. SnapDistance := 16                ; Margin of edges (px)
  7. MinimumMovement := 10             ; How far to move before activating (px)
  8.  
  9. ; Toggles
  10. EnableMoveHotkey := True
  11. EnableResizeHotkey := True
  12.     SnapToGrid := True
  13.     SnapToWindows := True
  14. EnableMoveResizeHotkey := True
  15. MoveToTopToMaximize := True
  16.     TopPos := 0
  17. MoveToBottomToClose := True
  18.     BottomPos := 1023
  19. VisibleGrid := True
  20.  
  21. ColorPreview := 0x272822
  22. TransparencyPreview := 128
  23. ColorGrid := 0x000000
  24. TransparencyGrid := 128
  25.  
  26. ; The default config results in this grid:
  27. ; +----+----+
  28. ; |    |    |
  29. ; |    +----+
  30. ; |    |    |
  31. ; +----+----+
  32. ;
  33. ; You can bind your own hotkeys to certain positions using
  34. ;   MoveWindowToTile(window-title, x0-index, x1-index, y0-index, y1-index)
  35. ; For example:
  36. ;   NumpadIns:: MoveWindowToTile("A", 1, 3, 1, 2)
  37.  
  38. ; V[n]:= [ x-coord, y0-index, y1-index, corner?, resize-only?]
  39. V := []
  40. V[1] := [ ScreenX,                  1,  3,  1, 0 ]
  41. V[2] := [ ScreenX+ScreenW/2,        1,  3,  0, 0 ]
  42. V[3] := [ ScreenX+ScreenW,          1,  3,  1, 0 ]
  43.  
  44. ; H[n]:= [ y-coord, x0-index, x1-index, corner?, resize-only?]
  45. H := []
  46. H[1] := [ ScreenY,                  1,  3,  1, 0 ]
  47. H[2] := [ ScreenY+(ScreenH-32)/2,   2,  3,  0, 0 ]
  48. H[3] := [ ScreenY+ScreenH-32,       1,  3,  1, 0 ]
  49.  
  50. ; Rectangles (higher priority)
  51. Rectangles := []
  52. ; [ left-index, right-index, top-index, bottom-index]
  53. Rectangles.insert([  ])
  54.  
  55. HotkeyMove := "MButton"
  56. HotkeyResize := "RButton"
  57. HotkeyMoveResize := "LButton"
  58.  
  59. HotkeyModifier := "!"
  60.  
  61. ; DO NOT EDIT BEYOND THIS POINT
  62.  
  63. GoSub Finalise
  64. Return
  65.  
  66. Initialise:
  67.     CoordMode, Mouse, Screen
  68.     CoordMode, Pixel, Screen
  69.     #SingleInstance, Force
  70.     #NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
  71.     ; #Warn  ; Recommended for catching common errors.
  72.     SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
  73.     SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
  74.     SetBatchLines, -1
  75.     SetWinDelay, -1
  76.  
  77.     SysGet, ScreenX, 76 ; SM_XVIRTUALSCREEN
  78.     SysGet, ScreenY, 77 ; SM_YVIRTUALSCREEN
  79.     SysGet, ScreenW, 78 ; SM_CXVIRTUALSCREEN
  80.     SysGet, ScreenH, 79 ; SM_CYVIRTUALSCREEN
  81. Return
  82.  
  83. Finalise:
  84.     If (EnableMoveHotkey)
  85.         Hotkey, % HotkeyModifier . HotkeyMove, MoveWindow
  86.     If (EnableResizeHotkey)
  87.         Hotkey, % HotkeyModifier . HotkeyResize, ResizeWindow
  88.     PreviewID := CreatePreview()
  89.     If (VisibleGrid) {
  90.         CreateBitmap()
  91.         CreateGrid()
  92.     }
  93. Return
  94.  
  95. MoveWindow:
  96.     MouseGetPos, X, Y, WinId
  97.     OrigX := X, OrigY := Y
  98.     WinTitle := "ahk_id " . WinId
  99.     DrawWindow(WinTitle)
  100. MoveWindowDo:
  101.     While 1 {
  102.         Sleep 10 ; Sleep until movement exceeds threshold or button is released
  103.         MouseGetPos, X, Y
  104.         If (abs(OrigX - X) > MinimumMovement or abs(OrigY - Y) > MinimumMovement)
  105.             Break
  106.         If (!GetKeyState(HotkeyMove, "P")) {
  107.             UndrawWindow(WinTitle)
  108.             Return
  109.         }
  110.     }
  111.  
  112.  
  113.     Left := 0, Right = 0, Top = 0, Bottom = 0
  114.     While (GetKeyState(HotkeyMove, "P")) {
  115.         If (GetKeyState(HotkeyMoveResize, "P")) {
  116.             GoSub MoveResizeWindowDo
  117.             Return
  118.         }
  119.         MouseGetPos, X, Y
  120.         If (MoveToTopToMaximize and Y = TopPos) {
  121.             MaximizePreview()
  122.             GoSub MoveWindowDo
  123.             Break
  124.         }
  125.         Left := 0, Right = 0, Top = 0, Bottom = 0
  126.         If (GetBestTile(X, Y, Left, Right, Top, Bottom))
  127.             ShowPreviewAtTile(Left, Right, Top, Bottom)
  128.     }
  129.     If (MoveToTopToMaximize and Y = TopPos)
  130.         WinMaximize, % WinTitle
  131.     Else
  132.         WinRestore, % WinTitle
  133.     HidePreview()
  134.     MoveWindowToTile(WinTitle, Left, Right, Top, Bottom)
  135.     UndrawWindow(WinTitle)
  136.     If (MoveToBottomToClose and Y = BottomPos) {
  137.         WinClose, % WinTitle
  138.     }
  139. Return
  140.  
  141. MoveResizeWindowDo:
  142.     MouseGetPos, OrigX, OrigY
  143.     Left := 0, Right = 0, Top = 0, Bottom = 0
  144.     While (GetKeyState(HotkeyMoveResize, "P") or GetKeyState(HotkeyMove, "P") or GetKeyState(HotkeyResize, "P")) {
  145.         MouseGetPos, X, Y
  146.         Left := 0, Right = 0, Top = 0, Bottom = 0
  147.         If (GetBoundingTile(OrigX, OrigY, X, Y, Left, Right, Top, Bottom))
  148.             ShowPreviewAtTile(Left, Right, Top, Bottom)
  149.     }
  150.     HidePreview()
  151.     MoveWindowToTile(WinTitle, Left, Right, Top, Bottom)
  152.     UndrawWindow(WinTitle)
  153. Return
  154.  
  155. ResizeWindow:
  156.     MouseGetPos, X, Y, WinId
  157.     OrigX := X, OrigY := Y
  158.     WinTitle := "ahk_id " . WinId
  159.     DrawWindow(WinTitle)
  160. ResizeWindowDo:
  161.     While 1 {
  162.         Sleep 10 ; Sleep until movement exceeds threshold or button is released
  163.         MouseGetPos, X, Y
  164.         If (abs(OrigX - X) > MinimumMovement or abs(OrigY - Y) > MinimumMovement)
  165.             Break
  166.         If (!GetKeyState(HotkeyResize, "P")) {
  167.             UndrawWindow(WinTitle)
  168.             Return
  169.         }
  170.     }
  171.  
  172.     WinGet, MinMax, MinMax, % WinTitle
  173.     If (MinMax = 1 and not (MoveToTopToMaximize and Y = TopPos)) {
  174.         WinRestore, % WinTitle
  175.         WinGetPos, WinX, WinY, WinW, WinH, % WinTitle
  176.         WinX := X - WinW/2
  177.         WinY := Y - WinH/2
  178.         WinMaximize, % WinTitle
  179.     } Else
  180.         WinGetPos, WinX, WinY, WinW, WinH, % WinTitle
  181.  
  182.     HorizontalResize := (X - WinX) * 3 // WinW - 1
  183.     VerticalResize := (Y - WinY) * 3 // WinH - 1
  184.     If (not HorizontalResize and not VerticalResize) {
  185.         NewX := WinX
  186.         NewY := WinY
  187.         While (GetKeyState(HotkeyResize, "P")) {
  188.             If (GetKeyState(HotkeyMoveResize, "P")) {
  189.                 GoSub MoveResizeWindowDo
  190.                 Return
  191.             }
  192.             MouseGetPos, X, Y
  193.             If (MoveToTopToMaximize and Y = TopPos) {
  194.                 MaximizePreview()
  195.                 GoSub ResizeWindowDo
  196.                 Break
  197.             }
  198.             NewX := X - OrigX + WinX
  199.             NewY := Y - OrigY + WinY
  200.             If (SnapToWindows) {
  201.                 OldX := NewX
  202.                 NewX := LoopWindows(True, True, NewX, NewY, WinTitle, WinW)
  203.                 If (OldX = NewX)
  204.                     NewX := LoopWindows(True, False, NewX + WinW, NewY, WinTitle, WinH) - WinW
  205.             } If (SnapToGrid) {
  206.                 Index := LoopVs(True, NewX, NewY, 0, 1)
  207.                 If (Index)
  208.                     NewX := GetX(Index) + (GetCornerV(Index) ? MarginWidth : MarginWidthHalf)
  209.                 Else
  210.                     Index := LoopVs(False, NewX + WinW, NewY, 0, 1)
  211.                     If (Index)
  212.                         NewX := GetX(Index) + (GetCornerV(Index) ? MarginWidth : MarginWidthHalf)
  213.             } If (SnapToWindows) {
  214.                 OldY := NewY
  215.                 NewY := LoopWindows(False, True, NewX, NewY, WinTitle, WinW)
  216.                 If (OldY = NewY)
  217.                     NewY := LoopWindows(False, False, NewX, NewY + WinH, WinTitle, WinH) - WinH
  218.             } If (SnapToGrid) {
  219.                 Index := LoopHs(True, NewX, NewY, 0, 1)
  220.                 If (Index)
  221.                     NewY := GetY(Index) + (GetCornerH(Index) ? MarginWidth : MarginWidthHalf)
  222.                 Else
  223.                     Index := LoopHs(False, NewX, NewY + WinH, 0, 1)
  224.                     If (Index)
  225.                         NewY := GetY(Index) + (GetCornerH(Index) ? MarginWidth : MarginWidthHalf)
  226.             }
  227.             ShowPreview(NewX, NewY, WinW, WinH)
  228.         }
  229.         If (MoveToTopToMaximize and Y = TopPos)
  230.             WinMaximize, % WinTitle
  231.         Else {
  232.             WinRestore, % WinTitle
  233.             WinMove, % WinTitle, , % NewX, % NewY
  234.         }
  235.     } Else {
  236.         While (GetKeyState(HotkeyResize, "P")) {
  237.             MouseGetPos, X, Y
  238.             NewX0 := WinX
  239.             NewY0 := WinY
  240.             NewX1 := WinX + WinW
  241.             NewY1 := WinY + WinH
  242.             If (HorizontalResize == -1) {
  243.                 NewX0 += X - OrigX
  244.                 If (SnapToWindows)
  245.                     NewX0 := LoopWindows(True, True, NewX0, Y, WinTitle)
  246.                 If (SnapToGrid) {
  247.                     Index := LoopVs(True, NewX0, Y, 0, 1)
  248.                     If (Index)
  249.                         NewX0 := GetX(Index) + (GetCornerV(Index) ? MarginWidth : MarginWidthHalf)
  250.                 }
  251.             } Else If (HorizontalResize == 1) {
  252.                 NewX1 += X - OrigX
  253.                 If (SnapToWindows)
  254.                     NewX1 := LoopWindows(True, False, NewX1, Y, WinTitle)
  255.                 If (SnapToGrid) {
  256.                     Index := LoopVs(False, NewX1, Y, 0, 1)
  257.                     If (Index)
  258.                         NewX1 := GetX(Index) - (GetCornerV(Index) ? MarginWidth : MarginWidthHalf)
  259.                 }
  260.             }
  261.             If (VerticalResize == -1) {
  262.                 NewY0 += Y - OrigY
  263.                 If (SnapToWindows)
  264.                     NewY0 := LoopWindows(False, True, X, NewY0, WinTitle)
  265.                 If (SnapToGrid) {
  266.                     Index := LoopHs(True, X, NewY0, 0, 1)
  267.                     If (Index)
  268.                         NewY0 := GetY(Index) + (GetCornerH(Index) ? MarginWidth : MarginWidthHalf)
  269.                 }
  270.             } Else If (VerticalResize == 1) {
  271.                 NewY1 += Y - OrigY
  272.                 If (SnapToWindows)
  273.                     NewY1 := LoopWindows(False, False, X, NewY1, WinTitle)
  274.                 If (SnapToGrid) {
  275.                     Index := LoopHs(False, X, NewY1, 0, 1)
  276.                     If (Index)
  277.                         NewY1 := GetY(Index) - (GetCornerH(Index) ? MarginWidth : MarginWidthHalf)
  278.                 }
  279.             }
  280.             ShowPreview(NewX0, NewY0, NewX1 - NewX0, NewY1 - NewY0)
  281.         }
  282.         WinMove, % WinTitle, , % NewX0, % NewY0, % NewX1 - NewX0, % NewY1 - NewY0
  283.     }
  284.     HidePreview()
  285.     UndrawWindow(WinTitle)
  286. Return
  287.  
  288. MoveWindowToTile(Title, Left, Right, Top, Bottom) {
  289.     Global MarginWidth, MarginWidthHalf
  290.     If (Left = 4 and Right = 8 and Top = 1 and Bottom = 5) {
  291.         X := GetX(Left)
  292.         Y := GetY(Top)
  293.         WinMove, % Title,
  294.             , % X
  295.             , % Y
  296.             , % GetX(Right) - X - MarginWidthHalf
  297.             , % GetY(Bottom) - Y - 2
  298.         Return
  299.     } Else {
  300.         X := GetX(Left) + (GetCornerV(Left) ? MarginWidth : MarginWidthHalf)
  301.         Y := GetY(Top) + (GetCornerH(Top) ? MarginWidth : MarginWidthHalf)
  302.         Width := GetX(Right) - X - (GetCornerV(Right) ? MarginWidth : MarginWidthHalf)
  303.         Height := GetY(Bottom) - Y  - (GetCornerH(Bottom) ? MarginWidth : MarginWidthHalf)
  304.         WinMove, % Title,, % X, % Y, % Width, % Height
  305.     }
  306. }
  307.  
  308. ShowPreviewAtTile(Left, Right, Top, Bottom) {
  309.     Global MarginWidth, MarginWidthHalf
  310.     X := GetX(Left) + (GetCornerV(Left) ? MarginWidth : MarginWidthHalf)
  311.     Y := GetY(Top) + (GetCornerH(Top) ? MarginWidth : MarginWidthHalf)
  312.     W := GetX(Right) - X - (GetCornerV(Right) ? MarginWidth : MarginWidthHalf)
  313.     H := GetY(Bottom) - Y - (GetCornerH(Bottom) ? MarginWidth : MarginWidthHalf)
  314.     Gui, 2:Show, % "x" . X . " y" . Y . " w" . W . " h" . H
  315. }
  316. ShowPreview(X, Y, W, H) {
  317.     Gui, 2:Show, % "x" . X . " y" . Y . " w" . W . " h" . H
  318. }
  319. MaximizePreview() {
  320.     Global ScreenX, ScreenY, ScreenW, ScreenH
  321.     Gui, 2:Show, % "x" . ScreenX . " y" . ScreenY . " w" . ScreenW . " h" . ScreenH
  322. }
  323. HidePreview() {
  324.     Gui, 2:Hide
  325. }
  326.  
  327. GetBestTile(MouseX, MouseY, ByRef Left, ByRef Right, ByRef Top, ByRef Bottom) {
  328.     Left := GetLeftLine(MouseX, MouseY, 1)
  329.     Right := GetRightLine(MouseX, MouseY, 1)
  330.     Top := GetTopLine(MouseX, MouseY, 1)
  331.     Bottom := GetBottomLine(MouseX, MouseY, 1)
  332.  
  333.     If (Left and Right and Top and Bottom)
  334.         Return 1
  335.     Else
  336.         Return 0
  337. }
  338. GetBoundingTile(MouseX0, MouseY0, MouseX1, MouseY1, ByRef Left, ByRef Right, ByRef Top, ByRef Bottom) {
  339.     If (MouseX0 > MouseX1)
  340.     {
  341.         Temp := MouseX0
  342.         MouseX0 := MouseX1
  343.         MouseX1 := Temp
  344.     }
  345.     If (MouseY0 > MouseY1)
  346.     {
  347.         Temp := MouseY0
  348.         MouseY0 := MouseY1
  349.         MouseY1 := Temp
  350.     }
  351.     Left := GetLeftLine(MouseX0, MouseY0)
  352.     Right := GetRightLine(MouseX1, MouseY1)
  353.     Top := GetTopLine(MouseX0, MouseY0)
  354.     Bottom := GetBottomLine(MouseX1, MouseY1)
  355.  
  356.     If (Left and Right and Top and Bottom)
  357.         Return 1
  358.     Else
  359.         Return 0
  360. }
  361. GetLeftLine(MouseX, MouseY, Best=0) {
  362.     Return LoopVs(False, MouseX, MouseY, Best)
  363. }
  364. GetRightLine(MouseX, MouseY, Best=0) {
  365.     Return LoopVs(True, MouseX, MouseY, Best)
  366. }
  367. GetTopLine(MouseX, MouseY, Best=0) {
  368.     Return LoopHs(False, MouseX, MouseY, Best)
  369. }
  370. GetBottomLine(MouseX, MouseY, Best=0) {
  371.     Return LoopHs(True, MouseX, MouseY, Best)
  372. }
  373. LoopVs(LeftToRight, MouseX, MouseY, Best=0, Nearest=0) {
  374.     Global V, SnapDistance
  375.     Loop % V.MaxIndex() {
  376.         Index := LeftToRight ? A_Index : (V.MaxIndex() - A_Index + 1)
  377.         If (Best and GetResizeV(Index))
  378.             Continue
  379.         If (GetY1(Index) > MouseY and MouseY >= GetY0(Index))
  380.         {
  381.             If (Nearest ? (abs(MouseX - GetX(Index)) < SnapDistance) : (LeftToRight ? (GetX(Index) > MouseX) : (MouseX >= GetX(Index))))
  382.             {
  383.                 Return Index
  384.             }
  385.         }
  386.     }
  387.     Return 0
  388. }
  389. LoopHs(TopToBottom, MouseX, MouseY, Best=0, Nearest=0) {
  390.     Global H, SnapDistance
  391.     Loop % H.MaxIndex() {
  392.         Index := TopToBottom ? A_Index : (H.MaxIndex() - A_Index + 1)
  393.         If (Best and GetResizeH(Index))
  394.             Continue
  395.         If (GetX1(Index) > MouseX and MouseX >= GetX0(Index))
  396.         {
  397.             If (Nearest ? (abs(MouseY - GetY(Index)) < SnapDistance) : (TopToBottom ? (GetY(Index) > MouseY) : (MouseY >= GetY(Index))))
  398.             {
  399.                 Return Index
  400.             }
  401.         }
  402.     }
  403.     Return 0
  404. }
  405. LoopWindows(IsHorizontal, IsReversed, X, Y, WinTitle, Length=0) {
  406.     Global SnapDistance, MarginWidth, PreviewID
  407.  
  408.     WinGetPos, CurrWinX, CurrWinY, CurrWinW, CurrWinH, % WinTitle
  409.     BestDistance := SnapDistance
  410.     NewPos := IsHorizontal ? X : Y
  411.     WinGet, id, list,,, Program Manager
  412.     Loop, % id
  413.     {
  414.         WinGet, WinExStyle, ExStyle, % WinTitle
  415.         If (WinExStyle & 0x80 or id%A_Index% = CurrentWindow)
  416.             Continue
  417.         If ("ahk_id " . id%A_Index% = WinTitle or id%A_Index% = PreviewID)
  418.             Continue
  419.         WinGetPos, WinX, WinY, WinW, WinH, % "ahk_id" . id%A_Index%
  420.         If (IsHorizontal ? (WinY - MarginWidth - Length < Y and Y < WinY + WinH + MarginWidth + Length) : (WinX - MarginWidth - Length < X and X < WinX + WinW + MarginWidth + Length)) {
  421.             NewDistance := IsHorizontal ? abs(WinX - X) : abs(WinY - Y)
  422.             If (NewDistance < BestDistance) {
  423.                 NewPos := (IsHorizontal ? WinX : WinY) + MarginWidth * (IsReversed ? 0 : -1)
  424.                 BestDistance := NewDistance
  425.             }
  426.             NewDistance := IsHorizontal ? abs(WinX + WinW - X) : abs(WinY + WinH - Y)
  427.             If (NewDistance < BestDistance) {
  428.                 NewPos := (IsHorizontal ? WinX + WinW : WinY + WinH) + MarginWidth * (IsReversed ? 1 : 0)
  429.                 BestDistance := NewDistance
  430.             }
  431.         }
  432.     }
  433.     Return NewPos
  434. }
  435. GetEdgeV(IsReversed, X, Y, WinTitle) {
  436.     Global MarginWidth, MarginWidthHalf
  437.     Index := LoopVs(IsReversed, X, Y, 0, 1)
  438.     If (Index)
  439.         Return GetX(Index) + (GetCornerV(Index) ? MarginWidth : MarginWidthHalf) * (IsReversed ? 1 : -1)
  440.     Return LoopWindows(True, IsReversed, X, Y, WinTitle)
  441. }
  442. GetEdgeH(IsReversed, X, Y, WinTitle) {
  443.     Global MarginWidth, MarginWidthHalf
  444.     Index := LoopHs(IsReversed, X, Y, 0, 1)
  445.     If (Index)
  446.         Return GetY(Index) + (GetCornerH(Index) ? MarginWidth : MarginWidthHalf) * (IsReversed ? 1 : -1)
  447.     Return LoopWindows(False, IsReversed, X, Y, WinTitle)
  448. }
  449. GetX(index) {
  450.     Global V
  451.     Return V[index][1]
  452. }
  453. GetY0(index) {
  454.     Global V, H
  455.     Return H[V[index][2]][1]
  456. }
  457. GetY1(index) {
  458.     Global V, H
  459.     Return H[V[index][3]][1]
  460. }
  461. GetY(index) {
  462.     Global H
  463.     Return H[index][1]
  464. }
  465. GetX0(index) {
  466.     Global V, H
  467.     Return V[H[index][2]][1]
  468. }
  469. GetX1(index) {
  470.     Global V, H
  471.     Return V[H[index][3]][1]
  472. }
  473. GetCornerV(index) {
  474.     Global V
  475.     Return V[index][4]
  476. }
  477. GetCornerH(index) {
  478.     Global H
  479.     Return H[index][4]
  480. }
  481. GetResizeV(index) {
  482.     Global V
  483.     Return V[index][5]
  484. }
  485. GetResizeH(index) {
  486.     Global H
  487.     Return H[index][5]
  488. }
  489. DrawWindow(title) {
  490.     Global VisibleGrid
  491.     WinActivate, % title
  492.     WinSet, AlwaysOnTop, On, % title
  493.     ; WinSet, Transparent, 212, % title
  494.     If (VisibleGrid)
  495.         ShowGrid()
  496. }
  497. UndrawWindow(title) {
  498.     Global VisibleGrid
  499.     WinSet, AlwaysOnTop, Off, % title
  500.     ; WinSet, Transparent, Off, % title
  501.     If (VisibleGrid)
  502.         HideGrid()
  503. }
  504.  
  505. CreateBitmap() {
  506.     Global ColorGrid
  507.     ; Create bitmap
  508.     VarSetCapacity( BMP, 64 )
  509.     NumPut( 19778, BMP, 0, "UShort" ) ; bfType - must always be set to 'BM' to declare that this is a .bmp-file.
  510.     NumPut(    62, BMP, 2, "UShort" ) ; bfSize - specifies the size of the file in bytes.
  511.     NumPut(     0, BMP, 6, "UInt"   ) ; reserved
  512.     NumPut(    54, BMP,10, "UInt"   ) ; bfOffBits - specifies the offset from the beginning of the file to the bitmap data.
  513.     NumPut(    40, BMP,14, "UInt"   ) ; biSize - 40 specifies the size of the BITMAPINFOHEADER structure, in bytes.
  514.     NumPut(     1, BMP,18, "UInt"   ) ; biWidth - specifies the width of the image, in pixels.
  515.     NumPut(     1, BMP,22, "UInt"   ) ; biHeight - specifies the height of the image, in pixels.  
  516.     NumPut(     1, BMP,26, "UShort" ) ; biPlanes - specifies the number of planes of the target device, must be set to zero.
  517.     NumPut(    24, BMP,28, "UShort" ) ; biBitCount - specifies the number of bits per pixel.
  518.     NumPut(     0, BMP,30, "UInt"   ) ; biCompression - Specifies the type of compression, usually set to zero (no compression).
  519.     NumPut(     8, BMP,34, "UInt"   ) ; biSizeImage - specifies the size of the image data, in bytes.
  520.     NumPut(     0, BMP,38, "UInt"   ) ; biXPelsPerMeter - specifies the the horizontal pixels per meter on the designated target device, usually set to zero.
  521.     NumPut(     0, BMP,42, "UInt"   ) ; biYPelsPerMeter - specifies the the horizontal pixels per meter on the designated target device, usually set to zero.
  522.     NumPut(     0, BMP,46, "UInt"   ) ; biClrUsed - specifies the number of colors used in the bitmap, if set to zero the number of colors is calculated using the biBitCount member.
  523.     NumPut(     0, BMP,50, "UInt"   ) ; biClrImportant - specifies the number of color that are 'important' for the bitmap, if set to zero, all colors are important
  524.  
  525.     ; RGBQUAD - Data area
  526.     NumPut( ColorGrid, BMP, 54, "UInt" ) ; Pixel Color - Black in BGR  
  527.  
  528.     GENERIC_WRITE = 0x40000000  ; Open the file for writing rather than reading.
  529.     CREATE_ALWAYS = 2  ; Create new file (overwriting any existing file).
  530.     hFile := DllCall( "CreateFile", Str, "pixel.bmp", Uint, GENERIC_WRITE, Uint, 0, UInt, 0, UInt, CREATE_ALWAYS, Uint, 0, UInt, 0)
  531.     DllCall("WriteFile", UInt, hFile, Str, BMP, UInt, 62, UIntP, BytesActuallyWritten, UInt, 0)
  532.     DllCall("CloseHandle", UInt, hFile)
  533. }
  534.    
  535. CreatePreview() {
  536.     Global ColorPreview, TransparencyPreview
  537.     Gui, 2:Default
  538.     Gui, Color, % ColorPreview
  539.     Gui, +LastFound
  540.     WinSet, Transparent, % TransparencyPreview
  541.     Gui, +Owner +AlwaysOnTop -Resize -SysMenu -MinimizeBox -MaximizeBox -Disabled -Caption -Border -ToolWindow
  542.     Return WinExist()
  543. }
  544. CreateGrid() {
  545.     Global V, H, MarginWidth, ScreenX, ScreenY, TransparencyGrid
  546.  
  547.     LineWidth := MarginWidth = 0 ? 2 : MarginWidth
  548.     Gui, 1:Default
  549.     Gui, Color, 0xFF00FF
  550.     Gui, +LastFound
  551.     WinSet, TransColor, 0xFF00FF %TransparencyGrid%
  552.     Gui, +Owner +AlwaysOnTop -Resize -SysMenu -MinimizeBox -MaximizeBox -Disabled -Caption -Border -ToolWindow
  553.     Loop, % V.MaxIndex() {
  554.         X := GetX(A_Index) - ScreenX   ; to convert the position to positive
  555.         Y := GetY0(A_Index) - ScreenY                         ; to convert the position to positive
  556.         Height := GetY1(A_Index) - GetY0(A_Index)
  557.         If (GetCornerV(A_Index))
  558.             Gui, Add, Picture, % "x" . X-LineWidth . " y" . Y . " w" . LineWidth*2 . " h" . Height, pixel.bmp
  559.         Else
  560.             Gui, Add, Picture, % "x" . X-LineWidth//2 . " y" . Y . " w" . LineWidth . " h" . Height, pixel.bmp
  561.     }
  562.     Loop, % H.MaxIndex() {
  563.         Y := GetY(A_Index) - ScreenY   ; to convert the position to positive
  564.         X := GetX0(A_Index) - ScreenX                         ; to convert the position to positive
  565.         Width := GetX1(A_Index) - GetX0(A_Index)
  566.         If (GetCornerH(A_Index))
  567.             Gui, Add, Picture, % "x" . X . " y" . Y-LineWidth . " w" . Width . " h" . LineWidth*2, pixel.bmp
  568.         Else
  569.             Gui, Add, Picture, % "x" . X . " y" . Y-LineWidth//2 . " w" . Width . " h" . LineWidth, pixel.bmp
  570.     }
  571. }
  572. ShowGrid() {
  573.     Global ScreenX, ScreenY, ScreenW, ScreenH
  574.     Gui, 1:Show, % "x" . ScreenX . " y" . ScreenY . " w" . ScreenW . " h" . ScreenH
  575. }
  576. HideGrid() {
  577.     Gui, 1:Hide
  578. }
  579. #If VisibleGrid
  580. NumpadDiv::ShowGrid()
  581. NumpadDiv Up::HideGrid()
  582. #If
Advertisement
Add Comment
Please, Sign In to add comment