300分,帮忙写个小程序,DELPHI中很简单,但是我不会的:用程序改变另外一个窗体里的TEXT中的文本. (300分)

I

ike

Unregistered / Unconfirmed
GUEST, unregistred user!
如何利用程序改变另一个窗体里的text中的文本.
 
我想应该根据哪些指标股当前的价格根据公式来计算的,你最好是看一下炒股方面的书吧。
 
哥哥,你两个窗体是不是关联的呀,如是关联的可以直接改变。在form1中可以这样:form2.text1.text:='xxxxxxxxxxxxxxxxxxxxxx';
不知我说的你满意否?
 
这两个窗体不是同一应用程序的啊.
 
找到Edit的Handle,用API SetWindowText
不过一般2000以后的程序都会对消息的来源句柄进行检查,需要另外采取一些措施。。。
 
用SendMessage(Handle, WM_SETTEXT, 0, Integer(Str));
↑ ↑
Text的句柄 要设置的文字
 
SendMessage(findwindow(nil,'你的应用程序的caption'), WM_SETTEXT, 0,Str);
 
implementation
uses unit1;
form1.text1.text:='xxxxxxxxxxxxxxxxxxxxxx';
 
只改变文本,不调出窗体吗?
 
将另一窗体中的文本与一文件关联,修改这个文件就行了
 
你知道内存映射文件的使用吗?找本书看看,加上消息机制,可在两个应用程序之间传递数据。改变文本,很简单的哟。
给点分吧。
 
各位大哥能否还写明白点呢?
我的意思是,如果一个应用程序的文本框是只读的,我现在想自己写一个程序去改变它里面的文字,该怎么做呢?
 
方面有很多种,你定义一个全局变量就行了,只是这个是一种最简单也最浪费资源的做法。
 
我想你是不是这样的呢?
首先可以找到该文本框的句柄,你可以用spy++这样的工具.然后利用sendmessage去改变文本框里的文本.
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
调用一个窗口的窗口函数,将一条消息发给那个窗口。除非消息处理完毕,否则该函数不会返回。SendMessageBynum,
SendMessageByString是该函数的“类型安全”声明形式
Long,由具体的消息决定
hwnd ----------- Long,要接收消息的那个窗口的句柄
wMsg ----------- Long,消息的标识符
wParam --------- Long,具体取决于消息
lParam --------- Any,具体取决于消息
'--------------------------------
'扩展文本框功能
Public Const EM_GETSEL = &HB0
Public Const EM_LINEFROMCHAR = &HC9
Public Const EM_LINEINDEX = &HBB
Public Const EM_GETLINE = &HC4
Public Const EM_GETLINECOUNT = &HBA
Public 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
Public Declare Function SendMessages Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Any) As Long
'获得文本的行数
Public Function GetTextLines(txtHwnd As Long) As Long
GetTextLines = SendMessage(txtHwnd, EM_GETLINECOUNT, 0, 0)
End Function
'功能描述:获得指定文本的光标
Public Sub GetCaretPos(ByVal TextHwnd As Long, LineNo As Long, ColNo As Long)
Dim i As Long, j As Long
Dim lParam As Long, wParam As Long
Dim k As Long

'首先向文本框传递EM_GETSEL消息以获取从起始位置到
'光标所在位置的字符数
i = SendMessage(TextHwnd, EM_GETSEL, wParam, lParam)
j = i / 2 ^ 16
'再向文本框传递EM_LINEFROMCHAR消息根据获得的字符
'数确定光标以获取所在行数
LineNo = SendMessage(TextHwnd, EM_LINEFROMCHAR, j, 0)
LineNo = LineNo + 1
'向文本框传递EM_LINEINDEX消息以获取所在列数
k = SendMessage(TextHwnd, EM_LINEINDEX, -1, 0)
ColNo = j - k + 1
End Sub
'功能描述:获得指定行的文本
Public Function ReadLine(ByVal TextHwnd As Long, intLine As Long) As String
Dim m_sLineString As String
Dim m_intRet As Long

m_sLineString = Space$(1056)
m_intRet = SendMessages(TextHwnd, EM_GETLINE, intLine, ByVal m_sLineString)
ReadLine = Left(m_sLineString, m_intRet)
End Function
'获得光标处的字符
Public Function GetWord(ByVal TextHwnd As Long) As String
'打开错误处理陷阱
On Error GoTo ErrGoto
'---------------------------------
'代码正文
Dim LineNo As Long
Dim ColNo As Long
Dim strData As String
Dim i As Integer
Dim intAsc As Integer
Dim intbegin
As Integer, intEnd As Integer

GetCaretPos TextHwnd, LineNo, ColNo
strData = ReadLine(TextHwnd, LineNo - 1)
'---------------------------
'修正含有汉字的列数
intCharNum = 0
For i = 0 To Len(strData) - 1
If Asc(Mid(strData, i + 1, 1)) < 0 then
intCharNum = intCharNum + 2
else
intCharNum = intCharNum + 1
End If

If intCharNum >= ColNo then
Exit For
End If
Next i
ColNo = i + 1
'-----------------------------
If Len(strData) > 0 then
For i = ColNo - 1 To 1 Step -1
intAsc = Asc(Mid(strData, i, 1))
If Not ((intAsc >= Asc("a") And intAsc <= Asc("z")) Or (intAsc >= Asc("A") And intAsc <= Asc("Z")) Or (intAsc >= Asc("0") And intAsc <= Asc("9")) Or intAsc = Asc("_")) then
intbegin
= i + 1
Exit For
End If
Next i
For i = ColNo To Len(strData)
intAsc = Asc(Mid(strData, i, 1))
If Not ((intAsc >= Asc("a") And intAsc <= Asc("z")) Or (intAsc >= Asc("A") And intAsc <= Asc("Z")) Or (intAsc >= Asc("0") And intAsc <= Asc("9")) Or intAsc = Asc("_")) then
intEnd = i - 1
Exit For
End If
Next i

If intbegin
<= 0 then
intbegin
= 1
If intEnd <= 0 then
intEnd = Len(strData)
If intEnd > intbegin
then
GetWord = Trim(Mid(strData, intbegin
, intEnd - intbegin
+ 1))
else
GetWord = ""
End If
else
GetWord = ""
End If
'----------------------
Exit Function
'----------------------
ErrGoto:
GetWord = ""
End Function
 
用EnumChildWindow找到要找的Text的句柄然后用SendMessage就可以了
 
多人接受答案了。
 
用一个全局变量
你先创建一个过程
在创建两个窗体
窗体1的代码
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,unit2, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
dd := edit1;
application.CreateForm(tform2,form2);
form2.ShowModal;
end;

end.
窗体2 的代码
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm2 = class(TForm)
Edit1: TEdit;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form2: TForm2;
dd : tedit;
implementation
{$R *.dfm}
procedure TForm2.FormCreate(Sender: TObject);
begin
edit1.Text := dd.Text;
end;

end.
 
顶部