Jump to content

Create Colored Line


WoodGrain
 Share

Recommended Posts

Following on from my other post related to getting a title-less GUI with a 1px line, I'm wanting to color the line. I've attached a crude example of what i'm trying to explain/achieve.ColoredLineGui.png.da048089bbe164e023fd137afb4cb5be.png

I have percentages (based on ranges in an array) I will be able to use to apply to the different colours that will define how wide they are (relative to the screen width).

At this point, I'm considering creating multiple colored lines stacked horizontally next to each other using _GDIPlus_GraphicsDrawLine, but wanted to see if anyone else had a better way of achieveing this?

Thanks!

Link to comment
Share on other sites

I made the GUI 5px height just for convenience but you can make it 1px very easy. In the data array you just have to set percentages and color (AARRGGBB format) for that specific segment.

#include <GDIPlus.au3>
#include <WinAPISysWin.au3>

$hGUI = GUICreate('', 800, 5, Default, Default, 0x80000000) ; WS_POPUP
$idPic = GUICtrlCreatePic('', 0, 0, 800, 5)
GuiSetState()

Dim $aData[4][2] = [[30, 0xFF606C38], [20, 0xFFFFC300], [40, 0xFFEF233C], [10, 0xFF0D3B66]]
CreateStackedBar($idPic, $aData)

Do
Until GUIGetMsg() = -3  ; GUI_EVENT_CLOSE

Func CreateStackedBar($idCtrl, $aData)
    Local Const $STM_SETIMAGE = 0x0172, $IMAGE_BITMAP = 0
    Local $hCtrl = GUICtrlGetHandle($idCtrl)
    Local $hParent = _WinAPI_GetParent($hCtrl)
    Local $aPos = ControlGetPos($hParent, '', $idCtrl)
    Local $iCurrent = 0, $iWidth = 0
    _GDIPlus_Startup()
    Local $hBitmap = _GDIPlus_BitmapCreateFromScan0($aPos[2], $aPos[3])
    Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    Local $hBrush = _GDIPlus_BrushCreateSolid()
    For $Index = 0 To UBound($aData) - 1
        $iWidth = $aData[$Index][0] * $aPos[2] / 100
        _GDIPlus_BrushSetSolidColor($hBrush, $aData[$Index][1])
        _GDIPlus_GraphicsFillRect($hGraphics, $iCurrent, 0, $iWidth, $aPos[3], $hBrush)
        $iCurrent += $iWidth
    Next
    Local $hHBITMAP = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)
    _WinAPI_DeleteObject(GUICtrlSendMsg($idCtrl, $STM_SETIMAGE, $IMAGE_BITMAP, $hHBITMAP))
    _WinAPI_DeleteObject($hHBITMAP)
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_Shutdown()
EndFunc

 

When the words fail... music speaks.

Link to comment
Share on other sites

Another option that will give you the opportunity to identify which bar the user has clicked on.

#include <GUIConstants.au3>

Opt("MustDeclareVars", True)

Local $aData = [[30, 0xFF0000, "Red"], [20, 0xFFFF00, "Yellow"], [40, 0x00FF00, "Green"], [10, 0x0000FF, "Blue"]]
Local $aLab[UBound($aData)]

Local $hGUI = GUICreate('', 800, 5, Default, Default, $WS_POPUP)
Local $iStart = 0, $iWidth
For $i = 0 To UBound($aLab) - 1
  $iWidth = 800 * $aData[$i][0] / 100
  $aLab[$i] = GUICtrlCreateLabel("", $iStart, 0, $iWidth, 5)
  GUICtrlSetBkColor(-1, $aData[$i][1])
  $iStart += $iWidth
Next

GuiSetState()

Local $iMsg
While True
  $iMsg = GUIGetMsg()
  Switch $iMsg
    Case $GUI_EVENT_CLOSE
      ExitLoop
    Case $aLab[0] To $aLab[UBound($aLab) - 1]
      ConsoleWrite("You have click on the " & $aData[$iMsg - $aLab[0]][2] & " bar." & @CRLF)
  EndSwitch
WEnd

 

Link to comment
Share on other sites

Thank you both for your very full answers! I was really just after suggestions as to what possible better alternatives to what I was considering and you both went above and beyond with full examples, thanks.

@Andreik that really provided an end to end example of how to use the GDIPlus tools, thank you!

@Nine interesting, by using the labels I don't even need the GDI tools, I think I will explore this further, thank you!

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...