⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vb函数sendmessage的应用_不燃灯.htm

📁 自己总结的一些VB资料
💻 HTM
📖 第 1 页 / 共 4 页
字号:
				}			}		}	}	xhr.send(null);})();/*]]>*/</SCRIPT>

<META content="MSHTML 6.00.2900.3354" name=GENERATOR></HEAD>
<BODY onload=formatonlinpic();>
<CENTER>
<SCRIPT type=text/javascript>
/*<![CDATA[*/
if(top.location != self.location){
	top.location = self.location;
}
var myref = encodeURI("http://hi.baidu.com/29277723/blog/item/3ac19c0938a6ac86d0581b72%2Ehtml");
/*]]>*/
</SCRIPT>
<LINK href="VB函数SendMessage的应用_不燃灯.files/mods.css" type=text/css 
rel=stylesheet><LINK 
href="VB函数SendMessage的应用_不燃灯.files/de900636806e71dda2cc2b3f.css" type=text/css 
rel=stylesheet><LINK href="VB函数SendMessage的应用_不燃灯.files/space.css" type=text/css 
rel=stylesheet>
<STYLE type=text/css>#usrbar {
	PADDING-RIGHT: 10px; PADDING-LEFT: 0px; FONT-SIZE: 12px; BACKGROUND: #ffffff; FILTER: alpha(opacity=65); PADDING-BOTTOM: 3px; WIDTH: 100%; COLOR: #000000; LINE-HEIGHT: 19px; PADDING-TOP: 4px; FONT-FAMILY: Arial; LETTER-SPACING: normal; HEIGHT: 19px; TEXT-ALIGN: right; moz-opacity: 0.5
}
#usrbar A {
	COLOR: #0000cc; TEXT-DECORATION: underline
}
#usrbar A:link {
	COLOR: #0000cc; TEXT-DECORATION: underline
}
#usrbar A:visited {
	COLOR: #0000cc; TEXT-DECORATION: underline
}
#ft {
	CLEAR: both; FONT-SIZE: 12px; COLOR: #666666; LINE-HEIGHT: 20px; FONT-FAMILY: Arial; HEIGHT: 20px; TEXT-ALIGN: center
}
#ft A {
	COLOR: #7777cc; TEXT-DECORATION: underline
}
#ft A:link {
	COLOR: #7777cc; TEXT-DECORATION: underline
}
#ft A:visited {
	COLOR: #7777cc; TEXT-DECORATION: underline
}
#usrbar {
	LETTER-SPACING: normal
}
#usrbar A {
	LETTER-SPACING: normal
}
#usrbar A:link {
	LETTER-SPACING: normal
}
#usrbar A:visited {
	LETTER-SPACING: normal
}
#ft {
	LETTER-SPACING: normal
}
#ft A {
	LETTER-SPACING: normal
}
#ft A:link {
	LETTER-SPACING: normal
}
#ft A:visited {
	LETTER-SPACING: normal
}
</STYLE>

<DIV id=usrbar><NOBR><A href="http://www.baidu.com/" target=_blank>百度首页</A> | <A 
id=hi_index href="http://hi.baidu.com/" target=_blank>百度空间</A>
<SCRIPT type=text/javascript>
		document.write('| <a href="http://passport.baidu.com/?login&tpl=sp&tpl_reg=sp&u=http://hi.baidu.com' + encodeURI('/29277723/blog/item/3ac19c0938a6ac86d0581b72%2Ehtml') + '">登录</a>');
		</SCRIPT>
 </NOBR></DIV>
<DIV id=main align=left><!--[if IE]>
<SCRIPT>
var objmain = document.getElementById("main");
function updatesize(){ var bodyw = window.document.body.offsetWidth; if(bodyw <= 790) objmain.style.width="772px"; else if(bodyw >= 1016) objmain.style.width="996px"; else objmain.style.width="100%"; }
updatesize(); window.onresize = updatesize;
</SCRIPT>
<![endif]-->
<DIV id=header>
<DIV class=lc>
<DIV class=rc></DIV></DIV>
<DIV class=tit><A class=titlink title="wk198的空间 http://hi.baidu.com/29277723" 
href="http://hi.baidu.com/29277723">不燃灯</A></DIV>
<DIV class=desc>生活/电脑/打屁/吐槽</DIV>
<DIV id=tabline></DIV>
<DIV id=tab><A href="http://hi.baidu.com/29277723">主页</A><A class=on 
href="http://hi.baidu.com/29277723/blog">博客</A><A 
href="http://hi.baidu.com/29277723/album">相册</A><SPAN>|</SPAN><A 
href="http://hi.baidu.com/29277723/profile">个人档案</A> <SPAN>|</SPAN><A 
href="http://hi.baidu.com/29277723/friend">好友</A> </DIV></DIV>
<DIV class=stage>
<DIV class=stagepad>
<DIV style="WIDTH: 100%">
<TABLE class=modth cellSpacing=0 cellPadding=0 width="100%" border=0>
  <TBODY>
  <TR>
    <TD class=modtl width=7>&nbsp;</TD>
    <TD class=modtc noWrap>
      <DIV class=modhead><SPAN class=modtit>查看文章</SPAN></DIV></TD>
    <TD class=modtc noWrap align=right></TD>
    <TD class=modtr width=7>&nbsp;</TD></TR></TBODY></TABLE>
