Jump to content

Custom control don't work correctly


Recommended Posts

Hi,

I've been translated code from FreeBasic to AutoIt. And it didn't work correctly.

Please help me!

FreeBasic:

#Include Once "windows.bi"

#Define _RGB(r,g,b)  BGR(b,g,r)

CONST GRADIENT_FILL_RECT_H  =  0
CONST GRADIENT_FILL_RECT_V  =  1

Dim Shared hInstance As HINSTANCE

  ' This dll is located in Windows directory
DECLARE FUNCTION Gradientfill Lib "MSIMG32" ALIAS "GradientFill"           _
(hDC AS HDC, pVertex As PTRIVERTEX, dwNumVertex As Integer,pMesh AS PGRADIENT_RECT, dwNumMesh As Integer, dwMode  As Integer) As Integer

'********************************************************************
'     A FB Control Template  
'********************************************************************
Declare Function NiceButt(ByVal hWnd as HWND,byval Msg as UINT,byval wParam as WPARAM,byval lParam as LPARAM) as LRESULT 
Declare FUNCTION IsMouseOver (hWnd As HWND )As Integer
Declare SUB Draw_Gradient (hdc as HDC, x  As Integer, y  As integer, w  As integer, h  As Integer, r  As integer, g  As integer, b As integer)
Declare FUNCTION Register_NiceButt()As Integer

 ' 'Windows calls this function when the dll is loaded. 
/'Function DllMain alias "MAIN"(byval hModule as HMODULE,byval reason as Integer,byval lpReserved as LPVOID) as BOOL  

   Select case reason
      case DLL_PROCESS_ATTACH
         hInstance=hModule
         Register_NiceButt()
         MessageBox(GetActiveWindow(),"OK","OK",MB_OK)
         Return 0
      case DLL_PROCESS_DETACH
         '
   end select 
   return TRUE 

end function 
'/

FUNCTION Register_NiceButt()As Integer Export
DIM  wc AS WNDCLASSEX
DIM  szClassName As String
szClassName     =  "NiceButt"
wc.cbSize        =  SIZEOF(WNDCLASSEX)
wc.style         =  CS_HREDRAW OR CS_VREDRAW OR CS_GLOBALCLASS
wc.hInstance     =  GetmoduleHandle(0) 'hInstance
wc.hbrBackground =  Cast(HBRUSH,COLOR_BTNFACE+1)
wc.lpszClassName =  StrPtr(szClassName)
wc.lpfnWndProc   =  @NiceButt
wc.cbClsExtra    =  0
wc.cbWndExtra    =  0
wc.hIcon         =  0
wc.hCursor       =  0
wc.lpszMenuName  =  0
wc.hIconSm       =  0
FUNCTION         =  RegisterClassEx(@wc)
END FUNCTION

'********************************************************************
'                      Custom Control Procedure
'********************************************************************

Function NiceButt(ByVal hWnd as HWND,byval Msg as UINT,byval wParam as WPARAM,byval lParam as LPARAM) as LRESULT
STATIC  As Integer ButtDown,mouseover
STATIC Captured AS HWND

SELECT Case Msg

'**************************
  CASE WM_CREATE
'**************************

  DIM   Region AS HRGN
  DIM   Rct AS RECT
  DIM As Integer  x, y, w, h
  ButtDown = FALSE
  GetClientRect (hWnd,@Rct)      ' <<-- Get the size of our control
  x = Rct.left
  y = Rct.top
  w = Rct.right  - Rct.left
  h = Rct.bottom - Rct.top

  'Region = CreateRoundRectRgn(10,10,w,h, h * 0.90 , h * 0.90 )

  'SetWindowRgn (hWnd,Region,True)
  InvalidateRect(hWnd,0,0)
  'EXIT FUNCTION

' *******************
  CASE WM_PAINT
' *******************

  DIM   hDC       AS HDC
  DIM   ps        AS PAINTSTRUCT
  DIM   hPen      AS HPEN
  DIM   hBrush    AS HBRUSH
  DIM   hOldBrush AS HBRUSH
  DIM   Rct       AS RECT
  DIM   Size      AS SIZE
  DIM   T As ZString*2048
  DIM As Integer i
  DIM As Integer  XCtr
  DIM As Integer  YCtr
  DIM As Integer  x,y,w,h
  DIM As Integer  r,g,b

' *******************

  GetClientRect (hWnd,@Rct)      ' <<-- Get the size of our control
  x = Rct.left
  y = Rct.top
  w = Rct.right  - Rct.left
  h = Rct.bottom - Rct.top

  XCtr = (Rct.left + Rct.right)  / 2  ' Horizontal center of our ctrl
  YCtr = (Rct.top  + Rct.bottom) / 2  ' Vertical   center of our ctrl
  GetWindowText(hWnd,T ,255)          ' Grab a copy of control caption

'**********************************
'        Draw our control
'**********************************

  hDC = BeginPaint (hWnd, @ps)
  GetTextExtentPoint32(hDC, T , LEN(T),@Size)    ' Get caption size
  r = 30 : g = 90 : b = 90
  Draw_Gradient (hDC, x, y, w, h, r, g, b)
  SetBkMode (hDC,TRANSPARENT)

  IF ButtDown THEN
    SetTextColor(hDC,_RGB(255,0,0))
    TextOut(hDC, XCtr-(Size.cx/2)+1, YCtr-(Size.cy/2)+1,T,LEN(T))
  ELSE
    SetTextColor(hDC,_RGB(0,0,255))
    TextOut(hDC, XCtr-Size.cx/2, YCtr-Size.cy/2,T,LEN(T))
  END IF

  EndPaint (hWnd,@ps)
  'EXIT FUNCTION

