Trở về
Tham gia nhóm m Autoit đ đưc hưng dn và gii đáp trc tiếp : http://fb.com/groups/autoitscript
Tin tức công nghệ  -  Thủ thuật lập trình

Wednesday, October 2, 2013

Hiệu ứng chữ "rực lửa" cho GUI - Autoit


Toàn màn hìnhIn bài viết

Đây là UDF mình thiết kế riêng cho GUI dựa trên thư viện prospeed và UDF Co-Proc xử lý đa luồng tác vụ trong Autoit.

Demo đơn giản chương trình tải gói cài đặt và scite cho autoit mà mình đã giới thiệu ở bài đầu tiên, đây là mã nguồn gốc, các bạn cố gắng đọc và áp dụng được để sử dụng (không yêu cầu phải hiểu code mà chỉ cần biết cách sử dụng là đủ) :

Mã nguồn : AutoitDownloader.au3

#NoTrayIcon
#include <GUIConstants.au3>
#include "Prospeed30.au3"
#include "CoProc.au3"

Global $rThread[1] , $button , $progress ,$check1,$check2
$check1=False
$check2=False


$hGUI = GUICreate("Autoit Downloader", 501, 401, 192, 125)
GUISetBkColor(0x74B824)
$button = GUICtrlCreateButton("DOWNLOAD", 156, 312, 170, 50)
$progress1 = GUICtrlCreateProgress(68, 206, 348, 20)
$Progress2 = GUICtrlCreateProgress(68, 262, 348, 20)
$Label1 = GUICtrlCreateLabel("Download SciTE4AutoIt3.exe", 72, 184, 200, 17)
GUICtrlSetColor(-1, 0xFFFFFF)
$Label2 = GUICtrlCreateLabel("Download autoit-v3-setup.exe", 72, 240, 200, 17)
GUICtrlSetColor(-1, 0xFFFFFF)
;heading($frame_width,$frame_height,$text_set_left,$text_height,$text_font)
$heading= _CoProc("heading(500,100,'Yagami Raito:80|MultiThreads:80|With progresses:50|Example:120',60,'Algerian')")

GUISetState()
_CoProcReciver("Recive")




While 1

$msg = GUIGetMsg()
        Select
            Case $msg = $button
                GUICtrlSetData($button,'Downloading')
                GUICtrlSetState($button,$GUI_DISABLE)
$iPidSmall = _CoProc("Small")
$iPidBig = _CoProc("Big")

           Case $msg = $GUI_EVENT_CLOSE
              FreeAllImages()
                Exitloop
            Case Else
        EndSelect

WEnd
ProcessClose($heading)
Exit



Func heading($frame_width,$frame_height,$text_set_left,$text_height,$text_font)
    Local $text_set[10],$text_left[10],$array[10]
    $array[0]=0
$text_set_left=StringSplit($text_set_left,"|")
For $i=1 to $text_set_left[0]
    $text_set_left_split=StringSplit($text_set_left[$i],":")
    $text_set[$i]=$text_set_left_split[1]
    $text_left[$i]=$text_set_left_split[2]
Next
$hDC = GetHDC() ;get window DC

$createbitmap = CreateImage($frame_width,$frame_height) ;create empty bitmap (default = black)
PaintImage($hDC,0,0,$frame_width,$frame_height,$createbitmap,0,0,0) ;copy empty bitmap to window

CreateBuffer($frame_width,$frame_height) ;create buffer 800x600 for WM_PAINT
SetBuffer($createbitmap) ;fill buffer with empty bitmap

$load  = LoadFileImage("fire.jpg") ;load jpg for text effect

$imagedata = CreateImage($frame_width,$frame_height) ;create empty bitmap for text imagedata
$maskdata  = CreateImage($frame_width,$frame_height) ;create empty bitmap for text maskimage
ColorFillImage($maskdata,0,0,$frame_width,$frame_height,0xFFFFFF) ;fill maskimage with white color

$w    = GetInfosImage($maskdata, 2) ;get bitmap width
$h    = GetInfosImage($maskdata, 3) ;get bitmap height
$size = GetInfosImage($maskdata, 7)+54 ;get bitmap total bytes used in memory (+54 = bitmap header)

SetText($maskdata ,$text_set[1],$text_left[1],($frame_height-$text_height)/2,0x000000,0xFFFFFF,$text_height,$frame_width,$text_font) ;set text on maskimage
$array[1] = InitFx($imagedata) ;create array from imagedata
$array[2] = InitFx($maskdata) ;create array from maskimage
Antialiasing(0,0,0,$array[2],-1,0,1) ;antialiasing text on maskimage

For $i=2 to $text_set_left[0]
$imagedata = CreateImage($frame_width,$frame_height) ;create second empty bitmap for text imagedata
$maskdata  = CreateImage($frame_width,$frame_height) ;create second empty bitmap for text maskimage
ColorFillImage($maskdata,0,0,$frame_width,$frame_height,0xFFFFFF) ;fill maskimage with white color

