[C#] Find Text In File
-
Is there a way to use the find window (Ctrl+F) with a specified string? I know you can use the Find in Files dialog with NPPM_LAUNCHFINDINFILESDLG but can you use the regular find? I tried sending the shortcut but couldn’t get it to work and also tried using InputSimulator but that needs the WindowsInput.dll file to be loaded to notepad++ to work (would appreciate help with that as well, hope this is enough of a notepad++ question for this forum)
-
@sover-david said in [C#] Find Text In File:
hope this is enough of a notepad++ question for this forum
It is. :-)
Is there a way to use the find window (Ctrl+F)
There is not a “built in” (read: easy) way to manipulate the Find window via a plugin. However, with a bit of work, you could manipulate the windows controls on that window via code. It’s all straight windows api code, though – gets a bit tedious.
-
@sover-david said in [C#] Find Text In File:
also tried using InputSimulator but that needs the WindowsInput.dll file to be loaded to notepad++ to work (would appreciate help with that as well, hope this is enough of a notepad++ question for this forum)
Maybe I spoke too soon earlier about it being enough of a N++ question for this forum. I thought you mainly meant the first part of your question.
But if you mean specifically the “InputSimulator” and “WindowsInput.dll” stuff then I’d say no, it isn’t, and there are much better places to ask questions about that stuff. But of course because I don’t care about that stuff, I can’t point you to better places.
Discussing keystroke senders (e.g. AutoHotKey) is fair game here, when illustrating a technique you’ve done, say, but asking for generic help with such a thing is not. I hope you understand the difference, and know what is appropriate and isn’t.
The reason for limiting discussion here is:
-
we’re all here to read about Notepad++ (or we’d be somewhere else); non-N++ discussion wastes participants time
-
the people here are N++ “experts” and aren’t experts on other things (well, of course we are, but everyone’s an expert in different areas), and you aren’t likely to get responses on non-N++ generic questions anyway
-
-
@alan-kilborn said in [C#] Find Text In File:
It’s all straight windows api code
I did some digging and perhaps THIS THREAD gives some idea of what’s involved.
For sure read the whole thing, but THIS POSTING seems to be where the meat of the coding starts. -
Alright, I managed to get SendKeys.SendWait to work by hiding a dialog that was currently in focus, but that still didn’t work for Ctrl+F so I made it set the text I wanted to find to the clipboard and press Alt+S+Enter Ctrl+V Enter Escape.
Here’s the code:
Clipboard.SetText("foo"); HideDialog(); SendKeys.SendWait("%(S{ENTER})^(V){ENTER}{ESCAPE}");
-
@sover-david said in [C#] Find Text In File:
I managed to get SendKeys.SendWait to work by hiding a dialog that was currently in focus, but that still didn’t work for Ctrl+F so I made it set the text I wanted to find to the clipboard and press Alt+S+Enter Ctrl+V Enter Escape.
Sounds a bit convoluted, but hey, whatever works! :-)
-
@sover-david said in [C#] Find Text In File:
I know you can use the Find in Files dialog with NPPM_LAUNCHFINDINFILESDLG but can you use the regular find?
NPPM_MENUCOMMAND will run any builtin menu with lParam providing the command ID: in this case, IDM_SEARCH_FIND to run Search > Find to launch the FIND dialog.
// open the FIND dialog SendMessage( npp_hWnd, NPPM_MENUCOMMAND, 0, IDM_SEARCH_FIND);
Once inside the dialog, you should be able to search controls. If I understand things correctly, FindReplaceDlg_rc.h defines the control ID’s for each of the entries and buttons: in this case, I think you want IDFINDWHAT for the “Find What” text box.
I am starting to get out of my element, because for Win32 stuff, I am usually using a wrapper libary in Perl rather than truly programming in Win32 API code, so it sometimes provides wrapper functions that don’t map directly to Win32 API calls. But I think you would want to get the hWnd for the FIND dialog and maybe the edit control, then use WM_SETTEXT to the IDFINDWHAT entry:
find_dlg_hWnd = GetActiveWindow(); // or GetForegroundWindow(); // https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getactivewindow find_box_hWnd = GetDlgItem( find_dlg_hWnd , IDFINDWHAT ); // https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getdlgitem SendMessage( find_box_hWnd, WM_SETTEXT, 0, "search text");
or maybe instead of getting the extra hwnd, just
SetDlgItemText( find_dlg_hWnd, IDFINDWHAT, "search text"); // https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setdlgitemtexta
Then somehow, you would have to press the “FIND NEXT” button (but I wasn’t able to find the raw API equivalent of Perl’s Win32::GuiTest::PushChildButton or PushChildById, so I cannot link you to the right API command for pusing the button, sorry).
But if the SendText/SendWait is working for you, that’s probably sufficient.
For other reference, the NPP User Manual’s Searching Actions When Recorded As Macros seem to be using the control IDs as the parameters of the macro commands, so that might give you insight into the control IDs needed for other actions in the FIND family of dialogs … though I’m not sure they always line up with teh FindReplaceDlg_rc.h constants, so I’m not sure.