AutoHotkey Community

It is currently May 27th, 2012, 10:11 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 21 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: August 8th, 2006, 7:17 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
In Windows XP (maybe in other Windows versions, too) there is a small analog clock in the Date and Time settings window. We can cut it off and position anywhere on screen, and make it semi transparent. We got an analog clock always on top, no external file is needed, but we are stuck with fixed size, and shape and color like the moon.

With left dragging you can move the clock to a place you like, it will stay there. With editing the assignments to wX and wY you can choose the initial position of the clock. If you make the (thick enough) Taskbar its parent, you can keep it in there, maybe on top of the digital clock in the Tray. With the right transparency you can see both.

A double click on the clock toggles it between full window or just the clock face. In full window mode you can set the time, the time zone or let the PC synchronize to an Internet time server. If you close this window (also by clicking on OK or Cancel), the script exits. Click on Apply after changing time, to keep the clock open. Exitting the script also closes the clock.
Code:
#NoEnv
SetBatchLines -1

CoordMode Mouse, Screen
OnExit Exit

Run rundll32.exe shell32.dll`,Control_RunDLL timedate.cpl`,`,0,,,PID
GroupAdd Group1, ahk_pid %PID%         ; Work around forbidden variables in #IfWinActive
WinWait ahk_pid %PID%
WinSet AlwaysOnTop, ON, ahk_pid %PID%
WinGetPos wX0, wY0,,, ahk_pid %PID%    ; Default position of Date/Time settings
ControlGetPos cX,cY,cW,cH, ClockWndMain1, ahk_pid %PID%
oX := cX + cW//2                       ; Origin = center of clock
oY := cY + cH//2 - 1                   ; -1/-2 <-- Windows Theme
dX := 0.75 * 150                       ; Zoom factor * Diameter in x direction
dY := dx*A_ScreenHeight/A_ScreenWidth*4/3 ; Handle clock distortion, y-diameter
XY := Round(oX-dX/2) "-" Round(oY-dY/2)   ; Position of clock within control
wX := A_ScreenWidth//2 - oX
wY := -Round(oY-dY/2)                  ; Initial clock position
Full := Pos(0)                         ; Show cut-off clock
SetTimer IsClosed, 999                 ; Exit script if clock is closed

#IfWinActive ahk_group Group1          ; Group1 = Date/Time settings, in any language
~LButton::                             ; Double click toggles clock - full Date/Time window
   If (A_PriorHotKey = "~LButton Up" and A_TimeSincePriorHotkey < 500)
      Full := Pos(!Full)
   Else If !Full {                     ; Start left-drag
      MouseGetPos mX0, mY0             ; Mouse position at start of drag
      SetTimer Drag, 100
   }
Return                                 ; OK-Cancel: close clock! Click on Apply for changes

~LButton Up::SetTimer Drag, OFF

Drag:                                  ; Left-drag
   MouseGetPos mX, mY
   wX += mX-mX0                        ; change window position
   wY += mY-mY0
   WinMove ahk_pid %PID%,, %wX%, %wY%
   mX0 = %mX%                          ; remember new mouse position
   mY0 = %mY%
Return

Pos(Full) {                            ; Full Date/Time window, or cut-off Clock
   Global PID, wX, wY, wX0, wY0, XY, dX, dY
   If Full {                           ; Full window at default position
      WinSet Region,,ahk_pid %PID%
      WinSet Transparent, Off, ahk_pid %PID%
      WinMove ahk_pid %PID%,, %wX0%, %wY0%
   } Else {                            ; Clock at last position
      WinSet Region, %XY% w%dX% h%dY% E, ahk_pid %PID%
      WinSet Transparent, 200, ahk_pid %PID%
      WinMove ahk_pid %PID%,, %wX%, %wY%
   }
   Return Full
}

IsClosed:                              ; Check if Clock is closed
   IfWinExist ahk_pid %PID%
      Return
Exit:                                  ; Close clock at Exit script
   WinClose ahk_pid %PID%
ExitApp

Edit 20060810: Changes:
- Instead of control.exe, rundll32 is used (with shell32.dll) to start the date/time settings. This way there is no need for the window title, so the clock in a non-English Windows should work, too. The process ID is returned by the Run command, which is enough to identify the clock.
- The position of the clock within its window is not directly determined by constants, but first the clock-control position is inquired and constant offsets are only used within this control. Hopefully the position computed this way does not depend on system settings, like fonts, title bar width, etc.
- The full date/time settings window appear in its Windows default position, not centered (which would require to get the monitor working area and do some math).
Edit 20060810.1: Removed title reference from #IfWinActive directive
Edit 20060810.2: Changes:
- Guess clock size from the size of its control
- Allow setting a zoom factor (0.75)
Edit 20060810.3: Using screen size for aspect ratio (thanks to holomind)

Here is a simple variant for Vista.
Image
Code:
W:=H:=124, gL:=3, gT:=22               ; Clock Width,Height and Gaps (Left,Top)

#SingleInstance Force
#NoEnv
#Persistent

SetWinDelay -1
CoordMode Mouse, Screen                ; for moving the clock window
OnExit Exit

Menu Tray, NoStandard                  ; Short tray menu:
Menu Tray, Add, Move, Move             ; Move the clock with the mouse
Menu Tray, Add, Exit, Exit
Menu Tray, Icon, %A_WinDir%\system32\SHELL32.dll,266

WinGet ID, ID, A                       ; ID of active window
Run rundll32.exe shell32`,Control_RunDLL timedate.cpl`,`,0,,,PID
WinWait ahk_pid %PID%
WinSet AlwaysOnTop, ON, ahk_pid %PID%
WinGetPos wX0, wY0,,, ahk_pid %PID%    ; Default position of Date/Time settings
ControlGetPos cX,cY,cW,cH, ClockWndMain1, ahk_pid %PID%