'******************************
  CASE WM_LBUTTONUP
'******************************

  IF hWnd = Captured THEN
    DIM   hParent AS HWND
    ReleaseCapture()
    ButtDown = FALSE
    InvalidateRect(hWnd,0,0)
    hParent=GetParent(hWnd)
    SendMessage(hParent,WM_COMMAND,MAKELONG(GetWindowLong(hWnd,GWL_ID), BN_CLICKED),Cast(LONG,hWnd))
  END IF
  'EXIT FUNCTION

'******************************
  CASE WM_LBUTTONDOWN
'******************************

  SetCapture(hWnd)
  Captured = hWnd
  ButtDown = TRUE
  SetFocus (hWnd)
  InvalidateRect(hWnd,0,0)
  'EXIT FUNCTION

'******************************
  CASE WM_MOUSEMOVE
'******************************

  IF ButtDown THEN
    IF IsMouseOver(hWnd) THEN
      ButtDown = TRUE
      InvalidateRect(hWnd,0,0)
    ELSE
      ReleaseCapture()
      ButtDown = FALSE
      InvalidateRect(hWnd,0,0)
    END IF
  END IF
  'EXIT FUNCTION

'******************************
  CASE WM_MOVING
'******************************

  ReleaseCapture()
  ButtDown = FALSE
  InvalidateRect(hWnd,0,0)
  'EXIT FUNCTION

'******************************
  CASE WM_SIZE
'******************************

  ReleaseCapture()
  ButtDown = FALSE
  InvalidateRect(hWnd,0,0)
  'EXIT FUNCTION

END Select
Return DefWindowProc(hwnd,Msg,wparam,lparam)
END FUNCTION







SUB Draw_Gradient (hdc as HDC, x  As Integer, y  As integer, w  As integer, h  As Integer, r  As integer, g  As integer, b As integer)
DIM  Vert(2) AS TRIVERTEX
DIM   Rect    AS GRADIENT_RECT
'******************************************************
Vert (0).x      =    0
Vert (0).y      =    0
Vert (0).Red    =    65535-(65535-(r*256))
Vert (0).Green  =    65535-(65535-(g*256))
Vert (0).Blue   =    65535-(65535-(b*256))
Vert (0).Alpha  =    0
'******************************************************
Vert (1).x      =    w
Vert (1).y      =    h/2
Vert (1).Red    =    65535-(65535-(255*256))
Vert (1).Green  =    65535-(65535-(255*256))
Vert (1).Blue   =    65535-(65535-(255*256))
Vert (1).Alpha  =    0
'******************************************************
Rect.UpperLeft  =    0
Rect.LowerRight =    1
'******************************************************
Gradientfill(hdc,@Vert(0),2,@Rect,1,GRADIENT_FILL_RECT_V)
'******************************************************
Vert (0).x      =    0
Vert (0).y      =    h/2
Vert (0).Red    =    65535-(65535-(255*256))
Vert (0).Green  =    65535-(65535-(255*256))
Vert (0).Blue   =    65535-(65535-(255*256))
Vert (0).Alpha  =    0
'******************************************************
Vert (1).x      =    w
Vert (1).y      =    h
Vert (1).Red    =    65535-(65535-(r*256))
Vert (1).Green  =    65535-(65535-(g*256))
Vert (1).Blue   =    65535-(65535-(b*256))
Vert (1).Alpha  =    0
'******************************************************
Rect.UpperLeft  =    0
Rect.LowerRight =    1
'******************************************************
Gradientfill(hdc,@Vert(0),2,@Rect,1,GRADIENT_FILL_RECT_V)
END SUB

FUNCTION IsMouseOver (hWnd As HWND )As Integer
DIM  Rect As RECT
DIM  Pt   As POINT
GetWindowRect (hWnd, @Rect)
GetCursorPos(@Pt)
FUNCTION = PtInRect (@Rect, Pt)
END FUNCTION

 

AutoIt:

#include-once
#include <WinAPI.au3>
#include <WinAPIGdi.au3>
#include <WinAPISys.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
;~ #Include <windows.bi>

Global Const $_tagRect              = "struct;long left;long top;long right;long bottom;endstruct"
Global Const $_tagSize              = "struct;long cx;long cy;endstruct"
Global Const $_tagGradient_Rect     = "struct;ulong UpperLeft;ulong LowerRight;endstruct"
Global Const $_tagPoint             = "struct;long x;long y;endstruct"
Global Const $_tagTrivertex         = "struct;long x;long y;int Red;int Greed;int Blue;int Alpha;endstruct"
Global Const $_tagPaintStruct       = "struct;handle hdc;bool fErase;long left;long top;long right;long bottom;bool fRestore;bool fIncUpdate;byte rgbReserved[32];endstruct"
Global Const $_tagWNDCLASSEX        = "struct;uint cbSize;uint style;ptr lpfnWndProc;int cbClsExtra;int cbWndExtra;ptr hInstance;ptr hIcon;" & _
                                    "ptr hCursor; ptr hbrBackground; ptr lpszMenuName;ptr lpszClassName;ptr hIconSm;endstruct"
Global Const $CS_VREDRAW            = 0x0001, $CS_HREDRAW =0x0002, $CS_GLOBALCLASS = 0x4000
Global Const $BN_CLICKED            = 0
;~ #Define _RGB(r,g,b)  BGR(b,g,r)

