Leaderboard
Popular Content
Showing content with the highest reputation on 06/01/2024 in all areas
-
Most of the 7-Segment display examples I've encountered are rigid and inflexible: it's difficult to easily change the color of the clock or resize it with a single click. While they may be effective, they lack flexibility. Say no to unmaintainable code. This script creates a dynamic graphical clock that displays the current time in segmented digits. It utilizes the GDI+ and AutoItObject libraries to draw an intuitive and responsive graphical interface. This detailed presentation highlights the technical and professional aspects of the code. Objectives and Features Dynamic Time Display: The main goal is to visually and legibly represent the current time. The time digits are displayed as segments, with ":" separators indicating hours, minutes, and seconds. Interface Adaptability: The graphical interface dynamically adjusts to the window size, ensuring optimal presentation on any screen. When the window is resized, the grid and graphical elements are recalculated and redrawn to maintain a consistent appearance. Use of External Libraries: The script leverages the advanced features of GDI+ for graphical rendering and AutoItObject for object-oriented management. These external libraries offer extensive functionalities and a high-level abstraction to simplify development. #include-once #include <Array.au3> #include <AutoItObject.au3> #include <GDIPlus.au3> #include <GUIConstantsEx.au3> #include <WinAPI.au3> #include <WinAPIGdi.au3> #include <WindowsConstants.au3> Global $g___bEXIT = False HotKeySet("{ESC}", "_Exit") _GDIPlus_Startup() _AutoItObject_Startup() Const $COLOR_RED = 0xFFFF0000 Const $COLOR_GREEN = 0xFF00FF00 ; Line Class Func Line($x1, $y1, $x2, $y2, $color = $COLOR_RED, $thickness = 2) Local $cLine = _AutoItObject_Class() With $cLine .AddProperty("x1", $ELSCOPE_PUBLIC, $x1) .AddProperty("y1", $ELSCOPE_PUBLIC, $y1) .AddProperty("x2", $ELSCOPE_PUBLIC, $x2) .AddProperty("y2", $ELSCOPE_PUBLIC, $y2) .AddProperty("color", $ELSCOPE_PUBLIC, $color) .AddProperty("thickness", $ELSCOPE_PUBLIC, $thickness) EndWith Return $cLine.Object EndFunc ;==>Line ; GridPane Class Func GridPane($numCells, $title, $width, $height) Local $cGridPane = _AutoItObject_Class() Local $hGUI = GUICreate($title, $width, $height, -1, -1, $WS_SIZEBOX + $WS_SYSMENU) GUISetBkColor(0x000000, $hGUI) GUISetState(@SW_SHOW, $hGUI) Local $hDC = _WinAPI_GetDC($hGUI) Local $hGraphics = _GDIPlus_GraphicsCreateFromHDC($hDC) Local $hBitmap = _WinAPI_CreateCompatibleBitmap($hDC, $width, $height) Local $hMemDC = _WinAPI_CreateCompatibleDC($hDC) Local $hOldBmp = _WinAPI_SelectObject($hMemDC, $hBitmap) Local $hMemGraphics = _GDIPlus_GraphicsCreateFromHDC($hMemDC) Local $aArray[0] $width = _WinAPI_GetClientWidth($hGUI) $height = _WinAPI_GetClientHeight($hGUI) With $cGridPane .AddProperty("hGUI", $ELSCOPE_PUBLIC, $hGUI) .AddProperty("hDC", $ELSCOPE_PUBLIC, $hDC) .AddProperty("hGraphics", $ELSCOPE_PUBLIC, $hGraphics) .AddProperty("hBitmap", $ELSCOPE_PUBLIC, $hBitmap) .AddProperty("hMemDC", $ELSCOPE_PUBLIC, $hMemDC) .AddProperty("hOldBmp", $ELSCOPE_PUBLIC, $hOldBmp) .AddProperty("hMemGraphics", $ELSCOPE_PUBLIC, $hMemGraphics) .AddProperty("numCells", $ELSCOPE_PUBLIC, $numCells) .AddProperty("cellWidth", $ELSCOPE_PUBLIC, $width / $numCells) .AddProperty("cellHeight", $ELSCOPE_PUBLIC, $height) .AddProperty("lines", $ELSCOPE_PUBLIC, $aArray) .AddMethod("AddLine", "_AddLine") .AddMethod("Draw", "_Draw") .AddMethod("DisplayNumber", "_DisplayNumber") .AddMethod("DisplayFullTime", "_DisplayFullTime") .AddMethod("DisplaySeparator", "_DisplaySeparator") .AddMethod("Resize", "_Resize") .AddDestructor("_GridPaneDestroy") EndWith Return $cGridPane.Object EndFunc ;==>GridPane Func _AddLine($this, $line) Local $aArray = $this.lines _ArrayAdd($aArray, $line) $this.lines = $aArray EndFunc ;==>_AddLine Func _Draw($this) _GDIPlus_GraphicsClear($this.hMemGraphics, 0xFF000000) Local $aArray = $this.lines For $line In $aArray Local $hPen = _GDIPlus_PenCreate($line.color, $line.thickness) _GDIPlus_GraphicsDrawLine($this.hMemGraphics, $line.x1, $line.y1, $line.x2, $line.y2, $hPen) _GDIPlus_PenDispose($hPen) Next _WinAPI_BitBlt($this.hDC, 0, 0, _WinAPI_GetClientWidth($this.hGUI), _WinAPI_GetClientHeight($this.hGUI), $this.hMemDC, 0, 0, $SRCCOPY) EndFunc ;==>_Draw Func _GridPaneDestroy($this) _GDIPlus_GraphicsDispose($this.hMemGraphics) _WinAPI_SelectObject($this.hMemDC, $this.hOldBmp) _WinAPI_DeleteObject($this.hBitmap) _WinAPI_DeleteDC($this.hMemDC) _GDIPlus_GraphicsDispose($this.hGraphics) _WinAPI_ReleaseDC($this.hGUI, $this.hDC) EndFunc ;==>_GridPaneDestroy Func _DisplayNumber($this, $number, $cellIndex) ; Define segments for each digit (0 to 9) Local $segments[10][7] = [ _ [1, 1, 1, 1, 1, 1, 0], _ ; 0 [0, 1, 1, 0, 0, 0, 0], _ ; 1 [1, 1, 0, 1, 1, 0, 1], _ ; 2 [1, 1, 1, 1, 0, 0, 1], _ ; 3 [0, 1, 1, 0, 0, 1, 1], _ ; 4 [1, 0, 1, 1, 0, 1, 1], _ ; 5 [1, 0, 1, 1, 1, 1, 1], _ ; 6 [1, 1, 1, 0, 0, 0, 0], _ ; 7 [1, 1, 1, 1, 1, 1, 1], _ ; 8 [1, 1, 1, 1, 0, 1, 1] _ ; 9 ] ; Coordinates for the segments (x1, y1, x2, y2) relative to cell size Local $coords[7][4] = [ _ [0.1, 0.1, 0.9, 0.1], _ ; A (top) [0.9, 0.1, 0.9, 0.5], _ ; B (top right) [0.9, 0.5, 0.9, 0.9], _ ; C (bottom right) [0.1, 0.9, 0.9, 0.9], _ ; D (bottom) [0.1, 0.5, 0.1, 0.9], _ ; E (bottom left) [0.1, 0.1, 0.1, 0.5], _ ; F (top left) [0.1, 0.5, 0.9, 0.5] _ ; G (middle) ] ; Calculate offsets Local $xOffset = $cellIndex * $this.cellWidth Local $yOffset = 0 ; Add lines for the given digit with offsets For $i = 0 To 6 If $segments[$number][$i] Then $this.AddLine(Line($coords[$i][0] * $this.cellWidth + $xOffset, $coords[$i][1] * $this.cellHeight + $yOffset, $coords[$i][2] * $this.cellWidth + $xOffset, $coords[$i][3] * $this.cellHeight + $yOffset, $COLOR_GREEN, 2)) EndIf Next EndFunc ;==>_DisplayNumber Func _DisplaySeparator($this, $cellIndex) Local $xOffset = $cellIndex * $this.cellWidth Local $yOffset = 0 ; Draw two small dots to form the ":" $this.AddLine(Line($xOffset + 0.5 * $this.cellWidth, $yOffset + 0.3 * $this.cellHeight, $xOffset + 0.5 * $this.cellWidth, $yOffset + 0.4 * $this.cellHeight, $COLOR_RED, 4)) $this.AddLine(Line($xOffset + 0.5 * $this.cellWidth, $yOffset + 0.6 * $this.cellHeight, $xOffset + 0.5 * $this.cellWidth, $yOffset + 0.7 * $this.cellHeight, $COLOR_RED, 4)) EndFunc ;==>_DisplaySeparator Func _DisplayFullTime($this) ; Clear current lines Local $aArray = $this.lines ReDim $aArray[0] $this.lines = $aArray ; Get the current time Local $hours = @HOUR Local $minutes = @MIN Local $seconds = @SEC ; Display each part of the time in separate cells $this.DisplayNumber(Int(StringMid($hours, 1, 1)), 0) ; First digit of hours $this.DisplayNumber(Int(StringMid($hours, 2, 1)), 1) ; Second digit of hours ; Add first separator $this.DisplaySeparator(2) ; First separator $this.DisplayNumber(Int(StringMid($minutes, 1, 1)), 3) ; First digit of minutes $this.DisplayNumber(Int(StringMid($minutes, 2, 1)), 4) ; Second digit of minutes ; Add second separator $this.DisplaySeparator(5) ; Second separator $this.DisplayNumber(Int(StringMid($seconds, 1, 1)), 6) ; First digit of seconds $this.DisplayNumber(Int(StringMid($seconds, 2, 1)), 7) ; Second digit of seconds $this.Draw() EndFunc ;==>_DisplayFullTime Func _Resize($this) Local $width = _WinAPI_GetClientWidth($this.hGUI) Local $height = _WinAPI_GetClientHeight($this.hGUI) $this.cellWidth = $width / $this.numCells $this.cellHeight = $height ; Update memory bitmap size _GDIPlus_GraphicsDispose($this.hMemGraphics) _WinAPI_SelectObject($this.hMemDC, $this.hOldBmp) _WinAPI_DeleteObject($this.hBitmap) $this.hBitmap = _WinAPI_CreateCompatibleBitmap($this.hDC, $width, $height) $this.hOldBmp = _WinAPI_SelectObject($this.hMemDC, $this.hBitmap) $this.hMemGraphics = _GDIPlus_GraphicsCreateFromHDC($this.hMemDC) $this.DisplayFullTime() EndFunc ;==>_Resize Func _Exit() $g___bEXIT = True EndFunc ;==>_Exit ; Example usage Global $gridPane = GridPane(8, "Horloge", 480, 130) AdlibRegister("displayTime", 1000) While 1 If $g___bEXIT Then ExitLoop Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop Case $GUI_EVENT_RESIZED $gridPane.Resize() EndSwitch WEnd $gridPane = 0 _AutoItObject_Shutdown() _GDIPlus_Shutdown() Func displayTime() $gridPane.DisplayFullTime() EndFunc ;==>displayTime1 point
-
Graphical Clock: 7 Segments
argumentum reacted to Numeric1 for a topic
What would be interesting is to integrate more object-oriented programming into AutoIt. I don't want all of AutoIt to be object-oriented, but we could add some. This mix would enhance AutoIt's conceptual capabilities. It's true that a train cannot run off the tracks, but it can go faster and further.1 point -
WinPenPack's X-Launcher Update to x64 - Script error help please
ioa747 reacted to argumentum for a topic
You got the stuff from elsewhere but you are asking here for support. You should install the SciTE we use here. That, we can give support to. Let's start with that. What is your project ? Where is the code ?1 point -
Thank you @UEZ for the pertinent suggestion. I have edited and revised the code by applying double buffering, and everything works correctly.1 point