AClock Re: Simple drawing question

"valery_vi" <[email protected]>
Newsgroups gmane.comp.windows.autoit.user
Message-ID <[email protected]>
BTW About simple drawing.

I decided to make simple clock and measure it's influence on CPU 
usage.

Script AClock.au3:

#include <GUIConstants.au3>
Global $WinTrans = 210
Global $user = DllOpen("user32.dll")
Global $gdi = DllOpen("gdi32.dll")
Global $hDC, $BPen, $CPen, $HPen
Global $Size = 190, $R = $Size
Global Const $PI = 3.1413
Global $Sec
Global $FHour = False, $FMin = False, $FSec = False

Opt('PixelCoordMode',0)
HotKeySet("{Esc}", "On_Escape")

$hWnd = GUICreate("box", 600, 400, -1,  -1, $WS_POPUP)
$hDC = DllCall($user, "int", "GetDC", "hwnd", $hWnd)
$hDC = $hDC[0]
$BPen = DllCall($gdi, "int", "CreatePen", "int", 0, "int", 2, "int", 
0xAA0000)
$BPen = $BPen[0]
DllCall($gdi, "int", "SelectObject", "int", $hDC, "int", $BPen)
$CPen = DllCall($gdi, "int", "CreatePen", "int", 0, "int", 2, "int", 
0xFFAA00)
$CPen = $CPen[0]

GUISetState(@SW_SHOW)
$WinBkColor = PixelGetColor(1,1)
; Pen to hide trace of clock's hands
$HPen = DllCall($gdi, "int", "CreatePen", "int", 0, "int", 2, "int", 
$WinBkColor)
$HPen = $HPen[0]

WinSetTrans ($hWnd, "", $WinTrans)

$x1 = 10
$x2 = 590
$y1 = 10
$y2 = 390
DrawBox($x1, $y1, $x2, $y2)

PaintClock($BPen)
PaintHour($CPen, @Hour, @Min, @Sec)
PaintMin($CPen, @Min, @Sec)
PaintSec($CPen, @Sec)
$Sec = @Sec

While 1
 $Msg = GUIGetMsg()
 if $Msg = $GUI_EVENT_CLOSE then ExitLoop
  if $Sec <> @Sec then 
   $Sec = @Sec
   PaintSec($CPen, @Sec)
   PaintMin($CPen, @Min, @Sec)
   PaintHour($CPen, @Hour, @Min, @Sec)
  endif
WEnd

On_Exit()


func On_Escape()
 DllCall($user, "int", "ReleaseDC", "hwnd", 0, "int", $hDC)
 DllClose($user)
 DllClose($gdi)
  exit
endfunc

;-----------------------------------------------------
func PaintClock($Pen)
local $res, $xL, $yL, $xR, $yR
local $f = 0, $df = $pi/30
local $rL = $R * 47/60
local $rR = $R * 4/5
local $rC = $R * 2/3
local $rB = $R * 5/6

 DllCall ($gdi, "hwnd", "SelectObject", "hwnd", $hDC, "hwnd", $Pen)
 For $i = 0 To 60
  $f = $df * $i
  $xL = $Size
  $yL = $Size
  $xR = $Size + $rB*Cos($f)
  $yR = $Size - $rB*Sin($f)
  If Mod ($i, 5) = 0 Then
	$xL += $rC*Cos($f)
	$yL -= $rC*Sin($f)
  else
      $xL += $rL*Cos($f)
      $yL -= $rL*Sin($f)
  endif
   paintL($xL, $yL, $xR, $yR)
 Next
endfunc

;-----------------------------------------------------
func PaintHour($Pen, $Hour, $Min, $Sec)
local $res, $xL, $yL, $xR, $yR
local $f = 0, $df = $pi/30
local $rH = $R * 1/3
local $PMin, $PSec 

 $PMin = $Min -1 
 if $Min = 0 then $PMin = 59

 $PSec = $Sec -1 
 if $Sec = 0 then $PSec = 59

  if $FHour then 
  DllCall ($gdi, "hwnd", "SelectObject", "hwnd", $hDC, "hwnd", $HPen)
  $f = $df * (45 + $Hour * 5 + $PMin / 12 + $PSec / 1200)
  $xR = $Size + $rH*Cos($f)
  $yR = $Size + $rH*Sin($f)
  paintL($Size, $Size, $xR, $yR)
  endif

  $FHour = True

 DllCall ($gdi, "hwnd", "SelectObject", "hwnd", $hDC, "hwnd", $Pen)
 $f = $df * (45 + $Hour * 5 + $Min / 12 + $Sec / 1200)
 $xR = $Size + $rH*Cos($f)
 $yR = $Size + $rH*Sin($f)
 paintL($Size, $Size, $xR, $yR)
