【问题标题】:Notepad problem in delphidelphi记事本问题
【发布时间】:2011-07-11 03:12:19
【问题描述】:

您好,我们使用的是 Delphi 5 版本。在delphi中打开记事本时遇到问题。我们希望在单击按钮时打开记事本并将数据传递给它,以便记事本可以显示该数据。我不想保存它。请帮助我解决这个问题。谢谢。

【问题讨论】:

  • 您是在尝试打开记事本应用程序还是简单的 txt 文件?
  • 我想打开记事本应用程序,以便我们可以根据用户保存记事本数据。谢谢。
  • 也许你可以写一个带有 TMemo 的小表格然后放下记事本?
  • @naren 在 TForm 中使用 alClient 的 TMemo 与记事本几乎相同,您可以获得更多控制权。

标签: delphi delphi-5


【解决方案1】:

你可以使用类似的东西:

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;

【讨论】:

  • +1 用于提供答案,将文本传递到记事本而不将其保存到文件中,就像 OP 要求的那样。
  • +1 表示 WaitForInputIdle。我编辑了您的答案,以满足安装了 Notepad2、Notepad++ 等自定义记事本的用户(如我)的需求......虽然由于 unicode 问题我无法通过 WM_SETTEXT 让它工作,所以我使用了剪贴板。
  • @NGLN - 试图找到一堆文本编辑程序的(活动)编辑窗口不会很有趣,所以剪贴板可能是替代记事本案例的最佳选择。但是可能还有其他问题,例如我在记事本++中有“记住当前会话以供下次启动”,因此它永远不会在启动时打开一个空文档。无论如何,感谢您的编辑,也感谢您的编辑,而不是 newline ing 我的“开始”。 :)
  • @Sertac 是的,我真的很想知道你喜欢end else begin 语法。 ;)
  • @Sertac 好吧 - 再次阅读您的评论后 - 找到活动窗口似乎不是 GetGUITreadInfo 的问题,但我使用剪贴板的原因是发送 WM_SETTEXT 导致只插入第一个字符到记事本* 编辑控件,我将其翻译为 unicode 问题。
【解决方案2】:

您可以在按钮单击事件中使用以下命令。在textFileName.txt中指定要打开的文件名

ShellExecute(Handle,'open', 'c:\windows\notepad.exe','textFileName.txt', nil, SW_SHOWNORMAL) ;

如果您想打开一个空白的 txt 文件并且不想保存任何数据,您可以在点击事件中使用以下方法。 ShellExecute(Handle,'open', 'c:\windows\notepad.exe',nil, nil, SW_SHOWNORMAL) ;

在使用类中添加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;

【讨论】:

  • 实际上不想保存文件。我想通过delphi将数据传递到记事本并从记事本中保存该数据。谢谢。
  • 您想打开记事本应用程序,用户会在上面输入一些内容,但不一定要保存数据。我说的对吗?
  • 不,实际上我有一个字符串数据,我想在记事本中显示。在记事本中打开后,用户将决定是否保存该文件。谢谢
【解决方案3】:

如果您不希望他们能够保存数据,那么让您自己的记事本看起来像这样会更明智,毕竟这就是 TMemo 的好处,然后只允许他们编辑文本 - 如果那样的话你的要求。否则,很少有人会阻止他们保存文件。

【讨论】:

    【解决方案4】:

    Delphi 中的记事本。

    1. 取一个TMemo并命名为:(memNotepad)

    2. 拍一张TrzOpenDialog(rzopdlgOpenDialog)

    3. 拿一张TrzSaveDialog(rzsvdlgSaveDialog)
    4. 拿一个FontDialog (ftdlgFontDialog)
    5. 拿一个TMainMenu (mmMainMenu)
      • 添加菜单项文件:新建、打开、保存、另存为、退出、 编辑:剪切、复制、粘贴、字体

    代码:

    ********************************************************************
    unit ufrmNotePad;
    
    interface
    
    uses
    
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, Menus, RzShellDialogs, cxGraphics, cxCustomData,
      cxStyles, cxTL, cxControls, cxInplaceContainer, IniFiles;
    
    type
      TfrmNotepad = class(TForm)
    
        memNotePad: TMemo;
        rzopdlgOpenDialog: TRzOpenDialog;
        rzsvdlgSaveDialog: TRzSaveDialog;
        mmMainMenu: TMainMenu;
        mniFile: TMenuItem;
        mniSave: TMenuItem;
        mniOpen: TMenuItem;
        mniNew: TMenuItem;
        mniSaveAs: TMenuItem;
        mniEdit: TMenuItem;
        mniPaste: TMenuItem;
        mniCopy: TMenuItem;
        mniCut: TMenuItem;
        ftdlgFontDialog: TFontDialog;
        mniFont: TMenuItem;
        mniExit: TMenuItem;
        procedure mniOpenClick(Sender: TObject);
        procedure mniSaveClick(Sender: TObject);
        procedure mniNewClick(Sender: TObject);
        procedure mniSaveAsClick(Sender: TObject);
        procedure mniCopyClick(Sender: TObject);
        procedure mniPasteClick(Sender: TObject);
        procedure mniCutClick(Sender: TObject);
        procedure mniFontClick(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure mniEditClick(Sender: TObject);
        procedure mniExitClick(Sender: TObject);
        procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    
      private
    
        { Private declarations }
        procedure OpenDialog;
        procedure SaveDialog;
        procedure NewNotepad;
        procedure SaveAS;
        procedure Copy;
        procedure Cut;
        procedure Paste;
        procedure Font;
        procedure SaveSettingInToFile;
        procedure RetrieveSettingFromTheFile;
      public
    
        { Public declarations }
        FIniFile : TIniFile;
        FFileName : String;
        FCount : Integer;
      end;
    
    var
      frmNotepad : TfrmNotepad;
    
    implementation
    
    {$R *.dfm}
    
    { Open File from the disk }
    
    { FCount- helps for save the file, if file is already save then FCount := 1 else FCount := 0}
    
    { FFileName - helps for store the filename of open filename or saved filename }
    
    procedure TfrmNotepad.OpenDialog;
    
    begin
    
      FCount := 1;
    
      rzopdlgOpenDialog.Title := 'Open File';
    
      rzopdlgOpenDialog.Filter := 'Text Files|*.txt; RTF Files|*.rtf';
    
      if rzsvdlgSaveDialog.FileName = FFileName then
    
      begin
    
        if rzopdlgOpenDialog.Execute then
    
          memNotePad.Lines.LoadFromFile(rzopdlgOpenDialog.FileName);
    
      end
    
      { if any file already opened and without saving the file try to open new file
        then message dialog box appears on the screen for save the file or save modification of the file }
    
      else if (memNotePad.Modified) then
    
      begin
    
        ModalResult := MessageDlg('Do you want to save Changes to untitled?',mtConfirmation,mbYesNoCancel,0);
        if (ModalResult = mrYes) then
        begin
          if rzsvdlgSaveDialog.Execute then
            memNotepad.Lines.SaveToFile(rzsvdlgSaveDialog.FileName + '.txt');
          memNotePad.Clear;
          memNotePad.Modified := False;
        end
        else if (ModalResult = mrNo) then
        begin
          if rzopdlgOpenDialog.Execute then
          begin
            memNotePad.Lines.LoadFromFile(rzopdlgOpenDialog.FileName);
            FFileName := rzopdlgOpenDialog.FileName;
          end;
        end;
      end
    
      else if rzopdlgOpenDialog.Execute then
    
      begin
    
        memNotePad.Lines.LoadFromFile(rzopdlgOpenDialog.FileName);
        FFileName := rzopdlgOpenDialog.FileName;
      end;
      Caption := rzopdlgOpenDialog.FileName + ' - Delphi Notepad';
    
    end;
    
    { Saving the file }
    
    procedure TfrmNotepad.SaveDialog;
    
    begin
    
      rzsvdlgSaveDialog.Title := 'Save';
    
      rzsvdlgSaveDialog.Filter := 'Text Files|*.txt; RTF Files|*.rtf';
    
      { if file already exist and after modification we try to save then it directly save into the file without opening the save dialog box }
    
      if FCount = 1 then
    
      begin
    
        { filename store in the FFileName is compare with the save dialog box filename
          if it matches then modification dirctly save into that file without opening the save dialog box }
    
        if rzsvdlgSaveDialog.FileName = FFileName then
    
          memNotePad.Lines.SaveToFile(rzsvdlgSaveDialog.FileName + '.txt')
    
        { filename store in the FFileName is compare with the open dialog box filename
          if it matches then modification dirctly save into that file without opening the save dialog box }
    
        else if rzopdlgOpenDialog.FileName = FFileName then
    
        begin
    
          rzsvdlgSaveDialog.FileName := rzopdlgOpenDialog.FileName;
          memNotePad.Lines.SaveToFile(rzsvdlgSaveDialog.FileName);
        end;
      end
    
      { else file already not save then Save Dialog box open }
    
      else
    
      begin
    
        if rzsvdlgSaveDialog.Execute then
          memNotePad.Lines.SaveToFile(rzsvdlgSaveDialog.FileName + '.txt');
        Caption := rzsvdlgSaveDialog.FileName + ' - Delphi Notepad';
        FCount := 1;
      end;
      FFileName := rzsvdlgSaveDialog.FileName;
    
    end;
    
    { New Notepad }
    
    procedure TfrmNotepad.NewNotepad;
    
    begin
    
      if FCount = 1 then
    
      begin
    
        memNotePad.Clear;
    
        Caption := 'UnNamed Delphi Notepad';
    
      end
    
      else if not (memNotePad.Modified) then
    
      begin
    
        memNotePad.Clear;
    
        Caption := 'UnNamed Delphi Notepad';
    
      end
    
      { without saving the modification's of file if try to click on new then
        message dialog box appears on the screen for saving the modification's }
    
      else if (memNotePad.Modified) then
    
      begin
    
        ModalResult := MessageDlg('Do you want to save Changes',mtConfirmation,mbYesNoCancel,0);
    
        if (ModalResult = mrYes) then
    
        begin
    
          if rzsvdlgSaveDialog.Execute then
            memNotepad.Lines.SaveToFile(rzsvdlgSaveDialog.FileName + '.txt');
          memNotePad.Clear;
          memNotePad.Modified := False;
        end
        else if (ModalResult = mrNo) then
        begin
          memNotePad.Clear;
          memNotePad.Modified := False;
        end;
      end;
    
      FFileName := rzsvdlgSaveDialog.FileName;
    
      FCount := 1;
    
    end;
    
    { Save As }
    
    procedure TfrmNotepad.SaveAS;
    
    begin
    
      rzsvdlgSaveDialog.Title := 'Save As';
    
      rzsvdlgSaveDialog.Filter := 'Text Files|*.txt; RTF Files|*.rtf';
    
      if rzsvdlgSaveDialog.Execute then
    
        memNotepad.Lines.SaveToFile(rzsvdlgSaveDialog.FileName + '.txt');
    
      memNotePad.Clear;
    
      memNotePad.Modified := False;
    
    end;
    
    { Cut text }
    
    procedure TfrmNotepad.Cut;
    
    begin
    
      memNotePad.CutToClipboard;
    
    end;
    
    { Copy text }
    
    procedure TfrmNotepad.Copy;
    
    begin
    
      memNotePad.CopyToClipboard;
    
    end;
    
    { Paste }
    
    
    procedure TfrmNotepad.Paste;
    
    begin
    
      memNotePad.PasteFromClipboard;
    
    end;
    
    { Font Dialog is assign to memo }
    
    procedure TfrmNotepad.Font;
    
    begin
    
      if ftdlgFontDialog.Execute then
    
        memNotePad.Font := ftdlgFontDialog.Font;
    
    end;
    
    
    { Save the setting of font in to Ini file }
    
    procedure TfrmNotepad.SaveSettingInToFile;
    
    var
    
      LColorName : String;
    
    begin
    
      FIniFile.WriteString('Setting', 'FontName', memNotePad.Font.Name);
    
      FIniFile.WriteString('Setting', 'FontSize', IntToStr(memNotePad.Font.Size));
    
      LColorName := ColorToString(memNotePad.Font.Color);
    
      FIniFile.WriteString('Setting', 'FontColor', LColorName);
    
    end;
    
    
    { Retrieve the setting of font from Ini file }
    
    procedure TfrmNotepad.RetrieveSettingFromTheFile;
    
    var
    
      LFontName, LFontSize, LFontColor: String;
    
    begin
    
      FIniFile := TIniFile.Create('FontSettings.ini');
    
      LFontName := FIniFile.ReadString('Setting', 'FontName', 'Arial');
    
      memNotePad.Font.Name := LFontName;
    
      LFontSize := FIniFile.ReadString('Setting', 'FontSize', '8');
    
      memNotePad.Font.Size := StrToInt(LFontSize);
    
      LFontColor := FIniFile.ReadString('Setting', 'FontColor', 
    ColorToString(clBlack));
    
      memNotePad.Font.Color := StringToColor(LFonTColor);
    
      ftdlgFontDialog.Font := memNotePad.Font;
    
    end;
    
    procedure TfrmNotepad.mniOpenClick(Sender: TObject);
    
    begin
    
      OpenDialog;
    
    end;
    
    
    procedure TfrmNotepad.mniSaveClick(Sender: TObject);
    
    begin
    
      SaveDialog;
    
    end;
    
    
    procedure TfrmNotepad.mniNewClick(Sender: TObject);
    
    begin
    
      NewNotepad;
    
    end;
    
    
    procedure TfrmNotepad.mniSaveAsClick(Sender: TObject);
    
    begin
    
      SaveAS;
    
    end;
    
    
    { Exit from Notepad }
    
    procedure TfrmNotepad.mniExitClick(Sender: TObject);
    
    begin
    
      { While exit if the modification in file is not save then then message dialog appear on the screen }
    
      if (memNotePad.Modified) then
    
      begin
    
        ModalResult := MessageDlg('Do you want to save 
    Changes',mtConfirmation,mbYesNoCancel,0);
    
        if (ModalResult = mrYes) then
    
        begin
    
          if rzsvdlgSaveDialog.FileName = FFileName then
    
            memNotePad.Lines.SaveToFile(rzsvdlgSaveDialog.FileName + '.txt')
    
          else if rzopdlgOpenDialog.FileName = FFileName then
    
          begin
    
            rzsvdlgSaveDialog.FileName := rzopdlgOpenDialog.FileName;
    
            memNotePad.Lines.SaveToFile(rzsvdlgSaveDialog.FileName);
    
          end
    
          else if rzsvdlgSaveDialog.Execute then
    
            memNotepad.Lines.SaveToFile(rzsvdlgSaveDialog.FileName + '.txt');
    
          memNotePad.Clear;
    
          memNotePad.Modified := False;
    
          Close;
    
        end
    
        else if (ModalResult = mrNo) then
    
        begin
    
          memNotePad.Clear;
    
          memNotePad.Modified := False;
    
          Close;
    
        end;
    
      end;
    
      { call method for saving the font setting into Ini file }
    
      SaveSettingInToFile;
    
    end;
    
    procedure TfrmNotepad.mniCutClick(Sender: TObject);
    
    begin
    
      Cut;
    
    end;
    
    
    procedure TfrmNotepad.mniCopyClick(Sender: TObject);
    
    begin
    
      Copy;
    
    end;
    
    
    procedure TfrmNotepad.mniPasteClick(Sender: TObject);
    
    begin
    
      Paste;
    
    end;
    
    
    procedure TfrmNotepad.mniFontClick(Sender: TObject);
    begin
    
      Font;
    
    end;
    
    
    procedure TfrmNotepad.FormCreate(Sender: TObject);
    
    begin
    
      FCount := 0;
    
      FFileName := 'ok';
    
      { call method for retrieve the font setting from Ini file }
    
      RetrieveSettingFromTheFile;
    
    end;
    
    
    procedure TfrmNotepad.FormDestroy(Sender: TObject);
    
    begin
    
      { call method for saving the font setting into Ini file }
    
      SaveSettingInToFile;
    
    end;
    
    
    { when text is selected on notepad, only then the copy & cut are enabled else disabled }
    
    procedure TfrmNotepad.mniEditClick(Sender: TObject);
    
    begin
    
      mniCopy.Enabled := memNotePad.SelLength > 0;
    
      mniCut.Enabled := memNotePad.SelLength > 0;
    
    end;
    
    
    procedure TfrmNotepad.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    
    begin
    
      { While Destroying the form if the modification in file is not save then then
        message dialog appear on the screen }
    
      rzsvdlgSaveDialog.Filter := 'Text Files|*.txt; RTF Files|*.rtf';
    
      if (memNotePad.Modified) then
    
      begin
    
        ModalResult := MessageDlg('Do you want to save 
    Changes',mtConfirmation,mbYesNoCancel,0);
        if (ModalResult = mrYes) then
    
        begin
    
          if rzsvdlgSaveDialog.FileName = FFileName then
    
            memNotePad.Lines.SaveToFile(rzsvdlgSaveDialog.FileName + '.txt')
    
          else if rzopdlgOpenDialog.FileName = FFileName then
    
          begin
    
            rzsvdlgSaveDialog.FileName := rzopdlgOpenDialog.FileName;
    
            memNotePad.Lines.SaveToFile(rzsvdlgSaveDialog.FileName);
    
          end
    
          else if rzsvdlgSaveDialog.Execute then
    
            memNotepad.Lines.SaveToFile(rzsvdlgSaveDialog.FileName + '.txt');
    
          memNotePad.Clear;
    
          memNotePad.Modified := False;
    
        end
    
        else if (ModalResult = mrNo) then
    
        begin
    
          memNotePad.Clear;
    
          memNotePad.Modified := False;
    
        end;
    
      end;
    
      CanClose := True;
    
    end;
    
    
    end.
    

    【讨论】:

      猜你喜欢
      • 2023-03-09
      • 2014-08-19
      • 2014-12-28
      • 1970-01-01
      • 1970-01-01
      • 2021-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多