您好,我们使用的是Delphi5版本。在delphi中打开记事本时出现问题。我们希望在单击按钮时打开记事本,并将数据传递给记事本,以便记事本可以显示该数据。我不想保存它。关于这一点,请帮助我。谢谢。
发布于 2011-07-11 17:11:59
您可以使用类似以下内容:
uses
Clipbrd;
procedure LaunchNotepad(const Text: string);
var
SInfo: TStartupInfo;
PInfo: TProcessInformation;
Notepad: HWND;
NoteEdit: HWND;
ThreadInfo: TGUIThreadInfo;
begin
ZeroMemory(@SInfo, SizeOf(SInfo));
SInfo.cb := SizeOf(SInfo);
ZeroMemory(@PInfo, SizeOf(PInfo));
CreateProcess(nil, PChar('Notepad'), nil, nil, False,
NORMAL_PRIORITY_CLASS, nil, nil, sInfo, pInfo);
WaitForInputIdle(pInfo.hProcess, 5000);
Notepad := FindWindow('Notepad', nil);
// or be a little more strict about the instance found
// Notepad := FindWindow('Notepad', 'Untitled - Notepad');
if Bool(Notepad) then begin
NoteEdit := FindWindowEx(Notepad, 0, 'Edit', nil);
if Bool(NoteEdit) then begin
SendMessage(NoteEdit, WM_SETTEXT, 0, Longint(Text));
// To force user is to be asked if changes should be saved
// when closing the instance
SendMessage(NoteEdit, EM_SETMODIFY, WPARAM(True), 0);
end;
end
else
begin
ZeroMemory(@ThreadInfo, SizeOf(ThreadInfo));
ThreadInfo.cbSize := SizeOf(ThreadInfo);
if GetGUIThreadInfo(0, ThreadInfo) then begin
NoteEdit := ThreadInfo.hwndFocus;
if Bool(NoteEdit) then begin
Clipboard.AsText := Text;
SendMessage(NoteEdit, WM_PASTE, 0, 0);
end;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
LaunchNotepad('test string');
end;发布于 2011-07-11 11:20:28
您可以在按钮单击事件中使用以下命令。指定要在textFileName.txt中打开的文件名
ShellExecute(Handle,'open', 'c:\windows\notepad.exe','textFileName.txt', nil, SW_SHOWNORMAL) ;
如果你想打开一个空白的txt文件并且不想保存任何数据,你可以在你的click事件中使用下面的方法。ShellExecute(Handle,'open', 'c:\windows\notepad.exe',nil, nil, SW_SHOWNORMAL) ;
在uses类中也添加ShellApi。
已更新代码
procedure TForm1.Button1Click(Sender: TObject);
var
tempString : TStringList;
begin
tempString := TStringList.Create;
try
tempString.Add('The text you wanted to display');
tempString.SaveToFile('C:\~tempFile.txt');
finally
tempString.Free;
end;
ShellExecute(Handle,'open', 'c:\windows\notepad.exe','C:\~tempFile.txt', nil, SW_SHOWNORMAL) ;
end;发布于 2011-07-11 17:33:11
如果你不希望他们能够保存数据,更明智的做法是让你自己的记事本看起来像TMemo,毕竟,这就是TMemo的长处,然后只允许他们编辑文本-如果这是你的要求。否则,几乎不会阻止他们保存文件。
https://stackoverflow.com/questions/6645426
复制相似问题