Seite 4 von 4

Re: Text an notepad senden und unter bestehendem Text einfügen ?

Verfasst: 29.09.2022 21:00
von TWELVE
mk-soft hat geschrieben: 29.09.2022 20:30 Geht es noch unter Windows 10 ?
Ich habe kein Windows 11, aber es könnte eine Sache der Sicherheit sein.
Ja, unter Windows 10 funktioniert es einwandfrei, sowohl matbal's code als auch mein Tool.

Ich hab es auch schon als Administrator gestartet und verschiedene PureBasic Versionen ausprobiert, auch 6.0.Macht keinen Unterschied.

Es muß wohl am neuen Notepad liegen, man kann das in W11 deinstallieren, dann bekommt man das alte Notepad, wenn man notepad.exe startet.
Und mit dem geht es so wie unter Windows 10.Eine wirkliche Lösung ist das nicht...

Re: Text an notepad senden und unter bestehendem Text einfügen ?

Verfasst: 20.06.2024 13:28
von Lord
Hat schon jemand eine Lösung für dieses Problem gefunden.
Ich bin auch gerade darauf gestoßen, nachdem ich auf Win11
umsteigen mußte.

Re: Text an notepad senden und unter bestehendem Text einfügen ?

Verfasst: 20.06.2024 16:26
von Axolotl
vllt hilft das hier weiter ....

Code: Alles auswählen

EnableExplicit

