haseebhm Posted July 13, 2009 Posted July 13, 2009 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 Quote
DPrometheus Posted July 14, 2009 Posted July 14, 2009 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 Quote 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
haseebhm Posted July 14, 2009 Author Posted July 14, 2009 I would prefer the "WM_SETTEXT" method. But its a bit new to me. Can anyone please give me a sample code ? Quote
DPrometheus Posted July 17, 2009 Posted July 17, 2009 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 Quote 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
haseebhm Posted July 17, 2009 Author Posted July 17, 2009 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) Quote
Administrators PlausiblyDamp Posted July 17, 2009 Administrators Posted July 17, 2009 (edited) 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. Edited July 17, 2009 by PlausiblyDamp Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
forgottensoul Posted July 17, 2009 Posted July 17, 2009 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. Quote
haseebhm Posted July 20, 2009 Author Posted July 20, 2009 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. Quote
Administrators PlausiblyDamp Posted July 20, 2009 Administrators Posted July 20, 2009 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts