Vb.net/VB declares an API function to implement the parent form function. vb. netapi

Source: Internet
Author: User

Vb.net/VB declares an API function to implement the parent form function. vb. netapi

Looking back at the charges for the first knock on the data center, I called the api function to minimize the function of the parent form and its subforms. Now, I am thinking about it again. Can I continue to use the API function? Of Course the answer is Of Course!

In fact, there is not much difference between the two. First, let's look at how to call vb.net:

First, add a class module to encapsulate this API function:

<span style="font-size:14px;"> Public Declare Function SetParent Lib "<span style="color:#ff0000;">user32</span>" (ByVal hWndChild As Integer, ByVal hWndNewParent As Integer) As Integer</span>

Then you can directly call it in the form to meet your needs.

<span style="font-size:14px;">SetParent(FrmRegisterCard.Handle.ToInt<span style="color:#ff0000;">32</span>, Me.Handle.ToInt<span style="color:#ff0000;">32</span>)</span>

Note: although the desired function is implemented, the size of the self-aware form must be greater than that of the sub-form. Let me modify it:

             <span style="font-size:14px;">FrmRegisterCard.Width = Me.ClientRectangle.Width - 800        FrmRegisterCard.Height = Me.ClientRectangle.Height - 200        SetParent(FrmRegisterCard.Handle.ToInt32, Me.Handle.ToInt32)        FrmRegisterCard.Show()</span>
Next, let's talk about the differences with VB's API function declaration:

1. One of the major differences is that the data type defined in vb is long, while in vb.net, Integer is used to replace long, because in vb.net, the Integer type is 32-bit, same as long type.

2. Use ByVal and ByRef

  • ByVal is the value passed, and the source data is not modified. You can use this value as your local variable. (The parameter memory is passed to the caller)
  • ByRef is the transfer address, and the source data may be modified. (Pass the parameter address to the caller)

3. When calling the API function in the main form, pay attention to the use of "Handel. ToInt32", which is equivalent to. hwnd In the vb call process.

H: handle; Wnd: Variable object description, which is the so-called form. So Handel. ToInt32 is also the so-called window handle!


This is just your own understanding. Put your understanding of the API here to lay a foundation for learning again!
How to declare API functions in VB

1. There are two methods to declare an API function in VB: if we only use an API function in a form, we can declare it in the General part of the form code:

The declared syntax is:
Private Declare Function...
Private Declare Sub .....
Private Declaration must be used here, because this API function can only be called by programs in a form.

If our program has multiple forms, and we need to use the same API function in multiple forms, we need to declare it in the module.
Add a module (Display) first ),
Then use the following syntax statement:
Public Declare Function ....
Public Declare Sub ....
The meaning of the Public declaration is to use an API function as a Public function or process and call it directly at any position (including all forms and modules) in a project. After the Declaration, we can use this API function in the program.

2. You can use API functions in the following ways. Take the SetWindowPos function as an example:
(1) Call to ignore the function return value:
SetWindowPos Form1.hWnd,-2, 0, 0, 0, 0, 3
Note that function parameters are not enclosed in parentheses.
(2) Call method Call:
Call SetWindowPos (Form1.hWnd,-2, 0, 0, 0, 3)
Note that parentheses need to be added here, but we will not retrieve the return value of the function.
(3) Call to obtain the function return value:
MyLng = SetWindowPos (Form1.hWnd,-2, 0, 0, 0, 3)
In this case, brackets must be added, and a variable must be defined in advance (the type of the variable is the same as the type of the function return value) to store the return value of the API function.
Reference: www.hongen.com/.20.2.htm

Where should the vb api function declaration be written?

If it is used only in a form, it can be declared directly at the top of the form, Private
If multiple forms are used, declare them in the Public standard module.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.