Procedure SendMessageToNotepad(hwnd, Message$) 
  SendMessage_(hWnd, #EM_SETSEL, $fffffff, $fffffff)    ; no selection, caret at the end 
  SendMessage_(hWnd, #EM_REPLACESEL, 0, Message$)   ; + #CRLF$ 
EndProcedure

Procedure EnumDesktopChildWindows(hwnd, *Result.INTEGER) 
  Protected class${#MAX_PATH}, title${#MAX_PATH} 

	If hwnd 
		GetClassName_(hwnd, @class$, #MAX_PATH) 
	; GetWindowText_(hwnd, @title$, #MAX_PATH)  ; check title, if necessary 

		If class$ = "Notepad" 
; Debug #PB_Compiler_Procedure + "()  .. Class = " + #DQUOTE$ + class$ + #DQUOTE$ + " --> " + #DQUOTE$ + title$ + #DQUOTE$ 
			hwnd = FindWindowEx_(hwnd, 0, @"NotepadTextBox", 0)  ; child window ??? 
			If hwnd > 0 
  			hwnd = FindWindowEx_(hwnd, 0, @"RichEditD2DPT", 0)  ; child window ???
			  If hwnd > 0 
			    ; we need this window handle 
			    *Result\i = hwnd  			              ; : Debug " YES: found it -> " + hwnd  
        EndIf 
      EndIf 
		Else 
			ProcedureReturn 1  ; try again 
		EndIf
	EndIf
	ProcedureReturn 0  ; stop enumeration 
EndProcedure 

Procedure main() 
  Protected hwndNotepad
  Protected tries    

  ; look for a running instance 
  EnumChildWindows_(0, @EnumDesktopChildWindows(), @hwndNotepad)  ; with hwndParent = 0, this function is equivalent to EnumWindows. 
  If hwndNotepad = 0 
    ; start now, because nothing found
    RunProgram("notepad.exe", "", "", #PB_Program_Open) 
    Delay(500) ; give windows time to start the notepad .... 
  EndIf 

  ; Look for a running instance 
  For tries = 0 To 5 
    hwndNotepad = 0 
	  EnumChildWindows_(0, @EnumDesktopChildWindows(), @hwndNotepad)  ; with hwndParent = 0, this function is equivalent to EnumWindows. 
    If hwndNotepad : Break : EndIf ; with a valid window, we can go further 
  Next tries 

  ; start this test window 
  If hwndNotepad 
	  SendMessageToNotepad(hwndNotepad, "Test the best ...." + #CRLF$) 

    ; Demo-Fenster 
    If OpenWindow(0, 100, 100, 220, 80, "Fenster", #PB_Window_SystemMenu) 
      StringGadget(1, 10, 10, 200, 20, "Textzeile")
      ButtonGadget(2, 10, 40, 100, 25, "Senden")

      Repeat
        Select WaitWindowEvent() 
          Case #PB_Event_CloseWindow 
            Break   ; say good bye 
          Case #PB_Event_Gadget 
            Select EventGadget() 
              Case 2  ; Button 
                SendMessageToNotepad(hwndNotepad, GetGadgetText(1) + #CRLF$) 
            EndSelect 
        EndSelect 
      ForEver 
    EndIf 
  Else 
    MessageRequester("Error", "Cannot find the Notepad-Editor") 
  EndIf 
  ProcedureReturn 0 
EndProcedure 

End main()  

Debug "done " 

Re: Text an notepad senden und unter bestehendem Text einfügen ?

Verfasst: 20.06.2024 20:03
von Lord
Hallo!
Axolotl hat geschrieben: 20.06.2024 16:26 llt hilft das hier weiter ....
Das hilft schon einen Schritt weiter, indem ein Text mit Notepad
angezeigt wird.
Das Problem ist noch, daß nicht grundsätzlich ein neues Notepad geöffnet
wird, sondern daß auch in ein (noch zufällig geöffnetes) Notepad geschrieben
wird. Notepad nutzt jetzt ja auch noch Tabs, so daß dort dan in das letzte
Tab geschrieben wird.
Nichts desto trotz, ein Anfang ist gemacht. Es wist nur nicht einfacher ge-
worden dank MicroSoft.

Re: Text an notepad senden und unter bestehendem Text einfügen ?

Verfasst: 21.06.2024 15:49
von Axolotl
Ja, leider wahr.
Laut MS-Blog:
Im Januar 2020 wurde mit Microsoft 365 RichEdit ein D2D/DirectWrite RichEdit Fenster Control mit den neuen Fensterklassen "RichEditD2D" und "RichEditD2DPT" eingeführt. Sie verwenden D2D/DirectWrite für Text und Bilder und den HDC des Fensters zum Rendern eingebetteter Objekte und zum Drucken. Der Windows 11 Notepad verwendet die Fensterklasse RichEditD2DPT mit einem aktuellen Microsoft 365 RichEdit.
Probier mal das hier.
Ich verwende immer noch ein offenes Notepad (mit einigen offenen Tabs), du kannst aber auch die zwei zeilen auskommentieren, dann öffnet sich ein neues Notepad.
Findet alle offenen Tab-basierten RichEdit-Controls (win11) in die du dann schreiben kannst.
Leider reichen mein Win32 Kenntnisse nicht mehr für den o.g. Kram. (Vielleicht ist es doch Zeit mal wieder Linux zu versuchen.... )

Code: Alles auswählen

EnableExplicit

Global NewMap hWnd()    ; avoid double items 
Global NewList hwndEditor() ; the tabs of the notepad 


; ---------------------------------------------------------------------------------------------------------------------

Procedure.s GetClassName(hWnd)  ; returns the Classname of the specified window 
  Protected className${#MAX_PATH} 

  GetClassName_(hWnd, @className$, #MAX_PATH) 

  ProcedureReturn className$ 
EndProcedure 

; ---------------------------------------------------------------------------------------------------------------------

Procedure.s GetWindowText(hWnd)  ; returns the window title (caption) of the specified window 
  Protected text${#MAX_PATH} 

  GetWindowText_(hWnd, @text$, #MAX_PATH) 

  ProcedureReturn text$ 
EndProcedure 

; ---------------------------------------------------------------------------------------------------------------------

Procedure SendMessageToNotepad(hwnd, Message$) 
  SendMessage_(hWnd, #EM_SETSEL, $fffffff, $fffffff)    ; no selection, caret at the end 
  SendMessage_(hWnd, #EM_REPLACESEL, 0, Message$)   ; + #CRLF$ 
EndProcedure 

; ---------------------------------------------------------------------------------------------------------------------

Procedure EnumChildProc(hWnd, lParam)
  Protected PID 
  Protected title$, classname$ 

  If Not FindMapElement(hWnd(), Str(hWnd)) 
    GetWindowThreadProcessId_(hWnd, @PID) ; get the process id 

    title$ = GetWindowText(hWnd)
    classname$ = GetClassName(hWnd) 

    AddGadgetItem(0, -1, "PID: " + Right("000" + Str(PID), 4) + " Window: " + Right("0000" + Hex(hWnd, #PB_Long), 8) + " " + #DQUOTE$ + title$ + #DQUOTE$ + " " + classname$, 0, lParam + 1)

    ; works on win11 
    ; 
    If classname$ = "RichEditD2DPT" 
      AddElement(hwndEditor()) : hwndEditor() = hWnd  ; : Debug "  found tab --> hwnd == " + hwnd 
    EndIf 

    hWnd(Str(hWnd)) = 0   ; only to avoid double items 
  EndIf
  EnumChildWindows_(hWnd, @EnumChildProc(), lParam + 1)
  ProcedureReturn #True 
EndProcedure

Procedure EnumDesktopChildProc(hwnd, lparam) ;*Result.INTEGER) 
	If hwnd 
		If GetClassName(hwnd) = "Notepad" 
		  ClearMap(hwnd()) 
      EnumChildWindows_(hwnd, @EnumChildProc(), 0) 
		EndIf
	EndIf
	ProcedureReturn 1  ; try again 
EndProcedure 

; ---------------------------------------------------------------------------------------------------------------------

Procedure main() 
  Protected hwnd 

  ; start the notepad with the next two lines 
  ; I use my open notepad with all tabs instead .... 
  
; RunProgram("notepad.exe", "", "", #PB_Program_Open) 
; Delay(500) ; give windows time to start the notepad .... 

  If OpenWindow(0, 0, 0, 600, 400, "Open Notpad with all Tabs ... ", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    TreeGadget(0, 0, 0, 600, 300, #PB_Tree_AlwaysShowSelection) 

    TextGadget(5, 160, 308, 120, 16, "Test to send") 
    StringGadget(1, 160, 324, 400, 20, "Textline to Notepad.") 

    TextGadget(4, 16, 308, 120, 16, "Tabs List (hwnd)") 
    ListViewGadget(2, 16, 324, 120, 64) 
    ButtonGadget(3, 220, 368, 160, 24, "Send to Notepad") 

    ; init 
    ; 
    ClearGadgetItems(0)   
    EnumChildWindows_(GetDesktopWindow_(), @EnumDesktopChildProc(), 0)

    If CountGadgetItems(0) > 0 
      SetGadgetItemState(0, 0, #PB_Tree_Expanded)
      SendMessage_(GadgetID(0), #TVM_SORTCHILDREN, #True, SendMessage_(GadgetID(0), #TVM_GETNEXTITEM, #TVGN_ROOT, 0)) 
    EndIf 

    If ListSize(hwndEditor()) > 0 
      ForEach(hwndEditor()) 
        AddGadgetItem(2, -1, Str(hwndEditor())) 
      Next 
      SetGadgetState(2, 0) 
    Else 
      AddGadgetItem(2, -1, "Error: No running Notepad found.") 
      DisableGadget(3, 1) 
    EndIf 

    Repeat
      Select WaitWindowEvent() 
        Case #PB_Event_CloseWindow 
          Break   ; say good bye 
        Case #PB_Event_Gadget 
          Select EventGadget() 
            Case 3  ; Button 
              If GetGadgetState(2) > -1 
                hwnd = Val(GetGadgetText(2)) 
              EndIf 
              SendMessageToNotepad(hwnd, GetGadgetText(1) + #CRLF$) 
          EndSelect 
      EndSelect 
    ForEver 
  EndIf
  ProcedureReturn 0 
EndProcedure 

End main()  

; BoF

Re: Text an notepad senden und unter bestehendem Text einfügen ?

Verfasst: 23.06.2024 11:17
von Lord
Hallo Axolotl!

Danke für Deine Antwort!
Ich werde Deinen zweiten Vorschlag leider erst in zwei Wochen
testen können, wenn ich wieder an meinen Win-Rechner komme.
Ich melde mich wieder.

Re: Text an notepad senden und unter bestehendem Text einfügen ?

Verfasst: 09.07.2024 11:24
von Lord
Besser spät als nie.

Hallo Axolotl!

Ich habe Deinen Code mal bei mir eingebaut.
Soweit ich das bisher sehen kann scheint es zu funktionieren.
Danke für die Hilde

Lord