wX := gL - cX,    wY := gT - cY        ; Initial clock position: Top left of screen
oX := W//2 - wX,  oY := H//2 - wY      ; Origin = center of clock
Rg := -wX . "-" . -wY . " E w"W " h" H ; Shape of clock (Region)

SetTimer Status                        ; Activate | Exit script if clock is closed
WinActivate ahk_id %ID%                ; Activate original window

Status:                                ; Check if Clock is active | closed
   IfWinNotExist ahk_pid %PID%         ; Clock shown when "Date and Time Settings" is inactive
      ExitApp
   If WinActive("ahk_pid " PID) {
      If(Full = 0)                     ; Make Full window
         Full := Pos("","","+0x800000","","Off",wX0,wY0,PID)
   }
   Else
      If(Full = "")                    ; Truncate window, click-through (Initial case)
         Full := Pos(0,Rg,"-0x800000","0x20",200,wX,wY,PID)
Return

Move:
   mX0 := wX+oX,  mY0 := wY+oY         ; remember starting mouse position
   MouseMove mX0, mY0, 10              ; move cursor to center of clock
   SetTimer Drag, 10                   ; start dragging
   HotKey LButton, Stop, On            ; Click: stop
Return

Drag:
   MouseGetPos mX, mY                  ; current cursor position
   wX += mX-mX0,  wY += mY-mY0         ; new clock position
   WinMove ahk_pid %PID%,, %wX%, %wY%
   mX0 := mX,  mY0 := mY               ; remember cursor
Return

Stop:                                  ; Stop dragging
   SetTimer Drag, Off
   HotKey LButton,Off
Return

Exit:                                  ; Close clock at Exit script
   WinClose ahk_pid %PID%
ExitApp

Pos(P,R,S,E,T,X,Y,PID) {               ; Set the clock window full or truncated
   WinSet Region, %R%, ahk_pid %PID%
   WinSet Style,  %S%, ahk_pid %PID%
   WinSet ExStyle,%E%, ahk_pid %PID%
   WinSet Transparent, %T%, ahk_pid %PID%
   WinMove ahk_pid %PID%,, %X%, %Y%
   Return P
}

It is somewhat different: The clock is semi-transparent, always on top and click-through, that is, mouse clicks activate controls beneath the clock. This way you don’t have to move it that often.

The top line contains the metrics of the clock, which might change with a different theme or personalization of Windows. Only these numbers need to be tuned to non-standard settings.

If you start the date/time settings applet of Windows (e.g. from the tray) or click the taskbar icon, the moon clock will turn into the standard data/time settings, staying on the top. If you make another window active, the clock gets back to its small form and jumps back to its previous position.

