Jump to content
Sign in to follow this  
Digisoul

Trying to create GDI Frame

Recommended Posts

Hello there,

I am trying to simulate the GroupBox with GDI, the GDI fram is round rect and background color of frame is Gradient.

i am bit successfull but totally failed to round the corners.

please see this test script

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <GdiPlus.au3>

#region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 625, 443, 192, 186)
$Label1 = GUICtrlCreateLabel("", 184, 112, 200, 200)
GUICtrlSetStyle(-1, 0)
GUICtrlSetBkColor(-1, -2) ; $GUI_BKCOLOR_TRANSPARENT = -2

GUISetState(@SW_SHOW)

$Col = 0xDFE9FD
$GradCol = 0xFFFFFF

_GDIPlus_Startup()
Global $graphic = _GDIPlus_GraphicsCreateFromHWND(GUICtrlGetHandle($Label1))
Global $bitmap = _GDIPlus_BitmapCreateFromGraphics(200, 200, $graphic)
Global $backbuffer = _GDIPlus_ImageGetGraphicsContext($bitmap)

Global $bmpfront = _GDIPlus_BitmapCreateFromGraphics(200, 200, $graphic)
_CreateGradientImg($bmpfront, 200 - 1, 200 - 1, $Col, $GradCol)
$width = _GDIPlus_ImageGetWidth($bmpfront)
$height = _GDIPlus_ImageGetHeight($bmpfront)

_GDIPlus_GraphicsClear($backbuffer, 0xFFFFFFFF)
_GDIPlus_GraphicsDrawImageRectRect($backbuffer, $bmpfront, 0, 0, 200, 200, 0, 0, 200, 200)
_GDIPlus_GraphicsDrawImage($graphic, $bmpfront, 0, 0)

_GuiRoundCorners(GUICtrlGetHandle($Label1), 0, 0, 50, 50)

GUIRegisterMsg($WM_PAINT, "MYPANT")

Func MYPANT($1, $2, $3, $4)
 ConsoleWrite("WM_PAINT" & @CRLF)
 _GDIPlus_GraphicsClear($backbuffer, 0xFFFFFFFF)
 _GDIPlus_GraphicsDrawImageRectRect($backbuffer, $bmpfront, 0, 0, 200, 200, 0, 0, 200, 200)
 _GDIPlus_GraphicsDrawImage($graphic, $bmpfront, 0, 0)
EndFunc   ;==>MYPANT

#endregion ### END Koda GUI section ###
While 1
 $nMsg = GUIGetMsg()
 Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
 EndSwitch
WEnd
_GDIPlus_Shutdown()
Func _GUiRoundCorners($h_win, $i_x1, $i_y1, $i_x3, $i_y3);Written by GaFrost
 Local $XS_pos, $XS_ret, $XS_ret2
 $XS_pos = WinGetPos($h_win)
 $XS_ret = DllCall("gdi32.dll", "long", "CreateRoundRectRgn", "long", $i_x1, "long", $i_y1, "long", $XS_pos[2], "long", $XS_pos[3], "long", $i_x3, "long", $i_y3)
 If $XS_ret[0] Then
  $XS_ret2 = DllCall("user32.dll", "long", "SetWindowRgn", "hwnd", $h_win, "long", $XS_ret[0], "int", 1)
 EndIf
EndFunc   ;==>_GUiRoundCorners


#region Gradient
;===============================================================================
;
; Function Name:   _Gradient($RGB_Color1 ,$RGB_Color2, $Count)
; Description::    Returns an Array of Gradient Colors
; Parameter(s):    $RGB_Color1 : The Start-Color in RGB Hexadecimal
;                  $RGB_Color2 : The End-Color in RGB Hexadecimal
;                  $Count :      The number of Colors in the Gradient
; Requirement(s):
; Return Value(s): An Array with the Colors
; Author(s):       [email="Prog@ndy"]Prog@ndy[/email]
;
;===============================================================================
;

Func _Gradient($RGB_Color1, $RGB_Color2, $Count, $ARGB = False)
 Local $Color1_R, $Color1_G, $Color1_B, $Color2_R, $Color2_G, $Color2_B, $NeuCol_R, $NeuCol_G, $NeuCol_B
 $ARGB = StringLeft("FF", 2 * $ARGB)
 $Color1_R = BitAND(BitShift($RGB_Color1, 16), 0xff)
 $Color1_G = BitAND(BitShift($RGB_Color1, 8), 0xff)
 $Color1_B = BitAND($RGB_Color1, 0xff)

 $Color2_R = BitAND(BitShift($RGB_Color2, 16), 0xff)
 $Color2_G = BitAND(BitShift($RGB_Color2, 8), 0xff)
 $Color2_B = BitAND($RGB_Color2, 0xff)

 $Count -= 1 ; 0-basiert !
 Dim $arColors[$Count + 1], $pos1

 For $i = 0 To $Count
  $pos1 = $Count - $i
  $NeuCol_R = ($Color1_R * $pos1 + $Color2_R * $i) / ($Count)
  $NeuCol_G = ($Color1_G * $pos1 + $Color2_G * $i) / ($Count)
  $NeuCol_B = ($Color1_B * $pos1 + $Color2_B * $i) / ($Count)
  $arColors[$i] = Execute('0x' & $ARGB & Hex($NeuCol_R, 2) & Hex($NeuCol_G, 2) & Hex($NeuCol_B, 2))
 Next
 Return $arColors
EndFunc   ;==>_Gradient

; Author(s):       [email="Prog@ndy"]Prog@ndy[/email]
Func _CreateGradientImg(ByRef $bmpfront, $w, $h, $startRGB, $endRGB)
 Local $graph_front = _GDIPlus_ImageGetGraphicsContext($bmpfront)
 Local $hPen = _GDIPlus_PenCreate(0, 1)
 Local $hPen2 = _GDIPlus_PenCreate(0, 3)
 Local $Wechsel = Round((13 / 20) * $h)
 Local $Gradient = _Gradient($startRGB, $endRGB, $Wechsel, 1)
 Local $Gradient2 = _Gradient($Gradient, $endRGB, $h - $Wechsel, 1)
 Local $PenColor
 For $i = 0 To $h - 1
  If $i < $Wechsel Then
   $PenColor = $Gradient[$i]
   ;Else
   ; $PenColor = $Gradient2[$i - $Wechsel]
  EndIf
  _GDIPlus_PenSetColor($hPen, $PenColor)
  _GDIPlus_GraphicsDrawLine($graph_front, 0, $i, $w, $i, $hPen)
 Next
 Local $hPen2 = _GDIPlus_PenCreate(0, 3)
 ;Broder ;A = FF RGB=CCCCCC
 _GDIPlus_PenSetColor($hPen2, 0xFFCCCCCC)
 _GDIPlus_GraphicsDrawRect($graph_front, 0, 0, $w, $h, $hPen2)
 _GDIPlus_PenDispose($hPen)
 _GDIPlus_PenDispose($hPen2)
 _GDIPlus_GraphicsDispose($graph_front)
EndFunc   ;==>_CreateGradientImg

; Modified _Max() Function, directly included
; Author(s):       Jeremy Landes <jlandes at landeserve dot com>
Func _MyMax($nNum1, $nNum2)
 If Number($nNum1) > Number($nNum2) Then
  Return Number($nNum1)
 Else
  Return Number($nNum2)
 EndIf
EndFunc   ;==>_MyMax

#endregion Gradient

73 108 111 118 101 65 117 116 111 105 116

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...