Sign in to follow this  
Followers 0
Vegar

Clock in "GuiCtrlCreateMenu"

13 posts in this topic

#1 ·  Posted (edited)

hello ...

i'm trying to make a clock in a "GuiCtrlCreateMenu", but the clock just freezes, is there any way to prewent this`?

$n=GUICtrlCreateDate ( "", 20, 20, 100, 20, $DTS_TIMEFORMAT) 
GuiCtrlCreateMenu ("             " & GUICtrlRead($n))
Edited by Vegar

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

the GUICtrlCreateDate() control requires updating, otherwise it remains at the time set on control creation.

why do you want the time in a menu bar header?

anyway this rough modified example from the help files for timers and menu should give you something to work with.

post an example script demonstrating your method.

Edit missed the timer callback function when copy/pasting, my apologies

#include <GUIConstantsEx.au3>
#include <DateTimeConstants.au3>
#include <Timers.au3>

Opt('MustDeclareVars', 1)
Global $hGUI, $cMenu

Example1()

Func Example1()
    Local $msg, $filemenu, $openitem, $helpmenu

    $hGUI = GUICreate("Select 'File' menu item 'Open'", 400, 200)
    $filemenu = GUICtrlCreateMenu("&File")
    $openitem = GUICtrlCreateMenuItem("Open", $filemenu)
    GUICtrlCreateMenu("?")
    GUICtrlCreateMenuItem("Save", $filemenu)
    GUICtrlCreateMenuItem("Info", $helpmenu)
    GUICtrlCreateMenuItem("Exit", $filemenu)
    GUICtrlCreateMenu("Recent Files", $filemenu, 1)
    $cMenu = GuiCtrlCreateMenu("             " & StringFormat("%02d:%02d:%02d", @HOUR, @MIN, @SEC))

    _Timer_SetTimer($hGUI, 1000, "_UpdateMenuClock"); create timer
    GUISetState()

; Run the GUI until the dialog is closed
    While 1
        $msg = GUIGetMsg()
        Switch $msg
            Case $openitem
                MsgBox(262144,"Messageloop block test","See how the clock is still working?", 0, $hGUI)
            Case $GUI_EVENT_CLOSE
                GUIDelete()
                Exit
        EndSwitch
    WEnd
    
EndFunc

; call back function
Func _UpdateMenuClock($hWnd, $Msg, $iIDTimer, $dwTime)
    #forceref $hWnd, $Msg, $iIDTimer, $dwTime
    GUICtrlSetData($cMenu, "             " & StringFormat("%02d:%02d:%02d", @HOUR, @MIN, @SEC))
EndFunc ;==>_UpdateMenuClock
Edited by rover

I see fascists...

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I get som kind of error:

Badly formatted "Func" statement.:
$hCallBack = DllCallbackRegister($sTimerFunc, "none", "hwnd;int;int;dword")
Edited by Vegar

Share this post


Link to post
Share on other sites

I get som kind of error:

Badly formatted "Func" statement.:

$hCallBack = DllCallbackRegister($sTimerFunc, "none", "hwnd;int;int;dword")

Here is a rover's working example I experimented with.

#include <GUIConstantsEx.au3>
#include <DateTimeConstants.au3>
#include <Timers.au3>
#Include <Date.au3>

Opt('MustDeclareVars', 1)


Global $hGUI, $cMenu, $hLabel
 
Example1()

Func Example1()
    Local $msg, $filemenu, $openitem, $helpmenu, $cMenuSaveItem 

    $hGUI = GUICreate("Select 'File' menu item 'Open'", 400, 200)
    $filemenu = GUICtrlCreateMenu("&File")
    $openitem = GUICtrlCreateMenuItem("Open", $filemenu)
   ;GUICtrlCreateMenu("?")
    GUICtrlCreateMenuItem("Save", $filemenu)
    GUICtrlCreateMenuItem("Info", $helpmenu)
    GUICtrlCreateMenuItem("Exit", $filemenu)
    GUICtrlCreateMenu("Recent Files", $filemenu, 1)
    $cMenu = GuiCtrlCreateMenu("Time               ")
    $cMenuSaveItem = GUICtrlCreateMenuItem("Save", $cMenu)
    
    $hLabel = GUICtrlCreateLabel("Time             ",10,160,180,20)
    
    _Timer_SetTimer($hGUI, 1000, "_UpdateMenuClock"); create timer
    GUISetState()

