Search code examples
delphidelphi-5

Notepad problem in delphi


hi we are using the Delphi 5 version. We are getting problem while opening the notepad in delphi. We want to open notepad on a button click and pass the data to it so that notepad can display that data. I dont want to save it. please help me regarding this. thanks.


Solution

  • You can use something like:

    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;