Jump to content

Draw rounded rectangle


Recommended Posts

Hi,

i try to create a simple rounded rectangle which is filled with a specific color and assign it to a pic control with 

_SendMessage($hPic, $STM_SETIMAGE, 0, $hBitmap)

I searched for a solution but only found self made functions which require WM_PAINT, but i try to get it work only with _SendMessage($hPic, $STM_SETIMAGE, 0, $hBitmap) and _WINAPI_RoundRect().

Here i edited the _WINAPI_RoundRect() example from help file but don't understand why line 15 and 16 is needed and the part with merge bitmap. Can someone give a solution without line 15, 16 only a simple draw and assignment to pic control please?

#include <APIGdiConstants.au3>
#include <FontConstants.au3>
#include <StaticConstants.au3>
#include <WinAPIGdi.au3>
#include <WindowsConstants.au3>

; Create GUI
Local $hForm = GUICreate('Test', 400, 400)
Local $idPic = GUICtrlCreatePic('', 0, 0, 60, 60)
Local $hPic = GUICtrlGetHandle($idPic)

; Create bitmap
Local $hDev = _WinAPI_GetDC($hPic)
Local $hDC = _WinAPI_CreateCompatibleDC($hDev)
Local $hSource = _WinAPI_CreateCompatibleBitmapEx($hDev, 400, 400, _WinAPI_SwitchColor(_WinAPI_GetSysColor($COLOR_3DFACE)))
Local $hSv = _WinAPI_SelectObject($hDC, $hSource)

; Draw objects
Local $hOldBrush = _WinAPI_SelectObject($hDC, _WinAPI_GetStockObject($DC_BRUSH))
Local $hOldPen = _WinAPI_SelectObject($hDC, _WinAPI_GetStockObject($DC_PEN))
_WinAPI_SetDCBrushColor($hDC, 0x990404)
_WinAPI_SetDCPenColor($hDC, 0x990404)
$tRECT = _WinAPI_CreateRect(20, 20, 60, 60)
;~ _WinAPI_OffsetRect($tRECT, 220, 279)
_WinAPI_RoundRect($hDC, $tRECT, 20, 20)

; Merge bitmap
Local $hBitmap = _WinAPI_CreateCompatibleBitmap($hDev, 60, 60)
$hBrush = _WinAPI_SelectObject($hDC, $hOldBrush)
_WinAPI_DeleteObject($hBrush)
Local $hPen = _WinAPI_SelectObject($hDC, $hOldPen)
_WinAPI_DeleteObject($hPen)
_WinAPI_SelectObject($hDC, $hBitmap)
_WinAPI_DrawBitmap($hDC, 0, 0, $hSource, $MERGECOPY)
_WinAPI_ReleaseDC($hPic, $hDev)
_WinAPI_SelectObject($hDC, $hSv)
_WinAPI_DeleteObject($hSource)
_WinAPI_DeleteDC($hDC)

; Set bitmap to control
_SendMessage($hPic, $STM_SETIMAGE, 0, $hBitmap)
$hObj = _SendMessage($hPic, $STM_GETIMAGE)
If $hObj <> $hBitmap Then
    _WinAPI_DeleteObject($hBitmap)
EndIf

GUISetState(@SW_SHOW)

Do
Until GUIGetMsg() = -3

Edit: Line 15, 16 is

Local $hSource = _WinAPI_CreateCompatibleBitmapEx($hDev, 400, 400, _WinAPI_SwitchColor(_WinAPI_GetSysColor($COLOR_3DFACE)))
Local $hSv = _WinAPI_SelectObject($hDC, $hSource)
Edited by Trolleule
Link to comment
Share on other sites

Try this instead:

#include <GDIPlus.au3>

_GDIPlus_Startup()
Global Const $STM_SETIMAGE = 0x0172
; Create GUI
Local $hForm = GUICreate('Test', 400, 400)
Local $idPic = GUICtrlCreatePic('', 0, 0, 60, 60)
Local $hPic = GUICtrlGetHandle($idPic)
Local $hGDIBmp = _GDIPlus_CreateBitmapRoundCornerRect()
_WinAPI_DeleteObject(GUICtrlSendMsg($idPic, $STM_SETIMAGE, $IMAGE_BITMAP, $hGDIBmp))
GUISetState(@SW_SHOW)

Do
Until GUIGetMsg() = -3
_WinAPI_DeleteObject($hGDIBmp)
_GDIPlus_Shutdown()

Func _GDIPlus_CreateBitmapRoundCornerRect($iW = 60, $iH = 60, $iColorBg = 0xFFFFFFFF, $iColorBorder = 0xFF000000, $iRadius = 8, $bGDIBmp = 1)
    Local Const $iX = 0, $iY = 0, $iPenSize = 1
    Local Const $iWidth = $iW - $iPenSize, $iHeight = $iH - $iPenSize
    Local Const $hBitmap = _GDIPlus_BitmapCreateFromScan0($iW, $iH)
    Local Const $hCtxt = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    _GDIPlus_GraphicsSetSmoothingMode($hCtxt, $GDIP_SMOOTHINGMODE_HIGHQUALITY)
    _GDIPlus_GraphicsSetTextRenderingHint($hCtxt, $GDIP_TEXTRENDERINGHINT_ANTIALIASGRIDFIT)
    Local Const $hPath = _GDIPlus_PathCreate()
    _GDIPlus_PathAddArc($hPath, $iX + $iWidth - ($iRadius * 2), $iY, $iRadius * 2, $iRadius * 2, 270, 90)
    _GDIPlus_PathAddArc($hPath, $iX + $iWidth - ($iRadius * 2), $iY + $iHeight - ($iRadius * 2), $iRadius * 2, $iRadius * 2, 0, 90)
    _GDIPlus_PathAddArc($hPath,  $iX, $iY + $iHeight - ($iRadius * 2), $iRadius * 2, $iRadius * 2, 90, 90)
    _GDIPlus_PathAddArc($hPath,  $iX, $iY, $iRadius * 2, $iRadius * 2, 180, 90)
    _GDIPlus_PathCloseFigure($hPath)

    Local Const $hBrush = _GDIPlus_BrushCreateSolid($iColorBg)
    _GDIPlus_GraphicsFillPath($hCtxt, $hPath, $hBrush)
    Local Const $hPen = _GDIPlus_PenCreate($iColorBorder, $iPenSize)
    _GDIPlus_GraphicsDrawPath($hCtxt, $hPath, $hPen)

    _GDIPlus_PathDispose($hPath)
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_PenDispose($hPen)
    _GDIPlus_GraphicsDispose($hCtxt)
    If $bGDIBmp Then
        Local $hGDIBmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)
        _GDIPlus_BitmapDispose($hBitmap)
        Return $hGDIBmp
    EndIf
    Return $hBitmap
EndFunc

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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