Jump to content
Sign in to follow this  
Klovis

Add a pin to an existing script

Recommended Posts

Klovis

I need to be able to have the gauge pin in almost done. I just have the 1pin.png there for an example so if someone can quickly implement gauge(2).au3 with almostdone.au3 It should take no time at all but im sorta clueless...

Thanks so much autoit community!

-Klovis

autoitsub.zip

Share this post


Link to post
Share on other sites
Tvern

This works:

#include <GDIPlus.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <GuiConstantsEx.au3>
#include <SliderConstants.au3>
#include <StaticConstants.au3>
#include <Misc.au3>

Opt("MustDeclareVars", 1)

Global Const $AC_SRC_ALPHA = 1
Global $hGUI_Slider, $iLabelPerc, $iSlider
Global $hGUI_Graphics
Global $hImageGS, $hImageRed1, $hImageRed2, $hImageRed3, $hImagePin

;pin position and size
Global $hPen, $radius = 60, $x = 615, $y = 335


$hGUI_Graphics = GUICreate("Graphics", 1280, 720, -1, -1, -1, BitOR($WS_EX_LAYERED,$WS_EX_TOPMOST))
GUISetState(@SW_SHOW)

$hGUI_Slider = GUICreate("Control", 200, 39, 192, 114, -1, $WS_EX_TOPMOST)
$iSlider = GUICtrlCreateSlider(0, 0, 136, 37)
GUICtrlSetLimit($iSlider, 255, 0)
GUICtrlSetData($iSlider, 0)
GUICtrlCreateLabel("Slider:", 138, 0, 33, 17)
$iLabelPerc = GUICtrlCreateLabel("0%", 165, 16, 30, 17)
GUISetState(@SW_SHOW)

_GDIPlus_Startup()
$hPen = _GDIPlus_PenCreate(0xFFFF0000, 5)
$hImageGS = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\gs.png")
$hImageRed1 = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\first.png")
$hImageRed2 = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\second.png")
$hImageRed3 = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\third.png")

GUIRegisterMsg($WM_HSCROLL  , "WM_HSCROLL"  )

Do

Until GUIGetMsg() = $GUI_EVENT_CLOSE

_GDIPlus_ImageDispose($hImageGS)
_GDIPlus_ImageDispose($hImageRed1)
_GDIPlus_ImageDispose($hImageRed2)
_GDIPlus_ImageDispose($hImageRed3)
_GDIPlus_PenDispose($hPen)
_GDIPlus_Shutdown()

Func WM_HSCROLL($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam, $ilParam
    _Update()
EndFunc

Func _Update()
    Local $iSliderValue = GUICtrlRead($iSlider)
    Local $iSliderPerc = $iSliderValue/2.55
    GUICtrlSetData($iLabelPerc,Round($iSliderPerc) & "%")
    Local $hBitmap
    Switch $iSliderPerc
        Case 0 To 49
            $hBitmap = $hImageRed1
        Case 50 To 79
            $hBitmap = $hImageRed2
        Case 80 To 100
            $hBitmap = $hImageRed3
    EndSwitch
    Local $hGraphic = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    _GDIPlus_GraphicsDrawImage ($hGraphic, $hImageGS, 515, 250)

    Local $radian = (($iSliderValue/190)+.8) * 3.14159265358979 ;use (($iSliderValue/255)+1) for 270 to 90 degrees
    _GDIPlus_GraphicsDrawLine($hGraphic, $x, $y, $x + Cos($radian) * $radius,$y +  Sin($radian) * $radius, $hPen)

    _GDIPlus_GraphicsDispose($hGraphic)
    SetBitMap($hGUI_Graphics, $hBitmap, $iSliderValue)
    _WinAPI_DeleteObject($hBitmap)
EndFunc

Func SetBitmap($hGUI, $hImage, $iOpacity)
  Local $hScrDC, $hMemDC, $hBitmap, $hOld, $pSize, $tSize, $pSource, $tSource, $pBlend, $tBlend
  $hScrDC  = _WinAPI_GetDC(0)
  $hMemDC  = _WinAPI_CreateCompatibleDC($hScrDC)
  $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
  $hOld = _WinAPI_SelectObject($hMemDC, $hBitmap)
  $tSize   = DllStructCreate($tagSIZE)
  $pSize   = DllStructGetPtr($tSize  )
  DllStructSetData($tSize, "X", _GDIPlus_ImageGetWidth($hImage))
  DllStructSetData($tSize, "Y", _GDIPlus_ImageGetHeight($hImage))
  $tSource = DllStructCreate($tagPOINT)
  $pSource = DllStructGetPtr($tSource)
  $tBlend  = DllStructCreate($tagBLENDFUNCTION)
  $pBlend  = DllStructGetPtr($tBlend)
  DllStructSetData($tBlend, "Alpha" , $iOpacity )
  DllStructSetData($tBlend, "Format", $AC_SRC_ALPHA)
  _WinAPI_UpdateLayeredWindow($hGUI, $hScrDC, 0, $pSize, $hMemDC, $pSource, 0, $pBlend, $ULW_ALPHA)
  _WinAPI_ReleaseDC   (0, $hScrDC)
  _WinAPI_SelectObject($hMemDC, $hOld)
  _WinAPI_DeleteObject($hBitmap)
  _WinAPI_DeleteDC  ($hMemDC)
EndFunc

It's not exactly like the one you had, in that it goes lower than horizontal. I thought that looked better. You could have just bumped the other thread for this btw. (also, the pin I linked in the old thread had the background removed, so it didn't move a big white square around)

