Jump to content

Simplest Gradient


qwert
 Share

Recommended Posts

This was a bit of a puzzle for me, but with Yashied’s help, I figured it out.

I’d been investigating the use of _WinAPI_GradientFill, but found the first couple of rungs of the ladder missing.  By that, I mean that the explanations and examples I found were unclear about how to do the simplest gradient of all.  It didn’t help that terms like “vertex” and “logical units” were involved.  Anyone who had already worked with color fills obviously knew what they meant.  I didn’t.

So, here’s a modification of one of the very first examples from 2009.  It produces a simple 200x600 linear gradient (from one color to another color), as well as the original 400x400 blended gradient (of multiple colors).  Note the subtle differences in the DC processing, related to the use of _WinAPI_GetDC(), I believe.  (BTW, if anyone sees a way for further simplification, please submit a working replacement example.) 

Also, I went ahead and made the original gradient frameless and draggable, but only as a way of showing some variety.

#Include <GUIConstantsEx.au3>
#include <GuiConstants.au3>
#Include <WinAPIEx.au3>

Global Const $STM_SETIMAGE = 0x0172
Global Const $STM_GETIMAGE = 0x0173

;========  ; basic 2-color gradient  ================
Dim $aVertex[2][3] = [[0, 0, 0x00FFAA], [200, 600, 0x00AA44]]       ; blue/green to different blue/green
$hDC = _WinAPI_CreateCompatibleDC(0)    ; create the bitmap
$hBitmap = _WinAPI_CreateBitmap(200, 600, 1, 32)
$hSv = _WinAPI_SelectObject($hDC, $hBitmap)
_WinAPI_GradientFill($hDC, $aVertex, 0, 1)          ; actual fill
_WinAPI_SelectObject($hDC, $hSv)
_WinAPI_DeleteDC($hDC)
$hForm = GUICreate('MyGUI', 200, 600, 300, 100)   ; create a GUI for the bitmap
GUICtrlCreatePic('', 0, 0, 200, 600)
GUICtrlSetState(-1, $GUI_DISABLE)
GUICtrlSendMsg(-1, $STM_SETIMAGE, 0, $hBitmap)
GUISetState()

;========   somewhat more complex gradient  + draggable ===========
Dim $aVertex[6][3] = [[0, 0, 0xFF0000],[400, 400, 0x00FF00],[0, 400, 0x0000FF],[0, 0, 0xFF0000],[400, 0, 0xFFFF00],[400, 400, 0x00FF00]]
$hColors = GUICreate('Color Wheel', 400, 400, 600, 200, $WS_POPUP)    ; Create square GUI
$idPic = GUICtrlCreatePic('', 0, 0, 400, 400,  -1, $GUI_WS_EX_PARENTDRAG)
$hPic = GUICtrlGetHandle($idPic)
$hDC = _WinAPI_GetDC($hPic)                         ; Create 6-point gradient
$hDestDC = _WinAPI_CreateCompatibleDC($hDC)
$hBitmap = _WinAPI_CreateCompatibleBitmap($hDC, 400, 400)
$hDestSv = _WinAPI_SelectObject($hDestDC, $hBitmap)
_WinAPI_GradientFill($hDestDC, $aVertex, 0, 2)
_WinAPI_GradientFill($hDestDC, $aVertex, 3, 5)
_WinAPI_ReleaseDC($hPic, $hDC)
_WinAPI_SelectObject($hDestDC, $hDestSv)
_WinAPI_DeleteDC($hDestDC)
_SendMessage($hPic, $STM_SETIMAGE, 0, $hBitmap)     ; Set gradient to control
Local $hObj = _SendMessage($hPic, $STM_GETIMAGE)
If $hObj <> $hBitmap Then _WinAPI_DeleteObject($hBitmap)
GUISetState()
GUIRegisterMsg($WM_NCHITTEST, "WM_NCHITTEST")

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

Func WM_NCHITTEST($hWnd, $iMsg, $iwParam, $ilParam)
    If ($hWnd = $hColors) And ($iMsg = $WM_NCHITTEST) Then Return $HTCAPTION
EndFunc

post-29172-0-92985400-1422203283_thumb.p

Link to comment
Share on other sites

  • 2 months later...

This is very useful.

But I want to ask How to make it as a background under Tabs