The tray menu of the script contains “move”. If you select it, the mouse cursor moves to the center of the clock, and the clock would follow the cursor to a new location. A mouse click pins the clock to its new location.
If you want to download, the same script is also here. Just copy it to somewhere an run.


Last edited by Laszlo on February 6th, 2008, 4:50 pm, edited 14 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 10th, 2006, 12:35 am 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
Lots of cool tricks in that script. :)

For some reason, it doesn't extract the clock portion quite right on mine (it's down and to the right too far by about one radius, making only the lower right quadrant of the clock visible). Apparently, the position of the clock inside its window varies on some XP systems. However, I'm running a taskbar timestamp alteration program (TClockEx), which might be impacting it somehow.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 10th, 2006, 1:23 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Chris wrote:
Lots of cool tricks in that script. :)
Thanks!
Chris wrote:
For some reason, it doesn't extract the clock portion quite right on mine
I think the position is affected by the font magnification factor, the width of the title bar and the system fonts, so some experimenting is necessary. I was just too lazy to use image search for a system independent solution, but maybe somebody extends the script this way.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 10th, 2006, 2:37 am 
Offline

Joined: March 11th, 2006, 12:44 pm
Posts: 341
Location: Munich, Germany
hi, nice script, seems you have recycled my LiveWindows clock-demo ;)
but you changed a lot in the sourcecode. interesting way to start the clock panel with ...ctl . But instead of grabbing the contents with PrintWindow you do it directy?

I analyzed the code a bit:
Code:
 WinSet Region, 286-139 w120 h100 E, ahk_id %cID%

does the trick and resizes/clips the window so only the clock is visible.

Unfortunately on a German System it doesnt work at all.
I even tried to change "Date and Time" to "Datum und Uhrzeit".
but cant get it to work. (the Window opens, but then the ahk script exits.
if if remove the
Code:
SetTimer IsClosed, 999      ; Exit script is clock is closed

the script keeps running but nothing happens.

i also had a older version of AHK, so the first line #NoEnv caused an syntax error. (i have upgraded to latest AHK-Version and it runs now.)

...
with this technique "Winset Region" instead of the slower DllCall PrintWindow. i could change my LiveWindows. (the only difference would be that it cannot be zoomed, but with clipping you dont need to zoom so much)
This could be change to a tool like "Minimize Clip to just above the Tray" ;) aka Minimize2Tray ;)


Last edited by holomind on August 10th, 2006, 2:57 am, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 10th, 2006, 2:46 am 
Offline

Joined: July 20th, 2006, 6:41 pm
Posts: 144
Location: Los Angeles
I had the same problem... and I don't think I know AHK well enough to change


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 10th, 2006, 6:41 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
I updated the original script, with the following changes:
- Instead of control.exe, rundll32 is used (with shell32.dll) to start the date/time settings. This way there is no need for the window title, so the clock in a non-English Windows should work, too. The process ID is returned by the Run command, which is enough to identify the clock.
- The position of the clock within its window is not directly determined by constants, but first the clock-control position is inquired and constant offsets are only used within this control. Hopefully the position computed this way does not depend on system settings, like fonts, title bar width, etc.
- The full date/time settings window appear in its Windows default position, not centered (which would require to get the monitor working area and do some math).

I used the date/time setting clock for a long time (but settled to my own analog clock, which is configurable). Recently, Holomind showed a print-screen implementation. I realized it could be interesting for others, too, so I cleaned up my script a little and posted. It shows the real thing, not snapshots of it, therefore it runs without any noticeable processor load. On the other hand, Holomind's solution allows stretching the picture, achieving different sizes or aspect ratios.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 10th, 2006, 7:00 am 
Offline

Joined: March 11th, 2006, 12:44 pm
Posts: 341
Location: Munich, Germany
Laszlo wrote:
- The position of the clock within its window is not directly determined by constants, but first the clock-control position is inquired and constant offsets are only used within this control. Hopefully the position computed this way does not depend on system settings, like fonts, title bar width, etc.
- The full date/time settings window appear in its Windows default position, not centered (which would require to get the monitor working area and do some math).


Now it works partially for me.

i only can toggle when i comment following line
Code:
;#IfWinActive Date and Time Properties ahk_class #32770

as this still looks for the correct window. and tab name ...

also the position like Chris told is not correct, but one can change the values easily to "fix" it to ones theme.

