Nghiên cứu Windows API

LỜI NÓI ĐẦU Khi đọc những lời nói này, tôi nghĩ rằng dù bạn là một người lập trình viên giỏi hay là một người mới chập chững bước vào thế giới của những đoạn code thì chắc hẳn tất cả các bạn đều là những người yêu thích công nghệ thông tin nói chung và lập trình nói riêng. Và bản thân tôi cũng vậy, là một người yêu thích công nghệ thông tin đứng ở khía cạnh này tôi tự nhận thấy để nghiên cứu, học tập, làm việc được tốt thì phải có nhiều tư liệu, sách báo phù hợp với mình. Chính bởi vì lẽđó, tôi đã soạn cuốn sách này với hy vọng đây là một cuốn sách tra cứu hữu ích bổ sung thêm vào kho tư liệu của mình. Cuốn sách này được biên soạn từ rất nhiều nguồn tư liệu, và chủ yếu tôi chỉ dùng cho riêng bản thân mình do đó khó tránh khỏi những sai xót, vì thế tôi rất mong nhận được nhiều ý kiến đóng góp của các bạn để những phiên bản sau ngày càng hoàn thiện hơn. Đồng thời tôi cũng xin tặng cuốn sách này cho tất cả các bạn yêu thích công nghệ thông tin với hy vọng nó sẽ giúp ích một phần nào đó trong công việc của các bạn, nhưng tôi mong các bạn hãy tôn trọng tác giả bằng cách không chỉnh sửa nội dung, xuất xứ của cuốn sách. Cuốn sách này hoàn toàn miễn phí, do đó các bạn có thể cho, tặng, biếu bất kỳ người nào nhưng tuyệt đối cấm thương mại (mua, bán) dưới bất kỳ hình thức nào. Xin cám ơn ! Luận văn gồm 80 trang và 4 chương rất hoàn chỉnh Nghiên cứu Windows API