Global Const $GRADIENT_FILL_RECT_H  =  0
Global Const $GRADIENT_FILL_RECT_V  =  1

;~ Dim Shared hInstance As HINSTANCE

;~   ' This dll is located in Windows directory
;~ DECLARE FUNCTION Gradientfill Lib "MSIMG32" ALIAS "GradientFill"           _
;~ (hDC AS HDC, pVertex As PTRIVERTEX, dwNumVertex As Integer,pMesh AS PGRADIENT_RECT, dwNumMesh As Integer, dwMode  As Integer) As Integer

;~ '********************************************************************
;~ '     A FB Control Template
;~ '********************************************************************
;~ Declare Function NiceButt(ByVal hWnd as HWND,byval Msg as UINT,byval wParam as WPARAM,byval lParam as LPARAM) as LRESULT
;~ Declare FUNCTION IsMouseOver (hWnd As HWND )As Integer
;~ Declare SUB Draw_Gradient (hdc as HDC, x  As Integer, y  As integer, w  As integer, h  As Integer, r  As integer, g  As integer, b As integer)
;~ Declare FUNCTION Register_NiceButt()As Integer

#cs
 ' 'Windows calls this function when the dll is loaded.
/'Function DllMain alias "MAIN"(byval hModule as HMODULE,byval reason as Integer,byval lpReserved as LPVOID) as BOOL

   Select case reason
      case DLL_PROCESS_ATTACH
         hInstance=hModule
         Register_NiceButt()
         MessageBox(GetActiveWindow(),"OK","OK",MB_OK)
         Return 0
      case DLL_PROCESS_DETACH
         '
   end select
   return True

end function
'/
#ce



#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 615, 437, 192, 124)

RegisterButton()
Global $Ctrl = _winapi_CreateWindowEx(0, "TestButton", "Test", BitOR($WS_VISIBLE, $WS_CHILD), 10, 10, 80, 30, $Form1)



ConsoleWrite(@CRLF & $Ctrl)
GUISetState(@SW_SHOW)

_WinAPI_UpdateWindow($Form1)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd


Func RegisterButton()

    Local $hDll = DllCallbackRegister('TestButtonProc', 'lresult', 'hwnd;uint;wparam;lparam')

;~  DIM  wc AS WNDCLASSEX
;~  DIM  szClassName As String
    Local $sClass = "TestButton"
    Local $wc = DllStructCreate($_tagWNDCLASSEX & ';wchar szClassName[' & (StringLen($sClass) + 1) & ']')

;~  szClassName         =  "NiceButt"

;~  wc.cbSize           =  SIZEOF(WNDCLASSEX)
    $wc.cbSize          =  DllStructGetPtr($wc, 'szClassName') - DllStructGetPtr($wc)

;~  wc.style            =  CS_HREDRAW OR CS_VREDRAW OR CS_GLOBALCLASS
    $wc.style           =  BitOR($CS_HREDRAW, $CS_VREDRAW, $CS_GLOBALCLASS)

;~  wc.hInstance        =  GetmoduleHandle(0) 'hInstance
    $wc.hInstance       =  _WinAPI_GetModuleHandle(0)

;~  wc.hbrBackground    =  Cast(HBRUSH,COLOR_BTNFACE+1) ;///
    $wc.hbrBackground   =  _WinAPI_CreateSolidBrush(_WinAPI_GetSysColor($COLOR_BTNFACE))

;~  wc.lpszClassName    =  StrPtr(szClassName)
    $wc.lpszClassName   =  DllStructGetPtr($wc, 'szClassName')

;~  wc.lpfnWndProc      =  @NiceButt
    $wc.lpfnWndProc     =  DllCallbackGetPtr($hDll)

;~  wc.cbClsExtra       =  0
    $wc.cbClsExtra      =  0

;~  wc.cbWndExtra       =  0
    $wc.cbWndExtra      =  0

;~  wc.hIcon            =  0
    $wc.hIcon           =  0

;~  wc.hCursor          =  0
    $wc.hCursor         =  0

;~  wc.lpszMenuName     =  0
    $wc.lpszMenuName    =  0

;~  wc.hIconSm          =  0
    $wc.hIconSm         =  0

    $wc.szClassName     =  $sClass

;~  FUNCTION            =  RegisterClassEx(@wc)
    Local $aRet = _WinAPI_RegisterClassEx($wc)
    Return $aRet
;~ END FUNCTION
EndFunc

;********************************************************************
;                      Custom Control Procedure
;********************************************************************

;~ Function NiceButt(ByVal hWnd as HWND,byval Msg as UINT,byval wParam as WPARAM,byval lParam as LPARAM) as LRESULT
Func TestButtonProc($hWnd, $iMsg, $wParam, $lParam)
    Static $bBtnDown, $bMouseOver, $hCaptured

    Switch $iMsg
        Case $WM_CREATE
            Local $Rct = DllStructCreate($_tagRect)
            Local $iX, $iY, $iW, $iH
            $bBtnDown = False
            GetClientRect($hWnd, $Rct)

            $iX = $Rct.left
            $iY = $Rct.top
            $iW = $Rct.right - $Rct.left
            $iH = $Rct.bottom - $Rct.top

            InvalidateRect($hWnd, 0, False)