i misunderstood the drag for selecting the size, but is only ment to move the small clock around. (as there is no window-title any more)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 10th, 2006, 3:08 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
holomind wrote:
I only can toggle when I comment following line...
Thanks! It is fixed in the original post.
holomind wrote:
the position like Chris told is not correct
Could you post your Windows version and the position, which works? Maybe we can determine the offset from the size and position of the controls in the settings window. I only have English XP SP2, so I need your help with the positions.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 10th, 2006, 3:29 pm 
Offline

Joined: March 11th, 2006, 12:44 pm
Posts: 341
Location: Munich, Germany
Laszlo wrote:
Maybe we can determine the offset from the size and position of the controls in the settings window. I only have English XP SP2, so I need your help with the positions.


I experimented a little bit and made the shrinking variable:
Code:
shrink := 20
XY := cX+ shrink "-" cY + shrink                ; Position of clock within control
wX := A_ScreenWidth//2-cX + shrink
wY := -cY +shrink                          ; Starting clock position
wW := 135 -shrink -shrink
wH := 140 -shrink - shrink

and
Code:
   Global PID, wX, wY, wW, wH, wX0, wY0, XY
;...
WinSet Region, %XY% w%wW% h%wH% E, ahk_pid %PID%


Then it works. perhaps there only was an error in calculating the sizes ?

My Windows is Windows XP Home SP2 German with Theme from Flyakite OSx v. 3.5 (you can see the german controlpanelsize and perhaps differences in the screenshot from LiveWindows thread)

Image
(The Theme/Skin can cause some differences in calculation, but you always have difference between Windows-xpTheme and windows-classic theme)

the draging now works with your change to Group1 detection.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 10th, 2006, 6:19 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
The date/time settings app was designed, when all monitors were of 4:3 aspect ratio, and if the screen resolution was set differently (e.g., instead of 640x480, to 720x400 pixels), the clock looked distorted. To compensate for this, the clock was changed to an ellipsis. However, today the monitors are of different shapes, and this distortion does not make sense. On my 1920x1200 resolution monitor the clock looks like if somebody is sitting on it (egg-clock).

We can try to guess the parameters of this stupid distortion from the size of the clock control. If the distortion were linearly dependent on this (just a hope), the latest version cuts off the clock face correctly. Also, you can set "z" to a zoom factor. z = 1 is good for testing (the hour dots should be on the edge, only partially visible), but for a nicer look set it to 0.8, or similar.

How does it look on your monitor?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 10th, 2006, 6:43 pm 
Offline

Joined: March 11th, 2006, 12:44 pm
Posts: 341
Location: Munich, Germany
now it is elipse, dX and dY must be the same. i dont think we have to compensate screenratio.