endfunc

;-----------------------------------------------------
func PaintMin($Pen, $Min, $Sec)
local $res, $xL, $yL, $xR, $yR
local $f = 0, $df = $pi/30
local $rM = $R * 1/2
local $PMin, $PSec 

 $PSec = $Sec -1 
 if $Sec = 0 then $PSec = 59

 if $FMin then 
  DllCall ($gdi, "hwnd", "SelectObject", "hwnd", $hDC, "hwnd", $HPen)
  $f = $df * (45 + $Min + $PSec / 60)
  $xR = $Size + $rM*Cos($f)
  $yR = $Size + $rM*Sin($f)
  paintL($Size, $Size, $xR, $yR)
  endif

  $FMin = True

 DllCall ($gdi, "hwnd", "SelectObject", "hwnd", $hDC, "hwnd", $Pen)
 $f = $df * (45 + $Min + $Sec / 60)
 $xR = $Size + $rM*Cos($f)
 $yR = $Size + $rM*Sin($f)
 paintL($Size, $Size, $xR, $yR)
endfunc

;-----------------------------------------------------
func PaintSec($Pen, $Sec)
local $res, $xL, $yL, $xR, $yR
local $f = 0, $df = $pi/30
local $rS = $R * 2/3

 DllCall ($gdi, "hwnd", "SelectObject", "hwnd", $hDC, "hwnd", $HPen)
 $f = $df * (45 + $Sec - 1)
 $xR = $Size + $rS*Cos($f)
 $yR = $Size + $rS*Sin($f)
 paintL($Size, $Size, $xR, $yR)

 DllCall ($gdi, "hwnd", "SelectObject", "hwnd", $hDC, "hwnd", $Pen)
 $f = $df * (45 + $Sec)
 $xR = $Size + $rS*Cos($f)
 $yR = $Size + $rS*Sin($f)
 paintL($Size, $Size, $xR, $yR)

endfunc

;-----------------------------------------------------
Func DrawBox($x1, $y1, $x2, $y2)
if $x1 = '' or $x2 = '' then return
DllCall($gdi, "int", "MoveToEx", "hwnd", $hDC, "int", $x1, "int", 
$y1, "int", 0)
DllCall($gdi, "int", "LineTo", "hwnd", $hDC, "int", $x1, "int", $y2)
DllCall($gdi, "int", "LineTo", "hwnd", $hDC, "int", $x2, "int", $y2)
DllCall($gdi, "int", "LineTo", "hwnd", $hDC, "int", $x2, "int", $y1)
DllCall($gdi, "int", "LineTo", "hwnd", $hDC, "int", $x1, "int", $y1)
EndFunc

;-----------------------------------------------------
Func MoveTo($x, $y)
 DllCall($gdi, "long", "MoveToEx", "hwnd", $hDC, "int", $x, "int", 
$y, "ptr", 0)
endfunc

;-----------------------------------------------------
Func LineTo($x, $y)
 DllCall($gdi, "int", "LineTo", "hwnd", $hDC, "int", $x, "int", $y)
endfunc

;-----------------------------------------------------
func paintLine($xL, $yL, $xR, $yR, $Pen)
 DllCall ($gdi, "hwnd", "SelectObject", "hwnd", $hDC, "hwnd", $Pen)
 DllCall ($gdi, "int", "MoveToEx", "hwnd", $hDC, "int", $xL, "int", 
$yL, "ptr", 0)
 DllCall ($gdi, "int", "LineTo", "hwnd", $hDC, "int", $xR, "int", $yR)
endfunc

;-----------------------------------------------------
func paintL($xL, $yL, $xR, $yR)
 DllCall ($gdi, "int", "MoveToEx", "hwnd", $hDC, "int", $xL, "int", 
$yL, "ptr", 0)
 DllCall ($gdi, "int", "LineTo", "hwnd", $hDC, "int", $xR, "int", $yR)
endfunc

It use 0% CPU as I see.
:-)

Valery