;~          EXIT FUNCTION
;~          Return 0

        Case $WM_PAINT
            Local $ps ;= DllStructCreate($_tagPaintStruct)
            Local $hPen, $hBrush, $hOldBrush
            Dim $Rct = DllStructCreate($_tagRect)
            Dim $Size = DllStructCreate($_tagSize)
            Dim $T = "", $i
            Dim $XCtr, $YCtr
            Dim $iX, $iY, $iW, $iH
            Dim $iR, $iG, $iB

            GetClientRect($hWnd, $Rct)

            $iX = $Rct.left
            $iY = $Rct.top
            $iW = $Rct.right  - $Rct.left
            $iH = $Rct.bottom - $Rct.top

            $XCtr = ($Rct.left + $Rct.right)  / 2
            $YCtr = ($Rct.top  + $Rct.bottom) / 2
;~          GetWindowText(hWnd,T ,255)          ' Grab a copy of control caption

;~          DllCall("user32.dll", "int", "GetWindowTextW", "hwnd", $hWnd, "LPTSTR ", $T, "int", 255) ; not work
            $T = "Test"
            ;**********************************
            ;        Draw our control
            ;**********************************

            Local $hDC = BeginPaint($hWnd, $ps)
;~          GetTextExtentPoint32(hDC, T , LEN(T),@Size)    ' Get caption size
            GetTextExtentPoint32($hDC, $T, StringLen($T), $Size)

            $iR = 30
            $iG = 90
            $iB = 90
            Draw_Gradient($hDC, $iX, $iY, $iW, $iH, $iR, $iG, $iB)
            SetBkMode($hDC, $TRANSPARENT)

            If $bBtnDown Then
;~              SetTextColor(hDC,_RGB(255,0,0))
                SetTextColor($hDC, Dec(0xFF0000))
                TextOut($hDC, $XCtr-($Size.cx/2)+1, $YCtr-($Size.cy/2)+1, $T)

            Else
;~              SetTextColor(hDC,_RGB(0,0,255))
                SetTextColor($hDC, Dec(0x0000FF))
                TextOut($hDC, $XCtr-$Size.cx/2, $YCtr-$Size.cy/2, $T)


            EndIf
            EndPaint($hWnd, $ps)

;~          EXIT FUNCTION
;~          Return 0

        Case $WM_LBUTTONUP
            If $hWnd = $hCaptured Then
                ReleaseCapture()
                $bBtnDown = False
                InvalidateRect($hWnd, 0, 0)
                Local $hParent = GetParent($hWnd)
;~              SendMessage(hParent,WM_COMMAND,MAKELONG(GetWindowLong(hWnd,GWL_ID), BN_CLICKED),Cast(LONG,hWnd))
                DllCall("user32.dll", "LRESULT", "SendMessageW", _
                        "hwnd",     $hParent, _
                        "uint",     $WM_COMMAND, _
                        "WPARAM",   _WinAPI_MakeLong(_WinAPI_GetWindowLong($hWnd, $GWL_ID), $BN_CLICKED), _
                        "LPARAM",   $hWnd)
            EndIf
;~          EXIT FUNCTION
            Return 0

        Case $WM_LBUTTONDOWN
            SetCapture($hWnd)
            $hCaptured = $hWnd
            $bBtnDown = True
            SetFocus($hWnd)
            InvalidateRect($hWnd, 0, False)

;~          EXIT FUNCTION
            Return 0

        Case $WM_MOUSEMOVE
            If $bBtnDown Then
                If IsMouseOver($hWnd) Then
                    $bBtnDown = True
                    InvalidateRect($hWnd, 0, False)
                ELSE
                    ReleaseCapture()
                    $bBtnDown = False
                    InvalidateRect($hWnd, 0, False)
                EndIf
            EndIf

;~          EXIT FUNCTION
;~          Return 0

        Case $WM_MOVING
            ReleaseCapture()
            $bBtnDown = False
            InvalidateRect($hWnd, 0, False)

;~          EXIT FUNCTION
;~          Return 0

        Case $WM_SIZE
            ReleaseCapture()
            $bBtnDown = False
            InvalidateRect($hWnd, 0, False)

;~          EXIT FUNCTION
;~          Return 0

    EndSwitch
;~  Return _WinAPI_DefWindowProc($hWnd, $iMsg, $wParam, $lParam)
    Return DllCall("user32.dll", "lresult", "DefWindowProcW", "hwnd", $hWnd, "uint", $iMsg, "wparam", $wParam, _
            "lparam", $lParam)[0]
EndFunc

;~ SUB Draw_Gradient (hdc as HDC, x  As Integer, y  As integer, w  As integer, h  As Integer, r  As integer, g  As integer, b As integer)
Func Draw_Gradient($hDC, $iX, $iY, $iW, $iH, $iR, $iG, $iB)
;~  DIM  Vert(2) AS TRIVERTEX
    Local $Vert[2]
    $Vert[0] = DllStructCreate($_tagTrivertex)
    $Vert[1] = DllStructCreate($_tagTrivertex)
;~  DIM  Rect    AS GRADIENT_RECT
    Local $Rect = DllStructCreate($_tagGradient_Rect)

;~  Vert (0).x          =   0
    $Vert[0].x          =   0
;~  Vert (0).y          =   0
    $Vert[0].y          =   0
;~  Vert (0).Red        =   65535-(65535-(r*256))
    $Vert[0].Red        =   65535-(65535-($iR*256))
;~  Vert (0).Green      =   65535-(65535-(g*256))
    $Vert[0].Green      =   65535-(65535-($iG*256))
;~  Vert (0).Blue       =   65535-(65535-(b*256))
    $Vert[0].Blue       =   65535-(65535-($iB*256))
;~  Vert (0).Alpha      =   0
    $Vert[0].Alpha      =   0
;~ '******************************************************
;~  Vert (1).x          =    w
    $Vert[1].x          =    $iW
;~  Vert (1).y          =    h/2
    $Vert[1].y          =    $iH/2
;~  Vert (1).Red        =    65535-(65535-(255*256))
    $Vert[1].Red        =    65535-(65535-(255*256))
;~  Vert (1).Green      =    65535-(65535-(255*256))
    $Vert[1].Green      =    65535-(65535-(255*256))
;~  Vert (1).Blue       =    65535-(65535-(255*256))
    $Vert[1].Blue       =    65535-(65535-(255*256))
;~  Vert (1).Alpha      =    0
    $Vert[1].Alpha      =    0
;~ '******************************************************
;~  Rect.UpperLeft      =    0
    $Rect.UpperLeft     =    0
;~  Rect.LowerRight     =    1
    $Rect.LowerRight    =    1
;~ '******************************************************
;~  Gradientfill(hdc,@Vert(0),2,@Rect,1,GRADIENT_FILL_RECT_V)
    GradientFill($hDC, $Vert[0], 2, $Rect, 1, $GRADIENT_FILL_RECT_V)
;~ '******************************************************
;~  Vert (0).x          =    0
    $Vert[0].x          =    0
;~  Vert (0).y          =    h/2
    $Vert[0].y          =    $iH/2
;~  Vert (0).Red        =    65535-(65535-(255*256))
    $Vert[0].Red        =    65535-(65535-(255*256))
;~  Vert (0).Green      =    65535-(65535-(255*256))
    $Vert[0].Green      =    65535-(65535-(255*256))
;~  Vert (0).Blue       =    65535-(65535-(255*256))
    $Vert[0].Blue       =    65535-(65535-(255*256))
;~  Vert (0).Alpha      =    0
    $Vert[0].Alpha      =    0
;~ '******************************************************
;~  Vert (1).x          =    w
    $Vert[1].x          =    $iW
;~  Vert (1).y          =    h
    $Vert[1].y          =    $iH
;~  Vert (1).Red        =    65535-(65535-(r*256))
    $Vert[1].Red        =    65535-(65535-($iR*256))
;~  Vert (1).Green      =    65535-(65535-(g*256))
    $Vert[1].Green      =    65535-(65535-($iG*256))
;~  Vert (1).Blue       =    65535-(65535-(b*256))
    $Vert[1].Blue       =    65535-(65535-($iB*256))
;~  Vert (1).Alpha      =    0
    $Vert[1].Alpha      =    0
;~ '******************************************************
;~  Rect.UpperLeft      =    0
    $Rect.UpperLeft     =    0
;~  Rect.LowerRight     =    1
    $Rect.LowerRight    =    1
;~ '******************************************************
;~  Gradientfill(hdc,@Vert(0),2,@Rect,1,GRADIENT_FILL_RECT_V)
    GradientFill($hDC, $Vert[0], 2, $Rect, 1, $GRADIENT_FILL_RECT_V)
;~ END SUB
EndFunc

Func IsMouseOver($hWnd)
    Local $Rect = DllStructCreate($_tagRect)
    Local $Pt = DllStructCreate($_tagPoint)
    GetWindowRect ($hWnd, $Rect)
    GetCursorPos($Pt)
    Return PtInRect($Rect, $Pt)
EndFunc

Func _RGB($iR, $iG, $iB)
    Return ('0x' & Hex($iR, 2) & Hex($iG, 2) & Hex($iB, 2))
EndFunc

Func _BGR($iB, $iG, $iR)
    Return ('0x' & Hex($iB, 2) & Hex($iG, 2) & Hex($iR, 2))
EndFunc

Func InvalidateRect($hWnd, $tRECT = 0, $bErase = True)
    DllCall("user32.dll", "bool", "InvalidateRect", "hwnd", $hWnd, "struct*", $tRECT, "bool", $bErase)
EndFunc

Func SetFocus($hWnd)
    DllCall("user32.dll", "hwnd", "SetFocus", "hwnd", $hWnd)
EndFunc

Func PtInRect($tRect, $tPoint)
    Local $aRet = DllCall("user32.dll", "bool", "PtInRect", "ptr", DllStructGetPtr($tRect), "struct*", $tPoint)
    If IsArray($aRet) Then Return $aRet[0]
    Return False
EndFunc

Func GetWindowRect($hWnd, ByRef $tRect)
    DllCall("user32.dll", "bool", "GetWindowRect", "hwnd", $hWnd, "struct*", $tRect)
EndFunc

Func GetCursorPos(ByRef $tPoint)
    DllCall("user32.dll", "bool", "GetCursorPos", "struct*", $tPoint)
EndFunc

Func SetCapture($hWnd)
    DllCall("user32.dll", "hwnd", "SetCapture", "hwnd", $hWnd)
EndFunc

Func ReleaseCapture()
    DllCall("user32.dll", "bool", "ReleaseCapture")
EndFunc

Func SetTextColor($hDC, $iColor)
    DllCall("gdi32.dll", "INT", "SetTextColor", "handle", $hDC, "INT", $iColor)
EndFunc

Func BeginPaint($hWnd, ByRef $tPAINTSTRUCT)
    $tPAINTSTRUCT = DllStructCreate($tagPAINTSTRUCT)
    Local $aRet = DllCall('user32.dll', 'handle', 'BeginPaint', 'hwnd', $hWnd, 'struct*', $tPAINTSTRUCT)
    If @error Then Return SetError(@error, @extended, 0)
    Return $aRet[0]
