Professional Documents
Culture Documents
LI NI U
Khi c nhng li ni ny, ti ngh rng d bn l mt ngi lp trnh vin gii hay l mt ngi mi chp chng bc vo th gii ca nhng on code th chc hn tt c cc bn u l nhng ngi yu thch cng ngh thng tin ni chung v lp trnh ni ring. V bn thn ti cng vy, l mt ngi yu thch cng ngh thng tin ng kha cnh ny ti t nhn thy nghin cu, hc tp, lm vic c tt th phi c nhiu t liu, sch bo ph hp vi mnh. Chnh bi v l , ti son cun sch ny vi hy vng y l mt cun sch tra cu hu ch b sung thm vo kho t liu ca mnh. Cun sch ny c bin son t rt nhiu ngun t liu, v ch yu ti ch dng cho ring bn thn mnh do kh trnh khi nhng sai xt, v th ti rt mong nhn c nhiu kin ng gp ca cc bn nhng phin bn sau ngy cng hon thin hn. ng thi ti cng xin tng cun sch ny cho tt c cc bn yu thch cng ngh thng tin vi hy vng n s gip ch mt phn no trong cng vic ca cc bn, nhng ti mong cc bn hy tn trng tc gi bng cch khng chnh sa ni dung, xut x ca cun sch. Cun sch ny hon ton min ph, do cc bn c th cho, tng, biu bt k ngi no nhng tuyt i cm thng mi (mua, bn) di bt k hnh thc no. Mi chi tit thc mc, gp xin vui lng lin h : xla0hu@yahoo.com Xin cm n !
Trang 1
Phn I
S LC V WINDOWS API
Trang 2
1. API l g :
API l vit tt ca Application Programming Interface (giao din lp trnh ng dng). API cung cp hu ht cc tnh nng thng dng cho tt c cc chng trnh chy trn nn Window. Hu ht cc hm API thng c cha trong file DLL trong th mc h thng Window (thng l C:/Windows/System).
Declare Function AddFontResource Lib "gdi32" Alias "AddFont" (ByVal lpFileName As String) As Long Phm vi s dng ca hm API cng ph thuc vo cc v tr khai bo n nh cch khai bo cc bin trong Visual Basic. Phn sau mnh Lib s bo cho VisualBasic bit file .dll c cha th tc khai bo. i vi cc th vin User32, Kernel32, GDI32 bn khng cn phi ghi thm phn tn m rng ca tp tin: V d : Declare Function GetTickCount Lib "kernel32" Alias "GetTickCount" () As Long Trong khi i vi nhng file .dll khc, bn phi khai bo ng dn y : V d: Declare Function lzCopy Lib "c:\windows\lzexpand.dll" (ByVal S As Integer, ByVal D As Integer) As Long Nu khng khai bo ng dn y , VisualBasic s tm ln lt trong: 1. Th mc cha file thc thi .exe 2. Th mc hin hnh 3. \Windows\System32 4. \Windows\System 5. \Windows 6. ng dn c khai bo trc
4. Handle l g :
Handle : (cn) tm gi l a ch L mt bin kiu Long c gi tr nhn bit duy nht dng nh ngha mt i tng. V trong Windows th mi i tng (control) s c gn cho 1 a ch ring, ging nh s CMND ca mnh vy , v khi chng ta mun lm vic vi i tng no th phi tr ti a ch ca i tng , ci a ch c gi l handle ca mi i tng. Mi ca s trong HH Windows th c nh ngha bi mt handle. Bn c th ly c tt c cc thng tin v mt i tng sau khi bn ly c handle ca n.
Trang 5
Phn II
CC HM API
Trang 6
1. AdjustWindowRect
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Declare Function AdjustWindowRect Lib "user32" Alias "AdjustWindowRect" (lpRect As RECT, ByVal dwStyle As Long, ByVal bMenu As Long) As Long - Cc tham s : lpRect : con tr ti mt cu trc RECT cha vng lm vic client. dwStyle : kiu ca s bMenu : tr v TRUE (khc 0) nu ca s c menu. - M t : hm AdjustWindowRect iu chnh kch thc ca ca s khi c vng lm vic client (khng tnh kch thc ca thanh tiu , ng vin v cc phn thm). Ngoi ra kch thc ca ca s c th s dng hm CreateWindow to mt ca s c din tch vng client tu . - Cc hm lin quan : AdjustWindowRectEx - V d minh ho : DeferWindowPos
Const WS_BORDER = &H800000 Const WS_DLGFRAME = &H400000 Const WS_THICKFRAME = &H40000 Const WS_CAPTION = &HC00000 Const HWND_BOTTOM = 1 Const HWND_TOP = 0 Const HWND_TOPMOST = -1 Const HWND_NOTOPMOST = -2 Const SWP_SHOWWINDOW = &H40 Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type
Trang 7
Private Declare Function AdjustWindowRect Lib "user32" (lpRect As RECT, ByVal dwStyle As Long, ByVal bMenu As Long) As Long Private Declare Function BeginDeferWindowPos Lib "user32" (ByVal nNumWindows As Long) As Long Private Declare Function DeferWindowPos Lib "user32" (ByVal hWinPosInfo As Long, 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 EndDeferWindowPos Lib "user32" (ByVal hWinPosInfo As Long) As Long Private Sub Form_Load() 'KPD-Team 2000 'URL: http://www.allapi.net/ 'E-Mail: KPDTeam@Allapi.net Dim R As RECT, hDWP As Long R.Left = 30 R.Top = 30 R.Bottom = 200 R.Right = 120 AdjustWindowRect R, WS_THICKFRAME Or WS_CAPTION, False hDWP = BeginDeferWindowPos(1) DeferWindowPos hDWP, Me.hwnd, HWND_TOP, R.Left, R.Top, R.Right - R.Left, R.Bottom R.Top, SWP_SHOWWINDOW EndDeferWindowPos hDWP End Sub
2. AdjustWindowRectEx
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Declare Function AdjustWindowRectEx Lib "user32" Alias "AdjustWindowRectEx" (lpRect As RECT, ByVal dsStyle As Long, ByVal bMenu As Long, ByVal dwEsStyle As Long) As Long - Cc tham s : lpRect : con tr ti mt cu trc RECT cha vng lm vic client. dwStyle : kiu ca s bMenu : tr v TRUE (khc 0) nu ca s c menu. dwExStyle : kiu m rng ca ca s - M t : hm AdjustWindowRectEx iu chnh kch thc ca ca s khi c vng lm vic client (khng tnh kch thc ca thanh tiu , ng vin v cc phn thm). Ngoi ra kch thc ca ca s c th s dng hm CreateWindowEx to mt ca s c din tch vng client tu . - Cc hm lin quan : AdjustWindowRect - V d minh ho : AdjustWindowRectEx
Const WS_BORDER = &H800000 Const WS_DLGFRAME = &H400000 Const WS_THICKFRAME = &H40000 Const WS_CAPTION = &HC00000 Const WS_EX_CLIENTEDGE = &H200 Private Type RECT
Trang 8
3. AnyPopup
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function AnyPopup Lib "user32" Alias "AnyPopup" () As Long - M t : a ra ch s ca s popup hin ang tn ti trn mn hnh. - Tr tr v: Hm tr v TRUE (khc 0) nu c ca s popup.
4. ArrangeIconicWindows
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo Public Declare Function ArrangeIconicWindows Lib "ArrangeIconicWindows" (ByVal hwnd As Long) As Long -
"user32"
Alias
Cc tham s : hwnd : handle ca ca s cha (m c parent) M t : Xp cc biu tng ca s trong mt ca s cha (mc parent). Tr tr v : Hm tr v s long chiu cao ca hng biu tng (bng 0 nu tht bi)
5. BeginDeferWindowPos
Th vin : user32.dll H iu hnh : Windows NT 3.1 or later; Windows 95 or later
Trang 9
Nghin cu Windows API - Khai bo : Declare Function BeginDeferWindowPos Lib "user32" Alias "BeginDeferWindowPos" (ByVal nNumWindows As Long) As Long - Cc tham s : nNumWindows : S ca s ban u cp pht vng nh trng. - M t : Bt u xy dng danh sch v tr cc ca s mi thnh cu trc bn ni b cha v tr cc ca s - Tr tr v : s long handle ca cu trc bn . 0 nu tht bi - Cc hm lin quan : DeferWindowPos EndDeferWindowPos - Cc v d minh ho : xem v d DeferWindowPos ca hm AdjustWindowRect trang 7.
6. BringWindowToTop
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Declare Function BringWindowToTop Lib "user32" Alias "BringWindowToTop" (ByVal hwnd As Long) As Long - Cc tham s : hwnd : handle ca ca s mun a ln trn cng ( ca s topmost ) trong danh sch Z-order - M t : a mt ca s ch nh ln trn cng trong danh sch th t Z-order (th t hin th cc ca s), lm cho n nm trn tt c cc ca s khc (topmost). Hm ny c tc dng tng t nh hm SetWindowPos t ca s ln trn cng trong Zorder. - Cc hm lin quan : SetWindowPos - Cc v d minh ho : Window to top
'This program needs two forms, two buttons and a module 'KPD-Team 1998 'URL: http://www.allapi.net/ 'E-Mail: KPDTeam@Allapi.net Private Declare Function BringWindowToTop Lib "user32" (ByVal hwnd As Long) As Long Private Sub Command1_Click() 'in form1 'Set Form2 on top BringWindowToTop Form2.hwnd End Sub Private Sub Form_Load() 'in form1 Form2.Visible = True End Sub Private Sub Command1_Click() 'in form2 'Set Form1 on top BringWindowToTop Form1.hwnd
Trang 10
7. BrowseCallbackProc
- Khai bo : Public Function BrowseCallbackProc (ByVal hwnd As Long, ByVal uMsg As Long, ByVal lParam As Long, ByVal lpData As Long) As Long on m xc nh ng dng t ti y End Function - Cc tham s : hwnd : handle ca ca duyt th mc ca hp thoi Folder ang gi hm ny. Handle ny dng gi cc thng ip cho hp thoi. uMsg : mt trong cc c di y xc nh cc s kin BFFM_INITIALIZED : hp thoi hon tt khi to, lParam = 0 BFFM_SELCHANGED : ngi dng thay i la chn hin thi, lParam l mt PIDL n la chn hin thi. BFFM_VALIDATEFAILED : t Intenet Explorer 4.0 tr i : thng bo rng ngi dng nhp mt ng dn sai vo hp son tho. lParam l mt con tr tr ti mt chui (kt thc bng k t NULL) cha tn ng dn sai ny. lParam : ph thuc vo gi tr uMsg . lpData : gi tr do ng dng nh ngha c trong cu trc BROWSEINFO dng to hp thoi. - M t : hm callback BrowseCallbackProc x l cc thng ip ca ca s duyt ca cc hp thoi Folder. C th, hm ny s a ra nhng thng ip thng bo khi hp thoi ang c khi chy v khi ngi dng thay i cc la chn hin thi. Hm callback c th lm vic vi hp thoi ang c khi chy v khi ngi dng thay i cc la chn hin thi. Hm callback c th lm vic vi hp thoi bng cch gi cho n mt trong nhng thng ip sau bng hm SendMessage BFFM_ENABLEOK : Enable hoc Disable nt OK ca hp thoi. Enable nt OK, thit lp tham s thng ip lParam l mt gi tr khc 0. Disable nt OK, thit lp tham s lParam bng 0. BFFM_SETSELECTION : thit lp la chn hin thi trong hp thoi. nh r ng dn nh mong mun bng cch dng mt chui, thit lp tham s thng ip lParam l chui v tham s thng ip wParam l mt s khc 0. nh r ng dn theo mong mun bng cch s dng mt con tr ti mt cu trc ITEMIDLIST, thit lp tham s thng ip lParam l PIDL v tham s thng ip wParam l 0. BFFM_SETSTATUSTEXT : thit lp trng thi ca vn bn c hin th bi hp thoi nu c. Thit lp tham s thng ip lParam l chui cha vn bn mong mun. - Gi tr tr v : Hm lun lun tr v 0 nu hp dialog khng x l thng ip BFFM_VALIDATEFAILED. Hm tr v 0 ng hp thoi, hoc tr v gi tr khc 0 gi cho n vn c hin th. - Hng nh ngha : Const BFFM_ENABLEOK = &H465 Const BFFM_SETSELECTION = &H466 Const BFFM_SETSTATUSTEXT = &H464
Trang 11
Nghin cu Windows API Const BFFM_INITIALIZED = 1 Const BFFM_SELCHANGED = 2 Const BFFM_VALIDATEFAILED = 3 Ghi ch : ging nh tt c cc hm callback khc, hm BrowseCallbackProc phi c khai bo public trong mt module. S dng bi : BROWSEINFO
8. CallWindowProc
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long - Cc tham s : lpPrevWndFunc : Con tr ti hm th tc Window gi mt cch tng minh. Hm ny dng x l thng tin. Nu gi tr ny c gi bi hm GetWindowLong vi tham s nIndex c thit lp l GWL_WNDPROC hoc DWL_DLGPROC, thm tr n cn l a ch ca ca s hoc th tc ca hp thoi hoc l handle i din cho a ch . hWnd : mt handle cho ca s x l thng ip. Msg : thng ip x l wParam : thng tin b xung v thng ip, ni dung ca tham s ny ph thuc vo tham s Msg. lParam : thng tin b sung v thng ip, ni dung tham s ny ph thuc vo tham s Msg. - M t : (gi tng minh l hm hook) hot ng nh l mt th tc ca ca s x l mt thng ip. N cho php mt thng ip ca ca s c x l bng mt th tc ca s m khng nht thit l th tc thng c gi bi ca s. - Tr tr v : hm tr v gi tr c to ra sau khi x l thng ip c gi - Cc hm lin quan : DefMDIChildProc DefWindowProc - Cc v d minh ho : + V d 1 : Clipboard Viewer
'Create a new project, add a module to it 'Add a command button to Form1 'In the form Private Sub Form_Load() 'KPD-Team 1999 'URL: http://www.allapi.net/ 'E-Mail: KPDTeam@Allapi.net 'Subclass this form HookForm Me 'Register this form as a Clipboardviewer SetClipboardViewer Me.hwnd End Sub
Trang 12
V d 2 : Call Procedure
Create a new project and add this code to Form1 Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Any, ByVal wParam As Any, ByVal lParam As Any) As Long Private Sub Form_Load()
Trang 13
'This Project needs a module and a form. The Form must contain two buttons, a horizontal scrollbar and a timer 'Add this code to the form: Dim fMovingSlider As Boolean Private Sub Command1_Click() Timer1.Interval = 100 Timer1.Enabled = True FileSeek 0 Play End Sub Private Sub Command2_Click() Timer1.Enabled = False StopPlay End Sub Private Sub Form_Load() 'KPD-Team 1999 'URL: http://www.allapi.net/ 'E-Mail: KPDTeam@Allapi.net Const sWavFile = "c:\windows\msremind.wav" 'Check if the file exists If Dir(sWavFile) = "" Or sWavFile = "" Then HScroll1.Enabled = False MsgBox "File not found !" + vbCrLf + "Please choose a valid file !", vbCritical + vbOKOnly Exit Sub End If HScroll1.Max = 100 HScroll1.SmallChange = 1 HScroll1.LargeChange = 5 Command1.Caption = "Play" Command2.Caption = "Stop" MsgBox "This method uses subclassing, so never press the Stop-button." + vbCrLf + "Unload this form properly by pressing the 'X'", vbInformation + vbOKOnly 'Initialize
Trang 14
Trang 15
Trang 16
Trang 17
Dim rc As Long ' Return code Dim hmmioIn As Long ' file handle Dim dataOffset As Long ' start of audio data in wave file Dim audioLength As Long ' number of bytes in audio data Dim pFormat As Long ' pointer to wave format Dim formatBuffer As String * 50 ' buffer to hold the wave format Dim startPos As Long ' sample where we started playback from Dim format As WAVEFORMAT ' waveformat structure Dim i As Long ' loop control variable Dim j As Long ' loop control variable Dim hmem(1 To NUM_BUFFERS) As Long ' memory handles Dim pmem(1 To NUM_BUFFERS) As Long ' memory pointers Dim hdr(1 To NUM_BUFFERS) As WAVEHDR ' wave headers Dim bufferSize As Long ' size of output buffers Dim fPlaying As Boolean ' is file currently playing Dim fFileOpen As Boolean ' is file currently open Dim hWaveOut As Long ' waveout handle Dim msg As String * 250 ' message buffer Dim hwnd As Long ' window handle Public Sub Initialize(hwndIn As Long) hwnd = hwndIn lpPrevWndProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindowProc) fPlaying = False fFileOpen = False startPos = 0 End Sub Public Sub CloseFile() mmioClose hmmioIn, 0 fFileOpen = False End Sub Public Sub OpenFile(soundfile As String) Dim mmckinfoParentIn As MMCKINFO Dim mmckinfoSubchunkIn As MMCKINFO Dim mmioinf As mmioinfo ' close previously open file (if any) CloseFile If (soundfile = "") Then Exit Sub End If ' Open the input file hmmioIn = mmioOpen(soundfile, mmioinf, MMIO_READ) If (hmmioIn = 0) Then MsgBox "Error opening input file, rc = " & mmioinf.wErrorRet Exit Sub End If ' Check if this is a wave file mmckinfoParentIn.fccType = mmioStringToFOURCC("WAVE", 0) rc = mmioDescendParent(hmmioIn, mmckinfoParentIn, 0, MMIO_FINDRIFF) If (rc <> MMSYSERR_NOERROR) Then
Trang 18
Trang 19
Trang 20
V d 4 : Classical
'This project needs one form ' Also set StartupObject to 'Sub Main' ' (-> Project Properties -> General Tab -> Startup Object) '---- Declarations Declare Function RegisterClass Lib "user32" Alias "RegisterClassA" (Class As WNDCLASS) As Long Declare Function UnregisterClass Lib "user32" Alias "UnregisterClassA" (ByVal lpClassName As String, ByVal hInstance As Long) As Long Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (ByVal dwExStyle As
Trang 21
Trang 22
Trang 23
Trang 24
Trang 25
' Add 2 Commandbuttons and a textbox to the form, and paste this code into the form Option Explicit Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Any, ByVal wParam As Any, ByVal lParam As Any) As Long Private Const ERROR_SUCCESS = &H0 Private Sub Form_Load() Text1.Text = "C:\WINDOWS\SYSTEM\COMCTL32.OCX"
Trang 26
V d 6 : Download File
'This project needs a TextBox '-> (Name)=Text1 '-> MultiLine=True 'in a form Private Sub Form_Load() 'KPD-Team 2000 'URL: http://www.allapi.net/ 'E-Mail: KPDTeam@Allapi.net
Trang 27
Trang 28
Trang 29
Trang 30
Trang 31
Trang 32
9.
ChildWindowFromPoint
Th vin : user32.dll H iu hnh : Windows NT 3.1 or later; Windows 95 or later
Trang 33
Nghin cu Windows API - Khai bo : Declare Function ChildWindowFromPoint Lib "user32" Alias "ChildWindowFromPoint" (ByVal hWndParent As Long, ByVal pt As POINTAPI) As Long - Cc tham s hWndParent : Cn ( handles ) ca ca s cha ( mc Parent ). pt : Tr ca im - M t : Ly cn ca ca s con ( mc Child ) u tin tho mn. - Tr tr v : Nu khng thy ca s con ( mc Child ) no tr v cn ca ca s cha (mc Parent ). Tr v 0 nu im nm ngoi ca s cha ( mc Parent ). - Cc hm lin quan : ChildWindowFromPointEx
10. ChildWindowFromPointEx
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Declare Function ChildWindowFromPointEx Lib "user32" Alias "ChildWindowFromPointEx" (ByVal hWnd As Long, ByVal pt As POINTAPI, ByVal un As Long) As Long - Cc tham s hWndParent : Cn ( handles ) ca ca s cha ( mc Parent ). pt : Tr ca im - M t : Ly cn ca ca s con ( mc Child ) u tin tho mn. - Tr tr v : Nu khng thy ca s con ( mc Child ) no tr v cn ca ca s cha (mc Parent ). Tr v 0 nu im nm ngoi ca s cha ( mc Parent ). - Cc hm lin quan : ChildWindowFromPoint:
11. ClientToScreen
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Declare Function ClientToScreen Lib "user32" Alias "ClientToScreen" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long - Cc tham s hWnd : Cn (handles) ca ca s lm cn c xc nh to . lpPoint : L bin cu trc kiu POINTAPI cha to ca s chuyn i. Nu hm thc hin thnh cng th n s copy to ca mn hnh mi vo trong cu trc ny. - M t : Chuyn to theo ca s sang to theo mn hnh. - Cc hm lin quan : ScreenToClient - Cc v d minh ho : + V d 1 : Move Cursor
'This project needs 2 Buttons Private Type POINTAPI x As Long y As Long End Type Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As
Trang 34
V d 2 : ClipCursor
Private Type RECT left As Long top As Long right As Long bottom As Long End Type Private Type POINT x As Long y As Long End Type Private Declare Sub ClipCursor Lib "user32" (lpRect As Any) Private Declare Sub GetClientRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) Private Declare Sub ClientToScreen Lib "user32" (ByVal hWnd As Long, lpPoint As POINT)
Trang 35
V d 3 : Window Placement
Private Const SW_MINIMIZE = 6 Private Type POINTAPI x As Long y As Long End Type Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Private Type WINDOWPLACEMENT Length As Long
Trang 36
12. CloseWindow
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Declare Function CloseWindow Lib "user32" Alias "CloseWindow" (ByVal hwnd As Long) As Long - Cc tham s hWnd : Cn ( handles ) ca ca s cn thu nh. - M t : Thu nh ca s .
Trang 37
Private Declare Function CloseWindow Lib "user32" (ByVal hwnd As Long) As Long Private Sub Form_Load() 'KPD-Team 2000 'URL: http://www.allapi.net/ 'E-Mail: KPDTeam@Allapi.net 'Minimize this Window CloseWindow Me.hwnd End Sub
13. CommDlgExtendedError
- Th vin : comdlg32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function CommDlgExtendedError Lib "comdlg32.dll" Alias "CommDlgExtendedError" () As Long - M t : Hm CommDlgExtendedError tr v m li t chc nng cui cng ca mt hp thoi common dialog no . Hm khng tr v m li cho bt k hm API no khc ( trong trng hp ny, dng GetLastError thay th ). Gi tr tr v ca hm khng c xc nh nu chc nng c gi sau cng ca hp thoi common dialog thnh cng. Nu c mt li xy ra vi chc nng ny, gi tr tr v chnh xc l mt trong nhng c li ca hp thoi common dialog sau y : CDERR_DIALOGFAILURE = &HFFFF Khng th m hp thoi. CDERR_FINDRESFAILURE = &H6 Tht bi khi mun tm tq nguyn cn thit. CDERR_GENERALCODES = &H0 Li lin quan n mt thuc tnh tng qut ca hp thoi common. CDERR_INITIALIZATION = &H2 Tht bi trong sut qu trnh khi to (thng l b nh khng ). CDERR_LOADRESFAILURE = &H7 Tht bi khi np ti nguyn yu cu. CDERR_LOADSTRFAILURE = &H5 Tht bi khi np chui yu cu. CDERR_LOCKRESFAILURE = &H8 Tht bi khi kho ti nguyn yu cu. CDERR_MEMALLOCFAILURE = &H9 Tht bi khi xc nh khi b nh. CDERR_MEMLOCKFAILURE = &HA Tht bi khi kho b nh yu cu. CDERR_NOHINSTANCE = &H4 Khng ng cung cp mt handles hp l ( nu handles c yu cu ). CDERR_NOHOOK = &HB Khng c cung cp mt handles ti hm hook hp l ( nu handles c yu cu ). CDERR_NOTEMPLATE = &H3 Khng c cung cp mu ban u hp l ( nu mu c yu cu ). Trang 38
Nghin cu Windows API CDERR_REGISTERMSGFAIL = &HC Khng th ng k mt thng ip ca s thnh cng. CDERR_STRUCTSIZE = &H1 c cung cp mt kch thc cu trc khng hp l. CFERR_CHOOSEFONTCODES = &H2000 Li lin quan n hp thoi Choose Font. CFERR_MAXLESSTHANMIN = &H2002 c cung cp gi tr kch thc font ln nht nh hn kch thc font nh nht c cung cp. CFERR_NOFONTS = &H2001 Khng th tm thy cc font ang tn ti. FNERR_BUFFERTOOSMALL = &H3003 c cung cp mt b m tn tp tin qu nh. FNERR_FILENAMECODES = &H3000 Li lin quan n hp thoi Open File hoc Save File. FNERR_INVALIDFILENAME = &H3002 c cung cp hay nhn mt tn tp tin khng hp l. FNERR_SUBCLASSFAILURE = &H3001 Khng b nh phn lp hp danh sch. FRERR_BUFFERLENGTHZERO = &H4001 c cung cp mt b m khng hp l. FRERR_FINDREPLACECODES = &H4000 Li lin quan n hp thoi Find hoc Replace. PDERR_CREATEICFAILURE = &H100A Khng th to mt ng cnh thng tin. PDERR_DEFAULTDIFFERENT = &H100C ang c thng tin c cung cp ca my in mc nh, nhng thc s th cc thit lp my in mc nh li khc. PDERR_DNDMMISMATCH = &H1009 D liu trong hai cu trc d liu m t cc my in khc nhau ( tc l chng cha cc thng tin mu thun ). PDERR_GETDEVMODEFAIL = &H1005 Driver my in tht bi khi khi to cu trc DEVMODE. PDERR_INITFAILURE = &H1006 Tht bi trong qu trnh khi to. PDERR_LOADDRVFAILURE = &H1004 Tht bi khi np driver thit b yu cu. PDERR_NODEFAULTPRN = &H1008 Khng th tm thy my in mc nh. PDERR_NODEVICES = &H1007 Khng th tm thy bt k my in no. PDERR_PARSEFAILURE = &H1002 Th bi khi phn tch cc chui quan h vi my in trong WIN.INI PDERR_PRINTERCODES = &H1000 Li lin quan n hp thoai Print. PDERR_PRINTERNOTFOUND = &H100B Khng th tm thy thng tin trong WIN.INI v my in c yu cu. PDERR_RETDEFFAILURE = &H1003 Cc handles ti cu trc d liu c cung cp l khc khng d hm c yu cu tr thng tin v my in mc nh. PDERR_SETUPFAILURE = &H1001 Nguyn Nam Trung Trang 39
Nghin cu Windows API Tht li khi np nhng ti nguyn yu cu. Cc hm lin quan : + CHOOSECOLOR + GetOpenFileName + CHOOSEFONT + GetSaveFileName + PrintDialog + PAGESETUPDLG + GetLastError Cc v d minh ho : + V d 1 : CommDlgExtendedError
Const CDERR_DIALOGFAILURE = &HFFFF Const CDERR_FINDRESFAILURE = &H6 Const CDERR_GENERALCODES = &H0 Const CDERR_INITIALIZATION = &H2 Const CDERR_LOADRESFAILURE = &H7 Const CDERR_LOADSTRFAILURE = &H5 Const CDERR_LOCKRESFAILURE = &H8 Const CDERR_MEMALLOCFAILURE = &H9 Const CDERR_MEMLOCKFAILURE = &HA Const CDERR_NOHINSTANCE = &H4 Const CDERR_NOHOOK = &HB Const CDERR_REGISTERMSGFAIL = &HC Const CDERR_NOTEMPLATE = &H3 Const CDERR_STRUCTSIZE = &H1 Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As Any) As Long Private Declare Function CommDlgExtendedError Lib "comdlg32.dll" () As Long Private Sub Form_Load() 'KPD-Team 1999 'URL: http://www.allapi.net/ 'E-Mail: KPDTeam@Allapi.net 'We're letting the GetOpenFileName-function crash GetOpenFileName ByVal 0& Select Case CommDlgExtendedError Case CDERR_DIALOGFAILURE MsgBox "The dialog box could not be created." Case CDERR_FINDRESFAILURE MsgBox "The common dialog box function failed to find a specified resource." Case CDERR_INITIALIZATION MsgBox "The common dialog box function failed during initialization." Case CDERR_LOADRESFAILURE MsgBox "The common dialog box function failed to load a specified resource." Case CDERR_LOADSTRFAILURE MsgBox "The common dialog box function failed to load a specified string." Case CDERR_LOCKRESFAILURE MsgBox "The common dialog box function failed to lock a specified resource." Case CDERR_MEMALLOCFAILURE MsgBox "The common dialog box function was unable to allocate memory for internal structures." Case CDERR_MEMLOCKFAILURE
Trang 40
V d 2 : Cho hp thoi Open File mt kch thc b m khng . Sau hin th m li cung cp.
'Trich tu Cam Nang Lap Trinh Windows API - NXB Giao Thong Van Tai 'Cho hop thoai Open File mot kich thuoc bo dem khong du. Sau do 'Hien thi ma loi da cung cap cho hop thoai OpenFile Private Type OPENFILENAME lStructSize As Long hwndOwner As Long hInstance As Long lpstrFilter As String lpstrCustomFilter As String nMaxCustFilter As Long nFilterIndex As Long lpstrFile As String nMaxFile As Long lpstrFileTitle As String nMaxFileTitle As Long lpstrInitialDir As String lpstrTitle As String flags As Long nFileOffset As Integer nFileExtension As Integer lpstrDefExt As String lCustData As Long lpfnHook As Long lpTemplateName As String End Type
Trang 41
14. CopyRect
Th vin : user32.dll
Trang 42
Nghin cu Windows API - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function CopyRect Lib "user32" Alias "CopyRect" (lpDestRect As RECT, lpSourceRect As RECT) As Long - Cc tham s lpDestRect : Hnh ch nht ch thit lp ( s nhn kt qu ). lpSourceRect : Hnh ch nht ngun ( b copy ). - M t : Hm CopyRect sao ni dung hnh ch nht. Hm ny gn mt hnh ch nht bng vi mt hnh ch nht khc. iu ny c thc hin bng cch gp i tt c gi tr thnh phn ca hnh ch nht ngun ti nhng gi tr tng ng trong hnh ch nht ch. Vic ny nhanh hn l phi gn bn to chnh bng m. - Tr tr v : Hm tr v gi tr 0 nu c mt li xy ra, hoc 1 nu thnh cng. - Cc hm lin quan : + EqualRect + SetRect + SetRectEmpty - Cc v d minh ho : CopyRect
Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Private Declare Function CopyRect Lib "user32" (lpDestRect As RECT, lpSourceRect As RECT) As Long Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long Private Sub Form_Load() 'KPD-Team 1999 'URL: http://www.allapi.net/ 'E-Mail: KPDTeam@Allapi.net Dim rectWindow As RECT, rectCopy As RECT 'Get the bounding rectangle of this window GetWindowRect Me.hwnd, rectWindow 'Copy the rectangle CopyRect rectCopy, rectWindow MsgBox "This form's width:" + Str$(rectCopy.Right - rectCopy.Left) + " pixels" End Sub
15. DeferWindowPos
Th vin : user32.dll H iu hnh : Windows NT 3.1 or later; Windows 95 or later Khai bo :
Public Declare Function DeferWindowPos Lib "user32" Alias "DeferWindowPos" (ByVal hWinPosInfo As Long, 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
Trang 43
Nghin cu Windows API Cc tham s hWinPosInfo : Cn ca cu trc bn , n cha ng thng tin kch thc v v tr ca mt hoc nhiu ca s. Cu trc ny c tr v (return) bi hm BeginDeferWindowPos hoc bi li gi hm DeferWindowPos. hWnd : Ca s cn nh v. hWndInsertAfter : Cn ca ca s m ca s hWnd t sau n trong danh sch. N c th l mt trong cc hng sau : o HWND_BOTTOM : t ca s v cui danh sch. Nu tham s hWnd xc nh mt ca s nm trn cng v c t cui tt c cc ca s khc. o HWND_NOTOPMOST : t ca s nm trn tt c cc ca s khc tr ca s topmost ( ngha l t ng sau tt c cc ca s topmost ). C ny s khng c hiu lc nu ca s ny l ca s non-topmost. o HWND_TOP : t ca s u danh sch. o HWND_TOPMOST : t ca s u danh sch ln trn cng nhn thy c. Ca s ny s lun lun nm trn tt c cc ca s khc thm tr khi n trng thi khng hot ng, tham s ny s khng c hiu lc nu c SWP_NOZORDER c bt ln trong tham s wFlags. x : Honh ca ca s hWnd theo to ca ca s cha (mc Parent) n. y : Tung ca ca s hWnd theo to ca ca s cha (mc Parent) n. cx : Chiu rng ca ca s mi. cy : Chiu cao ca ca s mi. wFlags : C xc nh kch thc v v tr ca ca s, c kt hp bi nhng hng s sau : o SWP_DRAWFRAME : V khung bao quanh ca s. o SWP_FRAMECHANGED : Gi thng ip WM_NCCALCSIZE n ca s cho d kch thc ca ca s khng thay i. Nu c ny cha c ch r (khng s dng) th thng ip WM_NCCALCSIZE ch c gi i khi kch thc ca ca s thay i. o SWP_HIDEWINDOW : n ca s. o SWP_NOACTIVATE : Khng kch hot ca s. Nu khng thit lp c ny, th ca s s c kch hot v di chuyn ln u ca ca s topmost hoc nontopmost (ph thuc vo s thit lp ca tham s hWndInsertAfter). o SWP_NOCOPYBITS : Hu b ton b ni dung ca vng Client. Nu c ny khng c thit lp th ni dung ca vng Client s c lu li v copied sau vo trong vng Client sau ca s c xc nh. o SWP_NOMOVE : Gi nguyn v tr hin ti ( b qua cc tham s x v y ). o SWP_NOOWNERZORDER : Khng thay i v tr ca ca s cha me trong danh sch. o SWP_NOREDRAW : Khng t ng v li. Nu c ny c thit lp th n s khng v li bt k ca s no xut hin. N c p dng trong vng client v nonclient (bao gm c thanh tiu v thanh cun), v bt k phn no ca ca s cha m khi c ca s khc che lp. o SWP_NOREPOSITION : Ging nh c SWP_NOOWNERZORDER. o SWP_NOSENDCHANGING : Ngn cn ca s nhn thng ip WM_WINDOWPOSCHANGING. o SWP_NOSIZE : Gi nguyn kch thc ( b qua cc tham s cx v cy ). o SWP_NOZORDER : Gi nguyn v tr hin hnh trong danh sch ( b qua tham s hWndInsertAfter ). o SWP_SHOWWINDOW : Hin th ca s.
Trang 44
Nghin cu Windows API M t : Hm DeferWindowPos nh ngha v tr ca ca s mi qua ca s khai bo v a vp cu trc bn ni b cha v tr cc ca s. Tr tr v : Long Cn (handle) mi i vi cu trc bn cha thng tin cp nht v tr. Tr v 0 nu tht bi. Cc hm lin quan : + BeginDeferWindowPos : to ra cu trc + EndDeferWindowPos : s dng thng tin trong cu trc ny thay i v tr v kch thc ca mt s ca s. Cc v d minh ho : DeferWindowPos
Const WS_BORDER = &H800000 Const WS_DLGFRAME = &H400000 Const WS_THICKFRAME = &H40000 Const WS_CAPTION = &HC00000 Const HWND_BOTTOM = 1 Const HWND_TOP = 0 Const HWND_TOPMOST = -1 Const HWND_NOTOPMOST = -2 Const SWP_SHOWWINDOW = &H40 Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type
Private Declare Function AdjustWindowRect Lib "user32" (lpRect As RECT, ByVal dwStyle As Long, ByVal bMenu As Long) As Long Private Declare Function BeginDeferWindowPos Lib "user32" (ByVal nNumWindows As Long) As Long Private Declare Function DeferWindowPos Lib "user32" (ByVal hWinPosInfo As Long, 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 EndDeferWindowPos Lib "user32" (ByVal hWinPosInfo As Long) As Long Private Sub Form_Load() 'KPD-Team 2000 'URL: http://www.allapi.net/ 'E-Mail: KPDTeam@Allapi.net Dim R As RECT, hDWP As Long R.Left = 30 R.Top = 30 R.Bottom = 200 R.Right = 120 AdjustWindowRect R, WS_THICKFRAME Or WS_CAPTION, False hDWP = BeginDeferWindowPos(1) DeferWindowPos hDWP, Me.hwnd, HWND_TOP, R.Left, R.Top, R.Right - R.Left, R.Bottom R.Top, SWP_SHOWWINDOW EndDeferWindowPos hDWP End Sub
Trang 45
16. DefWindowProc
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function DefWindowProc Lib "user32" Alias "DefWindowProcA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long - Cc tham s hWnd : Cn (handle) ca ca s x l thng ip. wMsg : Thng ip cn x l. wParam : Thng tin m rng v thng ip. Ni dung ca tham s ny ph thuc vo gi tr ca tham s wMsg. lParam : Thng tin m rng v thng ip. Ni dung ca tham s ny ph thuc vo gi tr ca tham s wMsg. - M t : Hm DefWindowProc gi tng minh th tc window mc nh ca h iu hnh x l mt thng ip cho mt ca s. Th tc window mc nh ny cung cp chc nng cn thit ti thiu cho mt th tc window v nn c dng cung cp hin thc mc nh ca thng ip ca s. - Tr tr v : Gi tr tr v ca hm ny l gi tr tr v ca thng ip c x l. - Cc hm lin quan : CallWindowProc - Cc v d minh ho : + V d 1 : Hotkey
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long Private Declare Function DefWindowProc Lib "user32" Alias "DefWindowProcA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Const WM_SETHOTKEY = &H32 Const WM_SHOWWINDOW = &H18 Const HK_SHIFTA = &H141 'Shift + A Const HK_SHIFTB = &H142 'Shift + B Const HK_CONTROLA = &H241 'Control + A Const HK_ALTZ = &H45A 'The value of the key-combination has to 'declared in lowbyte/highbyte-format 'That means as a hex-number: the last two 'characters specify the lowbyte (e.g.: 41 = a), 'the first the highbyte (e.g.: 01 = 1 = Shift) Private Sub Form_Load() 'KPD-Team 1999 'URL: http://www.allapi.net/ 'E-Mail: KPDTeam@Allapi.net Me.WindowState = vbMinimized 'Let windows know what hotkey you want for 'your app, setting of lParam has no effect erg& = SendMessage(Me.hwnd, WM_SETHOTKEY, HK_ALTZ, 0) 'Check if succesfull If erg& <> 1 Then MsgBox "You need another hotkey", vbOKOnly, "Error" End If
Trang 46
V d 2 : Classical
'This project needs one form ' Also set StartupObject to 'Sub Main' ' (-> Project Properties -> General Tab -> Startup Object) '---- Declarations Declare Function RegisterClass Lib "user32" Alias "RegisterClassA" (Class As WNDCLASS) As Long Declare Function UnregisterClass Lib "user32" Alias "UnregisterClassA" (ByVal lpClassName As String, ByVal hInstance As Long) As Long 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 Declare Function DefWindowProc Lib "user32" Alias "DefWindowProcA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Declare Sub PostQuitMessage Lib "user32" (ByVal nExitCode As Long) Declare Function GetMessage Lib "user32" Alias "GetMessageA" (lpMsg As Msg, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long) As Long Declare Function TranslateMessage Lib "user32" (lpMsg As Msg) As Long Declare Function DispatchMessage Lib "user32" Alias "DispatchMessageA" (lpMsg As Msg) As Long Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long Declare Function LoadCursor Lib "user32" Alias "LoadCursorA" (ByVal hInstance As Long, ByVal lpCursorName As Any) As Long Declare Function DefMDIChildProc Lib "user32" Alias "DefMDIChildProcA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long ' Define information of the window (pointed to by hWnd) Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Type WNDCLASS style As Long lpfnwndproc As Long cbClsextra As Long cbWndExtra2 As Long hInstance As Long hIcon As Long hCursor As Long hbrBackground As Long lpszMenuName As String lpszClassName As String End Type
Trang 47
Or
Trang 48
Trang 49
Trang 50
V d 3 : To th tc VB
Trang 51
17. DestroyWindow
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function DestroyWindow Lib "user32" Alias "DestroyWindow" (ByVal hwnd As Long) As Long - Cc tham s hWnd : Cn ca ca s s ph hu. - M t : Hm DestroyWindow ph hu ca s (k c cc ca s con ca n). Hm ny s gi thng ip WM_DESTROY v WM_NCDESTROY n ca s nhm ngng hot ng ca s v xo b focus ti n. Hm ny cng ph hu menu, thng ip trong hng i, ph hu timers, xo b quyn s hu clipboard, - Tr tr v : S nguyn khc 0 nu thnh cng v bng 0 nu tht bi. - Cc hm lin quan : + MoveWindow + IsWindowEnabled - Cc v d minh ho :
Trang 52
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
Trang 53
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&)
Trang 54
18. EnableWindow
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function EnableWindow Lib "user32" Alias "EnableWindow" (ByVal hwnd As Long, ByVal fEnable As Long) As Long - Cc tham s hWnd : mt handle ca ca s (hoc control) c enable hay b disable. fEnable : Nu bng 0, ca s hoc control s b disable. Nu khc 0 , ca s s c enable
Trang 55
Nghin cu Windows API M t : Hm EnableWindow dng lm tch cc (enable) hoc b ng (disable) mt ca s hoc mt iu khin ngi dng (control). Nu mt ca s hoc mt control b disable, n s khng nhn focus tc khng th tng tc vi ngi dng. Mt vi loi control nh nt nhn v cc loi control khc s c mu xm khi b disable, mc d bt k ca s no cng c enable hay b disable. Tr tr v : Hm s tr v 0 nu trc ca s (hoc control) ang enable; tr v gi tr khc 0 nu trc n b disable. Cc hm lin quan : IsWindowEnabled Cc 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: http://www.allapi.net/ '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
o trng thi enable ca control commandl, nu control ang b disable th cho php hot ng; nu ang hot ng th disable n . Dim wasenabled as long cha trng thi enable/disable ca commandl Dim retval as long gi tr tr v Xc nh control commandl hin ti c enable hay khng. Wasenabled = IswindowEnable(commandl.hwnd) If wasenabled = 0 then nu khng c enable th enable n retval = Enablewindow (commandl.hwnd,1) Else nu ang uc enable th disable n Retval = Enablewindow (commandl.hwnd,0) End if.
19. EndDeferWindowPos
Th vin : user32.dll H iu hnh : Windows NT 3.1 or later; Windows 95 or later Khai bo :
Trang 56
Nghin cu Windows API Public Declare Function EndDeferWindowPos Lib "user32" Alias "EndDeferWindowPos" (ByVal hWinPosInfo As Long) As Long - Cc tham s hWnd - M t : Hm EndDeferWindowPos cp nht v tr, kch thc v tnh trng ca mt hoc nhiu ca s trong mt chu trnh l ti (refreshing) ca mn hnh n. - Cc hm lin quan : + BeginDeferWindowPos + DeferWindowPos - Cc v d minh ho : Xem v d hm DeferWindowPos (hm s 15).
20. EnumChildWindows
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function EnumChildWindows Lib "user32" Alias "EnumChildWindows" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long - Cc tham s hWndParent : Mt handle ca ca s cha m ta cn lit k cc ca s con ca n. lpEnumFunc : Con tr ch ti hm callback EnumChildProc. lParam : Gi tr m rng truyn vo hm callback. - M t : Hm EnumChildWindows lit k v cung cp cc handle cho tt c cc ca s con ca mt ca s. Hm ny cng lit k bt k ca s con no ca cc ca s con. Mi ln mt ca s con c xc nh, hm s truyn handle ca n vo mt hm callback c nh ngha trong chng trnh. Hm tip tc thc hin cho n khi tt c cc ca s con u c lit k, hoc cho n khi tin trnh b ngng (hm callback tr v FALSE). Phi c Custom Control CBK.VBX mi s dng c. - Tr tr v : Nu xy ra li, hm tr v 0 (dng hm GetLastError nhn m li. Nu thnh cng hm tr v mt gi tr khc 0. - Cc hm lin quan : + EnumThreadWindows + EnumWindows - Cc v d minh ho : EnumChildWindows
'in a form Private Sub Form_Load() 'KPD-Team 2000 'URL: http://www.allapi.net/ '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,
Trang 57
21. EnumThreadWindows
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function EnumThreadWindows Lib "user32" Alias "EnumThreadWindows" (ByVal dwThreadId As Long, ByVal lpfn As Long, ByVal lParam As Long) As Long - Cc tham s dwThreadld : Mt nh danh ca thread cn lit k cc ca s ca n. lpfn : Mt con tr ca hm callback EnumThreadWndProc. lParam : Gi tr m rng c truyn vo hm callback. - M t : Hm EnumThreadWindows lit k v cung cp cc handle cho tt c cc ca s c s hu v iu khin bi mt thread. (Ch , cc ca s ny bao gm c cc ca s khng nhn thy c i vi ngi dng). Mi ln mt ca s c nh v, hm truyn handle ca n cho hm callback. Hm tip tc thc hin cho n khi tt c cc ca s u c lit k, hoc cho n khi tin trnh b b d. - Tr tr v : Nu xy ra li, hm tr v gi tr 0 (dng hm GetLastError ly m li). Nu thnh cng, hm tr v mt gi tr khc 0. - Cc hm lin quan : + EnumChildWindows + EnumWindows + GetCurrentThread - Cc v d minh ho : Enum Classnames
'in a form Private Sub Form_Load() 'KPD-Team 2000 'URL: http://www.allapi.net/ '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
Trang 58
22. EnumWindows
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function EnumWindows Lib "user32.dll" ( ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long Cc tham s lpEnumFunc : Mt con tr ch n hm callback EnumWindowsProc lParam : Mt gi tr m rng c truyn vo hm callback. M t : Hm EnumWindows lit k v cung cp handle cho tt c cc ca s top-level hin ang m. Hm ny s b qua cc ca s con. (Ch cc ca s top-level bao gm c cc ca s khng hin ra). Mi ln mt ca s c nh v, hm s truyn handle cho mt hm callback. Hm ny lin tc thc hin cho n khi tt c cc ca s u c lit k hay cho n khi tin trnh c b qua. Tr tr v : Nu xy ra li, hm tr v 0 (dng hm GetLastError nhn m li). Nu thnh cng, hm tr v mt gi tr khc 0. Cc hm lin quan : + EnumChildWindows + EnumThreadWindows Cc v d minh ho : EnumWindows
Trang 59
23. EnumWindowStations
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function EnumWindowStations Lib "user32" Alias "EnumWindowStationsA" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long - Cc tham s lpEnumFunc : Bin con tr ch n hm gi i vi mi ca s con (mc Child). S dng tnh cht ProcAddress ca Custom Control CBK.VBX nhn hm bin tr (function pointer) gi li (callbacks). lParam : Gi tr chuyn n cho s kin EnumWindows ca Custom Control trong lc lit k. ngha ca tr ny do lp trnh vin xc nh. - M t : Lit k danh sch ca s cp trn, cha ca s khai bo. Phi c Custom Control CBK.VBX mi s dng c. - Tr tr v : Hm tr v TRUE (khc 0) nu thnh cng, ngc li hm tr v FALSE (bng 0) nu tht bi. - Cc hm lin quan : + EnumChildWindows + EnumThreadWindows + EnumWindows
Trang 60
24. FindWindow
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long - Cc tham s lpClassName : Tn ca lp ca s hoc ca s cn tm. lpWindowName : Tn ta ca ca s cn tm, nu tham s ny l NULL th tt c cc tn ca s u hp l. - M t : Hm FindWindow tm tt c cc ca s ph hp vi mt tn lp ca s v / hoc tn ca s (chnh l tiu ca ca s). Hm tm kim khng phn bit cha hoa hay ch thng. Nu bn khng mun ch r mt khng s no , hy truyn chui rng cho n ( truyn chui rng cho mt thng s, dng hng vbNullString). - Tr tr v : Nu xy ra li, hoc khng tm thy ca s thch hp, hm tr v 0 (dng hm GetLastError nhn m li). Nu thnh cng hm s tr v mt handle ca ca s tm thy. - Cc hm lin quan : + FindWindowEx + GetActiveWindow + GetForegroundWindow - Cc 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: http://www.allapi.net/ '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)
Trang 61
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: http://www.allapi.net/ '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)
Trang 62
+ + +
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: http://www.allapi.net/ '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 vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : 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 - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan : - Cc v d minh ho :
26. FlashWindow
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function FlashWindow Lib "user32" Alias "FlashWindow" (ByVal hwnd As Long, ByVal bInvert As Long) As Long - Cc tham s hWnd - M t :
Trang 63
27. FlashWindowEx
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function FlashWindowEx Lib "user32.dll" (ByRef pfwi As PFLASHWINFO ) As Long - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan : - Cc v d minh ho :
28. GetActiveWindow
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function GetActiveWindow Lib "user32" Alias "GetActiveWindow" () As Long - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan : - Cc v d minh ho :
29. GetClassInfo
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function GetClassInfo Lib "user32" Alias "GetClassInfoA" (ByVal hInstance As Long, ByVal lpClassName As String, lpWndClass As WNDCLASS) As Long - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan : - Cc v d minh ho :
30. GetClassInfoEx
Th vin : user32.dll H iu hnh : Windows NT 3.1 or later; Windows 95 or later Khai bo : Trang 64
Nghin cu Windows API Public Declare Function GetClassInfoEx Lib "user32.dll" Alias "GetClassInfoExA" ( ByVal hinstance As Long, ByVal lpcstr As String, ByRef lpwndclassexa As WNDCLASSEX) As Long - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan : - Cc v d minh ho :
31. GetClassLong
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function GetClassLong Lib "user32" Alias "GetClassLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan : - Cc v d minh ho :
32. GetClassName
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan : - Cc v d minh ho :
33. GetDesktopWindow
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function GetDesktopWindow Lib "user32" Alias "GetDesktopWindow" () As Long - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan :
Trang 65
34. GetParent
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function GetParent Lib "user32" Alias "GetParent" (ByVal hwnd As Long) As Long - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan : - Cc v d minh ho :
35. GetWindow
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function GetWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wCmd As Long) As Long - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan : - Cc v d minh ho :
36. GetWindowLong
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan : - Cc v d minh ho :
37. GetWindowRect
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function GetWindowRect Lib "user32" Alias "GetWindowRect" (ByVal hwnd As Long, lpRect As RECT) As Long Trang 66
38. GetWindowText
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan : - Cc v d minh ho :
39. GetWindowTextLength
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function GetWindowTextLength Lib "GetWindowTextLengthA" (ByVal hwnd As Long) As Long - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan : - Cc v d minh ho :
"user32"
Alias
40. GetWindowThreadProcessId
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function GetWindowThreadProcessId Lib "user32" Alias "GetWindowThreadProcessId" (ByVal hwnd As Long, lpdwProcessId As Long) As Long - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan : - Cc v d minh ho :
41. GetWindowWord
Nguyn Nam Trung Trang 67
Nghin cu Windows API - Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function GetWindowWord Lib "user32" Alias "GetWindowWord" (ByVal hwnd As Long, ByVal nIndex As Long) As Integer - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan : - Cc v d minh ho :
42. InvalidateRect
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function InvalidateRect Lib "user32" Alias "InvalidateRect" (ByVal hwnd As Long, lpRect As RECT, ByVal bErase As Long) As Long - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan : - Cc v d minh ho :
43. IsChild
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function IsChild Lib "user32" Alias "IsChild" (ByVal hWndParent As Long, ByVal hwnd As Long) As Long - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan : - Cc v d minh ho :
44. IsIconic
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function IsIconic Lib "user32" Alias "IsIconic" (ByVal hwnd As Long) As Long - Cc tham s hWnd - M t :
Trang 68
45. IsRectEmpty
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function IsRectEmpty Lib "user32" Alias "IsRectEmpty" (lpRect As RECT) As Long - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan : - Cc v d minh ho :
46. IsWindow
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function IsWindow Lib "user32" Alias "IsWindow" (ByVal hwnd As Long) As Long - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan : - Cc v d minh ho :
47. IsWindowEnabled
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function IsWindowEnabled Lib "user32" Alias "IsWindowEnabled" (ByVal hwnd As Long) As Long - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan : - Cc v d minh ho :
48. IsWindowVisible
Th vin : user32.dll H iu hnh : Windows NT 3.1 or later; Windows 95 or later Khai bo : Trang 69
Nghin cu Windows API Public Declare Function IsWindowVisible Lib "user32" Alias "IsWindowVisible" (ByVal hwnd As Long) As Long - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan : - Cc v d minh ho :
49. IsZoomed
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function IsZoomed Lib "user32" Alias "IsZoomed" (ByVal hwnd As Long) As Long - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan : - Cc v d minh ho :
50. LockWindowUpdate
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function LockWindowUpdate Lib "user32" Alias "LockWindowUpdate" (ByVal hwndLock As Long) As Long - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan : - Cc v d minh ho :
51. MapWindowPoints
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : 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 - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan :
Trang 70
52. MoveWindow
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : 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 - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan : - Cc v d minh ho :
53. PostMessage
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : 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 - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan : - Cc v d minh ho :
54. RedrawWindow
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : 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 - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan : - Cc v d minh ho :
55. RegisterClass
Th vin : user32.dll H iu hnh : Windows NT 3.1 or later; Windows 95 or later Khai bo :
Trang 71
Nghin cu Windows API Public Declare Function RegisterClass Lib "user32" Alias "RegisterClassA" Alias "RegisterClass" (Class As WNDCLASS) As Long - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan : - Cc v d minh ho :
56. RegisterClassEx
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function RegisterClassEx Lib "user32" Alias "RegisterClassExA" (pcWndClassEx As WNDCLASSEX) As Integer - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan : - Cc v d minh ho :
57. ScreenToClient
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function ScreenToClient Lib "user32" Alias "ScreenToClient" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan : - Cc v d minh ho :
58. SendMessage
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : 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 - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan : - Cc v d minh ho :
Trang 72
59. SetActiveWindow
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function SetActiveWindow Lib "user32" Alias "SetActiveWindow" (ByVal hwnd As Long) As Long - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan : - Cc v d minh ho :
60. SetClassLong
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function SetClassLong Lib "user32" Alias "SetClassLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan : - Cc v d minh ho :
61. SetForegroundWindow
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function SetForegroundWindow Lib "SetForegroundWindow" (ByVal hwnd As Long) As Long - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan : - Cc v d minh ho :
"user32"
Alias
62. SetParent
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function SetParent Lib "user32" Alias "SetParent" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long - Cc tham s Trang 73
63. SetWindowLong
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan : - Cc v d minh ho :
64. SetWindowPos
- Th vin : user32.dll - H iu hnh : Windows NT 3.1 or later; Windows 95 or later - Khai bo : 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 - Cc tham s hWnd - M t : - Tr tr v : - Cc hm lin quan : - Cc v d minh ho :
Trang 74
Nghin cu Windows API M t : hm SetWindowRgn dng thit lp min lm vic (hnh dng) ca ca s. Min lm vic ny l phn din tch bn trong ca s ni h iu hnh cho php v. H iu hnh s khng hin th bt k vng no bn ngoi min lm vic ny. Cc hm lin quan : CreateEllipticRgn Cc 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
Trang 75
Trang 76
'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
Trang 77
+ 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: http://www.allapi.net/ '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
Trang 78
Trang 79
Trang 80