如何获取外部程序一个窗体中combox的值??

myhot-河马吃草 2003-08-19 07:58:56
如何获取外部程序一个窗体中combox的值??
...全文
696 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
hxy2003 2003-08-22
  • 打赏
  • 举报
回复
UP
jornet 2003-08-22
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/2110/2110175.xml?temp=3.596133E-02
goodname008 2003-08-22
  • 打赏
  • 举报
回复
to aiur2000(龙魔人) :
我的方法就是先用FindWindow找出来,然后SendMessage.
fslyw 2003-08-20
  • 打赏
  • 举报
回复
收藏
aiur2000 2003-08-20
  • 打赏
  • 举报
回复
哪要这么麻烦,通过findwindows和sendmessage就行了撒
zyl910 2003-08-20
  • 打赏
  • 举报
回复
CB_GETCURSEL
An application sends a CB_GETCURSEL message to retrieve the index of the currently selected item, if any, in the list box of a combo box.

CB_GETCURSEL
wParam = 0; // not used; must be zero
lParam = 0; // not used; must be zero

Parameters
This message has no parameters.

Return Values
The return value is the zero-based index of the currently selected item. If no item is selected, it is CB_ERR.

QuickInfo
Windows NT: Requires version 3.1 or later.
Windows: Requires Windows 95 or later.
Windows CE: Requires version 1.0 or later.
Header: Declared in winuser.h.






CB_GETLBTEXT
An application sends a CB_GETLBTEXT message to retrieve a string from the list of a combo box.

CB_GETLBTEXT
wParam = (WPARAM) index; // item index
lParam = (LPARAM) (LPCSTR) lpszBuffer; // address of buffer

Parameters
index
Value of wParam. Specifies the zero-based index of the string to retrieve.
lpszBuffer
Value of lParam. Pointer to the buffer that receives the string. The buffer must have sufficient space for the string and a terminating null character. You can send a CB_GETLBTEXTLEN message prior to the CB_GETLBTEXT message to retrieve the length, in bytes, of the string.
Return Values
The return value is the length of the string, in bytes, excluding the terminating null character. If the index parameter does not specify a valid index, the return value is CB_ERR.

Remarks
If you create the combo box with an owner-drawn style but without the CBS_HASSTRINGS style, the buffer pointed to by the lpszBuffer parameter of the message receives the 32-bit value associated with the item (the item data).

QuickInfo
Windows NT: Requires version 3.1 or later.
Windows: Requires Windows 95 or later.
Windows CE: Requires version 1.0 or later.
Header: Declared in winuser.h.





CB_GETLBTEXTLEN
An application sends a CB_GETLBTEXTLEN message to retrieve the length, in characters, of a string in the list of a combo box.

CB_GETLBTEXTLEN
wParam = (WPARAM) index; // item index
lParam = 0; // not used; must be zero

Parameters
index
Value of wParam. Specifies the zero-based index of the string.
Return Values
The return value is the length of the string, in characters, excluding the terminating null character. Under certain conditions, this value may actually be greater than the length of the text. For more information, see the following Remarks section.

If the index parameter does not specify a valid index, the return value is CB_ERR.

Remarks
Under certain conditions, the return value is larger than the actual length of the text. This occurs with certain mixtures of ANSI and Unicode, and is due to the operating system allowing for the possible existence of double-byte character set (DBCS) characters within the text. The return value, however, will always be at least as large as the actual length of the text; so you can always use it to guide buffer allocation. This behavior can occur when an application uses both ANSI functions and common dialogs, which use Unicode.

To obtain the exact length of the text, use the WM_GETTEXT, LB_GETTEXT, or CB_GETLBTEXT messages, or the GetWindowText function.

QuickInfo
Windows NT: Requires version 3.1 or later.
Windows: Requires Windows 95 or later.
Windows CE: Requires version 1.0 or later.
Header: Declared in winuser.h.
goodname008 2003-08-20
  • 打赏
  • 举报
回复
ComboBox主要是由ComboBox和一个Edit类组成的,关键在于要获得那个Edit的句柄并向其发消息,下面的代码是我在XP下调试通过的:获得资源管理器中的那个ComboBox中的内容(运行时先启动一个资源管理器)

Option Explicit
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 Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Const EM_GETLINE = &HC4

