首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >delphi中的记事本问题

delphi中的记事本问题
EN

Stack Overflow用户
提问于 2011-07-11 11:12:19
回答 4查看 2.4K关注 0票数 2

您好,我们使用的是Delphi5版本。在delphi中打开记事本时出现问题。我们希望在单击按钮时打开记事本,并将数据传递给记事本,以便记事本可以显示该数据。我不想保存它。关于这一点,请帮助我。谢谢。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-07-11 17:11:59

您可以使用类似以下内容:

代码语言:javascript
复制
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;
票数 10
EN

Stack Overflow用户

发布于 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

已更新代码

代码语言:javascript
复制
    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;
票数 1
EN

Stack Overflow用户

发布于 2011-07-11 17:33:11

如果你不希望他们能够保存数据,更明智的做法是让你自己的记事本看起来像TMemo,毕竟,这就是TMemo的长处,然后只允许他们编辑文本-如果这是你的要求。否则,几乎不会阻止他们保存文件。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6645426

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档