this version (code below) looks right to me, your version with radius is egg on my 4:3 screen (14" 1400x1050, same ratio like 1280x800 or 1024x786 or 800x600 or 640x480)

on new TFT/LCD, flatpanel displays the ratio might be different, but the screensize is also , so a 100x100 pixel is always a square. you only manage to make an egg if you choose a funny resolution, but then everything is distorted!! all videos and fotos will be wrong.
for example a 1650x1050 is NOT 4:3. The additional 150pixel in width are just added to the side and the distance of pixels are the same as on 1400x1050.

i changed the code to pixels instead of zoom, because this way i can adjust the width to the width of my taskbar (on right side 200px) exaclty.
with zoom i have to guess.

Code:
ControlGetPos cX,cY,cW,cH, ClockWndMain1, ahk_pid %PID%
oX := cX + cW//2                       ; Origin = center of clock
oY := cY + cH//2 -2
w = 200                               ; desired pixels
dX := w /2                          ; Diameter in x,y direction
dY := w /2                          ; same as dX to make it round and not elipse
XY :=Round(oX-dX/2) "-" Round(oY-dY/2) ; Position of clock within control
wX := A_ScreenWidth//2 - oX
wY := -Round(oY-dY/2)                  ; Initial clock position
Full := Pos(0)                         ; Start with cut-off clock
SetTimer IsClosed, 999                 ; Exit script if clock is closed


How does the "TestBild" apear on your screen, round or elipse ? it should be round! (on my screen its round) LCD.

http://bs.cyty.com/menschen/e-etzold/archiv/TV/test/testbild.htm
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 10th, 2006, 7:33 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
holomind wrote:
dX and dY must be the same
Unfortunately, not. On my laptop screen the right dX/dY = 150/126 = ~ 1.2
holomind wrote:
i dont think we have to compensate screenratio
It was just a hope. It worked in my office PC with traditional CRT monitor and in my laptop. The main difference was the screen aspect ratio, otherwise the same OS is running.
holomind wrote:
1400x1050, same ratio like 1280x800 or 1024x786 or 800x600 or 640x480
Not according to my calculator: 1400/1050=1.333, 1280/800=1.6, 1024/786=1.30 ...
holomind wrote:
on new TFT/LCD, flatpanel displays the ratio might be different, but the screensize is also, so a 100x100 pixel is always a square. you only manage to make an egg if you choose a funny resolution
Windows does not know the actual screen aspect ratio, without the proper monitor driver. If I set 1600x900 pixel resolution on my 4:3 CRT display the clock looks still round, that is, the horizontal and vertical diameters become very different in pixels. If the video signal is connected to a wide screen display, the clock becomes a flat ellipsis. The date/time settings app does not recognize the screen geometry data. There is no distortion in movie or photo playback, the left TestBild has also a perfectly round circle, only the clock is an egg in my laptop.

It looks like there is no universal solution. Your code is wrong on my laptop, mine is right, but it is wrong on yours (it can be the effects of the theme you use). Unless we figure the algorithm MS uses to set the aspect ratio, we are stuck with manually setting dX and dY in the script.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 10th, 2006, 7:47 pm 
Offline

Joined: March 11th, 2006, 12:44 pm
Posts: 341
Location: Munich, Germany
I see you are right. The 1280x800 does not fit in the row. the others are roughly 4:3.

it really seems the Clock-Applet is changing according to the screenresolution which is odd. so we *have* to compensate.

but it dont understand why you have to adopt the X/Y in the "WinSet Region". as this should not be connected to the screenresolution. but this is what AHK renders. so the shape of the mini-window should be always square? so the os does some funny "corrections" to your windows.

so perhaps the easiest way is that the user has to define dX and dY. to set it to the desired size. the rest of the calculation should fit hopefully.
then some people would set same dX/dY (mostly 4:3 or LCD's / Flatpanels) and some have to set different dX/dY for some odd resolutions and monitor-sizes.
i hardly use CRT ever (cant stand the flicker even at 100hz ;) and there are some odd sizes possible like 1280x800 and 1280x1024 which cannot be the same ratio. and one of them will always be distorted.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 10th, 2006, 8:17 pm 
Offline

Joined: March 11th, 2006, 12:44 pm
Posts: 341
Location: Munich, Germany
Laszlo wrote:
Unless we figure the algorithm MS uses to set the aspect ratio, we are stuck with manually setting dX and dY in the script.


But we already have it!
Code:
ControlGetPos cX,cY,cW,cH, ClockWndMain1, ahk_pid %PID%


on my computer cW and cH are the same.
on yours they should be different, as the OS is correcting them to make it "look" round. this way one could calculate against the distortion?

i would not remove pixels like -18 in your example but only correct the dY based on the screen ratio.

something like:
Code:
ControlGetPos cX,cY,cW,cH, ClockWndMain1, ahk_pid %PID%
oX := cX + cW//2                       ; Origin = center of clock
oY := cY + cH//2 -2
w := 200                               ; desired pixels

screenratio := A_ScreenWidth / A_ScreenHeight
clockratio  := cH / cW

distortion1 := 3 / 4 * screenratio
distortion2 := 3 / 4 * screenratio * clockratio
distortion3 := clockratio
;tooltip, %screenratio% %clockratio% %distortion%
dX := w / 2
dY := w / 2  / distortion3


you should try distortion 1,2 or 3 to see which one works
On my 1400 resolution all 3 work, as they cancel out themselfes.
if i use bad resolutions like 1280x800 or 1280x1024 which distort on my LCD i can correct them with different distoritions , my guess is distortion2 should work best. perhaps you also need to apply the distortion to other values (like oX or oY) for compensation ?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 10th, 2006, 10:52 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
"screenratio := A_ScreenWidth / A_ScreenHeight" works the best in my laptop and office PC. I updated the original post. Thanks holomind!

Still, with other themes there could be different results, so be prepared to edit the script, if the clock is not centered or it is not of the right size.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 21 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: specter333, XX0 and 24 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group