SetText($maskdata,$text_set[$i],$text_left[$i],($frame_height-$text_height)/2,0x000000,0xFFFFFF,$text_height,$frame_width,$text_font) ;set text on maskimage
$array[2*$i-1] = InitFx($imagedata) ;create array from imagedata
$array[2*$i] = InitFx($maskdata) ;create array from maskimage

Antialiasing(0,0,0,$array[2*$i],-1,0,1) ;antialiasing text on maskimage
Next

While 1

    For $r=1 to $text_set_left[0]-1
    $copy = CreateImage($frame_width,$frame_height) ;create empty bitmap for effect
    $png1 = FXToPng(0,$array[2*$r-1],$array[2*$r],$w,$h,$size) ;create png from imagedata and maskimage
    $png2 = FXToPng(0,$array[2*($r+1)-1],$array[2*($r+1)],$w,$h,$size) ;create png from imagedata and maskimage

    For $i = 1 to 200 Step 1
        PaintImage($copy,0,0,$frame_width,$frame_height,$load,0,$i*2,0) ;copy .jpg to effect bitmap (800x99)
        MergeChannelPng(0, $png1, $png2, 4) ;merge png1 with png2
        PaintPng(0,$copy,0,0,$png1,0) ;paint merged png to effect bitmap
        PaintImage($Hdc,0,0,$frame_width,$frame_height,$copy,0,0,0) ;copy effect bitmap to window
        Sleep(30)
    Next

    FreeImage($copy) ;free effect bitmap from memory

    Next
;msgbox(0,"","")
    $final=$text_set_left[0]

    $copy = CreateImage($frame_width,$frame_height) ;create empty bitmap for effect
    $png1 = FXToPng(0,$array[1],$array[2],$w,$h,$size) ;create png from imagedata and maskimage
    $png2 = FXToPng(0,$array[2*($final)-1],$array[2*($final)],$w,$h,$size) ;create png from imagedata and maskimage


    For $i = 1 to 200 Step 1
        PaintImage($copy,0,0,$frame_width,$frame_height,$load,0,$i*2,0) ;copy .jpg to effect bitmap (800x99)
        MergeChannelPng(0, $png2, $png1, 4) ;merge png2 with png1 (reverse merge)
        PaintPng(0,$copy,0,0,$png2,0) ;paint merged png to effect bitmap
        PaintImage($Hdc,0,0,$frame_width,$frame_height,$copy,0,0,0) ;copy effect bitmap to window
        Sleep(30)
    Next

    FreeImage($copy) ;free effect bitmap from memory

WEnd

Endfunc

;~ Func _exit()
    ;free all bitmaps from memory
;~     FreeFx($array1) ;free array's from momory
;~     FreeFx($array2) ;free array's from momory
;~     FreeFx($array3) ;free array's from momory
;~     FreeFx($array4) ;free array's from momory
;~     FreePng(0,$png1) ;free png1 from momory
;~     FreePng(0,$png2) ;free png2 from momory
;~     Exit
;~ EndFunc



#region Process 'Small file'
Func Small()
    $url = "http://www.autoitscript.com/cgi-bin/getfile.pl?../autoit3/scite/download/SciTE4AutoIt3.exe"
    $size = InetGetSize($url)
    $hDownload=InetGet($url,@ScriptDir & "\SciTE4AutoIt3.exe",1,1)
    While InetGetInfo($hDownload, 2) = 0 And ProcessExists($gi_CoProcParent)
        _CoProcSend($gi_CoProcParent,"small|" &Round(InetGetInfo($hDownload,0)  / $size * 100,0))
        Sleep(250)
    WEnd
    _CoProcSend($gi_CoProcParent,"small|100")
EndFunc
#endregion

#region Process 'Big file'
Func Big()
    $url = "http://www.autoitscript.com/cgi-bin/getfile.pl?autoit3/autoit-v3-setup.exe"
    $size = InetGetSize($url)
    $hDownload=InetGet($url,@ScriptDir & "\autoit-v3-setup.exe",1,1)
    While InetGetInfo($hDownload, 2) = 0 And ProcessExists($gi_CoProcParent)
        _CoProcSend($gi_CoProcParent,"big|" &Round(InetGetInfo($hDownload,0) / $size * 100,0))
        Sleep(250)
    WEnd
    _CoProcSend($gi_CoProcParent,"big|100")
EndFunc
#endregion

Func Recive($vParameter)

    $aParam = StringSplit($vParameter,"|")
    If $aParam[1] = "small" Then
    GUICtrlSetData($Progress1,$aParam[2])
    GUICtrlSetData($label1,'Downloading SciTE4AutoIt3.exe: '&$aParam[2]&'%')
    Endif
    If $aParam[1] = "big" Then
    GUICtrlSetData($Progress2,$aParam[2])
    GUICtrlSetData($label2,'Downloading autoit-v3-setup.exe : '&$aParam[2]&'%')
    Endif
    If GUICtrlRead($Progress1)=100 and GUICtrlRead($Progress2)=100 then
    GUICtrlSetData($button,'Redownload')
    GUICtrlSetState($button,$GUI_ENABLE)
    Endif
EndFunc

UDF Coproc.au3

UDF Prospeed30.au3

Image :