Jump to content
Sign in to follow this  
E1M1

Nees math help - How to dynamically draw time labels on Clock?

Recommended Posts

E1M1

I am trying tom make clock control. How do I dynamically put all 12 hour label to right place on clock? I don't want to use _GDIPlus_GraphicsDrawString ($hGraphic, "Hello world", 140, 110) with fixed coordinates, but I can't think how to make function that would place these dynamically. 3'o clock should be placed at 0 degrees, 12 o'clock should be at 90 degrees 9 => 180 deg and 6 => 270 deg.

How can I write function that would take $x,$y,$r1,$r2,$deg as args and then calculate x,y so that when I put label on x,y it would be on right place at clock?

#include <GuiConstantsEx.au3>
#include <GDIPlus.au3>

Opt('MustDeclareVars', 1)

_Main()

Func _Main()
    Local $hGUI, $hGraphic

    ; Create GUI
    $hGUI = GUICreate("GDI+", 400, 300)
    GUISetState()

    ; Draw an ellipse
    _GDIPlus_Startup ()
    GUiCtrlClockCreate($hGUI,30,30,200,200)


    ; Loop until user exits
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

    ; Clean up resources
    _GDIPlus_GraphicsDispose ($hGraphic)
    _GDIPlus_Shutdown ()

EndFunc   ;==>_Main

Func GUICtrlClockCreate($hwnd,$x,$y,$r1,$r2,$hGraphic = False)
    If $hGraphic = false Then  $hGraphic = _GDIPlus_GraphicsCreateFromHWND ($hwnd)
    _GDIPlus_GraphicsDrawEllipse ($hGraphic, $x,$y,$r1,$r2)
    _GDIPlus_GraphicsDrawEllipse ($hGraphic, $x + ($r1/2 -1),$y + ($r2/2 -1),3,3)
    ;_GDIPlus_GraphicsDrawString ($hGraphic, "Hello world", 140, 110)
EndFunc

edited

Share this post


Link to post
Share on other sites
UEZ

You mean something like this:

#include <GuiConstantsEx.au3>
#include <GDIPlus.au3>

Opt('MustDeclareVars', 1)

_Main()

Func _Main()
    Local $hGUI, $hGraphic

    ; Create GUI
    $hGUI = GUICreate("GDI+", 400, 300)
    GUISetState()

    ; Draw an ellipse
    _GDIPlus_Startup ()
    GUiCtrlClockCreate($hGUI,30,30,200,200)


    ; Loop until user exits
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

    ; Clean up resources
    _GDIPlus_GraphicsDispose ($hGraphic)
    _GDIPlus_Shutdown ()

EndFunc   ;==>_Main

Func GUICtrlClockCreate($hwnd,$x,$y,$r1,$r2,$hGraphic = False)
    Local $i, $cR3 = 90, $fs = 8, $deg = ACos(-1) / 180
    If $hGraphic = false Then  $hGraphic = _GDIPlus_GraphicsCreateFromHWND ($hwnd)
    _GDIPlus_GraphicsDrawEllipse ($hGraphic, $x,$y,$r1,$r2)
    _GDIPlus_GraphicsDrawEllipse ($hGraphic, $x + ($r1/2 -1),$y + ($r2/2 -1),3,3)
    For $i = 1 To 12
        _GDIPlus_GraphicsDrawString($hGraphic, $i, -$fs / 2 + $x + ($r1/2 -1) + Cos(-45 + ($i - 1) * 29.7 * $deg) * $cR3, -$fs * 0.75 + $y + ($r2/2 -1) + Sin(-45 + ($i - 1) * 29.7 * $deg) * $cR3, "Arial", $fs)
    Next
    ;_GDIPlus_GraphicsDrawString ($hGraphic, "Hello world", 140, 110)
EndFunc

I wrote last year a Maybe it is useful for you! :)

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
E1M1

Thanks a lot. I am looking for something like your clock but how to make clock pointers user dragable?

Lets say that user click on X,Y on client area. How to detect if that x,y is somewhere on clock pointer?

#include <GuiConstantsEx.au3>
#include <GDIPlus.au3>

Opt('MustDeclareVars', 1)

_Main()

Func _Main()
    Local $hGUI, $hGraphic

    ; Create GUI
    $hGUI = GUICreate("GDI+", 400, 300)
    GUISetState()

    ; Draw an ellipse
    _GDIPlus_Startup ()
    GUiCtrlClockCreate($hGUI,30,30,200,200)


    ; Loop until user exits
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

    ; Clean up resources
    _GDIPlus_GraphicsDispose ($hGraphic)
    _GDIPlus_Shutdown ()

EndFunc   ;==>_Main

Func GUICtrlClockCreate($hwnd,$x,$y,$r1,$r2,$hGraphic = False)
    Local $i, $cR3 = 90, $fs = 8, $deg = ACos(-1) / 180
    If $hGraphic = false Then  $hGraphic = _GDIPlus_GraphicsCreateFromHWND ($hwnd)
    _GDIPlus_GraphicsDrawEllipse ($hGraphic, $x,$y,$r1,$r2)
    _GDIPlus_GraphicsDrawEllipse ($hGraphic, $x + ($r1/2 -1),$y + ($r2/2 -1),3,3)
    For $i = 1 To 12
        _GDIPlus_GraphicsDrawString($hGraphic, $i, -$fs / 2 + $x + ($r1/2 -1) + Cos(-45 + ($i - 1) * 29.7 * $deg) * $cR3, -$fs * 0.75 + $y + ($r2/2 -1) + Sin(-45 + ($i - 1) * 29.7 * $deg) * $cR3, "Arial", $fs)
    Next
    $i = 1
    _GDIPlus_GraphicsDrawLine($hGraphic, $x + ($r1/2 -1), $y + ($r2/2 -1), -$fs / 2 + $x + ($r1/2 -1) + Cos(-45 + ($i - 1) * 29.7 * $deg) * $cR3, -$fs * 0.75 + $y + ($r2/2 -1) + Sin(-45 + ($i - 1) * 29.7 * $deg) * $cR3)
        $i = 3
    _GDIPlus_GraphicsDrawLine($hGraphic, $x + ($r1/2 -1), $y + ($r2/2 -1), -$fs / 2 + $x + ($r1/2 -1) + Cos(-45 + ($i - 1) * 29.7 * $deg) * $cR3, -$fs * 0.75 + $y + ($r2/2 -1) + Sin(-45 + ($i - 1) * 29.7 * $deg) * $cR3)
    ;_GDIPlus_GraphicsDrawString ($hGraphic, "Hello world", 140, 110)
EndFunc
Edited by E1M1

edited

Share this post


Link to post
Share on other sites
Malkey

.... how to make clock pointers user dragable?

....

Dragable clock hands reminded me of the example

Don't forget to right click either clock's center black dot.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.