Friday, October 15, 2010

Feeling so steady and VB API (1)


Write VB program, frequently encountered problem is that VB gives us something we would use to prepare, control Zuzhuang is a process, but once the VB did not want to implement functions provided directly, broke new. In fact, Windows operating system itself, we are prepared to give a lot of things, VB does not, perhaps Windows has. We certainly hope to spend less effort, a little more effect. Here to talk about the content of that - learn to use Windows to us what: API.

However, the learned one thing to pay a considerable effort, API is the same, it can bring great convenience to us, but want to grasp it, it is not so easy to use several control things. So in reading this article, hope that readers can adopt a correct attitude is to learn programming is not for fun, but for use; do not have their own interest and in this regard to see, but for the API is for you program brought effect to watch. I'm not saying everything is so serious and difficult, but on the VB API is already available for the application of advanced operator, so "serious" and "careful" is needed. Well, relax, let's step by step from now appreciate the benefits of it API.

Preface

1.API

All API called Application Programming Interface, literal translation, you might call it "application program interface." From the sense, API is an operating system or a program itself to other uses of the function. In the Windows operating system, there are thousands of functions available to the Windows application uses, the paper said the API, is that these functions.

2.VB and API

The reason I wrote this article (not written VC or other language) because VB is not a direct support to the API, and is incomplete; in use, Windows's API is the assumption that the caller when the preparation is the C or C + + language, VB API call is not very convenient, are often unnecessary errors or do not understand how to use the situation. This object is mainly a deep study on the API does not readers, if you do not want to know too much detail, you can use an appropriate function to move past, or you do not know the API, or only know a few, not many functions clear how, or if you wish to use the API from the article to learn more skills or techniques VB (hope I can let you learn), I think you should read this article. But there is a major premise, you must already be using VB's readers, as mentioned after the contents of an entire project from start to finish will not teach you to do, there may be a brief statement and call the code, it may be a combination of several functions, if necessary, will talk about the content of VB related skills, but will not have a complete instance of the model.

3. This principle and agreed

As the API usage in some simple, some complex, some can be used alone, but some not, with the primary purpose of each API is different from the first to say which is difficult to judge to say what can make people easier to understand, so as far as possible from this comparison Speaking of common, from can have a greater effect on the program talking about. In order to allow most people to understand, if related to other knowledge, but also will try to talk about, so readers can learn the knowledge to use API and can use the knowledge in this article meet the new API. An API if you have any questions, please email (webmaster@neocactus.com), but due to time constraints, not on the letter to respond to, if necessary, will be described in the text.

Basics

Before talking about API, let me first explain some basic knowledge of VB API-related, will no longer be involved later in any detail. Not mentioned here will be the first contact in this article do explain.

1. Custom type

Type keywords you can use VB to an existing data type combinations, a new type, the type known as user-defined types. Such as:

Type NewType
sName As String
lNumber As Long
End Type

Define a custom type called NewType. After Dim MyType As NewType can define a variable of type NewType.

sName As String, there are two types of variables, one is longer, which runs the length of the string is variable, another is a fixed length, running the length of the string is fixed. Usual we define a string variable: Dim strA As String that defines a variable-length string, but when using the API often use the fixed-length string, it should be so defined: Dim strB As String backup bin bin_old conf config crawler.tar.gz crawler_bin.tar.gz data eshow eshow_sitemap.html generate.sh google.html google.html.md5 log maint news: 10 news: 11 news: 12 news: 13 news: 14 news: 15 news: 16 news : 17 news: 18 news: 2 news: 3 news: 4 news: 5 news: 6 news: 7 news: 8 news: 9 outboundLinksMgr.sql seeds sitemap.html svn tasks tmp xml2dict-2008.6-tar.gz xml2dict-read- only 30, which defines a character can hold 30 bytes of variables.

2. Statement

Before using the API in VB, need to be declared on the API, the statement is to use the Declare keyword, such as:
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
Declares the API function called SendMessage. Many API's statement can be found in the API browser, and this paper describes an API will also give a statement when more in-depth knowledge will be described later.

3. Handle

API most frequently used word is probably "handle" the. If you want to put it more complicated, the handle can indeed say a lot, but as a VB application, we can be more simple to understand it. Can be said that handle is given Windows system memory for each object, including windows, buttons (actually also a window), or file, icons, menus, etc. all things identity. Like the so-called identity as identity cards, and will not be repeated, and corresponded to the actual object. Its role is to let Windows know to be the targets of the operation. Many VB controls provide a long form of the hWnd property, under normal circumstances, this property is the control handle to control the control with the API need to use this property when the.

4. Device context

Actually, I think "device context" read very strange word, but its literal terms, Device Context (DC, not the DC or DreamCast oh) is the mean, so many people called it. But I would like to translate as "intermediary device" Everyone should be easier to understand it. It is the role of computer equipment and procedures as an intermediary between, such as displays and procedures, or between the printer and between programs. In the operation of these devices when it is operated through the intermediary (and handle similar), some control, such as Picture, when drawing to the above, we must use the hDC property.




[Next]



5. Explicit statement and automatically saved

By default, Visual Basic will not declare a variable that is a new variable (even if you're not careful typing error in a), it is prone to error, but also to identify such errors are often not easy. With API will basically involve the operation of the system itself, once the error is likely to occur Visual Basic crashes and even system crashes. So to set the Visual Basic option selected on the Require Variable Declaration (required variable declaration). In Enviroment (Environment) page When a program starts (when the program starts) Department elected Prompt To Save Changes (prompted to save) or Save Changes (auto save).杩欐牱Visual Basic浼氭鏌ュ彉閲忔槸鍚﹀凡缁忓0鏄庯紝涓嶅啀鍏佽娌℃湁澹版槑鐨勫彉閲忓嚭鐜颁簡銆?br />
銆??6.API娴忚鍣?br />
銆??鍓嶉潰璁测?澹版槑鈥濈殑鏃跺?鎻愬埌浜咥PI娴忚鍣紝杩欓噷涔熻涓?瀹冦?鍦ㄥ畨瑁呭畬VB鏃讹紝瀹夎绋嬪簭浼氭妸API娴忚鍣ㄥ鍒跺埌浣犵殑璁$畻鏈洪噷銆傚惎鍔ㄥ悗鐣岄潰濡傚浘1锛堜互VB6涓哄噯锛夈?鍙互鐪嬪埌锛屼粠API娴忚鍣ㄩ噷鎴戜滑鍙互寰楀埌API鐨勫0鏄庛?甯搁噺鍊间互鍙婁笌API鐩稿叧鐨勮嚜瀹氫箟绫诲瀷鐨勫畾涔夈?鍏朵腑Declare Scope锛堝0鏄庤寖鍥达級鏄寚璇ュ0鏄庢槸鍏湁鐨勮繕鏄鏈夌殑銆傚浜庡湪鏍囧噯妯″潡涓殑澹版槑锛屽鏋滃0鏄庝负绉佹湁锛屽垯鍙璇ユā鍧楁湁鏁堬紝濡傛灉涓哄叕鏈夛紝鍒欏鏁翠釜绋嬪簭閮芥湁鏁堛?鍦ㄧ獥浣撴ā鍧椾腑鍙兘澹版槑涓虹鏈夈?

銆??7.MSDN

銆??MSDN鏄井杞彂甯冪殑涓?瀹屾暣鐨刉indows寮?彂鑰呮妧鏈枃妗o紙濡傚浘2锛夈?閲岄潰涓嶄粎鏈塚B銆乂C锛嬶紜銆乂FP銆丮SDEV銆乂SS绛夊紑鍙戝伐鍏风殑瀹屾暣甯姪锛岃?涓斿寘鍚簡Windows骞冲彴寮?彂鐨勫嚑涔庢墍鏈夌殑鎶?湳璧勬枡锛屽苟涓嶆柇鍦版洿鏂般?鎴戜滑闇?鐨凙PI璧勬枡杩欓噷閮芥湁寰堣缁嗙殑璇存槑锛屽寘鎷娇鐢ㄥ钩鍙般?鍙傛暟绫诲瀷銆佸弬鏁颁綔鐢ㄧ瓑锛堝綋鐒跺畠骞堕潪瀹屽叏姝g‘锛岄敊璇殑鍦版柟涔熸椂鏈夊嚭鐜帮級銆備笉杩囦綔涓轰互C涓哄熀纭?殑API锛岃繖閲岀殑璧勬枡鏄嫳鏂囩殑锛岃?涓旀牸寮忎篃鏄疌璇█鏍煎紡銆傛兂瑕佽鎳傦紝閭d綘鐨勮嫳鏂囨按骞冲拰C璇█灏辫鏈変袱鎶婂埛瀛愪簡銆備笉杩囦笉蹇呮媴蹇冿紝瀹冨彧鏄垜寤鸿涓?畾瑕佹湁鐨勫弬鑰冩枃妗o紝鏈?噸瑕佺殑杩樻槸鎴戞帴涓嬫潵瑕佽鐨勫唴瀹癸紝瀹冧笉浠呮槸涓枃鐨勶紝杩樻槸VB鐨勶紝鍙堟湁浣跨敤绀轰緥锛岃繕鏈夌浉鍏崇煡璇嗚瑙c?璁颁綇MSDN鍙槸鍙傝?鏂囨。锛屼綔鐢ㄥ氨濂藉儚瀛楀吀锛屽彲鍗冧竾涓嶈涔颁簡涓?MSDN灏辫窇鍘昏窡鍒汉璇翠綘宸茬粡鍙樺緱澶氬帀瀹充簡锛屼笉鐒垛?鈥?br />
銆??椤轰究璇翠竴璇达紝浠ュ墠鐨凪SDN鏄弻CD鐨勶紝鐜板湪宸茬粡鍙樻垚3CD浜嗐?濡傛灉涔扮殑璇濆簲灏介噺涔版渶鏂扮殑锛堜絾娌℃湁蹇呰姣忔鏇存柊閮芥崲涓?锛夛紝鍥犱负鏇存柊鐨勭増鏈妸涓?簺閿欒鏀规浜嗭紝骞跺鍔犱簡鏂扮殑鍐呭锛屾瘮濡傛渶鏂扮殑MSDN宸茬粡澧炲姞浜嗚澶歐indows XP鐨勫唴瀹广?浣犲彲浠ヤ粠寰蒋鐨勪腑鍥界綉绔欒鍒拌繖濂楁枃妗o紝涓嶈繃涓?勾鐨勪环閽变粠涓?竾鍑犲埌涓変竾鍑狅紝涔颁笉璧风殑璇濃?鈥﹁嚜宸辨兂鍔炴硶鍚с?

銆??濂戒簡锛屽紑绡囧啓浜嗚繖涔堝锛屾棤闈炰篃鏄兂璁╄鑰呭湪浠ュ悗鍙互鏇村鏄撶悊瑙f墍璁茬殑鍐呭銆傝繖浜涙槸鍩虹锛屾槸涓?畾瑕佽浣忕殑銆備笅闈㈡垜浠氨鍏堜粙缁嶄竴涓瘮杈冪畝鍗曠殑API锛屽紑濮嬫垜浠殑API涔嬫梾鍚э紒

銆??绗竴璇?浠庢秷鎭璧?br />
銆??鐢变簬杩欐槸銆婄粏姘撮暱娴佽瘽API銆嬬殑绗竴璇濓紝鎴戝繀椤绘敞鎰忓埌鎵?鐨勫唴瀹硅绠?崟锛屽苟涓旇浣犳湁鑰愬績鍙互鐪嬪埌寰?悗鐨勬枃绔狅紝鎵?互鎴戝笇鏈涘彲浠ラ?杩囦竴涓瘮杈冪壒鍒殑渚嬪瓙鏉ュ紩璧蜂綘鐨勬敞鎰忥紙杩欐牱鐨勬儏鍐典笉浼氭?鏄湁鐨勶級銆傝鎴戜滑鎯虫兂锛孷B閲岀殑CommandButton鎺т欢璁╂垜浠彲浠ュ仛浠?箞锛熸寜涓嬨?寮硅捣锛岃繕鏈夊憿锛熻鐪嬬湅鍥?锛岃繖鏍风殑鎯呭喌鍦ㄤ綘鐨勭▼搴忚繍琛屾椂鍑虹幇杩囧悧锛?br />
銆??Windows鏄互娑堟伅鏉ヤ紶閫掍俊鎭殑銆傚綋鍑虹幇鏌愪釜鎿嶄綔锛屾瘮濡傛寜閽鎸変笅锛屽氨浜х敓鎸夐挳琚寜涓嬬殑娑堟伅銆傛秷鎭浼犻?鍒拌鎿嶄綔瀵硅薄锛堟寜閽級锛屼簨浠跺氨浜х敓浜嗐?搴旀敞鎰忎笉鏄寜閽骇鐢熸秷鎭紝鑰屾槸Windows鐭ラ亾杩欎釜鎿嶄綔鐨勫彂鐢燂紝鍚戞寜閽彂閫佽繖涓秷鎭紝鎸夐挳鏀跺埌鍚庡啀鍋氱浉搴旂殑澶勭悊鈥斺?濡傛敼鍙樺瑙傛垚涓烘寜涓嬬殑鐘舵?銆?br />
銆??Windows鍏佽绗笁鑰呭悜鏌愪釜瀵硅薄鍙戦?娑堟伅锛屽洜姝ゅ綋鏌愪釜鎿嶄綔娌℃湁鍙戠敓鏃讹紝鎴戜滑鏄彲浠ヨ瀵硅薄濡傚悓鏀跺埌娑堟伅涓?牱浜х敓鏁堟灉鐨勶紝杩欏氨闇?鐢ㄥ埌API鍑芥暟鈥斺?SendMessage浜嗐?

銆??SendMessage鐨勫0鏄庡墠闈㈠凡缁忚杩囷紙娉ㄦ剰浠ublic寮?ご搴旀斁鍦ㄦ爣鍑嗘ā鍧椾腑锛屽惁鍒欑敤Private寮?ご锛夛紝瀹冪殑鍚勪釜鍙傛暟涓紝hwnd鏄璞$殑鍙ユ焺锛寃Msg鏄秷鎭殑鍊硷紙鍏蜂綋浠?箞娑堟伅锛夛紝鍙﹀涓や釜鍙傛暟鏍规嵁涓嶅悓娑堟伅鍜屼笉鍚屽簲鐢ㄦ湁涓嶅悓鐨勫?銆?br />
銆??浣犵湅鍒扮殑鍥?鐨勬儏鍐碉紝鏄敱浜庢垜鐨勭▼搴忓悜Command Button鎺т欢鍙戦?浜哤M_NCLBUTTONDOWN娑堟伅銆傝繖涓秷鎭彂鐢熷湪榧犳爣鍦ㄧ獥鍙g殑闈炲鎴峰尯鍩熶笂鎸変笅鏃躲?鎵?皳闈炲鎴峰尯鍩燂紝浣犲彲浠ョ悊瑙f垚涓?釜绐楀彛鐨勮竟缂樺拰鏍囬鏍忥紙褰撶劧鏄寚涓?埇鎯呭喌锛岃繖绉嶆儏鍐垫槸鍙互琚▼搴忔敼鍙樼殑锛夈?

銆??鍦ㄦ垜杩欎釜鎸夐挳鐨凪ouseDown浜嬩欢涓紝鍙啓浜嗙煭鐭殑鍑犲彞:

Private Sub cmdResize_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim nParam As Long

With cmdResize
''涔嬫墍浠ュ湪0鍜?00涔嬮棿浠ュ強涓嬮潰 .Width-100 鍜?.Width 涔嬮棿锛屾槸璁╅紶鏍囧彧鍦ㄦ寜閽竟缂樻墠鍙互鎷夊姩鎸夐挳
If X > 0 And X < 100 Then
nParam = HTLEFT
ElseIf X > .Width - 100 And X < .Width Then
nParam = HTRight
End If
If nParam Then
Call ReleaseCapture
Call SendMessage(.hwnd, WM_NCLBUTTONDOWN, nParam, 0)
End If
End With
End Sub




[Next]



銆??鍙互鐪嬪埌锛屾垜璁╅紶鏍囨媺鍔ㄦ寜閽椂锛屾媺鎸夐挳宸﹁竟鏄敤 HTLEFT鍋氬弬鏁帮紝鎷夊彸杈规槸鐢℉TRIGHT鍋氬弬鏁般?杩欎袱涓兘鏄父閲忥紝鍙互浠嶢PI娴忚鍣ㄤ腑寰楀埌鍊笺?鍚屾牱鐨勶紝鑻ユ兂鎷夋寜閽殑涓婇潰鍜屼笅闈紝鍙敤HTTOP鍜孒TBOTTOM鍋氬弬鏁帮紝鑰?HTTOPLEFT鍜孒TBOTTOMRIGHT鍒欏垎鍒槸宸︿笂瑙掑拰鍙充笅瑙掋?

銆??鍦ㄥ彂閫佹秷鎭箣鍓嶆湁涓?釜ReleaseCapture鐨凙PI銆傝繖涓狝PI鏄Windows閲婃斁瀵归紶鏍囩殑鎹曟崏浠ヤ究浣块紶鏍囦綅缃殑淇℃伅涓嶈兘琚敹鍒帮紝CommandButton涓嶇煡閬撻紶鏍囧湪鍝噷锛屼篃灏变笉浼氬彂鐢熸寜閽湪杩欐椂琚寜涓嬬殑鎯呭喌銆傚綋鐒讹紝鍙互鏀惧績锛學indows閲婃斁瀵归紶鏍囩殑鎹曟崏鍙槸鏆傛椂鐨勶紝褰撲綘鏀惧紑榧犳爣鍐嶆鍙戠敓绉诲姩鏃讹紝Windows鍙堜細鎹曟崏榧犳爣浜嗏?鈥斿畠鏄椂鏃堕兘鍦ㄥ彂鐢熺殑銆?br />
銆??浣犲彲鑳藉笇鏈涘鍚屾垜鐨勭▼搴忎竴鏍峰湪鎸夐挳杈圭紭鍏夋爣浼氬彉鍖栵紝涓嬮潰鏄垜鍐欑殑绋嬪簭娈?

Private Sub cmdResize_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim NewPointer As MousePointerConstants

With cmdResize
If X > 0 And X < 100 Then
NewPointer = vbSizeWE
ElseIf X > .Width - 100 And X < .Width Then
NewPointer = vbSizeWE
Else
NewPointer = vbDefault
End If
If NewPointer <> .MousePointer Then
.MousePointer = NewPointer
End If
End With

End Sub

銆??浣滅敤寰堟槑鏄撅紝鑰屼笖寰堢畝鍗曪紝鎵?互鎴戝氨涓嶅杩欐浠g爜浣滆В閲婁簡銆?br />
銆??杩欎釜渚嬪瓙寰堢畝鍗曪紝浣嗙浉淇¤捣鐨勪綔鐢ㄦ槸涓嶅皬鐨勩?SendMessage鍙互鍙戦?寰堝娑堟伅锛屽綋鐒舵垜涓嶄細瀵硅繖浜涙秷鎭竴涓?綔瑙i噴锛屼絾浠ュ悗杩樻槸浼氱粡甯告帴瑙﹀埌鐨勶紝鎵?互鏇村鐨勭煡璇嗗氨绛夋參鎱㈠啀瀛﹀惂銆?br />
銆??鏂囦腑绋嬪簭鍦╓indows2000+VB6鐜涓嬭皟璇曢?杩囥?
锛堟簮绋嬪簭涓嬭浇閾炬帴:http://www.cfan.net.cn/qikan/cxg/0202gwv.zip锛?br />





相关链接:



VOB to MP4



Using "replace" one-time elimination of the hard carriage return



Catalogs Audio And Multimedia



Desktop Wallpapers



Ridicule: The Workplace, Inoffensive "mantra"



GIS work?



Ashes of modern CAE



DV to AVI



Chinese way: do THE best CAD China



Embarked from Oblog to PERSONAL portal to the X-Space is that simple



Firefox 3.7 will update The interface denies plagiarism Chrome



Astronomy Wizard



Matroska TO MPEG



News About Graphic CAD



Sichuan Instrument Flow Meters: Precision Production Of Personalized Quote



Approach new and SUCCESSFUL



No comments:

Post a Comment