Jump to content

Win32 Api - Application Example


Yuraj
 Share

Recommended Posts

Hi all,

I have created for you small application in autoit that uses Windows API functions to the maximum (no autoit gui). It's a text reader, you can read text files. It's only show of win32 api use. :D

Thanks for _WinAPI_RegisterClassEx UDF (originally by amel27, updated by Kip)

; Small AutoIt Application that uses Windows API
; Written by Yuraj
#NoTrayIcon

#include <WinAPI.au3>
#include <_RegisterClassEx.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <FontConstants.au3>

AutoItSetOption("MustDeclareVars", 1)

; Window definitions
Const $WinWidth = 370
Const $WinHeight = 350
Const $WinXPos = (@DesktopWidth / 2) - ($WinWidth / 2)
Const $WinYPos = (@DesktopHeight / 2) - ($WinHeight / 2)
Const $WinTitle = "Win32 Application - Text reader"
Const $WinClass = "mainapp"
Const $WinIcon = _WinAPI_LoadIcon(_WinAPI_GetModuleHandle("shell32.dll"), 13)
; Windows handles
Global $hwnd, $edit1, $btn1, $btn2
; Fonts
Global $fnt1

; Register class, Create the window
Local $retVal = _WinAPI_RegisterClassEx($WinClass, "WindowCallback", $WinIcon, 0, _WinAPI_GetSysColor($COLOR_BTNFACE), BitOR($CS_DEFAULTSTYLE, $CS_DROPSHADOW)) ;
If $retVal == 0 Then ; If registerclass fails
    MsgBox(16, "Error", "Error while registering window class!")
    Exit
EndIf

; Create windows/controls
$hwnd = _WinAPI_CreateWindowEx(0, $WinClass, $WinTitle, BitOR($WS_OVERLAPPED,$WS_SYSMENU, $WS_MINIMIZEBOX), $WinXPos, $WinYPos, $WinWidth, $WinHeight, 0)
$btn1 = _WinAPI_CreateWindowEx(0, "Button", "Open file ...", BitOR($WS_VISIBLE, $WS_CHILD), 25, 270, 100, 30,$hwnd)
$btn2 = _WinAPI_CreateWindowEx(0, "Button", "Exit", BitOR($WS_VISIBLE, $WS_CHILD), 235, 270, 100, 30, $hwnd)
$edit1 = _WinAPI_CreateWindowEx($WS_EX_CLIENTEDGE, "Edit", "", BitOR($WS_VISIBLE, $WS_CHILD, $WS_VSCROLL, $ES_MULTILINE, $ES_AUTOVSCROLL), 5, 5, $WinWidth - 15, $WinHeight - 100, $hwnd)

; Set controls identifiers
_WinAPI_SetWindowLong($btn1,$GWL_ID,150)
_WinAPI_SetWindowLong($btn2,$GWL_ID,160)

; Set (controls) fonts
$fnt1 = _CreateFont("MS Sans Serif", 15)
_WinAPI_SetFont($btn1, $fnt1)
_WinAPI_SetFont($btn2, $fnt1)
_WinAPI_SetFont($edit1, $fnt1)

; Set focus to edit
_WinAPI_SetFocus($edit1)

; Show window
_WinAPI_ShowWindow($hwnd)
_WinAPI_UpdateWindow($hwnd)

; Main loop that keep application opened
While 1
    Sleep(100)
WEnd

;=================================================================;
; WINDOW CALLBACK ...
;=================================================================;

Func WindowCallback($_hwnd, $iMsg, $wParam, $lParam)
    Local $iNC, $iID
    Switch $iMsg
        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        Case $WM_CLOSE
            ; Show message on closing
            If MsgBox(48 + 4, $WinTitle, "Do you want really exit?", 0, $hwnd) <> 6 Then Return 0
            ; Call destructor and then exit main thread
            FinalizeApp()
            Exit
            ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        Case $WM_COMMAND
            $iNC = _WinAPI_HiWord($wParam)
            $iID = _WinAPI_LoWord($lParam)

            Switch $iNC
                Case $BN_CLICKED ; When is control clicked
                    Switch _WinAPI_GetDlgCtrlID($iID)
                        Case _WinAPI_GetDlgCtrlID($btn1)
                            BtnOpenFileClick()
                        Case _WinAPI_GetDlgCtrlID($btn2)
                            BtnExitClick()
                    EndSwitch
            EndSwitch
            ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    EndSwitch

    Return _WinAPI_DefWindowProc($_hwnd, $iMsg, $wParam, $lParam)
EndFunc   ;==>WindowCallback

Func FinalizeApp()
    _WinAPI_DeleteObject($fnt1)
    _WinAPI_DestroyWindow($hwnd)
    _WinAPI_UnregisterClass($WinClass)
EndFunc   ;==>FinalizeApp

Func _CreateFont($fontName, $height = 16, $style = $FW_NORMAL, $italic = False, $underline = False, $strikeout = False)
    Local $hFont = _WinAPI_CreateFont($height, 0, 0, 0, $style, $italic, $underline, $strikeout, $DEFAULT_CHARSET, _
            $OUT_DEFAULT_PRECIS, $CLIP_DEFAULT_PRECIS, $DEFAULT_QUALITY, $DEFAULT_PITCH, $fontName)
    Return $hFont
EndFunc   ;==>_CreateFont

;=================================================================;
; WINDOW EVENTS
;=================================================================;

Func BtnOpenFileClick()
    Local $ret = _WinAPI_GetOpenFileName("", "Text files (*.txt)|All files (*.*)", ".", "", "", 1, 0, 0, $hwnd)
    If ($ret[0] > 0) Then
        Local $path = $ret[1] & "\" & $ret[2]
        
        Local $file = _WinAPI_CreateFile($path, 2, 2)
        Local $buf = DllStructCreate("byte[" & _WinAPI_GetFileSizeEx($file) & "]")
        Local $i = 0

        _WinAPI_ReadFile($file, DllStructGetPtr($buf), _WinAPI_GetFileSizeEx($file), $i)
        
        ; Close file handle
        _WinAPI_CloseHandle($file)
        _WinAPI_SetWindowText($edit1, BinaryToString(DllStructGetData($buf, 1)))
    EndIf
EndFunc   ;==>BtnOpenFileClick

Func BtnExitClick()
    FinalizeApp()
    Exit
EndFunc   ;==>BtnExitClick
Edited by Yuraj

ShellExecute("http://www.yuraj.ucoz.com")ProcessClose(@AutoItPID)

Link to comment
Share on other sites

Where do we find _WinAPI_LoadIcon?

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

You could replace it with this one (msdn says, LoadImage is better)

_WinAPI_LoadImage(_WinAPI_GetModuleHandle("shell32.dll"), "#13", $IMAGE_ICON, 0, 0, 0)

//Edit: sorry, had $IMAGE_BITMAP instead of $IMAGE_ICON

Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

Where do we find _WinAPI_LoadIcon?

Ah, I forgot it:

Func _WinAPI_LoadIcon($hInstance, $icon)
    Local $aIcon = DllCall("user32.dll", "hwnd", "LoadIcon", "hwnd", $hInstance, "int", $icon)
    Return $aIcon[0]
EndFuncoÝ÷ Ù¬.'«m秲ê^®ÇyÖò¶¡§H¨~éܶ*'jëh×6_WinAPI_LoadImage(_WinAPI_GetModuleHandle("shell32.dll"), "#13", $IMAGE_ICON , 0, 0, 0)
Edited by Yuraj

ShellExecute("http://www.yuraj.ucoz.com")ProcessClose(@AutoItPID)

Link to comment
Share on other sites

  • 9 years later...

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