SendMessage Function Zone Application (API)

xiaoxiao2021-03-06  38

'*************************' Free Pentium WGSCD'QQQ: 153964481'Website: http://www.topds.com'* ************************** SENDMAESSAGE Function Qiansheng Application (1) In Windows Programming, to text box controls, list controls, button controls, etc. It is the control we are most often in contact. But these controls in VB

Sometimes we need to achieve our needs. At this moment, we can expand the features of these controls as long as it is simple to use the Windows API function. As the name suggestions, the SendMessage function is to the window (here the window refers to the control of the HWND attribute to the button, list box, edit box, etc.

Parts The function of sending messages, the function is defined as follows: Declare Function SendMessage Lib "User32" Alias ​​"SendMessagea" _ (Byval Hwnd As Long, _ Byval WParam As Long, _ LParam as any) As long Where hWnd specifies the window of accepting the message, the parameter WMSG specifies the message value, and the parameter WPARAM LPARAM separately defines the additional parameters passed to the window. and

In a lot of messages in the Windows system, there are not only to provide a window message. They can control the action and properties of the window. Below I will

This section describes the application of the SendMessage function in the expansion of the basic control function.

First, ListBox Control In Windows, there is a list of list messages starting with LB_, which is introduced by using LB message controlled ListBox application 1, so that the cursor in the list is different on different list items. Tote (Tooltip has a TooltExt property in the list box control, which determines the prompt text that occurs when the cursor moves on the list box. But if

What makes the prompt text when the cursor moves on different list items? The key to the problem is to know the list items where the cursor is moving when the cursor moves.

Index, you can get the LB_ItemFromPoint message using the SendMessage function. Below is the program example:

Option expedition

Const lb_itemfrompoint = & h1a9

Private Declare Function SendMessage Lib "User32" Alias ​​"SendMessagea" _ (Byval Hwnd As Long, _ Byval WMSG As Long, _ Byval WParam As long, _ lparam as any) As long

PRIVATE SUB FORM_LOAD () DIM I for i = 1 To 200 List1.AddItem Str (i) "Samples in this List is" Str (i) Next Ind Sub

Private Sub List1_mousemove (Button As Integer, Shift As Integer, X As Single, Y AS Single) DIM LXPOINT AS Long Dim Lypoint As Long Dim Lindex As Long

If button = 0 THEN 'determines that there is no point button or mouse button in the mobile mouse to get the cursor, in pixels, lxpoint = clng (x / screen.twipsperpixelx) lypoint = clng (Y / screen.twipsperpixly) 'With list1' The index of the title line where the cursor is located, LINDEX = SendMessage (.hWnd, LB_ItemFromPoint, 0, _ Byval ((Lypoint * 65536) LXPoint)) "Set the Listbox's Tooltip Text IF (Lindex > = 0) and (lindex <= .ListCount) Then .Tooltiptext = .List (lindex) 'Return the text = .list (lindex) else .tooltiptext = "" End if End with end ifnd Sub first add one in Form1 ListBox control, then add the above code to the code window of the Form1. Operating the program, when the cursor moves in the list, you can see the different list items in accordance with the cursor, the prompt text is also different.

2. Add the horizontal scroll bar to the list so that you can browse the long list item When the list item added to the list exceeds the list of display ranges, the list does not appear horizontal scroll bars to let you browse through scrolling.

All content of the project. Use the lb_sethorizontalExtent message to set the horizontal scroll bar of the list and the scroll length. Below is a sample program: Option Explicit

Private Type RECT Left As Long Top As Long Right As Long Bottom As LongEnd TypePrivate Declare Function DrawText Lib "user32" Alias ​​"DrawTextA" _ (ByVal hdc As Long, _ ByVal lpStr As String, _ ByVal nCount As Long, _ lpRect As RECT _ Byval WFORMAT AS long) As long

Private Declare Function SendMessage Lib "User32" Alias ​​"SendMessagea" _ (Byval Hwnd As Long, _ Byval WMSG As Long, _ Byval WParam As long, _ lparam as any) As long

Const lb_sethorizontaleXtent = & H194Const Dt_calcRect = & H400

Public Function ListTextWidth (ByRef lstThis As ListBox) As Long Dim i As Long Dim tR As RECT Dim lW As Long Dim lWidth As Long Dim lHDC As LongWith lstThis.Parent.Font .Name = lstThis.Font.Name .Size = lstThis.Font .Size .bold = lstthis.font.bold .italic = lstthis.font.italic end with lhdc = lstthis.parent.hdc 'to create the longest item for i = 0 to Lstthis.listCount - 1 DrawText LHDC, LSTTHIS.LIST (I), -1, TR, DT_CALCRECT LW = tr.right - Tr.LEFT 8 IF (LW> LWIDTH) THEN LWIDTH = LW End if Next I 'Returns the length of the longest list item ( Pixel) ListTextWidth = LWIDTHEND FUNCTION

Private Sub Form_Load () DIM Astr AS String Dim I Diml AS Long L = List1.FontSize * 20 / Screen.twipsPerpixelx for i = 1 To 10 astr = astr "Our this is a very long item" STR (i) Next I list1.addItem Astr "AAA" adds a list of list L = ListTextWidth (List1) SendMessage List1.hWnd, LB_SETHORIZONTALEXTENT, L, 0END SUB first adds a listbox control in Form1, and then the code above Add to the FORM1 code window. Run the program, you can see that the horizontal scroll bar appears in the list, and the scrolling range is just the length of the list item. 3. Make the list can respond to the user's hysterger sometimes we need a list to automatically adjust the list of ListIndex to the closest list item based on the user's knocking string, just like the editor of the VB in VB. The key to the problem is how to find a list item containing the specified string in the list.

LB_FINDSTRING messages can look for specified strings in the list. Here are examples:

Private Declare Function SendMessagestr Lib "User32" Alias ​​"SendMessagea" _ (Byval Hwnd As Long, _ Byval WParam As stay, _ Byval LParam as string AS Long

Const lb_findstring = & h18fdim astr AS STRING

Private Sub Form_KeyPress (KeyAscii As Integer) Dim l As Long astr = astr Chr (KeyAscii) l = SendMessageStr (List1.hwnd, LB_FINDSTRING, -1, astr) If l Then List1.ListIndex = l End IfEnd SubPrivate Sub Form_Load () 'Add a list item to list for i = 65 to 85 for j = 65 to 85 list1.additem chr (i) chr (j) Next J Next IEND SUB

Private sub list1_dblclick () 'Clear the original lookup string astr = "" "" End Sub

Private Sub List1_KeyPress (Keyascii AS Integer) If the letter button is pressed, the key message will be passed to Form1 if ((Keyascii> = 65 and keyascii <= 90) or (keyasciii> = 97 _ or keyascii <= 122) "Then Keyascii = 0 End IFEND SUB first adds a ListBox control in Form1, and then add the above code to the Form1's code window. And List1

The sorted property is set to True. Run the program, type the character in the list, such as "AV" "GM", the list will highlight similar list items, double click on the list

You can clear the original input. SendMessage functions (2) In the previous article I introduced to you about the application of the Listbox class control message, in which this chapter I will introduce you how to use the message

Control TextBox Class Control.

1. Getting the row and columns of the cursor and columns generally have the function of displaying the rows and columns of the current cursor in the status bar. Use SendMessage to

TextBox control Sends an edit control type message. Such functions can also be achieved. Let's first look at the program and then analyze. First establish a new project in VB and add a TextBox control and two Label controls in Form1. Put the TextBox control

The Multiline property is set to True. Then add the following code in the code window of the Form1:

Option expedition

Private Declare Function SendMessage Lib "user32" Alias ​​"SendMessageW" _ (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _ lParam As Any) As LongPrivate Declare Function SendMessageByRef Lib "user32" Alias ​​"SendMessageA" _ (ByVal hwnd As Long, ByVal wMsg As Long, wParam As Long, _ lParam As Long) As Long Const EM_LINEFROMCHAR = & HC9Const EM_LINEINDEX = & HBBConst EM_GETLINE = & HC4Const EM_GETSEL = & HB0Dim iLineX, iLineY As Long

Sub getCurpos (TXTA As Textbox) DIM L, L1, L2 AS Long Dim astr AS String * 256 L = SendMessage (txta.hwnd, em_line "= sendMessage (txta.hwnd, em_linefromchar, l, 0) SendMessageByref txta.hwnd, em_getsel, l1, l2 ilinex = L1 - l label1.caption = "Colum:" Str (ilINEx) label2.caption = "line:" Str (iliney) End Sub

Private Sub Form_Load () DIM IFILE DIM ASTR AS STRING LABEL1.HEIGHT = 300: label2.height = 300 text1.Top = 0 text1.text = "" Label1.caption = "Label2.caption =" "Ifile = freefile open" c: /Windows/readme.txt "for input as #ifile do line input #ifile, astr text1.text = text1.text astr vbcrlf loop unsub (ifile) Close Ifileend SUB

PRIVATE SUB FORM_RESize () label1.top = me.scaleHeight - 300 label2.top = me.scaleHeight - 300

Label1.Left = 0: Label2.Left = 1200 Label1.Width = 1200 Label2.Width = 1200 Text1.Width = Me.ScaleWidth Text1.Height = Me.ScaleHeight - Label1.HeightEnd SubPrivate Sub Text1_Click () GetCurPos Text1End Sub

Private sub text1_keyup (keycode as integer) getcurpos text1nd Sub Before running the program, make sure there is a C: /Windows/readme.txt on your hard drive. Otherwise the program will be wrong. Then run the program. When editing the text, you can see the row, column value where the current cursor is located at the bottom of the window. In the program above. We first send an EM_LINEINDEX message, send the message to return the first character of a row in the entire text control, if the WPARAM parameter is set to -1, then return the character position of the current line. Then send EM_LineFromChar to send the message to return the line number where the character is located according to the character position specified by the parameter wPARAM, and the position of the first line of the text is 0. This two messages are used to get the line number where the current cursor is located. To get the column number, first send an EM_GETSEL message, send the message to return the currently selected text's starting position, if there is no text selected, return the current cursor where the character is located in the text. Since the above EM_LINEINDEX message returns the position of the first character of the current row in the text. So the two values ​​are subtracted, the column position of the character where the cursor is located. In the above program, if you have a Chinese character in your text, when your cursor moves a position in the Chinese characters, you will see the list of columns in the tab 2, which is due to the message sent by SendMessage. The result is not supported, it calculates a Chinese word to do two characters. This is also a bug in the program (this is why I want to use the EM_Getsel message without directly using the selstart property of the TextBox control, because if the value returned using SELSTART will be a character, Missid value is subtimate with the EM_LINEINDEX return value

2. Get the text control text using the EM_GETLINE message we can get the text of a row in the text control. Specific examples are as follows:

Option expedition

Private Declare Function SendMessage Lib "user32" Alias ​​"SendMessageW" _ (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _ lParam As Any) As LongPrivate Declare Function SendMessageStr Lib "user32" Alias ​​"SendMessageA" _ (ByVal HWND As Long, BYVAL WMSG As Long, Byval WParam As String AS Long const EM_LINEFROMCHAR = & HC9CONST EM_LINEINDEX = & hbbconst em_getline = & hc4

Private sub text1_click ()

Dim Astr As String Diml, ILiney As Long Astr = Space (1024) L = SendMessage (Text1.hWnd, EM_LINEY = SendMessage (Text1.hwnd, EM_LINEFROMCHAR, L, 0) L = SendMessagestr (Text1 .hwnd, em_getline, iliney, byval asse) me.currentx = 30: me.currenty = 30 me.print "This line contains text length:" Str (L) text2.text = trim (ask) End Sub To run The program first, first join two TextBox controls in the Form and set the multiline property of Text1 to True. Course

After running the program, click on the mouse in Text1, and the text of the row in the cursor is displayed in Text2.

3, other messages describe some messages of the Control TextBox control behavior EM_GETFIRSTVISIBLINE Send the EM_GETFIRSTVISIBLINE message to get the line where the top of the top text in the visible position is available in the text control. If the message

The process is successful, and the index of the row will be returned, and 0 is the base. EM_LINESCROLL Send this message to control TextBox horizontally or vertically. Parameter wPARAM Specifies the number of characters that horizontally scrolled. Parameter LPARAM Specifies vertical roll

The number of rows, definitions, and call methods are as follows: private declare function sendMessage lib "user32" alias "sendMessagew" _ (Byval Hwnd As Long, Byval WMSG As Long, Byval WParam as long, _ lparam as any) as long const em_linescroll = & HB6 SendMessage Text1.hWnd, EM_LINESCROLL, 5, BYVAL 1 'Turn the text in Text1 to scroll down the line, scroll to the right 5

character

EM_SETREADOONLY Send this message to set the text control with read-only properties, if you set the sendMessage parameter wPARAM, the control has

Read-only properties, otherwise the control is readily read. Examples are as follows: const em_setreadonly = & hcf sendMessage text1.hwnd, em_setreadonly, true, 0 'Settings Text1 is read-only

EM_UNDO Sends the message will cause the text control to reply to the previous operation, which is equivalent to the "revoking" operation in the Right-click menu of the text control.

SendMessage functions (3) In this SendMessage function application, I will introduce you how to use message functions to extend the tree list (TreeView) control

Features I believe that the tree list control is very familiar, typical application is a list of directory in the Windows Explorer. In VB, tree type column

The table control is included in Microsoft Windows Common Control 6.0 (page may be 5.0, depending on your VB or system version). In Windows

API, there is a series of message values ​​starting with TVM_, these messages are to extend the message value specific to the tree list control, and introduce some of these applications.

1. Set the background color of the tree list control first as the following definition: private declare function sendMessage lib "user32" alias "sendMessagea" _ (byval hwnd as long, _ byval wmsg as long, _ byval wparam as long, _ byval lparam As Long) As Long Const TV_FIRST = & H1100 Const TVM_SETBKCOLOR = TV_FIRST 29 then do the following calls: call SendMessage (TreeView1.hwnd, TVM_SETBKCOLOR, 0, RGB (255, 0, 0)) the above SendMessage call the background color TreeView1 Set to red. Everyone may notice. In the above SendMessage function definition, we define lparam as ByVal LParam as long.

It is not as an example of the example as an ANY or String type. About this problem, I will introduce in the final chapter.

2, set the tree list control header line height to use the TVM_SETITEMHEIGHT message to set the height of the title line of the control, the message definition and calling method is as follows: Const TV_first = & H1100 const TVM_SetItemHeight = TV_First 27

Private Declare Function SendMessage Lib "user32" Alias ​​"SendMessageA" _ (ByVal hwnd As Long, _ ByVal wMsg As Long, _ ByVal wParam As Long, _ lParam As Any) As Long call: Call SendMessage (TreeView1.hwnd, TVM_SETITEMHEIGHT, 60 0) The above code sets the title line height of TreeView1 to 60 pixels.

3. Set different tips for different headline rows in the tree list control in the first phase of the listbox control introduction, I introduce you how to set different tips for each title line in the list (Tooltips)

Here is to show you how to set a different tips for each title in the tree list control. With the ListBox control, the tree list control does not have a message from the title line index according to the cursor location, we need to find a way.

There is a TVM_HitTest message in the TVM class message, which sends this message to detect some point on the surface of the control, if the point is on one title, then return to the label

The handle of the question. With the TVM_GETITEM message, you can return the text of the title line according to the title handle. So combine these two messages can get the cursor

Title text in the headline line. The specific sample code is as follows:

Option expedition

Private Type Tpoint X As Long Y As Longend Type

Private Type Tvhittectinfo Pt As Tpoint Flags As Long Hitem As LONGEND TYPE

Private Type TVItem Mask As Long HtreeItem As Long Psztext As Long CchtextMax As Long IImage As Long IselectedImage As Long Cchildren As Long LParam As LONGEND TYPE

Const TV_first = & H1100Const TVM_Hittest = TV_First 17const TVM_GETITEM = TV_First 12const TVHT_ONITEMLABEL = & H4const TVif_text = & H1Const GMEM_FIXED = & H0

Private Declare Function SendMessageRef Lib "user32" Alias ​​"SendMessageA" _ (ByVal hwnd As Long, _ ByVal wMsg As Long, _ ByVal wParam As Long, _ lParam As Any) As LongPrivate Declare Sub CopyMemory Lib "kernel32" Alias ​​"RtlMoveMemory" _ (ByVal Destination As String, _ ByVal Source As Long, _ ByVal Length As Long) Private Declare Function GlobalAlloc Lib "kernel32" _ (ByVal wFlags As Long, _ ByVal dwBytes As Long) As LongPrivate Declare Function GlobalFree Lib "kernel32" (ByVal HMEM as long

DIM HITEMPRV As Long

Private Sub Form_Load () DIM NDX As Node 'Add Several Item Set NDX = TreeView1.Nodes.add (,, "R", "Root") Set ndx = treec1.nodes.add ("r", tvwchild, "key1" , "Node1") set ndx = treeview1.nodes.add ("Key1", TVWchild, "Subkey1", "Subnode1") set ndx = treView1.nodes.add ("Subkey1", TVWChild, "Subkeys1", "Subnode1" ) Set ndx = treaiview1.nodes.add ("Key1", TVWchild, "Subkey2", "Subnode2") set ndx = TreeView1.nodes.add ("Key1", TVWChild, "Subkey3", "Subnode3") set ndx = TreeView1.nodes.add ("Key1", TVWChild, "Subkey4", "Subnode4") End Sub

Private Sub TreeView1_MouseMove (Button As Integer, Shift As Integer, _ x As Single, y As Single) Dim ptA As TPoint Dim tf As TVHITTESTINFO Dim tv As TVITEM Dim hStr As Long Dim hItem As Long Dim astr As String * 1024 Dim bstr On Error Goto Errlab 'Get the position coordinate PTA.x = int (x / screen.twipsPixelx) PTA.y = int (Y / screen.twipsperpixely) TF.PT = PTA TF.FLAGS = TVHT_ONITEMLABEL' Get the cursor Item = sendMessageref (TreeView1.hWnd, TVM_HitTest, 0, TF) 'If the handle is not obtained or the same Item is exit IF ((HITEM <= HITEMPRV)) THEN EXIT Sub HitemPRV = HITEM 'Assign a certain memory space to store the title hstr = GlobalAlloc (GMEM_FIXED, 1024) IF HSTR> 0 THEN TV.MASK = TV1XTEXT' Get Title Text TV.htreeItem = HITEM 'ITEM Handle TV.psztext = HSTR TV.cchtextmax = 1023 'Send TVM_GETITEM Get Title Text Call SendMessageRef (TreeView1.hWnd, TVM_GetItem, 0, TV)' Copy the title text to string astr, HSTR 1024 BSTR = Left $ (Astr, (INSTR (Astr, Chr (0)) - 1)) TreeView1.Tooltiptext = BSTR 'Release Allocated Memory Space GlobalFree HSTR End If Exit SuberRlab: Resume Nextend Sub runs the above program, When the cursor moves on TreeView1, Tooltips of TreeView1 will be based on the different headings of the cursor.

Change. The above program is written in WIN98, WIN2000, and VB6. Of course, it can be used in the ".NET" environment. The "QQ message bomber" in front use this,

转载请注明原文地址:https://www.9cbs.com/read-73468.html

New Post(0)