Here is my attempt:

#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <WindowsConstants.au3>
#include <WinAPIGdi.au3>

Global $iMemo, $MainGUI, $hStatus

Example()

Func Example()
    Local $hGUI
    Local $aParts[3] = [75, 150, -1]

    ; Create GUI
    $hGUI = GUICreate("Example", 400, 300)

    ; Gradient made by qwert
    ; http://www.autoitscript.com/forum/topic/167016-simplest-gradient/
    Local Const $STM_SETIMAGE = 0x0172
    Local $aVertex[2][3] = [[0, 0, 0x00FFAA], [400, 300, 0x00AA44]]       ; blue/green to different blue/green
    Local $hDC = _WinAPI_CreateCompatibleDC(0)    ; create the bitmap
    Local $hBitmap = _WinAPI_CreateBitmap( 400 , 300, 1, 32)
    Local $hSv = _WinAPI_SelectObject($hDC, $hBitmap)
    _WinAPI_GradientFill($hDC, $aVertex, 0, 1)          ; actual fill
    _WinAPI_SelectObject($hDC, $hSv)
    _WinAPI_DeleteDC($hDC)
;~  GUICtrlCreatePic('', 0, 0, 400, 300,-1,$WS_EX_TRANSPARENT)
    GUICtrlCreatePic('', 0, 0, 400, 300)
    GUICtrlSetState(-1, $GUI_DISABLE)
    GUICtrlSendMsg(-1, $STM_SETIMAGE, 0, $hBitmap)

    GUICtrlCreateTab(5, 5, 390, 270)

    GUICtrlCreateTabItem("tab0")
    ; Create memo control
    $iMemo = GUICtrlCreateEdit("", 10, 30, 380, 270-30, $WS_VSCROLL)
    GUICtrlSetFont($iMemo, 9, 400, 0, "Courier New")
    GUICtrlSendMsg($iMemo, $EM_SETREADONLY, True, 0)
    GUICtrlSetBkColor($iMemo, 0xFFFFFF)
    GUICtrlCreateTabItem("tab1")

    GUICtrlCreateTabItem("") ; end tabitem definition


    ;===============================================================================
    ; defaults to 1 part, no text
    $hStatus = _GUICtrlStatusBar_Create($hGUI)
    ;===============================================================================
    _GUICtrlStatusBar_SetParts($hStatus, $aParts)

    GUISetState(@SW_SHOW)

    MemoWrite("StatusBar Created with:" & @CRLF & _
            @TAB & "Handle to GUI window" & @CRLF)

    GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

    ; Get border sizes
    MemoWrite("Horizontal border width .: " & _GUICtrlStatusBar_GetBordersHorz($hStatus))
    MemoWrite("Vertical border width ...: " & _GUICtrlStatusBar_GetBordersVert($hStatus))
    MemoWrite("Width between rectangles : " & _GUICtrlStatusBar_GetBordersRect($hStatus))

    ; Loop until the user exits.
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUISetState(@SW_ENABLE, $MainGUI)
    GUIDelete($hGUI)
EndFunc   ;==>Example

; Write message to memo
Func MemoWrite($sMessage = "")
    GUICtrlSetData($iMemo, $sMessage & @CRLF, 1)