If the straight line is good for you I'd stick with that. I drew a filled polygon that would replace $hImagePin instead of the picture, (and be rotated in the same way) but the result looked a little messy.

I've also looked into the red background as I wanted to increase my knowledge of GDI+, but it seems more difficult than I thought to create the transparent hole with blurred edge. Would love if someone could give some pointers there.

Share this post


Link to post
Share on other sites
Klovis

Well as long as were in this then, maybe we can instead of it coming right in a 1 transparency we wait until the needle hits the yellow which would be at 36 then start the easing in but then you'd have to times the guictrlread($slider) to like Guictrlread($slider)*1.33(not actually 1.33 but something that will get it two 100% by starting at 36. I had that running with the gauge with the first script I sent you but how would this happen with the script you sent me?

Edited by Klovis

Share this post


Link to post
Share on other sites
Tvern

Replace _Update with this:

Func _Update()
    Local $iSliderValue = GUICtrlRead($iSlider)
    Local $iSliderPerc = $iSliderValue/2.55
    GUICtrlSetData($iLabelPerc,Round($iSliderPerc) & "%")

    If $iSliderValue < 92 Then Return ; 92 is 36% of 255
    ;no need to do all the other stuff if we're not gonna show it.

    Local $hBitmap
    Switch $iSliderPerc
        Case 0 To 49
            $hBitmap = $hImageRed1
        Case 50 To 79
            $hBitmap = $hImageRed2
        Case 80 To 100
            $hBitmap = $hImageRed3
    EndSwitch
    Local $hGraphic = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    _GDIPlus_GraphicsDrawImage ($hGraphic, $hImageGS, 515, 250)

    Local $radian = (($iSliderValue/190)+.8) * 3.14159265358979 ;use (($iSliderValue/255)+1) for 270 to 90 degrees
    _GDIPlus_GraphicsDrawLine($hGraphic, $x, $y, $x + Cos($radian) * $radius,$y +  Sin($radian) * $radius, $hPen)

    _GDIPlus_GraphicsDispose($hGraphic)

    Local $iTrans = Round(($iSliderValue-92)*1.564)  ;1.564 is 255/163, 163 is 255-92, 92 is 36% of 255
    ConsoleWrite($iTrans & @CRLF)
    If $iTrans < 0 Then $iTrans = 0
    SetBitMap($hGUI_Graphics, $hBitmap, $iTrans)

    _WinAPI_DeleteObject($hBitmap)
EndFunc

36% of 255 is 92, so to start at 36% I reduced the value of the slider by 92, then the maximum value will only be 163, so to raise the maximum to 255 we have to divide the value by 163, and multiply it by 255, which effectively means multiplying with 1.564

I later realised that it would look a little better to increase the maximum value of the slider so the transparency would still increase with steps of 1 so there;s a challenge for you. :)

You may want to review the switch statement too, so the steps are more evenly spread.

Share this post


Link to post
Share on other sites
Klovis

Cool now I'm going to implement TCP with it and hope for the best, thanks much!

-Klovis

Share this post


Link to post
Share on other sites
Klovis

Problem! when you first make the images visible they dont go away, make the gauge and first appear now try to make them dissapear. at first you cant see it, it's hardly visible but that isnt the problem, the problem is you cannot click through it, so we need to be able to make so that when it's below 36% it's not there then when it's above 36 then it'll be there.

Share this post


Link to post
Share on other sites
Klovis

never mind fixed it just changed

If $iSliderValue < 92 Then Return

to

If $iSliderValue < 91 Then Return

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  

×