Private Sub Command1_Click()
Dim Handle As Long
Dim ParentHandle As Long
Dim ComboText As String
ParentHandle = FindWindow("ExploreWClass", vbNullString)
ParentHandle = FindWindowEx(ParentHandle, 0&, "WorkerW", vbNullString)
ParentHandle = FindWindowEx(ParentHandle, 0&, "ReBarWindow32", vbNullString)
ParentHandle = FindWindowEx(ParentHandle, 0&, "ComboBoxEx32", vbNullString)
ParentHandle = FindWindowEx(ParentHandle, 0&, "ComboBox", vbNullString)
Handle = FindWindowEx(ParentHandle, 0&, "Edit", vbNullString)
ComboText = Space(255)
SendMessage Handle, EM_GETLINE, 0, ByVal ComboText
MsgBox "Combo中的内容为:" & Left(ComboText, InStr(ComboText, Chr(0)) - 1)
End Sub
wumy_ld 2003-08-19
  • 打赏
  • 举报
回复
用SendMeaasge函数

一、SendMeaasge函数简介
顾名思义,SendMessage函数的功能是“发送消息”,即将一条消息发送到指定对象(操作系统、窗口或控件等)上,以产生特定的动作(如滚屏、修改对象外观等)。
SendMessage函数在VB中的函数说明如下:
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (Byval hwnd As Long, Byval wMsg As Long,Byval wParam As Long,lParam As Any) As Long
其中四个自变量的含义和说明如下:
hWnd:对象的句柄。希望将消息传送给哪个对象,就把该对象的句柄作为实参传送,在VB中可以简单地用“对象.hWnd”获得某个对象的句柄,如Text1.hWnd和Form1.hWnd分别可以得到Text1和Form1的句柄。
wMsg:被发送的消息。根据具体需求和不同的对象,将不同的消息作为实参传送,以产生预期的动作。
wParam、lParam:附加的消息信息。这两个是可选的参数,用来提供关于wMsg消息更多的信息,不同的wMsg可能使用这两个参数中的0、1或2个,如果不需要哪个附加参数,则将实参赋为NULL(在VB中赋为0)。
在简单了解了SendMessage函数的格式和功能后,让我们以几个例子来看看它的威力。
二、SendMessage函数使用实例
例1 多行TextBox中的快速处理功能在处理多行TextBox时我们经常会碰到以下几种情况:
希望了解多行TextBox中目前共有多少行文字。
想快速返回第N行的文字。
对于上面的情况,如果用VB自身的语句或函数来实现的话,要写不短的代码,而且由于要采用顺序查找的办法来完成,因此代码的执行效率也很低。如果使用SendMessage函数则可以大大减少代码量,并大幅度的提高执行效率。
用SendMessage函数完成上面两个任务的方法非常简单,每个任务只需简单地发送一条消息给多行TextBox即可,两个消息分别为:EM_GETLINECOUNT、EM_GETLINE,其它参数和返回值见附表。
下面用一个简单的实例演示这两个功能:
新建工程,在Form1上添加三个TextBox(名称分别为Text1、txtLineCount、TxtString,将Text1的Multi
Line属性置为True)、三个标签和一个命令按钮。为工程添加一个模块Moudle1,在其中写如下声明(其中
SendMessage函数的声明可以从VB的“API浏览器”中复制): 消息常量名 消息值 wParam lParam 返回值
EM_GETLINECOUNT &HBA 未用 未用 行数
EM_GETLINE &HC4 要找的行号 存结果的字节串 结果字节串的字节数

Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long,lParam As Any) As Long
Public Const EM_GETLINECOUNT=&HBA
Public Const EM_GETLINE=&HC4
在Form1的代码模块中写如下代码:
Private Sub Command1_Click()
Dim str(256) As Byte
str(1)=1 ’最大允许存放256个字符
’获取总行数,结果显示在文本框txtLineCount中
txtlineCount=SendMessage(Text1.hwnd,EM_GETLINECOUNT,0,0)
’获取第3行的数据放在str中,转换为字符串后显示在文本框txtString中
SendMessage Text1.hwnd,EM_GETLINE,2,str(0)
txtString= StrConv(str,vbUnicode)
End Sub
之后,按F5运行程序,在多行文本框中随便键入几行文字,然后按下[确定]按钮,出现如图画面,说明程序正确统计出了总行数和第3行的文字。
两点补充说明:在调用SendMessage获取第N行字符串时,lParam需要说明为字节数组,在调用完成后,再将字节数组转换为字符串;另外,调用前必须在lParam的前两个字节指明允许存放的最大长度,其中第一个字节为低位,第二个字节为高位,本例将高位(即str(1))置1.说明最大允许存放256个字符。
例2 程序控制拉下或收起组合框的下拉列来
一般情况下,为了拉下或收起组合框的下拉列表,需要用键盘或鼠标进行操作,而有时我们希望程序运行的某个时刻自动拉出下拉列表(比如在一些演示程序中),为了实现这个目的,我们也只有借助于SendMessage函数,方法是发一个CB_SHOWDROPDOWN(&H14F)消息给组合框。
在发CB_SHOWDROPDOWN消息时,wParam参数决定了是拉下列表(=True时)还是收起列表(=False时),lParam无用(设为0)。
为说明具体的使用方法,下面提供简单的程序片段。首先在代码模块中做如下声明:
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 CB_SHOWDROPDOWN=&H14F
当程序中某处需要拉下组合框Combol的列表时,写如下调用语句:
SendMessage Combol.hwnd,CB_SHOWDROPDOWN,True,0
当需要收起组合框Combol的列表时,写如下语句:
SendMessage Combol.hwnd,CB_SHOWDROPDOWNN,False,0
例3 在列表框中查找匹配的项目
在Win95风格的帮助系统中一般都有一个“索引”页,索引页含有一个文本框和一个列表框,当用户在文本框中输入文字时,下拉列表会动态地显示与文本框中文字最匹配的项目,为用户提供了最大的方便。这种效果在应用程序的帮助系统中很容易实现(只要按照Win95帮助系统的正常制作过程制作就可以实现),如果想在应用程序的其它地方实现这种特性就需费一番心思了。
而使用SendMessage函数实现上述特性则非常简单,甚至只需一条语句就足够了,那就是在文本框的Change事件中给列表框发一条LB_FINDSTRING(&H18F)消息,该消息告诉列表框在列表中查找匹配的项目。
在发LB_FINDSTRING消息时,wParam参数代表从列表框的哪一个项目后面开始查找,一般情况下该参数可定为-1,表示从List1(0)即第一项开始向后循环查找,lParam则传进欲搜索的字符串(必须采用值传递)。
具体的代码和运行画面与后面的例4合并在一起演示。
例4 为ListBox添加水平滚动条
在VB中,列表框控件仅提供垂直滚动条,没有设置水平滚动条的能力,当某些项目的文本宽度较长时,超出列表框宽度部分的文本就无法显示出来,因此,很有必要为ListBox添加一个水平滚动条来方便操作。
为添加水平滚动条,只需发一条LB_SETHORIZONTALEXTENT(&H194)消息给列表框即可。发送消息时,wParam为滚动条的长度(以像素为单位,可通过计算得出准确的长度,也可随便给一个大于最大文本宽度的数字,如本例的250),lParam无用。下面是例3和例4合并在一起的代码和运行画面
Declare Function SendMessage Lib "user32" Alias "SendMessageA"(ByVal hwnd As Long,ByVal wMsg As Long,ByVal wParam As Long,lParam As Any) As Long
Public Const LB_FINDSTRING=&H18F
Public Const LB_SETHORIZONTALEXTENT=&H194
Private Sub Form_Load()
List1.AddItem "软件"
List1.AddItem "电脑游戏"
List1.AddItem "电视机"
List1.AddItem "电视台"
List1.AddItem "电脑"
List1.AddItem "电脑游戏软件"
’下一句为列表框添加水平滚动条
SendMessage List1.hwnd,LB_SETHORIZONTALEXTENT,250,0
End Sub
Private Sub Text1_Change()
’注意!当lParam传入的是字符串时,必须用ByVal传递
List1.ListIndex = SendMessage(List1.hwnd,LB_FINDSTRING,-1,ByVal Text1.Text)
End Sub
Wang_xiaoni 2003-08-19
  • 打赏
  • 举报
回复
窗体名称.控件名称.属性名称

1,486

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