How to Display Text in notepad ?

haseebhm

Member
Joined
Jul 13, 2009
Messages
6
I want to display a string (stored in a variable) in notepad(or any other default text editor depending upon the system). Saving to a text file is not necessary. I just want to open Notepad with an unsaved file containing the text. Please help
 
Open notepad with
Code:
System.Diagnostics.Process.Start("notepad.exe")
Then you can get the window handle and send a WM_SETTEXT message to set the text.

also you could write to a (temporary) text file and start notepad and the textfile as second parameter:
Code:
System.Diagnostics.Process.Start("notepad.exe", "mydocument.txt")

~ DP
 
I would prefer the "WM_SETTEXT" method. But its a bit new to me. Can anyone please give me a sample code ?
 
VB.NET

Code:
Private Declare Function SendMessageByString Lib "user32.dll" Alias "SendMessageA" _
			(ByVal hwnd As IntPtr, _
			 ByVal uMsg As Int32, _
			 ByVal wParam As IntPtr, _
			 ByVal lParam As String) As Integer
    
	Private Const WM_SETTEXT As Int32 = &HC
	
	Private Declare Function SendMessageByInt Lib "user32.dll" Alias "SendMessageA" _
			(ByVal hwnd As IntPtr, _
			 ByVal uMsg As Int32, _
			 ByVal wParam As Int32, _
			 ByVal lParam As Int32) As Integer
	
	Private Const WM_GETTEXTLENGTH As Int32 = &HE 
	
	Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
			(ByVal lpClassName As String, _
		 	 ByVal lpWindowName As String) As IntPtr

	Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" _
			(ByVal hWnd1 As IntPtr, _
			ByVal hWnd2 As IntPtr, _
			ByVal lpsz1 As String, _
			ByVal lpsz2 As String) As IntPtr 
	
	Public Function GetHandle(ByVal ClassName As String) As IntPtr
		Dim hwnd As IntPtr = FindWindow(ClassName, Nothing)
		hwnd = FindWindowEx(hwnd, IntPtr.Zero, "Edit", Nothing)
		
		If (Not hwnd.Equals(IntPtr.Zero)) Then
			Return hwnd
		Else
			Return IntPtr.Zero 
		End If
	End Function

	Public Sub SetNewText(ByVal hwnd As IntPtr, ByVal txt As String)
		If (Not hwnd.Equals(IntPtr.Zero)) Then
			Call SendMessageByString(hwnd, WM_SETTEXT, IntPtr.Zero, txt)
		End If
	End Sub

~DP
 
Great! That worked for me but with a small error. First time the notepad window is opened without any text. But when the code is executed the second time the desired text appears in the first notepad window, and a new empty notepad window appears.:confused:
Need your help pleaseeeeeeee

Code:
Dim p As IntPtr
System.Diagnostics.Process.Start("notepad.exe")
p = GetHandle("notepad")
SetNewText(p, log)
 
On the occasions that fail is p a valid handle? It might be a case of trying to get the handle before the window has been created.
 
Last edited by a moderator:
I believe GetHandle will return the a process with "notepad" in the title. If you have more than one open it may not be returning the process you want. Instead of finding the handle use the Process rturned by the start. Try:
Code:
System.Diagnostics.Process p = System.Diagnostics.Process.Start("notepad.exe")
SetNewText(myProcess.Handle(), log)
PlausiblyDamp comments should also be taken into account. There are timing and error states that should be taken into account.
 
I believe GetHandle will return the a process with "notepad" in the title. If you have more than one open it may not be returning the process you want. Instead of finding the handle use the Process rturned by the start. Try:
Code:
System.Diagnostics.Process p = System.Diagnostics.Process.Start("notepad.exe")
SetNewText(myProcess.Handle(), log)
PlausiblyDamp comments should also be taken into account. There are timing and error states that should be taken into account.


Didnt work.:confused: Didnt even show the text after second execution through this method. Please help. I am new with handling windows.
 
You could try getting the handle to notepad with something like
C#:
Process [] notepads=Process.GetProcessesByName("notepad");
IntPtr notepad = FindWindowEx(notepads[0].MainWindowHandle, IntPtr.Zero, "Edit", null);
[code=csharp]
and see if that helps.
 
Back
Top