EndFunc

Func GetTextExtentPoint32($hDC, $sText, $iTextLen, ByRef $tSize)
    DllCall("gdi32.dll", "bool", "GetTextExtentPoint32W", "handle", $hDC, "wstr", $sText, "int", $iTextLen, "struct*", $tSize)
EndFunc

Func GradientFill($hDC, $tVertex, $nVertex, $tMesh, $nMesh, $ulMode)
    DllCall("Msimg32.dll", "BOOL", "GradientFill", _
            "handle",   $hDC, _
            "struct*",  $tVertex, _
            "ulong",    $nVertex, _
            "struct*",  $tMesh, _
            "ulong",    $nMesh, _
            "ulong",    $ulMode)
EndFunc

Func GetParent($hWnd)
    Local $aResult = DllCall("user32.dll", "hwnd", "GetParent", "hwnd", $hWnd)
    If @error Then Return SetError(@error, @extended, 0)
    Return $aResult[0]
EndFunc

Func SetBkMode($hDC, $iBkMode)
    Local $aResult = DllCall("gdi32.dll", "int", "SetBkMode", "handle", $hDC, "int", $iBkMode)
    If @error Then Return SetError(@error, @extended, 0)

    Return $aResult[0]
EndFunc

Func EndPaint($hWnd, ByRef $tPAINTSTRUCT)
    Local $aRet = DllCall('user32.dll', 'bool', 'EndPaint', 'hwnd', $hWnd, 'struct*', $tPAINTSTRUCT)
    If @error Then Return SetError(@error, @extended, False)
    Return $aRet[0]
EndFunc

Func GetClientRect($hWnd, ByRef $tRect)
    Local $aRet = DllCall("user32.dll", "bool", "GetClientRect", "hwnd", $hWnd, "struct*", $tRect)
    If @error Or Not $aRet[0] Then Return SetError(@error + 10, @extended, 0)
    Return $tRect
EndFunc

Func TextOut($hDC, $iX, $iY, $sText, $iTextLen = Default)
    If $iTextLen = Default Then $iTextLen = StringLen($sText)
    DllCall('gdi32.dll', 'bool', 'TextOutW', 'handle', $hDC, 'int', $iX, 'int', $iY, 'wstr', $sText, 'int', $iTextLen)
EndFunc

 

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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By kcvinu
      Hi all,
      I want to show a color dialog box to user from my script. Is it possible ? I searched in help file but can't find anything related to a color dialog. 
    • By UEZ
      I'm searching for a way to set an icon for a system menu entry in the console window. I can add / remove entries but I didn't find a way to set an icon for the entry I made.
      Example:
      #AutoIt3Wrapper_Change2CUI=y #include <GDIPlus.au3> #include <GUIConstantsEx.au3> #include <GuiMenu.au3> #include <WindowsConstants.au3> Global $id_Test = 5000 Global $hConsole = HWnd(DllCall("kernel32.dll", "hwnd", "GetConsoleWindow")[0]) If Not $hConsole Then Exit HotKeySet("{ESC}", "_Exit") Global $hSysmenu = _GUICtrlMenu_GetSystemMenu($hConsole) Global $iCount = _GUICtrlMenu_GetItemCount ($hSysmenu) _GUICtrlMenu_InsertMenuItem($hSysmenu, $iCount, "Test", $id_Test) _GDIPlus_Startup() $hBitmap_GDI = _GDIPlus_BitmapCreateFromMemory(_Test(), True) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $hBitmap_GDI = ' & $hBitmap_GDI & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console $hBmp1 = _WinAPI_CreateSolidBitmap($hConsole, 0xFF0000, 16, 16) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $hBmp1 = ' & $hBmp1 & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console $hBmp2 = _WinAPI_CreateSolidBitmap($hConsole, 0x00FF00, 16, 16) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $hBmp2 = ' & $hBmp2 & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console ;ConsoleWrite(_GUICtrlMenu_SetItemBmp($hSysmenu, $id_Test, $hBitmap_GDI, False) & @CRLF) ConsoleWrite(_GUICtrlMenu_SetItemBitmaps($hSysmenu, $id_Test, $hBmp1, $hBmp2, False) & ", " & @error & @CRLF) ;_GUICtrlMenu_SetItemBmp($hSysmenu, $id_Test, 8, False) ;set the default close icon _GDIPlus_Shutdown() Do Sleep(1000) Until False Func _Exit() ConsoleWrite("Bye..." & @CRLF) _GUICtrlMenu_DeleteMenu ($hSysmenu, $id_Test, False) _WinAPI_DeleteObject($hBitmap_GDI) _WinAPI_DeleteObject($hBmp1) _WinAPI_DeleteObject($hBmp2) Exit EndFunc ;Code below was generated by: 'File to Base64 String' Code Generator v1.20 Build 2020-06-05 Func _Test($bSaveBinary = False, $sSavePath = @ScriptDir) Local $Test $Test &= 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAACXBIWXMAAA3XAAAN1wFCKJt4AAADKElEQVQ4yz2RT2gcdRTHP7+Z36y7yzZZdvJv22i0rBpiAwnWCMZrUCGIBxEvpQSEHAQhePLkyUKg6Em9iZCcahH0pFYxkIqiIdHKYmPUUHcDmU2TuJnZzc7s/H7PQ5M8ePAu7/M+X56y1hKGYY+IOEDqOI6TzWY1gFIKEcF1XRzHUWmaJnF8bHBy5pfbnz9fb0hBh2F4bm5ubjWKogudTqdVqVT0/Py853kehUKBQqHA2tqaVKtVfenS2P6HH396PDri9D9+cdC/8dkXNW2t9fb29spRFPmtVqtULBaVMYZ8Pk82m8X3farVKjdv3qCn+FbpnFuXfNpQkkwlX33z3RtaKYXW2mqtcV1XtNZks9kzQCaTIeNp4sSh8rAnzSe0HO5bt1arHR51y79pEcEYo05Aylp7OqOUIk0TRp+aYO5Ki3B3VR3s70pictzZGlC5jHW0UgprLVEU4Xke29vbLCwsoLXGcRRxYjk/2MtLz2X4/tvbaO3y9XoPlUe7CI7VAMYYPM+jXC5jrcUYAwipUeSzHhV/k59+bFK5OMytOxco9ezQTQ2A4ujoqDQ+Ph4sLS2JiNiDgwM5PDyQ3SCQ/5ptubv6rsy9XJIfvnxbNv/8W+7d+9deu/aeTE9PB2EYljSAiKje3l7SNCUIAkDQXp77f3zE4uJ1Xnv9KoWRK+zs1DlfHiCXy2OtBcA5AdDpdHBdl4F+n77BEdz2z3zw/iKvvHqVZ194h0wmz0B/Ca094jjmtE4NEBFarRa7jfvkHgqp/rrKxOQzjF6+wtY/u4g9Jo5TfL+EiGCtxVqLFhGUUhhjsNZgrcvqrU948rFhKlNvEkUdtNvBoFEqPVtUSqGUQiulaDabtNttPC/D5t3fqTU0Y5dfpNFogFhEOLsaxzHtdpsgCNT6+rrSSZIwOztrhoeH' $Test &= 'qdfrRmutJp6e4q+tKsbYs3in7+7r65OhoSE9MzNjkiRBt9ttkiRxjTHkcjl3bGxMPYhjSdP0rLvdLt1uF9d1BcDzPHdyclL0yspKuLGxcT0IgkeKxaINgsADXBFxAefEQB4ImNT3/W4Yhk6z2awtLy9H/wPnrsNEnFPl4QAAAABJRU5ErkJggg==' Local $bString = _WinAPI_Base64Decode($Test) If @error Then Return SetError(1, 0, 0) $bString = Binary($bString) If $bSaveBinary Then Local Const $hFile = FileOpen($sSavePath & "\AutoSave_16x16_04.png", 18) If @error Then Return SetError(2, 0, $bString) FileWrite($hFile, $bString) FileClose($hFile) EndIf Return $bString EndFunc ;==>_Test Func _WinAPI_Base64Decode($sB64String) Local $aCrypt = DllCall("Crypt32.dll", "bool", "CryptStringToBinaryA", "str", $sB64String, "dword", 0, "dword", 1, "ptr", 0, "dword*", 0, "ptr", 0, "ptr", 0) If @error Or Not $aCrypt[0] Then Return SetError(1, 0, "") Local $bBuffer = DllStructCreate("byte[" & $aCrypt[5] & "]") $aCrypt = DllCall("Crypt32.dll", "bool", "CryptStringToBinaryA", "str", $sB64String, "dword", 0, "dword", 1, "struct*", $bBuffer, "dword*", $aCrypt[5], "ptr", 0, "ptr", 0) If @error Or Not $aCrypt[0] Then Return SetError(2, 0, "") Return DllStructGetData($bBuffer, 1) EndFunc ;==>_WinAPI_Base64Decode  
      You must compile and run it to see the menu entry in the console window.

       
      Any idea?
    • By kcvinu
      Hi all,
      I am creating a GUI library in Nim with win32 API functions. Even though the syntax is similar to Python, coding is almost similar to C in Nim. 
      I can change the back color of the button in NM_CUSTOMDRAW. But I can't change the button text color. here is my pseudo code.  
      proc setBtnBackColor*(me : Button, lp: LPNMCUSTOMDRAW ) = if lp.uItemState and CDIS_SELECTED : #------------------ btn clicked # Here i am changing the button color with SelectObject & FillRect. elif lp.uItemState and CDIS_HOT : # ----------------Mouse over # Here i am changing the button's mouse hover color with SelectObject & FillRect. else: # -------------------------Default color set # Here i am changing the button's default color with SelectObject & FillRect. # Here i tried---> SetTextColor(lp.hdc, RGB(102, 255, 51) ) #------- But no luck. #------------------------------------------------------------------------- Please guide me. What i am doing wrong here ?  I am returning "CDRF_SKIPDEFAULT" after calling this function.
      Note : I am using subclassed button. So when the parent window receives WM_NOTIFY message, it sends that to my button's WndProc. There i am handling the message.
    • By UEZ
      Here another approach to check if a script was already started using atoms and semaphores.
       
      Atom:
      #include <MsgBoxConstants.au3> Global $iSingleton = Singleton() If Not $iSingleton Then Exit MsgBox($MB_TOPMOST, "Singleton Test", "Process is already running!") EndIf MsgBox($MB_TOPMOST, "Singleton Test", "Singleton atom initialized: " & $iSingleton) Singleton_Delete($iSingleton) ; #FUNCTION# ==================================================================================================================== ; Name ..........: Singleton ; Description ...: Checks if the script has been started already. ; Syntax ........: Singleton([$sOccurrenceName = @ScriptFullPath]) ; Parameters ....: $sOccurrenceName - [optional] a string value. Default is @ScriptFullPath. ; Return values .: If the function succeeds, the return value is the newly created atom or 0 else error is set and false is returned. ; Author ........: UEZ ; Modified ......: ; Remarks .......: If Singleton finds the atom it will return 0 and the atom token will be set to extended macro. It can be used to get the atom string using _WinAPI_AtomGlobalGetName. ; Related .......: ; Link ..........: https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-globalfindatomw ; https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-globaladdatomw ; Example .......: No ; =============================================================================================================================== Func Singleton($sOccurrenceName = @ScriptFullPath) Local $iFind = _WinAPI_AtomGlobalFind($sOccurrenceName) If @error Then Return SetError(1, 0, False) If $iFind Then Return SetExtended($iFind, 0) Local $iAtom = _WinAPI_AtomGlobalAdd($sOccurrenceName) If @error Then Return SetError(2, 0, False) Return $iAtom EndFunc ;==>Singleton ; #FUNCTION# ==================================================================================================================== ; Name ..........: Singleton_Delete ; Description ...: Deletes the atom generated by the first started script. ; Syntax ........: Singleton_Delete($iAtom) ; Parameters ....: $iAtom - an integer value which was generated by Singleton ; Return values .: True if successful else false. ; Author ........: UEZ ; Modified ......: ; Remarks .......: Don't forget to call Singleton_Delete before first started script ends. ; Related .......: ; Link ..........: https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-globaldeleteatom ; Example .......: No ; =============================================================================================================================== Func Singleton_Delete($iAtom) _WinAPI_AtomGlobalDelete($iAtom) If @error Then Return SetError(1, 0, False) Return True EndFunc ;==>Singleton_Delete ;internal functions Func _WinAPI_AtomGlobalFind($sAtomString) Local $aReturn = DllCall("kernel32.dll", "short", "GlobalFindAtomW", "wstr", $sAtomString) If @error Then Return SetError(1, 0, -1) Return $aReturn[0] EndFunc ;==>_WinAPI_AtomGlobalFind Func _WinAPI_AtomGlobalAdd($sAtomString) Local $aReturn = DllCall("kernel32.dll", "short", "GlobalAddAtomW", "wstr", $sAtomString) If @error Then Return SetError(1, 0, -1) Return $aReturn[0] EndFunc ;==>_WinAPI_AtomGlobalAdd Func _WinAPI_AtomGlobalDelete($nAtom) Local $aReturn = DllCall("kernel32.dll", "short", "GlobalDeleteAtom", "short", $nAtom) If @error Then Return SetError(1, 0, -1) Return $aReturn[0] = 0 EndFunc ;==>_WinAPI_AtomGlobalDelete Func _WinAPI_AtomGlobalGetName($nAtom, $iBufferSize = 512) Local $tBufferAtom = DllStructCreate("wchar name[" & $iBufferSize & "]") Local $aReturn = DllCall("kernel32.dll", "uint", "GlobalGetAtomNameW", "short", $nAtom, "struct*", $tBufferAtom, "int", $iBufferSize) If @error Or Not $aReturn[0] Then Return SetError(1, 0, -1) Return $tBufferAtom.name EndFunc ;==>_WinAPI_AtomGlobalGetName  
      Semaphore:
      #include <MsgBoxConstants.au3> #include <WinAPIError.au3> Global $iSingleton = Singleton("&]8h/x87</htFV4-K*&.b.w~") If Not $iSingleton Then Exit MsgBox($MB_TOPMOST, "Singleton Test", "Process is already running!") EndIf MsgBox($MB_TOPMOST, "Singleton Test", "Singleton Semaphore initialized: " & $iSingleton) ; #FUNCTION# ==================================================================================================================== ; Name ..........: Singleton ; Description ...: Checks if the script has been started already. ; Syntax ........: Singleton($sOccurrenceName) ; Parameters ....: $sOccurrenceName - a string value which will be used to create the semaphore handle. ; Return values .: True if Singleton started the first time. False if script was already started ; Author ........: UEZ ; Modified ......: ; Remarks .......: The system closes the handle automatically when the process terminates. The semaphore object is destroyed when its last handle has been closed. ; Related .......: ; Link ..........: https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createsemaphorea ; Example .......: No ; =============================================================================================================================== Func Singleton($sOccurrenceName) If StringLen($sOccurrenceName) > 260 Then $sOccurrenceName = StringLeft($sOccurrenceName, 260) Local $aReturn = DllCall("kernel32.dll", "handle", "CreateSemaphoreA", "ptr", Null, "long", 0, "long", 1, "str", $sOccurrenceName) If @error Or Not $aReturn[0] Then Return SetError(1, 0, -1) Return SetExtended($aReturn[0], $aReturn[0] And _WinAPI_GetLastErrorMessage() = "The operation completed successfully.") EndFunc ;==>Singleton  
      Just start the script twice to see if it works.
      The disadvantage of using atoms is that atoms have a memory that means when your app is crashing or you forgot to delete the atom then the atom does still have the $sOccurrenceName saved and thus Singleton will not work if you use the same same value for $sOccurrenceName.
      With semaphore you don't have this issue.
       
      Thanks to jj2007 and SARG.
    • By BlueSkyMemory
      Hello guys! I'm a rookie in AutoIt lol.
      I've tried to looking up in MSDN and the UDFs, but it can only get the GUID of a usual partition and with the GUID to control it. Now I have no ways😥.
      Thanks a lot for your help!
×
×
  • Create New...