Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
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
Posted

Open notepad with


System.Diagnostics.Process.Start("notepad.exe")
[/Code]

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")
[/Code]

 

~ DP

My Development System

Intel Core i7 920 @2.66Ghz

6 GB DDR3 SDRAM

Windows 7 Ultimate x64 & Windows Vista Home Premium x64 dual boot

GeForce GTX295 1.8 GB

3.5 TB HD

Posted

VB.NET

 

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

My Development System

Intel Core i7 920 @2.66Ghz

6 GB DDR3 SDRAM

Windows 7 Ultimate x64 & Windows Vista Home Premium x64 dual boot

GeForce GTX295 1.8 GB

3.5 TB HD

Posted

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

 

Dim p As IntPtr
System.Diagnostics.Process.Start("notepad.exe")
p = GetHandle("notepad")
SetNewText(p, log)

Posted

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:

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.

Posted
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:

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.

 

 

Didn't work.:confused: Didn't even show the text after second execution through this method. Please help. I am new with handling windows.

  • Administrators
Posted

You could try getting the handle to notepad with something like

Process [] notepads=Process.GetProcessesByName("notepad");

IntPtr notepad = FindWindowEx(notepads[0].MainWindowHandle, IntPtr.Zero, "Edit", null);

[code=csharp]

and see if that helps.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...