<DIV class=modbox id=m_blog>
<DIV class=tit>VB函数SendMessage的应用</DIV>
<DIV class=date>2008-01-08 16:07</DIV>
<TABLE style="TABLE-LAYOUT: fixed">
  <TBODY>
  <TR>
    <TD>
      <DIV class=cnt 
      id=blog_text>在Windows编程中,向文本框控件、列表控件、按钮控件等是我们最常接触的控件了。但是在VB中这些控件有时无法实现我们的需要。在这时,我们只要简单的利用Windows 
      API函数就可以扩充这些控件的功能了。<BR>顾名思义,SendMessage函数就是向窗口(这里的窗口指的是向按钮、列表框、编辑框等具有hWnd属性的控件)发送消息的函数,该函数的定义如下:<BR>Declare 
      Function SendMessage Lib "user32" Alias "SendMessageA" _<BR>(ByVal hwnd As 
      Long, _<BR>ByVal wMsg As Long, _<BR>ByVal wParam As Long, _<BR>lParam As 
      Any) As Long 
      <P><FONT style="BACKGROUND-COLOR: #ffffff" 
      color=#000000>其中hwnd指定接受消息的窗口,参数wMsg指定消息值,参数wParam 
      lParam分别定义传递到窗口的附加参数。而在Windows系统的很多消息中,有一些不仅仅是提供一个窗口消息那么简单。它们可以控制窗口的动作和属性。下面我将分次向向大家介绍SendMessage函数在扩充基本控件功能方面的应用。 
      <BR>一、列表(ListBox)控件<BR>在Windows中,有一系列的以LB_开头的列表消息,这里介绍的就是利用LB消息控制的ListBox的应用<BR>1、使列表中光标移动到不同的列表项上有不同的提示(ToolTip)<BR>在列表框控件中有一个ToolTipText属性,该属性决定了当光标在列表框上移动时出现的提示文字。但是如何使得 
      当光标在不同的列表项上移动时的提示文字也不同呢?问题的关键是要知道在光标移动时光标所在的列表项的索引,使 
      用SendMessage函数发送LB_ITEMFROMPOINT消息就可以获得。下面是程序范例:</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>Option 
      Explicit</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>Const 
      LB_ITEMFROMPOINT = &amp;H1A9</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>Private Declare 
      Function SendMessage Lib "user32" Alias "SendMessageA" _<BR>(ByVal hwnd As 
      Long, _<BR>ByVal wMsg As Long, _<BR>ByVal wParam As Long, _<BR>lParam As 
      Any) As Long</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>Private Sub 
      Form_Load()<BR>Dim i</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>For i = 1 To 
      200<BR>List1.AddItem Str(i) + " Samples in this list is " + Str(i)<BR>Next 
      i<BR>End Sub</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>Private Sub 
      List1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As 
      Single)<BR>Dim lXPoint As Long<BR>Dim lYPoint As Long<BR>Dim lIndex As 
      Long</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>If Button = 0 
      Then '确定在移动鼠标的同时没有按下功能键或者鼠标键<BR>'获得光标的位置,以像素为单位<BR>lXPoint = CLng(X / 
      Screen.TwipsPerPixelX)<BR>lYPoint = CLng(Y / 
      Screen.TwipsPerPixelY)<BR>'<BR>With List1<BR>'获得 光标所在的标题行的索引<BR>lIndex = 
      SendMessage(.hwnd, LB_ITEMFROMPOINT, 0, _<BR>ByVal ((lYPoint * 65536) + 
      lXPoint))<BR>'将ListBox的Tooltip设置为该标题行的文本<BR>If (lIndex &gt;= 0) And 
      (lIndex &lt;= .ListCount) Then<BR>.ToolTipText = .List(lIndex) 'Return the 
      text = .list(lIndex)<BR>Else<BR>.ToolTipText = ""<BR>End If<BR>End 
      With<BR>End If<BR>End Sub</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" 
      color=#000000>首先在Form1中加入一个ListBox控件,然后再将上面的代码加入到Form1的代码窗口中。运行程序,当光标在 
      列表中移动时,可以看到根据光标所在的不同的列表项,提示文字也不相同。</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" 
      color=#000000>2、向列表中加入横向滚动条使得可以浏览长列表项<BR>当向列表中加入的列表项超出了列表的显示范围后,列表并不会出现横向滚动条让你可以通过滚动来浏览项目 
      的全部内容。利用LB_SETHORIZONTALEXTENT消息可以设置列表的横向滚动条以及滚动长度。下面是范例程序:<BR>Option 
      Explicit</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>Private Type 
      RECT<BR>Left As Long<BR>Top As Long<BR>Right As Long<BR>Bottom As 
      Long<BR>End Type</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>Private Declare 
      Function DrawText Lib "user32" Alias "DrawTextA" _<BR>(ByVal hdc As Long, 
      _<BR>ByVal lpStr As String, _<BR>ByVal nCount As Long, _<BR>lpRect As 
      RECT, _<BR>ByVal wFormat As Long) As Long</FONT></P>
      <P><BR><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>Private 
      Declare Function SendMessage Lib "user32" Alias "SendMessageA" _<BR>(ByVal 
      hwnd As Long, _<BR>ByVal wMsg As Long, _<BR>ByVal wParam As Long, 
      _<BR>lParam As Any) As Long</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>Const 
      LB_SETHORIZONTALEXTENT = &amp;H194<BR>Const DT_CALCRECT = 
      &amp;H400</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>Public Function 
      ListTextWidth(ByRef lstThis As ListBox) As Long<BR>Dim i As Long<BR>Dim tR 
      As RECT<BR>Dim lW As Long<BR>Dim lWidth As Long<BR>Dim lHDC As 
      Long</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>With 
      lstThis.Parent.Font<BR>.Name = lstThis.Font.Name<BR>.Size = 
      lstThis.Font.Size<BR>.Bold = lstThis.Font.Bold<BR>.Italic = 
      lstThis.Font.Italic<BR>End With</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>lHDC = 
      lstThis.Parent.hdc</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" 
      color=#000000>'便历所有的列表项以找到最长的项<BR>For i = 0 To lstThis.ListCount - 
      1<BR>DrawText lHDC, lstThis.List(i), -1, tR, DT_CALCRECT<BR>lW = tR.Right 
      - tR.Left + 8<BR>If (lW &gt; lWidth) Then<BR>lWidth = lW<BR>End If<BR>Next 
      i</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" 
      color=#000000>'返回最长列表项的长度(像素)<BR>ListTextWidth = lWidth<BR>End 
      Function</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>Private Sub 
      Form_Load()<BR>Dim astr As String<BR>Dim i<BR>Dim l As Long</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>l = 
      List1.FontSize * 20 / Screen.TwipsPerPixelX<BR>For i = 1 To 10<BR>astr = 
      astr + "我们This is a very long item " + Str(i)<BR>Next i<BR>List1.AddItem 
      astr + "aaa"<BR>'加入一个很长的列表项<BR>l = ListTextWidth(List1)</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>SendMessage 
      List1.hwnd, LB_SETHORIZONTALEXTENT, l, 0<BR>End Sub</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" 
      color=#000000>首先在Form1中加入一个ListBox控件,然后再将上面的代码加入到Form1的代码窗口中。运行程序,可以看到列表中出现了横向滚动条,而且滚动范围正好是列表项的长度。</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" 
      color=#000000>3、使列表可以响应用户击键<BR>有时我们需要列表根据用户的敲入字符串自动调整列表的ListIndex到最接近的列表项,就象VB中动态感应用户输入控件属性的编辑器一样。问题的关键是如何在列表中查找含有指定字符串的列表项,使用LB_FINDSTRING消息可以在列表中查找指定字符串。下面是范例:</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>Private Declare 
      Function SendMessageStr Lib "user32" Alias "SendMessageA" _<BR>(ByVal hwnd 
      As Long, _<BR>ByVal wMsg As Long, _<BR>ByVal wParam As Long, _<BR>ByVal 
      lParam As String) As Long</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>Const 
      LB_FINDSTRING = &amp;H18F<BR>Dim astr As String</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>Private Sub 
      Form_KeyPress(KeyAscii As Integer)<BR>Dim l As Long</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>astr = astr + 
      Chr(KeyAscii)</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>l = 
      SendMessageStr(List1.hwnd, LB_FINDSTRING, -1, astr)</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>If l 
      Then<BR>List1.ListIndex = l<BR>End If<BR>End Sub</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>Private Sub 
      Form_Load()<BR>'向List中加入列表项<BR>For i = 65 To 85<BR>For j = 65 To 
      85<BR>List1.AddItem Chr(i) + Chr(j)<BR>Next j<BR>Next i<BR>End 
      Sub</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>Private Sub 
      List1_DblClick()<BR>'清除原来的查找字符串<BR>astr = ""<BR>End Sub</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>Private Sub 
      List1_KeyPress(KeyAscii As Integer)<BR>'如果按下的是字母键就将击键消息传递到Form1<BR>If 
      ((KeyAscii &gt;= 65 And KeyAscii &lt;= 90) Or (KeyAscii &gt;= 97 _<BR>Or 
      KeyAscii &lt;= 122)) Then<BR>KeyAscii = 0<BR>End If<BR>End Sub</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" 
      color=#000000>首先在Form1中加入一个ListBox控件,然后再将上面的代码加入到Form1的代码窗口中。并将List1的Sorted属性设置为True。运行程序,在列表中敲入字符,例如"av" 
      "gm",列表就会高亮显示相近的列表项,双击列表就可以清除原来的输入。</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" 
      color=#000000>在上一篇文章中我向大家介绍了关于ListBox类控件消息的应用,在这一章我将向大家介绍如何利用消息操控TextBox类控件。</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" 
      color=#000000>1、获得光标所在的行和列<BR>一般的比较完善的文本编辑器一般都有在状态栏中显示当前光标所在行和列的功能。利用SendMessage向TextBox控件发送编辑控件类型消息。也可以实现这样的功能。下面首先来看程序,然后再分析。<BR>首先在VB中建立一个新工程,并在Form1中加入一个TextBox控件和两个Label控件。将TextBox控件的MultiLine属性设置为True。然后在Form1的代码窗口中加入如下代码:</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>Option 
      Explicit</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>Private Declare 
      Function SendMessage Lib "user32" Alias "SendMessageW" _<BR>(ByVal hwnd As 
      Long, ByVal wMsg As Long, ByVal wParam As Long, _<BR>lParam As Any) As 
      Long</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>Private Declare 
      Function SendMessageByRef Lib "user32" Alias "SendMessageA" _<BR>(ByVal 
      hwnd As Long, ByVal wMsg As Long, wParam As Long, _<BR>lParam As Long) As 
      Long</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>Const 
      EM_LINEFROMCHAR = &amp;HC9<BR>Const EM_LINEINDEX = &amp;HBB<BR>Const 
      EM_GETLINE = &amp;HC4<BR>Const EM_GETSEL = &amp;HB0</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>Dim iLineX, 
      iLineY As Long</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>Sub 
      GetCurPos(txtA As TextBox)<BR>Dim l, l1, l2 As Long<BR>Dim astr As String 
      * 256</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>l = 
      SendMessage(txtA.hwnd, EM_LINEINDEX, -1, 0)<BR>iLineY = 
      SendMessage(txtA.hwnd, EM_LINEFROMCHAR, l, 0)</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>SendMessageByRef 
      txtA.hwnd, EM_GETSEL, l1, l2<BR>iLineX = l1 - l<BR>Label1.Caption = "列:" + 
      Str(iLineX)<BR>Label2.Caption = "行:" + Str(iLineY)<BR>End Sub</FONT></P>
      <P><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>Private Sub 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -