Advertisement
Guest User

Overlay clock

a guest
Jun 18th, 2012
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 7.66 KB | None | 0 0
  1. #include <GDIPlus.au3>
  2. #include <WindowsConstants.au3>
  3. #include <WinAPI.au3>
  4. #include <Constants.au3>
  5.  
  6. Opt("TrayMenuMode", 1)
  7. Opt("TrayOnEventMode", 1)
  8.  
  9. Global $dial = 0, $aD, $m_nWidth, $m_nHeight, $hourNeedle = 0, $aH, $minuteNeedle = 0, $aM, $secondNeedle = 0, $aS
  10. Global $hClock, $aThemes
  11.  
  12. _Main()
  13.  
  14. Exit
  15.  
  16. Func _Main()
  17.     _GDIPlus_Startup()
  18.     $hClock = _CreateClockWindow()
  19.     _CreateTrayMenu()
  20.     _LoadTheme()
  21.  
  22.     While True
  23.         Sleep(60000)
  24.     WEnd
  25. EndFunc   ;==>_Main
  26.  
  27. Func _ShutDown()
  28.     _GDIPlus_ImageDispose($secondNeedle)
  29.     _GDIPlus_ImageDispose($minuteNeedle)
  30.     _GDIPlus_ImageDispose($hourNeedle)
  31.     _GDIPlus_Shutdown()
  32.     _WinAPI_DestroyWindow($hClock)
  33.     Exit
  34. EndFunc   ;==>_ShutDown
  35.  
  36. Func _DrawClock()
  37.     Local $rc = _WinAPI_GetWindowRect($hClock)
  38.     Local $ptSrc = DllStructCreate($tagPOINT)
  39.     DllStructSetData($ptSrc, "X", 0)
  40.     DllStructSetData($ptSrc, "Y", 0)
  41.     Local $ptWinPos = DllStructCreate($tagPOINT)
  42.     DllStructSetData($ptWinPos, "X", DllStructGetData($rc, "left"))
  43.     DllStructSetData($ptWinPos, "Y", DllStructGetData($rc, "top"))
  44.     Local $szWin = DllStructCreate($tagSIZE)
  45.     DllStructSetData($szWin, "X", $m_nWidth)
  46.     DllStructSetData($szWin, "Y", $m_nHeight)
  47.     Local $stBlend = DllStructCreate($tagBLENDFUNCTION)
  48.     DllStructSetData($stBlend, 1, 0x00)
  49.     DllStructSetData($stBlend, 2, 0x00)
  50.     DllStructSetData($stBlend, 3, 0xff)
  51.     DllStructSetData($stBlend, 4, 0x01)
  52.  
  53.     Local $hDC = _WinAPI_GetDC($hClock)
  54.     Local $hdcMemory = _WinAPI_CreateCompatibleDC($hDC)
  55.  
  56.     Local $tagBITMAPINFOHEADER = "dword biSize;long biWidth;long biHeight;short biPlanes;short biBitCount;"
  57.     $tagBITMAPINFOHEADER &= "dword biCompression;dword biSizeImage;long biXPelsPerMeter;long biYPelsPerMeter;"
  58.     $tagBITMAPINFOHEADER &= "dword biClrUsed;dword biClrImportant"
  59.     Local $bmih = DllStructCreate($tagBITMAPINFOHEADER)
  60.     Local $nBytesPerLine = BitShift((($m_nWidth * 32 + 31) & (-31)), -3)
  61.     DllStructSetData($bmih, "biSize", DllStructGetSize($bmih))
  62.     DllStructSetData($bmih, "biWidth", $m_nWidth)
  63.     DllStructSetData($bmih, "biHeight", $m_nHeight)
  64.     DllStructSetData($bmih, "biPlanes", 1)
  65.     DllStructSetData($bmih, "biBitCount", 32)
  66.     DllStructSetData($bmih, "biCompression", 0)
  67.     DllStructSetData($bmih, "biClrUsed", 0)
  68.     DllStructSetData($bmih, "biSizeImage", $nBytesPerLine * $m_nHeight)
  69.  
  70.     $aRet = DllCall("Gdi32.dll", "hwnd", "CreateDIBSection", "ptr", 0, "ptr", DllStructGetPtr($bmih), _
  71.             "uint", 0, "ptr*", 0, "ptr", 0, "dword", 0)
  72.     Local $hbmpMem = $aRet[0]
  73.     Local $pvBits = $aRet[4]
  74.  
  75.     If $hbmpMem Then
  76.         Local $hbmpOld = _WinAPI_SelectObject($hdcMemory, $hbmpMem)
  77.         Local $graph = _GDIPlus_GraphicsCreateFromHDC($hdcMemory)
  78.         _GDIPlus_GraphicsSetSmoothingMode($graph, 3)
  79.  
  80.         Local $hBrush = _GDIPlus_BrushCreateSolid(0xa0000000)
  81.         _GDIPlus_GraphicsFillRect($graph, 0, 0, $m_nWidth, $m_nHeight, $hBrush)
  82.         _DrawNeedle($graph, $hourNeedle, $m_nWidth / 4, $m_nHeight / 4, @HOUR * 30 + (@MIN / 2), 4)
  83.         _DrawNeedle($graph, $minuteNeedle, $m_nWidth / 4, $m_nHeight / 4, @MIN * 6 + (@SEC / 10), 2)
  84.         _DrawNeedle($graph, $secondNeedle, $m_nWidth / 4, $m_nHeight / 4, @SEC * 6, 1)
  85.  
  86.         _WinAPI_UpdateLayeredWindow($hClock, $hDC, DllStructGetPtr($ptWinPos), DllStructGetPtr($szWin), $hdcMemory, _
  87.                 DllStructGetPtr($ptSrc), 0, DllStructGetPtr($stBlend), 2)
  88.  
  89.         _GDIPlus_GraphicsReleaseDC($graph, $hdcMemory)
  90.         _WinAPI_SelectObject($hdcMemory, $hbmpOld)
  91.         _WinAPI_DeleteObject($hbmpMem)
  92.        
  93.         _GDIPlus_BrushDispose($hBrush)
  94.     EndIf
  95.  
  96.     _WinAPI_DeleteDC($hdcMemory)
  97.     _WinAPI_DeleteDC($hDC)
  98.  
  99. EndFunc   ;==>_DrawClock
  100.  
  101. Func _DrawNeedle($graph, $pNeedle, $nNeedleCenterX, $nNeedleCenterY, $rAngle, $needleWidth)
  102.     Local $status = DllCall($ghGDIPDll, "int", "GdipRotateWorldTransform", "hwnd", $graph, "float", $rAngle, "int", 1)
  103.     $status = DllCall($ghGDIPDll, "int", "GdipTranslateWorldTransform", "hwnd", $graph, "float", $m_nWidth / 2, "float", $m_nHeight / 2, "int", 1)
  104.    
  105.     $hBrush = _GDIPlus_PenCreate(0xFFFFFFFF, $needleWidth)
  106.    
  107.     _GDIPlus_GraphicsDrawLine($graph, - ($rAngle = 180), - ($rAngle = 180), $nNeedleCenterX, $nNeedleCenterY, $hBrush)
  108.     ;_GDIPlus_GraphicsDrawImageRect($graph, $pNeedle, -$nNeedleCenterX - ($rAngle = 180), -$nNeedleCenterY - ($rAngle = 180), _
  109.     ;       _GDIPlus_ImageGetWidth($pNeedle), _GDIPlus_ImageGetHeight($pNeedle))
  110.     $status = DllCall($ghGDIPDll, "int", "GdipResetWorldTransform", "hwnd", $graph)
  111.        
  112.     _GDIPlus_PenDispose($hBrush)
  113.  
  114. EndFunc   ;==>_DrawNeedle
  115.  
  116. Func _CreateClockWindow()
  117.     Local Const $CS_VREDRAW = 1
  118.     Local Const $CS_HREDRAW = 2
  119.     Local $sClassName = "ClockWndClass"
  120.     Local $hWndProc = DllCallbackRegister("_MyWndProc", "int", "hwnd;int;wparam;lparam")
  121.     Local $tagWNDCLASS = "uint cbSize;uint style;ptr lpfnWndProc;int cbClsExtra;int cbWndExtra;hwnd hInstance;"
  122.     $tagWNDCLASS &= "hwnd hIcon;hwnd hCursor;hwnd hbrBackground;ptr lpszMenuName;ptr lpszClassName;hwnd hIconSm"
  123.     Local $tWndClassEx = DllStructCreate($tagWNDCLASS)
  124.     Local $tClassName = DllStructCreate("char[" & StringLen($sClassName) + 1 & "]")
  125.     DllStructSetData($tClassName, 1, $sClassName)
  126.     DllStructSetData($tWndClassEx, "cbSize", DllStructGetSize($tWndClassEx))
  127.     DllStructSetData($tWndClassEx, "style", BitOR($CS_VREDRAW, $CS_HREDRAW))
  128.     DllStructSetData($tWndClassEx, "lpfnWndProc", DllCallbackGetPtr($hWndProc))
  129.     DllStructSetData($tWndClassEx, "cbClsExtra", 0)
  130.     DllStructSetData($tWndClassEx, "cbWndExtra", 0)
  131.     DllStructSetData($tWndClassEx, "hInstance", _WinAPI_GetModuleHandle(""))
  132.     DllStructSetData($tWndClassEx, "hIcon", 0)
  133.     DllStructSetData($tWndClassEx, "hCursor", _WinAPI_LoadCursor(0, 32512))
  134.     DllStructSetData($tWndClassEx, "hbrBackground", 6)
  135.     DllStructSetData($tWndClassEx, "lpszMenuName", 0)
  136.     DllStructSetData($tWndClassEx, "lpszClassName", DllStructGetPtr($tClassName))
  137.     DllStructSetData($tWndClassEx, "hIconSm", 0)
  138.     Local $aRet = DllCall("user32.dll", "dword", "RegisterClassEx", "ptr", DllStructGetPtr($tWndClassEx))
  139.     $aRet = DllCall("user32.dll", "hwnd", "CreateWindowEx", "dword", BitOR($WS_EX_LAYERED, $WS_EX_TRANSPARENT, $WS_EX_TOPMOST, $WS_EX_TOOLWINDOW), _
  140.             "ptr", DllStructGetPtr($tClassName), "ptr", 0, "dword", $WS_VISIBLE, "int", 0, "int", 0, "int", 100, _
  141.             "int", 100, "hwnd", 0, "hwnd", 0, "hwnd", _WinAPI_GetModuleHandle(""), "ptr", 0)
  142.     _WinAPI_ShowWindow($aRet[0], @SW_HIDE)
  143.     Local $rc = _WinAPI_GetWindowRect($hClock)
  144.     _WinAPI_MoveWindow($aRet[0], _
  145.             (_WinAPI_GetSystemMetrics($SM_CXSCREEN) - (DllStructGetData($rc, "right") - DllStructGetData($rc, "left"))) / 2, _
  146.             (_WinAPI_GetSystemMetrics($SM_CYSCREEN) - (DllStructGetData($rc, "bottom") - DllStructGetData($rc, "top"))) / 2, _
  147.             DllStructGetData($rc, "right") - DllStructGetData($rc, "left"), _
  148.             DllStructGetData($rc, "bottom") - DllStructGetData($rc, "top"), False)
  149.     DllCall("user32.dll", "ptr", "SetTimer", "hwnd", $aRet[0], "ptr", 0xff00, "uint", 1000, "ptr", 0)
  150.     _WinAPI_ShowWindow($aRet[0], @SW_SHOWNORMAL)
  151.     _WinAPI_UpdateWindow($aRet[0])
  152.     Return $aRet[0]
  153. EndFunc   ;==>_CreateClockWindow
  154.  
  155. Func _MyWndProc($hWnd, $iMessage, $wParam, $lParam)
  156.     Switch $iMessage
  157.         Case $WM_PAINT, $WM_TIMER
  158.             _DrawClock()
  159.         Case 0x0201 ; $WM_LBUTTONDOWN
  160.             _SendMessage($hWnd, 0x00a1, 2, 2) ; $WM_NCLBUTTONDOWN
  161.     EndSwitch
  162.     Return _WinAPI_DefWindowProc($hWnd, $iMessage, $wParam, $lParam)
  163. EndFunc   ;==>_MyWndProc
  164.  
  165. Func _WinAPI_LoadCursor($hInstance, $lpCursorName)
  166.     Local $aRet = DllCall("user32.dll", "hwnd", "LoadCursor", "hwnd", $hInstance, "ptr", $lpCursorName)
  167.     Return $aRet[0]
  168. EndFunc   ;==>_WinAPI_LoadCursor
  169.  
  170. Func _LoadTheme()
  171.     $m_nWidth = 300
  172.     $m_nHeight = 300
  173.  
  174.     _WinAPI_SetWindowPos($hClock, 0, 0, 0, $m_nWidth, $m_nHeight, $SWP_NOMOVE + $SWP_NOZORDER)
  175.     _DrawClock()
  176. EndFunc   ;==>_LoadTheme
  177.  
  178. Func _CreateTrayMenu()
  179.     Global $mExit = TrayCreateItem("Exit")
  180.     TrayItemSetOnEvent($mExit, "_ShutDown")
  181. EndFunc   ;==>_CreateTrayMenu
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement