The application of message passing in VB

Source: Internet
Author: User

Some forms define special feature messages at design time, and when we pass these messages to such a form, such a window executes a program and returns the result of execution. In order for the program to send out messages, Windows provides SendMessage API functions.

Declare Function sendmessage Lib "user32" Alias "SendMessageA" (ByVal hwnd as Long, ByVal wmsg as Long, ByVal WParam as Lo Ng, LParam as any) as Long

HWnd: A window that receives messages;

Wmsg: The number of the message;

WParam: The first parameter of the message;

LParam: The second parameter of the message.

The meaning of wparam and lparam parameters varies with wmsg parameters, so when we pass a message to a form, we have to understand the meaning of wparam and lparam in addition to the meaning of the message.

The LParam parameter is "LParam as any" in the SendMessage definition sentence, so it has the following words:

When the value is 0 o'clock, write: ByVal 0&

When the string constant is written as: ByVal "The contents of the string"

When it is a string variable, write: ByVal S

First instance: Working with a form

Below we explain and apply several messages to the form: Wm_gettext: Read the Caption property of the form; Wmsettext: Set the Caption property of the form; Wm_syscommand (wparam=sc_maximize) : Set the form's properties to 2;wm_syscommand (wparam=sc_minimize): Set the form's properties to 1; Wm_syscommand (Wparam=sc_restore): Sets the form's properties to 0;wm_syscommand (wparam=sc_close): Unload form.

Below we place several command controls and a text control on the form:

We first define the required parameters and API functions into the module:

Public Const WM_SYSCOMMAND = &H112
Public Const SC_CLOSE = &HF060& '关闭窗体
Public Const SC_MINIMIZE = &HF020& '最小化窗体
Public Const SC_MAXIMIZE = &HF030& '最大化窗体
Public Const SC_RESTORE = &HF120& '恢复窗体大小
Public Const WM_SETTEXT = &HC '设置窗体的Caption
Public Const WM_GETTEXT = &HD '取得窗体的caption
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Double-click the command to add:

Private Sub Command_Click(Index As Integer)
  Dim S As String
  S = String(80, Chr(0))
Select Case Index
   Case 0
    SendMessage Me.hwnd, WM_GETTEXT, Len(S), ByVal S '读出窗体的Caption
    Text1.Text = Left(S, InStr(S, Chr(0)) - 1)
   Case 1
    '因为Text1.text属于Variant类型,所以一定先要用CStr把它转换成字符串
    SendMessage Me.hwnd, WM_SETTEXT, 0, ByVal CStr(Text1.Text)'设置窗体的Caption
   Case 2
    SendMessage Me.hwnd, WM_SYSCOMMAND, SC_MAXIMIZE, ByVal 0&'使窗体最大化
   Case 3
    SendMessage Me.hwnd, WM_SYSCOMMAND, SC_MINIMIZE, ByVal 0&'使窗体最小化
   Case 4
    SendMessage Me.hwnd, WM_SYSCOMMAND, SC_RESTORE, ByVal 0&'使窗体恢复原来的大小
   Case 5
    SendMessage Me.hwnd, WM_SYSCOMMAND, SC_CLOSE, ByVal 0&'关闭窗体
   End Select
End Sub

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.