EndFunc   ;==>MemoWrite

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Local $tinfo
    Switch $hWndFrom
        Case $hStatus
            Switch $iCode
                Case $NM_CLICK ; The user has clicked the left mouse button within the control
                    $tinfo = DllStructCreate($tagNMMOUSE, $ilParam)
                    $hWndFrom = HWnd(DllStructGetData($tinfo, "hWndFrom"))
                    $iIDFrom = DllStructGetData($tinfo, "IDFrom")
                    $iCode = DllStructGetData($tinfo, "Code")
                    _DebugPrint("$NM_CLICK" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode & @CRLF & _
                            "-->ItemSpec:" & @TAB & DllStructGetData($tinfo, "ItemSpec") & @CRLF & _
                            "-->ItemData:" & @TAB & DllStructGetData($tinfo, "ItemData") & @CRLF & _
                            "-->X:" & @TAB & DllStructGetData($tinfo, "X") & @CRLF & _
                            "-->Y:" & @TAB & DllStructGetData($tinfo, "Y") & @CRLF & _
                            "-->HitInfo:" & @TAB & DllStructGetData($tinfo, "HitInfo"))
                    Return True ; indicate that the mouse click was handled and suppress default processing by the system
                    ; Return FALSE ;to allow default processing of the click.
                Case $NM_DBLCLK ; The user has double-clicked the left mouse button within the control
                    $tinfo = DllStructCreate($tagNMMOUSE, $ilParam)
                    $hWndFrom = HWnd(DllStructGetData($tinfo, "hWndFrom"))
                    $iIDFrom = DllStructGetData($tinfo, "IDFrom")
                    $iCode = DllStructGetData($tinfo, "Code")
                    _DebugPrint("$NM_DBLCLK" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode & @CRLF & _
                            "-->ItemSpec:" & @TAB & DllStructGetData($tinfo, "ItemSpec") & @CRLF & _
                            "-->ItemData:" & @TAB & DllStructGetData($tinfo, "ItemData") & @CRLF & _
                            "-->X:" & @TAB & DllStructGetData($tinfo, "X") & @CRLF & _
                            "-->Y:" & @TAB & DllStructGetData($tinfo, "Y") & @CRLF & _
                            "-->HitInfo:" & @TAB & DllStructGetData($tinfo, "HitInfo"))
                    Return True ; indicate that the mouse click was handled and suppress default processing by the system
                    ; Return FALSE ;to allow default processing of the click.
                Case $NM_RCLICK ; The user has clicked the right mouse button within the control
                    $tinfo = DllStructCreate($tagNMMOUSE, $ilParam)
                    $hWndFrom = HWnd(DllStructGetData($tinfo, "hWndFrom"))
                    $iIDFrom = DllStructGetData($tinfo, "IDFrom")
                    $iCode = DllStructGetData($tinfo, "Code")
                    _DebugPrint("$NM_RCLICK" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode & @CRLF & _
                            "-->ItemSpec:" & @TAB & DllStructGetData($tinfo, "ItemSpec") & @CRLF & _
                            "-->ItemData:" & @TAB & DllStructGetData($tinfo, "ItemData") & @CRLF & _
                            "-->X:" & @TAB & DllStructGetData($tinfo, "X") & @CRLF & _
                            "-->Y:" & @TAB & DllStructGetData($tinfo, "Y") & @CRLF & _
                            "-->HitInfo:" & @TAB & DllStructGetData($tinfo, "HitInfo"))
                    Return True ; indicate that the mouse click was handled and suppress default processing by the system
                    ; Return FALSE ;to allow default processing of the click.
                Case $NM_RDBLCLK ; The user has double-clicked the right mouse button within the control
                    $tinfo = DllStructCreate($tagNMMOUSE, $ilParam)
                    $hWndFrom = HWnd(DllStructGetData($tinfo, "hWndFrom"))
                    $iIDFrom = DllStructGetData($tinfo, "IDFrom")
                    $iCode = DllStructGetData($tinfo, "Code")
                    _DebugPrint("$NM_RDBLCLK" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode & @CRLF & _
                            "-->ItemSpec:" & @TAB & DllStructGetData($tinfo, "ItemSpec") & @CRLF & _
                            "-->ItemData:" & @TAB & DllStructGetData($tinfo, "ItemData") & @CRLF & _
                            "-->X:" & @TAB & DllStructGetData($tinfo, "X") & @CRLF & _
                            "-->Y:" & @TAB & DllStructGetData($tinfo, "Y") & @CRLF & _
                            "-->HitInfo:" & @TAB & DllStructGetData($tinfo, "HitInfo"))
                    Return True ; indicate that the mouse click was handled and suppress default processing by the system
                    ; Return FALSE ;to allow default processing of the click.
                Case $SBN_SIMPLEMODECHANGE ; Simple mode changes
                    _DebugPrint("$SBN_SIMPLEMODECHANGE" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode)
                    ; No return value
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Func _DebugPrint($s_text, $line = @ScriptLineNumber)
    ConsoleWrite( _
            "!===========================================================" & @CRLF & _
            "+======================================================" & @CRLF & _
            "-->Line(" & StringFormat("%04d", $line) & "):" & @TAB & $s_text & @CRLF & _
            "+======================================================" & @CRLF)
EndFunc   ;==>_DebugPrint

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

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...