Jumat, 25 November 2011

:: Visual Basic - Pengenalan Windows API (Application Programming Interface) ::

Window API merupakan kumpulan fungsi-fungsi eksternal yang disediakan library windows untuk mengatur kemampuan dan tingkah laku setiap element di Windows (dari tampilan di desktop hingga alokasi memory) sehingga dapat dimanfaatkan suatu program untuk meningkatkan kemampuan program tersebut, contohnya saja untuk pengaksesan registry windows dengan fasilitas built in visual basic sangat sukar dilakukan, tetapi dengn adanya fasilitas API untuk registry dari library advapi32.dll, pengaksesan tersebut menjadi lebih mudah. Untuk memasukkan Window API ke dalam proyek Visual Basic, cara yang paling mudah adalah dengan menggunakan program API Viewer yang terdapat di menubar Add-Ins yaitu Add-In Manager. Pilih Loaded API Viewer, lalu buka text file darinya, pilih Win32api.txt. Project Sederhana Dengan Ekspresi Program sederhana memanipulasi form standard Visual Basic 6.0 Menggunakan file resource gambarlayar.res diambil/diimport dari jendela explorer Visual Basic dari file: gambarlayar.res Kontrol yang digunakan pada form1: - picture1, dengan property visible=false - command1 - image1, dengan property visible=true Kode pada jendela form1: Private Declare Function SendMessage Lib _ "user32" Alias "SendMessageA" _ (ByVal hwnd As Long, _ ByVal wMsg As Long, _ ByVal wParam As Long, _ lParam As Any) As Long Private Const CCM_FIRST = &H2000 Private Const CCM_SETBKCOLOR = (CCM_FIRST + 1) Private Const PBM_SETBKCOLOR = CCM_SETBKCOLOR Private Const WM_USER = &H400 Private Const PBM_SETBARCOLOR = (WM_USER + 9) Private Declare Sub ReleaseCapture Lib "user32" () Const WM_NCLBUTTONDOWN = &HA1 Const HTCAPTION = 2 Public Function prosesLAH() Me.MousePointer = 11 Me.Hide DoEvents Set Picture1.Picture = LoadResPicture(101, Bitmap) Picture1.ScaleMode = vbPixels Picture1.AutoRedraw = True Picture1.AutoSize = True Picture1.BorderStyle = vbBSNone Me.Width = Picture1.Width Me.Height = Picture1.Height WindowRegion = BuatRegion(Picture1) SetWindowRgn Me.hwnd, WindowRegion, True Me.Show Me.Refresh Me.MousePointer = 0 End Function Private Sub Command1_Click() Unload Me End Sub Private Sub Form_Load() Me.BackColor = RGB(214, 219, 191) Set Image1.Picture = LoadResPicture(101, Bitmap) Call prosesLAH End Sub Private Sub Form_MouseMove(Button As Integer, Shift As Integer, _ x As Single, y As Single) Dim lngReturnValue As Long If Button = 1 Then Call ReleaseCapture lngReturnValue = SendMessage(Me.hwnd, WM_NCLBUTTONDOWN, _ HTCAPTION, 0&) End If End Sub Public ask As Boolean Public DE As Integer Buat module dengan nama module1, Masukkan kode program pada jendela kode module, sebagai berikut: Public Declare Function SetWindowPos Lib "user32" _ (ByVal hwnd As Long, _ ByVal hWndInsertAfter As Long, _ ByVal x As Long, ByVal y As Long, _ ByVal cx As Long, ByVal cy As Long, _ ByVal wFlags As Long) As Long Public Const SWP_NOMOVE = &H2 Public Const SWP_NOSIZE = &H1 Public Const HWND_TOPMOST = -1 Public Const HWND_NOTOPMOST = -2 Public withFrameFlag As Boolean Public Declare Function GetPixel Lib "gdi32" (ByVal hDC As Long, ByVal x As Long, ByVal y As Long) As Long Public Declare Function SetWindowRgn Lib "user32" (ByVal hwnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long Public Declare Function CreateRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long Public Declare Function CombineRgn Lib "gdi32" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long Public Const RGN_OR = 2 Public xpos As Long Public ypos As Long Public Function BuatRegion(skinGbr As PictureBox) As Long Dim x As Long, y As Long, awalBrsX As Long Dim RegionSemua As Long, BrsRegion As Long Dim TransparentColor As Long Dim pdAwalRegion As Boolean Dim BrsPada As Boolean Dim hDC As Long Dim LebarGbr As Long Dim TinggiGbr As Long hDC = skinGbr.hDC LebarGbr = skinGbr.ScaleWidth TinggiGbr = skinGbr.ScaleHeight pdAwalRegion = True: BrsPada = False x = y = awalBrsX = 0 warnaTransparan = GetPixel(hDC, 0, 0) For y = 0 To TinggiGbr - 1 For x = 0 To LebarGbr - 1 If GetPixel(hDC, x, y) = warnaTransparan Or x = LebarGbr Then If BrsPada Then BrsPada = False BrsRegion = CreateRectRgn(awalBrsX, y, x, y + 1) If pdAwalRegion Then RegionSemua = BrsRegion pdAwalRegion = False Else CombineRgn RegionSemua, RegionSemua, BrsRegion, RGN_OR DeleteObject BrsRegion End If End If Else If Not BrsPada Then BrsPada = True awalBrsX = x End If End If Next Next BuatRegion = RegionSemua End Function
Scroll to top