; Run the GUI until the dialog is closed
    While 1
        $msg = GUIGetMsg()
        Switch $msg
            Case $openitem
                MsgBox(262144,"Messageloop block test","See how the clock is still working?", 0, $hGUI)
             Case $cMenuSaveItem
                MsgBox(262144,"Messageloop Save Item test","See how the clock is still working?", 0, $hGUI) 
            Case $GUI_EVENT_CLOSE
                GUIDelete()
                Exit
        EndSwitch
    WEnd    
EndFunc

func _UpdateMenuClock($hWnd, $Msg, $iIDTimer, $dwTime)
    GUICtrlSetData ( $cMenu, _DateDayOfWeek( @WDAY, 0 ) & ", " & _now())
    GUICtrlSetData ( $hLabel, _DateDayOfWeek( @WDAY, 0 ) & ", " & _now())
    return
EndFunc

Share this post


Link to post
Share on other sites

Excellent, Malkey ! I'm sure Vegar will be happy !

I'm happy too ! :) But now, can I right align the clock on the menu bar -- for aesthetic reasons -- ?

Cheers, Charvi

Share this post


Link to post
Share on other sites

Thanks Malkey

It would have helped if I hadn't missed the timer callback function when copying to the clipboard :)

corrected my post above.


I see fascists...

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Thanks Malkey

It would have helped if I hadn't missed the timer callback function when copying to the clipboard :)

corrected my post above.

@rover

To forget is human :o It happens often to me too...