--- In [email protected], "valery_vi" <shehrer1@...> wrote:
>
> Someone from AutoItScript forum wrote:
> "
> Maybe possible to make a transparent window ?
> "
> 
> It's possible, too:
> 
> #include <GUIConstants.au3>
> Global $Transp_Flag = 100
> Global $user = DllOpen("user32.dll")
> Global $gdi = DllOpen("gdi32.dll")
> Global $hDC, $Pen
> 
> HotKeySet("{Esc}", "On_Escape")
> 
> $hWnd = GUICreate("box", 600, 400, -1,  -1, $WS_POPUP)
> WinSetTrans ($hWnd, "", $Transp_Flag)
> $hDC = DllCall($user, "int", "GetDC", "hwnd", $hWnd)
> $hDC = $hDC[0]
> $Pen = DllCall($gdi, "int", "CreatePen", "int", 0, "int", 4, "int", 
> 0x00AAFF)
> $Pen = $Pen[0]
> DllCall($gdi, "int", "SelectObject", "int", $hDC, "int", $Pen)
> 
> GUISetState(@SW_SHOW)
> 
> $x1 = 10
> $x2 = 590
> $y1 = 10
> $y2 = 390
> DrawBox($x1, $y1, $x2, $y2)
> 
> While 1
> $Msg = GUIGetMsg()
> if $Msg = $GUI_EVENT_CLOSE then ExitLoop
> WEnd
> On_Exit()
> 
> 
> func On_Escape()
> DllCall($user, "int", "ReleaseDC", "hwnd", 0, "int", $hDC)
> DllClose($user)
> DllClose($gdi)
>   exit
> endfunc
> 
> Func DrawBox($x1, $y1, $x2, $y2)
> if $x1 = '' or $x2 = '' then return
> DllCall($gdi, "int", "MoveToEx", "hwnd", $hDC, "int", $x1, "int", 
> $y1, "int", 0)
> DllCall($gdi, "int", "LineTo", "hwnd", $hDC, "int", $x1, "int", $y2)
> DllCall($gdi, "int", "LineTo", "hwnd", $hDC, "int", $x2, "int", $y2)
> DllCall($gdi, "int", "LineTo", "hwnd", $hDC, "int", $x2, "int", $y1)
> DllCall($gdi, "int", "LineTo", "hwnd", $hDC, "int", $x1, "int", $y1)
> EndFunc
> 
> Valery
> --- In [email protected], "valery_vi" <shehrer1@> wrote:
> >
> > Someone from AutoItScript forum wrote:
> > "
> > I want to draw a box on my screen but i don't manage to make it 
> stay 
> > on the screen. When the box is finished drawind, it disappears !
> > 
> > What sould i do ?
> > "
> > 
> > This script works for me:
> > #include <GUIConstants.au3>
> > Global $user = DllOpen("user32.dll")
> > Global $gdi = DllOpen("gdi32.dll")
> > Global $hDC, $Pen
> > 
> > $hWnd = GUICreate("box", 600, 400)
> > $hDC = DllCall($user, "int", "GetDC", "hwnd", $hWnd)
> > $hDC = $hDC[0]
> > $Pen = DllCall($gdi, "int", "CreatePen", "int", 0, "int", 
4, "int", 
> > 0x00AAFF)
> > $Pen = $Pen[0]
> > DllCall($gdi, "int", "SelectObject", "int", $hDC, "int", $Pen)
> > 
> > GUISetState(@SW_SHOW)
> > 
> > $x1 = 10
> > $x2 = 590
> > $y1 = 10
> > $y2 = 390
> > DrawBox($x1, $y1, $x2, $y2)
> > 
> > While 1
> >  $Msg = GUIGetMsg()
> >  if $Msg = $GUI_EVENT_CLOSE then ExitLoop
> > WEnd
> > 
> > DllCall($user, "int", "ReleaseDC", "hwnd", 0, "int", $hDC)
> > DllClose($user)
> > DllClose($gdi)
> > 
> > Func DrawBox($x1, $y1, $x2, $y2)
> > if $x1 = '' or $x2 = '' then return
> > DllCall($gdi, "int", "MoveToEx", "hwnd", $hDC, "int", $x1, "int", 
> > $y1, "int", 0)
> > DllCall($gdi, "int", "LineTo", "hwnd", $hDC, "int", $x1, "int", 
$y2)
> > DllCall($gdi, "int", "LineTo", "hwnd", $hDC, "int", $x2, "int", 
$y2)
> > DllCall($gdi, "int", "LineTo", "hwnd", $hDC, "int", $x2, "int", 
$y1)
> > DllCall($gdi, "int", "LineTo", "hwnd", $hDC, "int", $x1, "int", 
$y1)
> > EndFunc
> > 
> > Valery
> >
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.