pdf80 trang | Chia sẻ: banmai | Lượt xem: 2012 | Lượt tải: 0download
Bạn đang xem trước 20 trang tài liệu Nghiên cứu Windows API, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Public Const GWL_WNDPROC = (-4) ‘Con trỏ trỏ tới một thủ tục window của Visual Basic Public pVBProc as long ‘ (Biến trên mặc định mang giá trị 0). ‘Hàm sau thực hiện chức năng bọc. Tất cả công việc nó làm là gọi thủ tục window mặc định. Public Function WindowProc (ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long ‘Gọi thủ tục window mặc định và trả về giá trị. WindowProc = DefWindowProc (hWnd, uMsg, wParam, lParam) End Function ‘*** Đặt đoạn mã sau vào nơi bạn muốn. *** Dim retval As Long ‘giá trị trả về If pVBProc = 0 Then ‘Cửa sổ Form1 đang dùng thủ tục của VB cung cấp. Chuyển qua dùng thủ tục mặc định pVBProc = SetWindowLong (Form1.hWnd, GWL_WNDPROC, AddressOf WindowProc) Else ‘Cửa sổ Form1 đang dùng thủ tục mặc định (qua hàm bao bọc). Chuyển qua dùng thủ tục của VB. Retval = SetWindowLong (Form1.hWnd, GWL_WNDPROC, pVBProc) ‘Lập pVBProc về 0 để chúng ta biết được thủ tục nào đang được dùng. pVBProc = 0 End If ‘Bằng cách cho phép người dùng chuyển tới lui giữa các thủ tục, sự khác nhau trở nên rõ ràng 17. DestroyWindow - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function DestroyWindow Lib "user32" Alias "DestroyWindow" (ByVal hwnd As Long) As Long - Các tham số • hWnd : Cán của cửa sổ sẽ phá huỷ. - Mô tả : Hàm DestroyWindow phá huỷ cửa sổ (kể cả các cửa sổ con của nó). Hàm này sẽ gửi thông điệp WM_DESTROY và WM_NCDESTROY đến cửa sổ nhằm ngưng hoạt động cửa sổ đó và xoá bỏ focus tới nó. Hàm này cũng phá huỷ menu, thông điệp trong hàng đợi, phá huỷ timers, xoá bỏ quyền sở hữu clipboard, - Trị trả về : Số nguyên khác 0 nếu thành công và bằng 0 nếu thất bại. - Các hàm liên quan : + MoveWindow + IsWindowEnabled - Các ví dụ minh hoạ : Nghiên cứu Windows API Nguyễn Nam Trung Trang 53 + Ví dụ 1 : Move Window Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long Private Declare Function IsWindowEnabled Lib "user32" (ByVal hwnd As Long) As Long Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long Private Sub Form_Load() Dim bl As Boolean 'Is the window enabled? bl = IsWindowEnabled(Me.hwnd) MsgBox "Is the form enabled? " + Str$(bl) 'Move the window MoveWindow Me.hwnd, 0, 0, 200, 200, 1 'Show the window Me.Show 'Wait 5 seconds t = Timer Do 'Show the remaining time in the form's caption Me.Caption = 5 - Int(Timer - t) DoEvents Loop Until Timer > t + 5 'Destroy the window DestroyWindow Me.hwnd End Sub + Ví dụ 2 : New Start-button Const WS_CHILD = &H40000000 Const WM_LBUTTONDOWN = &H201 Const WM_LBUTTONUP = &H202 Const SW_HIDE = 0 Const SW_NORMAL = 1 Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long Private Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) As Long Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long Nghiên cứu Windows API Nguyễn Nam Trung Trang 54 Dim tWnd As Long, bWnd As Long, ncWnd As Long Private Sub Form_Load() 'KPD-Team 1998 'URL: 'E-Mail: KPDTeam@Allapi.net Dim R As RECT 'Get the taskbar's window handle tWnd = FindWindow("Shell_TrayWnd", vbNullString) 'Get the start-button's window handle bWnd = FindWindowEx(tWnd, ByVal 0&, "BUTTON", vbNullString) 'Get the start button's position GetWindowRect bWnd, R 'Create a new button ncWnd = CreateWindowEx(ByVal 0&, "BUTTON", "Hello !", WS_CHILD, 0, 0, R.Right - R.Left, R.Bottom - R.Top, tWnd, ByVal 0&, App.hInstance, ByVal 0&) 'Show our button ShowWindow ncWnd, SW_NORMAL 'Hide the start button ShowWindow bWnd, SW_HIDE End Sub Private Sub Form_Unload(Cancel As Integer) 'show the start button ShowWindow bWnd, SW_NORMAL 'destroy our button DestroyWindow ncWnd End Sub + Ví dụ 3 : Start In Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long Private Declare Function GetDesktopWindow Lib "user32" () As Long Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long Private Declare Function GetCurrentProcess Lib "kernel32" () As Long Private Declare Function Putfocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long Const GW_HWNDNEXT = 2 Dim mWnd As Long Function InstanceToWnd(ByVal target_pid As Long) As Long Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long 'Find the first window test_hwnd = FindWindow(ByVal 0&, ByVal 0&) Nghiên cứu Windows API Nguyễn Nam Trung Trang 55 Do While test_hwnd 0 'Check if the window isn't a child If GetParent(test_hwnd) = 0 Then 'Get the window's thread test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid) If test_pid = target_pid Then InstanceToWnd = test_hwnd Exit Do End If End If 'retrieve the next window test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT) Loop End Function Private Sub Form_Load() 'KPD-Team 1999 'URL: 'E-Mail: KPDTeam@Allapi.net Dim Pid As Long 'Lock the window update LockWindowUpdate GetDesktopWindow 'Execute notepad.Exe Pid = Shell("c:\windows\notepad.exe", vbNormalFocus) If Pid = 0 Then MsgBox "Error starting the app" 'retrieve the handle of the window mWnd = InstanceToWnd(Pid) 'Set the notepad's parent SetParent mWnd, Me.hwnd 'Put the focus on notepad Putfocus mWnd 'Unlock windowupdate LockWindowUpdate False End Sub Private Sub Form_Unload(Cancel As Integer) 'Unload notepad DestroyWindow mWnd 'End this program TerminateProcess GetCurrentProcess, 0 End Sub 18. EnableWindow - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function EnableWindow Lib "user32" Alias "EnableWindow" (ByVal hwnd As Long, ByVal fEnable As Long) As Long - Các tham số • hWnd : một handle của cửa sổ (hoặc control) được enable hay bị disable. • fEnable : Nếu bằng 0, cửa sổ hoặc control sẽ bị disable. Nếu khác 0 , cửa sổ sẽ được enable Nghiên cứu Windows API Nguyễn Nam Trung Trang 56 - Mô tả : Hàm EnableWindow dùng để làm tích cực (enable) hoặc bị động (disable) một cửa sổ hoặc một điều khiển người dùng (control). Nếu một cửa sổ hoặc một control bị disable, nó sẽ không nhận focus tức không thể tương tác với người dùng. Một vài loại control như nút nhấn và các loại control khác sẽ có màu xám khi bị disable, mặc dù bất kỳ cửa sổ nào cũng được enable hay bị disable. - Trị trả về : Hàm sẽ trả về 0 nếu trước đó cửa sổ (hoặc control) đang enable; trả về giá trị khác 0 nếu trước nó bị disable. - Các hàm liên quan : IsWindowEnabled - Các ví dụ minh hoạ : + Ví dụ 1 : EnableWindow 'This project needs two command buttons Private Declare Function IsWindowEnabled Lib "user32" (ByVal hwnd As Long) As Long Private Declare Function EnableWindow Lib "user32" (ByVal hwnd As Long, ByVal fEnable As Long) As Long Private Sub Command2_Click() 'KPD-Team 1999 'URL: 'E-Mail: KPDTeam@Allapi.net ' Reverse the enabled status of Command1. If the window is ' disabled, enable it; if it is enabled, disable it. Dim wasenabled As Long ' receives enabled/disabled status of Command1 Dim retval As Long ' return value ' Determine if the window Command1 is currently enabled or not. wasenabled = IsWindowEnabled(Command1.hwnd) If wasenabled = 0 Then ' if not enabled, enable it retval = EnableWindow(Command1.hwnd, 1) Else ' if enabled, disable it retval = EnableWindow(Command1.hwnd, 0) End If End Sub + Ví dụ 2 : Đảo trạng thái enable của control Command1 ‘Đảo trạng thái enable của control commandl, nếu control đang bị ‘ disable thì cho phép hoạt động; nếu đang hoạt động thì disable nó . Dim wasenabled as long ‘chứa trạng thái enable/disable của commandl Dim retval as long ‘giá trị trả về’ ‘Xác định control commandl hiện tại được enable hay không. Wasenabled = IswindowEnable(commandl.hwnd) If wasenabled = 0 then ‘nếu không được enable thì enable nó retval = Enablewindow (commandl.hwnd,1) Else ‘nếu đang đuợc enable thì disable nó Retval = Enablewindow (commandl.hwnd,0) End if. 19. EndDeferWindowPos - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Nghiên cứu Windows API Nguyễn Nam Trung Trang 57 Public Declare Function EndDeferWindowPos Lib "user32" Alias "EndDeferWindowPos" (ByVal hWinPosInfo As Long) As Long - Các tham số • hWnd - Mô tả : Hàm EndDeferWindowPos cập nhật vị trí, kích thước và tình trạng của một hoặc nhiều cửa sổ trong một chu trình là tươi (refreshing) của màn hình đơn. - Các hàm liên quan : + BeginDeferWindowPos + DeferWindowPos - Các ví dụ minh hoạ : Xem ví dụ hàm DeferWindowPos (hàm số 15). 20. EnumChildWindows - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function EnumChildWindows Lib "user32" Alias "EnumChildWindows" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long - Các tham số • hWndParent : Một handle của cửa sổ cha mà ta cần liệt kê các cửa sổ con của nó. • lpEnumFunc : Con trỏ chỉ tới hàm callback EnumChildProc. • lParam : Giá trị mở rộng truyền vào hàm callback. - Mô tả : Hàm EnumChildWindows liệt kê và cung cấp các handle cho tất cả các cửa sổ con của một cửa sổ. Hàm này cũng liệt kê bất kỳ cửa sổ con nào của các cửa sổ con. Mỗi lần một cửa sổ con được xác định, hàm sẽ truyền handle của nó vào một hàm callback được định nghĩa trong chương trình. Hàm tiếp tục thực hiện cho đến khi tất cả các cửa sổ con đều được liệt kê, hoặc cho đến khi tiến trình bị ngừng (hàm callback trả về FALSE). Phải có Custom Control CBK.VBX mới sử dụng được. - Trị trả về : Nếu xảy ra lỗi, hàm trả về 0 (dùng hàm GetLastError để nhận mã lỗi. Nếu thành công hàm trả về một giá trị khác 0. - Các hàm liên quan : + EnumThreadWindows + EnumWindows - Các ví dụ minh hoạ : EnumChildWindows 'in a form Private Sub Form_Load() 'KPD-Team 2000 'URL: 'E-Mail: KPDTeam@Allapi.net Me.AutoRedraw = True EnumChildWindows GetDesktopWindow, AddressOf EnumChildProc, ByVal 0& End Sub 'in a module Declare Function GetDesktopWindow Lib "user32" () As Long Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, Nghiên cứu Windows API Nguyễn Nam Trung Trang 58 ByVal lpString As String, ByVal cch As Long) As Long Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long Public Function EnumChildProc(ByVal hwnd As Long, ByVal lParam As Long) As Long Dim sSave As String 'Get the windowtext length sSave = Space$(GetWindowTextLength(hwnd) + 1) 'get the window text GetWindowText hwnd, sSave, Len(sSave) 'remove the last Chr$(0) sSave = Left$(sSave, Len(sSave) - 1) If sSave "" Then Form1.Print sSave 'continue enumeration EnumChildProc = 1 End Function 21. EnumThreadWindows - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function EnumThreadWindows Lib "user32" Alias "EnumThreadWindows" (ByVal dwThreadId As Long, ByVal lpfn As Long, ByVal lParam As Long) As Long - Các tham số • dwThreadld : Một định danh của thread cần liệt kê các cửa sổ của nó. • lpfn : Một con trỏ của hàm callback EnumThreadWndProc. • lParam : Giá trị mở rộng được truyền vào hàm callback. - Mô tả : Hàm EnumThreadWindows liệt kê và cung cấp các handle cho tất cả các cửa sổ được sở hữu và điều khiển bởi một thread. (Chú ý, các cửa sổ này bao gồm cả các cửa sổ không nhìn thấy được đối với người dùng). Mỗi lần một cửa sổ được định vị, hàm truyền handle của nó cho hàm callback. Hàm tiếp tục thực hiện cho đến khi tất cả các cửa sổ đều được liệt kê, hoặc cho đến khi tiến trình bị bỏ dở. - Trị trả về : Nếu xảy ra lỗi, hàm trả về giá trị 0 (dùng hàm GetLastError để lấy mã lỗi). Nếu thành công, hàm trả về một giá trị khác 0. - Các hàm liên quan : + EnumChildWindows + EnumWindows + GetCurrentThread - Các ví dụ minh hoạ : Enum Classnames 'in a form Private Sub Form_Load() 'KPD-Team 2000 'URL: 'E-Mail: KPDTeam@Allapi.net Dim ThreadID As Long, ProcessID As Long ' receive id to thread and process of Form1 ' Determine the thread which owns this window ThreadID = GetWindowThreadProcessId(Me.hWnd, ProcessID) ' Use the callback function to list all of the enumerated thrad windows EnumThreadWindows ThreadID, AddressOf EnumThreadWndProc, 0 'Show the results Nghiên cứu Windows API Nguyễn Nam Trung Trang 59 Me.AutoRedraw = True Me.Print sClasses End Sub 'In a module Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long Declare Function EnumThreadWindows Lib "user32" (ByVal dwThreadId As Long, ByVal lpfn As Long, ByVal lParam As Long) As Long Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long 'variable used to list all the classnames Public sClasses As String Public Function EnumThreadWndProc(ByVal hWnd As Long, ByVal lParam As Long) As Long Dim Ret As Long, sText As String 'create a string-buffer sText = Space(255) 'get the classname of the window handle Ret = GetClassName(hWnd, sText, 255) 'cut off the unnecessary part of Chr$(0)'s sText = Left$(sText, Ret) 'add this classname to the list of classnames sClasses = sClasses + sText + vbCrLf 'continue the enumeration EnumThreadWndProc = 1 End Function 22. EnumWindows - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function EnumWindows Lib "user32.dll" ( ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long - Các tham số • lpEnumFunc : Một con trỏ chỉ đến hàm callback EnumWindowsProc • lParam : Một giá trị mở rộng được truyền vào hàm callback. - Mô tả : Hàm EnumWindows liệt kê và cung cấp handle cho tất cả các cửa sổ top-level hiện đang mở. Hàm này sẽ bỏ qua các cửa sổ con. (Chú ý các cửa sổ top-level bao gồm cả các cửa sổ không hiện ra). Mỗi lần một cửa sổ được định vị, hàm sẽ truyền handle đó cho một hàm callback. Hàm này liên tục thực hiện cho đến khi tất cả các cửa sổ đều được liệt kê hay cho đến khi tiến trình được bỏ qua. - Trị trả về : Nếu xảy ra lỗi, hàm trả về 0 (dùng hàm GetLastError để nhận mã lỗi). Nếu thành công, hàm trả về một giá trụ khác 0. - Các hàm liên quan : + EnumChildWindows + EnumThreadWindows - Các ví dụ minh hoạ : EnumWindows Nghiên cứu Windows API Nguyễn Nam Trung Trang 60 'Add this code to a form Private Sub Form_Load() 'KPD-Team 2000 'URL: 'E-Mail: KPDTeam@Allapi.net 'Set the form's graphics mode to persistent Me.AutoRedraw = True 'call the Enumwindows-function EnumWindows AddressOf EnumWindowsProc, ByVal 0& End Sub 'Add this code to a module Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean Dim sSave As String, Ret As Long Ret = GetWindowTextLength(hwnd) sSave = Space(Ret) GetWindowText hwnd, sSave, Ret + 1 Form1.Print Str$(hwnd) + " " + sSave 'continue enumeration EnumWindowsProc = True End Function 23. EnumWindowStations - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function EnumWindowStations Lib "user32" Alias "EnumWindowStationsA" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long - Các tham số • lpEnumFunc : Biến con trỏ chỉ đến hàm để gọi đối với mỗi cửa sổ con (mức Child). Sử dụng tính chất ProcAddress của Custom Control CBK.VBX để nhận hàm biến trỏ (function pointer) để gọi lại (callbacks). • lParam : Giá trị chuyển đến cho sự kiện EnumWindows của Custom Control trong lúc liệt kê. Ý nghĩa của trị này do lập trình viên xác định. - Mô tả : Liệt kê danh sách cửa sổ cấp trên, chứa cửa sổ khai báo. Phải có Custom Control CBK.VBX mới sử dụng được. - Trị trả về : Hàm trả về TRUE (khác 0) nếu thành công, ngược lại hàm trả về FALSE (bằng 0) nếu thất bại. - Các hàm liên quan : + EnumChildWindows + EnumThreadWindows + EnumWindows Nghiên cứu Windows API Nguyễn Nam Trung Trang 61 24. FindWindow - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long - Các tham số • lpClassName : Tên của lớp cửa sổ hoặc cửa sổ cần tìm. • lpWindowName : Tên tựa của cửa sổ cần tìm, nếu tham số này là NULL thì tất cả các tên cửa sổ đều hợp lệ. - Mô tả : Hàm FindWindow tìm tất cả các cửa sổ phù hợp với một tên lớp cửa sổ và / hoặc tên cửa sổ (chính là tiêu đề của cửa sổ). Hàm tìm kiếm không phân biệt chữa hoa hay chữ thường. Nếu bạn không muốn chỉ rõ một không số nào đó, hãy truyền chuỗi rỗng cho nó (để truyền chuỗi rỗng cho một thông số, dùng hằng vbNullString). - Trị trả về : Nếu xảy ra lỗi, hoặc không tìm thấy cửa sổ thích hợp, hàm trả về 0 (dùng hàm GetLastError để nhận mã lỗi). Nếu thành công hàm sẽ trả về một handle của cửa sổ tìm thấy. - Các hàm liên quan : + FindWindowEx + GetActiveWindow + GetForegroundWindow - Các ví dụ minh hoạ : + Ví dụ 1 : Get Classname Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long Const SW_SHOWNORMAL = 1 Const WM_CLOSE = &H10 Const gcClassnameMSWord = "OpusApp" Const gcClassnameMSExcel = "XLMAIN" Const gcClassnameMSIExplorer = "IEFrame" Const gcClassnameMSVBasic = "wndclass_desked_gsk" Const gcClassnameNotePad = "Notepad" Const gcClassnameMyVBApp = "ThunderForm" Private Sub Form_Load() 'KPD-Team 1998 'URL: 'E-Mail: KPDTeam@Allapi.net Dim WinWnd As Long, Ret As String, RetVal As Long, lpClassName As String 'Ask for a Window title Ret = InputBox("Enter the exact window title:" + Chr$(13) + Chr$(10) + "Note: must be an exact match") 'Search the window WinWnd = FindWindow(vbNullString, Ret) Nghiên cứu Windows API Nguyễn Nam Trung Trang 62 If WinWnd = 0 Then MsgBox "Couldn't find the window ...": Exit Sub 'Show the window ShowWindow WinWnd, SW_SHOWNORMAL 'Create a buffer lpClassName = Space(256) 'retrieve the class name RetVal = GetClassName(WinWnd, lpClassName, 256) 'Show the classname MsgBox "Classname: " + Left$(lpClassName, RetVal) 'Post a message to the window to close itself PostMessage WinWnd, WM_CLOSE, 0&, 0& End Sub + Ví dụ 2 : Hide Start-button Const SWP_HIDEWINDOW = &H80 Const SWP_SHOWWINDOW = &H40 Const GW_CHILD = 5 Const GW_HWNDNEXT = 2 Private 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 Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long Dim tWnd As Long, bWnd As Long, sSave As String * 250 Private Sub Form_Load() 'KPD-Team 1999 'URL: 'E-Mail: KPDTeam@Allapi.net 'This code will hide the Start-button 'Find the taskbar's handle tWnd = FindWindow("Shell_traywnd", vbNullString) 'Search for a child window bWnd = GetWindow(tWnd, GW_CHILD) Do 'get the child window's classname GetClassName bWnd, sSave, 250 'We have the handle of the Start button If the classname is 'button' If LCase(Left$(sSave, 6)) = "button" Then Exit Do 'Search the next child bWnd = GetWindow(bWnd, GW_HWNDNEXT) Loop 'Hide the start button SetWindowPos bWnd, 0, 0, 0, 0, 0, SWP_HIDEWINDOW End Sub Private Sub Form_Unload(Cancel As Integer) Nghiên cứu Windows API Nguyễn Nam Trung Trang 63 'Show the start button SetWindowPos bWnd, 0, 0, 0, 0, 0, SWP_SHOWWINDOW End Sub + Ví dụ 3 : New Start-button (xem ví dụ 2 hàm DestroyWindow) + Ví dụ 4 : Start In (xem ví dụ 3 hàm DestroyWindow) + Ví dụ 5 : ForeGround Window Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long Private Sub Form_Load() 'KPD-Team 1999 'URL: 'E-Mail: KPDTeam@Allapi.net Dim lHandle As Long 'First we're going to retrieve the handle of this window ' "ThunderRT5Form" is the classname of a VB-window lHandle = FindWindow("ThunderRT5Form", Me.Caption) 'Set this window to the foreground lHandle = SetForegroundWindow(lHandle) End Sub 25. FindWindowEx - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 26. FlashWindow - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function FlashWindow Lib "user32" Alias "FlashWindow" (ByVal hwnd As Long, ByVal bInvert As Long) As Long - Các tham số • hWnd - Mô tả : Nghiên cứu Windows API Nguyễn Nam Trung Trang 64 - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 27. FlashWindowEx - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function FlashWindowEx Lib "user32.dll" (ByRef pfwi As PFLASHWINFO ) As Long - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 28. GetActiveWindow - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function GetActiveWindow Lib "user32" Alias "GetActiveWindow" () As Long - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 29. GetClassInfo - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function GetClassInfo Lib "user32" Alias "GetClassInfoA" (ByVal hInstance As Long, ByVal lpClassName As String, lpWndClass As WNDCLASS) As Long - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 30. GetClassInfoEx - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Nghiên cứu Windows API Nguyễn Nam Trung Trang 65 Public Declare Function GetClassInfoEx Lib "user32.dll" Alias "GetClassInfoExA" ( ByVal hinstance As Long, ByVal lpcstr As String, ByRef lpwndclassexa As WNDCLASSEX) As Long - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 31. GetClassLong - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function GetClassLong Lib "user32" Alias "GetClassLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 32. GetClassName - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 33. GetDesktopWindow - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function GetDesktopWindow Lib "user32" Alias "GetDesktopWindow" () As Long - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : Nghiên cứu Windows API Nguyễn Nam Trung Trang 66 - Các ví dụ minh hoạ : 34. GetParent - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function GetParent Lib "user32" Alias "GetParent" (ByVal hwnd As Long) As Long - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 35. GetWindow - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function GetWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wCmd As Long) As Long - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 36. GetWindowLong - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 37. GetWindowRect - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function GetWindowRect Lib "user32" Alias "GetWindowRect" (ByVal hwnd As Long, lpRect As RECT) As Long Nghiên cứu Windows API Nguyễn Nam Trung Trang 67 - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 38. GetWindowText - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 39. GetWindowTextLength - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 40. GetWindowThreadProcessId - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function GetWindowThreadProcessId Lib "user32" Alias "GetWindowThreadProcessId" (ByVal hwnd As Long, lpdwProcessId As Long) As Long - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 41. GetWindowWord Nghiên cứu Windows API Nguyễn Nam Trung Trang 68 - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function GetWindowWord Lib "user32" Alias "GetWindowWord" (ByVal hwnd As Long, ByVal nIndex As Long) As Integer - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 42. InvalidateRect - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function InvalidateRect Lib "user32" Alias "InvalidateRect" (ByVal hwnd As Long, lpRect As RECT, ByVal bErase As Long) As Long - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 43. IsChild - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function IsChild Lib "user32" Alias "IsChild" (ByVal hWndParent As Long, ByVal hwnd As Long) As Long - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 44. IsIconic - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function IsIconic Lib "user32" Alias "IsIconic" (ByVal hwnd As Long) As Long - Các tham số • hWnd - Mô tả : Nghiên cứu Windows API Nguyễn Nam Trung Trang 69 - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 45. IsRectEmpty - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function IsRectEmpty Lib "user32" Alias "IsRectEmpty" (lpRect As RECT) As Long - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 46. IsWindow - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function IsWindow Lib "user32" Alias "IsWindow" (ByVal hwnd As Long) As Long - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 47. IsWindowEnabled - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function IsWindowEnabled Lib "user32" Alias "IsWindowEnabled" (ByVal hwnd As Long) As Long - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 48. IsWindowVisible - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Nghiên cứu Windows API Nguyễn Nam Trung Trang 70 Public Declare Function IsWindowVisible Lib "user32" Alias "IsWindowVisible" (ByVal hwnd As Long) As Long - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 49. IsZoomed - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function IsZoomed Lib "user32" Alias "IsZoomed" (ByVal hwnd As Long) As Long - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 50. LockWindowUpdate - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function LockWindowUpdate Lib "user32" Alias "LockWindowUpdate" (ByVal hwndLock As Long) As Long - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 51. MapWindowPoints - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function MapWindowPoints Lib "user32" Alias "MapWindowPoints" (ByVal hwndFrom As Long, ByVal hwndTo As Long, lppt As Any, ByVal cPoints As Long) As Long - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : Nghiên cứu Windows API Nguyễn Nam Trung Trang 71 - Các ví dụ minh hoạ : 52. MoveWindow - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function MoveWindow Lib "user32" Alias "MoveWindow" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 53. PostMessage - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 54. RedrawWindow - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function RedrawWindow Lib "user32" Alias "RedrawWindow" (ByVal hwnd As Long, lprcUpdate As RECT, ByVal hrgnUpdate As Long, ByVal fuRedraw As Long) As Long - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 55. RegisterClass - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Nghiên cứu Windows API Nguyễn Nam Trung Trang 72 Public Declare Function RegisterClass Lib "user32" Alias "RegisterClassA" Alias "RegisterClass" (Class As WNDCLASS) As Long - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 56. RegisterClassEx - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function RegisterClassEx Lib "user32" Alias "RegisterClassExA" (pcWndClassEx As WNDCLASSEX) As Integer - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 57. ScreenToClient - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function ScreenToClient Lib "user32" Alias "ScreenToClient" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 58. SendMessage - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : Nghiên cứu Windows API Nguyễn Nam Trung Trang 73 59. SetActiveWindow - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function SetActiveWindow Lib "user32" Alias "SetActiveWindow" (ByVal hwnd As Long) As Long - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 60. SetClassLong - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function SetClassLong Lib "user32" Alias "SetClassLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 61. SetForegroundWindow - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hwnd As Long) As Long - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 62. SetParent - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function SetParent Lib "user32" Alias "SetParent" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long - Các tham số Nghiên cứu Windows API Nguyễn Nam Trung Trang 74 • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 63. SetWindowLong - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 64. SetWindowPos - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Public Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" (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 - Các tham số • hWnd - Mô tả : - Trị trả về : - Các hàm liên quan : - Các ví dụ minh hoạ : 65. SetWindowRgn (tạo hình dáng cho cửa sổ) - Thư viện : user32.dll - Hệ điều hành : Windows NT 3.1 or later; Windows 95 or later - Khai báo : Declare Function SetWindowRgn Lib "user32" Alias "SetWindowRgn" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long - Các tham số • hWnd : Handle của cửa sổ. • hRgn : Handle của vùng làm việc. Hàm sẽ thiết lập vùng làm việc (hình dạng) của cửa sổ dựa vào thông số này. Nếu hRgn là Null thì hàm sẽ thiết lập vùng làm việc là NULL. • bRedraw : nhận vào giá tri Boolean thông báo cho hệ điều hành vẽ lại sau khi thiết lập vùng làm việc. Ví dụ : bạn đặt bRedraw là TRUE nếu như cửa sổ nhìn thấy được (visible). Nghiên cứu Windows API Nguyễn Nam Trung Trang 75 - Mô tả : hàm SetWindowRgn dùng để thiết lập miền làm việc (hình dáng) của cửa sổ. Miền làm việc này là phần diện tích bên trong cửa sổ nơi hệ điều hành cho phép vẽ. Hệ điều hành sẽ không hiển thị bất kỳ vùng nào bên ngoài miền làm việc này. - Các hàm liên quan : CreateEllipticRgn - Các ví dụ minh hoạ : + Ví dụ 1 : Create Font 'used with fnWeight Const FW_DONTCARE = 0 Const FW_THIN = 100 Const FW_EXTRALIGHT = 200 Const FW_LIGHT = 300 Const FW_NORMAL = 400 Const FW_MEDIUM = 500 Const FW_SEMIBOLD = 600 Const FW_BOLD = 700 Const FW_EXTRABOLD = 800 Const FW_HEAVY = 900 Const FW_BLACK = FW_HEAVY Const FW_DEMIBOLD = FW_SEMIBOLD Const FW_REGULAR = FW_NORMAL Const FW_ULTRABOLD = FW_EXTRABOLD Const FW_ULTRALIGHT = FW_EXTRALIGHT 'used with fdwCharSet Const ANSI_CHARSET = 0 Const DEFAULT_CHARSET = 1 Const SYMBOL_CHARSET = 2 Const SHIFTJIS_CHARSET = 128 Const HANGEUL_CHARSET = 129 Const CHINESEBIG5_CHARSET = 136 Const OEM_CHARSET = 255 'used with fdwOutputPrecision Const OUT_CHARACTER_PRECIS = 2 Const OUT_DEFAULT_PRECIS = 0 Const OUT_DEVICE_PRECIS = 5 'used with fdwClipPrecision Const CLIP_DEFAULT_PRECIS = 0 Const CLIP_CHARACTER_PRECIS = 1 Const CLIP_STROKE_PRECIS = 2 'used with fdwQuality Const DEFAULT_QUALITY = 0 Const DRAFT_QUALITY = 1 Const PROOF_QUALITY = 2 'used with fdwPitchAndFamily Const DEFAULT_PITCH = 0 Const FIXED_PITCH = 1 Const VARIABLE_PITCH = 2 'used with SetBkMode Const OPAQUE = 2 Nghiên cứu Windows API Nguyễn Nam Trung Trang 76 Const TRANSPARENT = 1 Const LOGPIXELSY = 90 Const COLOR_WINDOW = 5 Const Message = "Hello !" Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long Private Declare Function CreateEllipticRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long Private Declare Function CreateFont Lib "gdi32" Alias "CreateFontA" (ByVal nHeight As Long, ByVal nWidth As Long, ByVal nEscapement As Long, ByVal nOrientation As Long, ByVal fnWeight As Long, ByVal fdwItalic As Boolean, ByVal fdwUnderline As Boolean, ByVal fdwStrikeOut As Boolean, ByVal fdwCharSet As Long, ByVal fdwOutputPrecision As Long, ByVal fdwClipPrecision As Long, ByVal fdwQuality As Long, ByVal fdwPitchAndFamily As Long, ByVal lpszFace As String) As Long Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long Private Declare Function TextOut Lib "gdi32" Alias "TextOutA" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, ByVal nCount As Long) As Long Private Declare Function MulDiv Lib "kernel32" (ByVal nNumber As Long, ByVal nNumerator As Long, ByVal nDenominator As Long) As Long Private Declare Function SetBkMode Lib "gdi32" (ByVal hdc As Long, ByVal nBkMode As Long) As Long Private Declare Function GetSysColorBrush Lib "user32" (ByVal nIndex As Long) As Long Private Declare Function FillRect Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As Long Private Declare Function SetRect Lib "user32" (lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long Dim mDC As Long, mBitmap As Long Private Sub Form_Click() Unload Me End Sub Private Sub Form_Load() 'KPD-Team 1999 Nghiên cứu Windows API Nguyễn Nam Trung Trang 77 'URL: 'E-Mail: KPDTeam@Allapi.net Dim mRGN As Long, Cnt As Long, mBrush As Long, R As RECT 'Create a device context, compatible with the screen mDC = CreateCompatibleDC(GetDC(0)) 'Create a bitmap, compatible with the screen mBitmap = CreateCompatibleBitmap(GetDC(0), Me.Width / Screen.TwipsPerPixelX, Me.Height / Screen.TwipsPerPixelY) 'Select the bitmap nito the device context SelectObject mDC, mBitmap 'Set the bitmap's backmode to transparent SetBkMode mDC, TRANSPARENT 'Set the rectangles' values SetRect R, 0, 0, Me.Width / Screen.TwipsPerPixelX, Me.Height / Screen.TwipsPerPixelY 'Fill the rect with the default window-color FillRect mDC, R, GetSysColorBrush(COLOR_WINDOW) For Cnt = 0 To 350 Step 30 'Select the new font into the form's device context and delete the old font DeleteObject SelectObject(mDC, CreateMyFont(24, Cnt)) 'Print some text TextOut mDC, (Me.Width / Screen.TwipsPerPixelX) / 2, (Me.Height / Screen.TwipsPerPixelY) / 2, Message, Len(Message) Next Cnt 'Create an elliptical region mRGN = CreateEllipticRgn(0, 0, Me.Width / Screen.TwipsPerPixelX, Me.Height / Screen.TwipsPerPixelY) 'Set the window region SetWindowRgn Me.hWnd, mRGN, True 'delete our elliptical region DeleteObject mRGN End Sub Function CreateMyFont(nSize As Integer, nDegrees As Long) As Long 'Create a specified font CreateMyFont = CreateFont(-MulDiv(nSize, GetDeviceCaps(GetDC(0), LOGPIXELSY), 72), 0, nDegrees * 10, 0, FW_NORMAL, False, False, False, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, DEFAULT_PITCH, "Times New Roman") End Function Private Sub Form_Paint() 'Copy the picture to the form BitBlt Me.hdc, 0, 0, Me.Width / Screen.TwipsPerPixelX, Me.Height / Screen.TwipsPerPixelY, mDC, 0, 0, vbSrcCopy End Sub Private Sub Form_Unload(Cancel As Integer) 'clean up DeleteDC mDC DeleteObject mBitmap End Sub Nghiên cứu Windows API Nguyễn Nam Trung Trang 78 + Ví dụ 2 : Path2Region Private Declare Function BeginPath Lib "gdi32" (ByVal hdc As Long) As Long Private Declare Function EndPath Lib "gdi32" (ByVal hdc As Long) As Long Private Declare Function PathToRegion Lib "gdi32" (ByVal hdc As Long) As Long Private Declare Function TextOut Lib "gdi32" Alias "TextOutA" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, ByVal nCount As Long) As Long Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long Private Sub Form_Click() 'end.. Unload Me End Sub Private Sub Form_Load() 'KPD-Team 2000 'URL: 'E-Mail: KPDTeam@Allapi.net Dim hRgn As Long Const sText = "Click Here!" 'set the font to 'Times New Romen, size 72' Me.FontName = "Times New Roman" Me.FontSize = 72 'set the backcolor to Red Me.BackColor = vbRed 'open a path bracket BeginPath Me.hdc 'draw the text TextOut Me.hdc, 0, 0, sText, Len(sText) 'close the path bracket EndPath Me.hdc 'convert the path to a region hRgn = PathToRegion(Me.hdc) 'set the Window-region SetWindowRgn Me.hWnd, hRgn, True 'destroy our region DeleteObject hRgn End Sub + Ví dụ 3 : Clipping Regions Private Declare Function GetClipRgn Lib "gdi32" (ByVal hdc As Long, ByVal hRgn As Long) As Long Private Declare Function IntersectClipRect Lib "gdi32" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long Private Declare Function SelectClipRgn Lib "gdi32" (ByVal hdc As Long, ByVal hRgn As Long) As Long Private Declare Function OffsetClipRgn Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long Private Declare Function CreateEllipticRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long Nghiên cứu Windows API Nguyễn Nam Trung Trang 79 Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long Dim hRgn As Long Private Sub Form_Load() 'KPD-Team 2000 'URL: 'E-Mail: KPDTeam@Allapi.net Me.ScaleMode = vbPixels End Sub Private Sub Form_Paint() Form_Resize End Sub Private Sub Form_Resize() Dim Ret As Long 'destroy the previous region DeleteObject hRgn 'create an elliptic region hRgn = CreateEllipticRgn(0, 0, Me.ScaleWidth, Me.ScaleHeight) 'select this elliptic region into the form's device context SelectClipRgn Me.hdc, hRgn 'move the clipping region OffsetClipRgn Me.hdc, 10, 10 'generate a new clipping region IntersectClipRect Me.hdc, 10, 10, 500, 300 'clear the form Me.Cls 'draw a Black rectangle over the entire form Me.Line (0, 0)-(Me.ScaleWidth, Me.ScaleHeight), vbBlack, BF 'create a temporary region Ret = CreateEllipticRgn(0, 0, 1, 1) 'copy the current clipping region into the temporary region GetClipRgn Me.hdc, Ret 'set the new window region SetWindowRgn Me.hWnd, Ret, True End Sub Private Sub Form_Unload(Cancel As Integer) 'clean up DeleteObject hRgn End Sub Private Sub Form_Click() 'unload the form when the user clicks on it Unload Me End Sub Nghiên cứu Windows API Nguyễn Nam Trung Trang 80 TÀI LIỆU THAM KHẢO • www.allapi.net • www.caulacbovb.com • Website Lê Hoàn • Cẩm nang lập trình Windows API – nhà xuất bản Giao Thông Vận Tải – 2005

Các file đính kèm theo tài liệu này:

  • pdfapi_5891.pdf
Tài liệu liên quan