Anyway, I made a little addition to your script to right align the clock on the menubar, with spaces (it's the easiest way I think). On my system, with the dialog you defined, the maximum allowed is 95 before the menubar has a double height.

Is there a way to get the value 95 programmatically? Because every menu and dialog is different...

Cheers, Charvi

EDIT: forgot to add the script - you see, it happens... (what a coincidence that it happens just now!)

#include <GUIConstantsEx.au3>
#include <DateTimeConstants.au3>
#include <Timers.au3>
#include <String.au3>

Opt('MustDeclareVars', 1)
Global $hGUI, $cMenu

Example1()

Func Example1()
    Local $msg, $filemenu, $openitem, $helpmenu

    $hGUI = GUICreate("Select 'File' menu item 'Open'", 400, 200)
    $filemenu = GUICtrlCreateMenu("&File")
    $openitem = GUICtrlCreateMenuItem("Open", $filemenu)
    GUICtrlCreateMenu("?")
    GUICtrlCreateMenuItem("Save", $filemenu)
    GUICtrlCreateMenuItem("Info", $helpmenu)
    GUICtrlCreateMenuItem("Exit", $filemenu)
    GUICtrlCreateMenu("Recent Files", $filemenu, 1)
    $cMenu = GuiCtrlCreateMenu(_StringRepeat(" ", 95) & StringFormat("%02d:%02d:%02d", @HOUR, @MIN, @SEC))

    _Timer_SetTimer($hGUI, 1000, "_UpdateMenuClock"); create timer
    GUISetState()

; Run the GUI until the dialog is closed
    While 1
        $msg = GUIGetMsg()
        Switch $msg
            Case $openitem
                MsgBox(262144,"Messageloop block test","See how the clock is still working?", 0, $hGUI)
            Case $GUI_EVENT_CLOSE
                GUIDelete()
                Exit
        EndSwitch
    WEnd
    
EndFunc

; call back function
Func _UpdateMenuClock($hWnd, $Msg, $iIDTimer, $dwTime)
    #forceref $hWnd, $Msg, $iIDTimer, $dwTime
    GUICtrlSetData($cMenu, _StringRepeat(" ", 95) & StringFormat("%02d:%02d:%02d", @HOUR, @MIN, @SEC))
EndFunc;==>_UpdateMenuClock
Edited by charvi

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

did you compensate for the extra digit for the hours of 10 11 and 12? or will they extend the menu bar, or cut it off.

Edit: I really like how the clock keeps ticking... I'm going to have to use that.

Edited by youknowwho4eva

Giggity

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

@charvi

I addressed the OP's issue at hand with the menubar clock and didn't bother to change the right-justifying spaces in the code posted.

this next example illustrates the proper way to do it.

@youknowwho4eva

using Stringformat() sets padding for digits

@Vegar, All

menubar with right justified clock menu item

look at Malkeys example, the help file, Date.au3 and forum examples for time/date formatting of your choice

subclassing or message processing would be needed to eliminate the mouseover highlite of the clock.

probably an example somewhere on the forum of this already...

see also time in StatusBar example in helpfile for _Timer_SetTimer()

Edit added missing _Timer_KillAllTimers($hGUI)

#include <GUIConstantsEx.au3>
#include <DateTimeConstants.au3>
#include <Timers.au3>
#include <String.au3>
#include <GuiMenu.au3>

Opt('MustDeclareVars', 1)
Global $hGUI, $hMenu, $cMenu

MenuBarClock()

Func MenuBarClock()
    Local $msg, $filemenu, $openitem, $helpmenu, $iMenuCnt, $cMenuTimeItem

    $hGUI = GUICreate("Select 'File' menu item 'Open' or select clock item", 400, 200)
    
    $filemenu = GUICtrlCreateMenu("&File")
    $openitem = GUICtrlCreateMenuItem("Open", $filemenu)
    GUICtrlCreateMenu("Recent Files", $filemenu, 1)
    GUICtrlCreateMenuItem("Save", $filemenu)
    GUICtrlCreateMenuItem("Exit", $filemenu)
    
    $helpmenu = GUICtrlCreateMenu("?")
    GUICtrlCreateMenuItem("Info", $helpmenu)

    $cMenu = GUICtrlCreateMenu(StringFormat("%02d:%02d:%02d", @HOUR, @MIN, @SEC))
    $cMenuTimeItem = GUICtrlCreateMenuItem("Set Time", $cMenu)
    
    $hMenu = _GUICtrlMenu_GetMenu($hGUI)
    $iMenuCnt = _GUICtrlMenu_GetItemCount($hMenu)
    _GUICtrlMenu_SetItemType($hMenu, ($iMenuCnt - 1), $MFT_RIGHTJUSTIFY, True)
    
    _Timer_SetTimer($hGUI, 1000, "_UpdateMenuClock"); create timer
    GUISetState()

    Sleep(2000)
    Beep(1000, 5)
    GUICtrlCreateMenu("Add menu item test", -1, 2); if menu position not used, menu item would be added after clock

    While 1
        $msg = GUIGetMsg()
        Switch $msg
            Case $openitem, $cMenuTimeItem
                MsgBox(262144, "Messageloop block test", "See how the clock is still working?", 0, $hGUI)
            Case $GUI_EVENT_CLOSE
                GUIDelete()
                Exit
        EndSwitch
    WEnd

EndFunc ;==>MenuBarClock

; call back function
Func _UpdateMenuClock($hWnd, $msg, $iIDTimer, $dwTime)
    #forceref $hWnd, $Msg, $iIDTimer, $dwTime
    GUICtrlSetData($cMenu, StringFormat("%02d:%02d:%02d", @HOUR, @MIN, @SEC))
EndFunc ;==>_UpdateMenuClock
Edited by rover

I see fascists...

Share this post


Link to post
Share on other sites

Really good, it looks perfect. Many thanks, Rover.

Share this post


Link to post
Share on other sites

Really good, it looks perfect. Many thanks, Rover.

thanks

I should add you can use AdlibEnable()/AdlibDisable()

instead of the timers UDF.

I prefer the timers UDF.

over to Vegar now :)


I see fascists...

Share this post


Link to post
Share on other sites

Very Nice :D

Exactly what i needed :)

Perfect :o

Share this post


Link to post
Share on other sites

thanks Vegar

glad it works for you

Cheers


I see fascists...

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  
Followers 0