Community
    • Login

    How to start a search automatically

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    41 Posts 7 Posters 5.9k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • PeterJonesP
      PeterJones @Alan Kilborn
      last edited by

      @Alan-Kilborn said in How to start a search automatically:

      You can populate it after you get a handle to it, using the dialog control IDs in FindReplaceDlg_rc.h.

      I had never noticed that before. And now I know where the 1700 and similar IDs come from, as referenced in the search-and-replace macros docs. Thanks!

      I continue to get the feeling that this is NOT the case.

      And the OP continues to be mum on the subject, neither confirming nor denying, no matter how many times it is brought up. It is an answer that would definitely help craft any future discussion.

      However, at this point, I probably won’t weigh in much more, because you seem to understand the OP’s desires better than I do, and I don’t want to get in the way or muddle things any more.

      kaifuziK 1 Reply Last reply Reply Quote 1
      • kaifuziK
        kaifuzi @PeterJones
        last edited by

        @PeterJones @Alan-Kilborn I did it, thanks a lot for your help!!!
        I use FindWindow and FindWindowEx to get window handles which I need, then I use SendMessage to control them, with NPPM_MENUCOMMAND I can open Find dialog.

        kaifuziK Alan KilbornA 2 Replies Last reply Reply Quote 2
        • kaifuziK
          kaifuzi @kaifuzi
          last edited by

          @Alan-Kilborn @PeterJones In fact, I sitll have a small issue. When I use ShellExexute to open a file by Notepad++, I don’t know when it’s ready, I mean I can get the handle of Notepad++. So I use a loop to wait, once the handle value of Notepad++ is greater than 0, then I think Notepad++ it’s ready, then I can start search. I’m not sure is there any other best way.

          Alan KilbornA 1 Reply Last reply Reply Quote 0
          • Alan KilbornA
            Alan Kilborn @kaifuzi
            last edited by

            @kaifuzi said in How to start a search automatically:

            I did it, thanks a lot for your help!!!

            Well, that’s good.
            It is sort of tradition to provide more details in the form of code about your working solution, for those that read this later wanting to do the same or a similar thing.
            Not mandatory, though.

            kaifuziK 1 Reply Last reply Reply Quote 2
            • kaifuziK
              kaifuzi @Alan Kilborn
              last edited by

              @Alan-Kilborn Yes, that’s good, I will post my code in there. I did it in VB.net, same for C#.

              1 Reply Last reply Reply Quote 1
              • kaifuziK
                kaifuzi
                last edited by

                Public Sub NppFindAllInCurDoc(ByVal fileFullPath As String, ByVal searchString As String)
                    Dim i As Integer = 0
                    Dim hNppWnd As IntPtr = IntPtr.Zero
                    While CInt(hNppWnd) = 0
                        hNppWnd = FindWindow("Notepad++", fileFullPath & " - Notepad++")  'Notepad++ handle
                        i += 1
                        If i > 10 ^ 5 Then
                            MsgBox("Timeout!" & Environment.NewLine & "Please run this command again.", MsgBoxStyle.Exclamation, "Warning")
                            Exit While
                        End If
                    End While
                
                    If CInt(hNppWnd) > 0 Then
                        'Open find dialog
                        SendMessage(hNppWnd, NppMessage.NPPM_MENUCOMMAND, 0, New IntPtr(NppCmdID.IDM_SEARCH_FIND))
                        'Get find dialog
                        Dim hFindWnd As IntPtr = IntPtr.Zero
                        Dim hChildWnd As IntPtr = IntPtr.Zero
                        hFindWnd = FindWindow("#32770", "Find")  'Find dialog handle
                        hChildWnd = FindWindowEx(hFindWnd, IntPtr.Zero, "ComboBox", vbNullString)
                        hChildWnd = FindWindowEx(hChildWnd, IntPtr.Zero, "Edit", vbNullString)
                        SendMessage(hChildWnd, WindowMessage.WM_SETTEXT, 0, searchString)
                        'Start search
                        hChildWnd = FindWindowEx(hFindWnd, IntPtr.Zero, "Button", "Find All in Current &Document")
                        SendMessage(hChildWnd, WindowMessage.BM_CLICK, 0, IntPtr.Zero)
                    End If
                End Sub
                
                1 Reply Last reply Reply Quote 2
                • Alan KilbornA
                  Alan Kilborn @kaifuzi
                  last edited by

                  @kaifuzi said in How to start a search automatically:

                  When I use ShellExexute to open a file by Notepad++, I don’t know when it’s ready, I mean I can get the handle of Notepad++. So I use a loop to wait, once the handle value of Notepad++ is greater than 0, then I think Notepad++ it’s ready, then I can start search.

                  It appears from your code that you found a solution to this?

                  kaifuziK 1 Reply Last reply Reply Quote 0
                  • kaifuziK
                    kaifuzi @Alan Kilborn
                    last edited by

                    @Alan-Kilborn Yes, in my code, I use a loop to wait the Notepad++ handle. But I don’t think this is a good solution:

                    Dim hNppWnd As IntPtr = IntPtr.Zero
                    While CInt(hNppWnd) = 0
                        hNppWnd = FindWindow("Notepad++", fileFullPath & " - Notepad++")  'Notepad++ handle
                        i += 1
                        If i > 10 ^ 5 Then
                            MsgBox("Timeout!" & Environment.NewLine & "Please run this command again.", MsgBoxStyle.Exclamation, "Warning")
                            Exit While
                        End If
                    End While
                    
                    PeterJonesP 1 Reply Last reply Reply Quote 1
                    • PeterJonesP
                      PeterJones @kaifuzi
                      last edited by

                      @kaifuzi ,

                      I use a loop to wait the Notepad++ handle. But I don’t think this is a good solution

                      Waiting for a handle is not bad practice. In the Perl Win32::GuiTest library previously mentioned, two of the commonly-used wrapper functions are WaitWindow and WaitWindowLike, which wrap around the FindWindow interface, like you’ve done. That’s really the best way to make sure the Window exists after you’ve created it.

                      Unfortunately, there isn’t a similar “it exists, but is it ready for me?” call. Sometimes, I’ve found that trying to edit the text or launch menus immediately after the window exists will intermittently fail; in those cases, I add a 100ms or 1s delay (normally, what I’m automating in Notepad++ isn’t time critical; one second difference doesn’t matter).

                      So I think wait-for-hwnd is a good first step; if that’s not sufficient, wait a bit after you have the handle before trying to do something with it.

                      kaifuziK 1 Reply Last reply Reply Quote 2
                      • kaifuziK
                        kaifuzi @PeterJones
                        last edited by

                        @PeterJones Thanks for your suggestion! Yes, for now, waiting the handle it’s enought for me. But I will try WaitWindow funciton.

                        PeterJonesP 1 Reply Last reply Reply Quote 0
                        • PeterJonesP
                          PeterJones @kaifuzi
                          last edited by

                          @kaifuzi said in How to start a search automatically:

                          I will try WaitWindow funciton.

                          I don’t think they exist in the raw win32 api. I think they are just wrappers in that specific Perl library. But if you can find them for your library, great.

                          kaifuziK 1 Reply Last reply Reply Quote 0
                          • kaifuziK
                            kaifuzi @PeterJones
                            last edited by

                            @PeterJones Yes, you are right, there is no function WaitWindow and WaitWindowLike in raw win32 api. Then before I have better solution, I will use loop to wait handle.

                            Alan KilbornA 1 Reply Last reply Reply Quote 0
                            • dinkumoilD
                              dinkumoil @kaifuzi
                              last edited by dinkumoil

                              @kaifuzi

                              The whole approach to remotely control Notepad++ via a VB or C# program seems to take too much effort and is not very reliable. Thus, I would like to recommend using the tool grepWin for searching (download it from >> here <<). It provides subfolder recursion, regular expression search (and replace), folder exclusion by pattern and filtering by file type, size and time stamp.

                              This tool integrates itself into the Windows Explorer files and folders context menu, can also be started via command line/batch script (providing a lot of command line options) and can be configured to start Notepad++ or any other text editor by double-clicking its search results. It is even possible to place the cursor in Notepad++ at a certain search result using the appropriate Npp command line options.

                              Alan KilbornA 2 Replies Last reply Reply Quote 1
                              • Alan KilbornA
                                Alan Kilborn @kaifuzi
                                last edited by

                                @kaifuzi

                                One more note about your code.
                                I notice that you do not set up ALL of the controls on the Find window before running your search.
                                It could be dangerous (i.e. wrong results) in certain circumstances, e.g. Match case checkbox is set one way but logically the user of your code thinks it is set the other way.
                                So you may want to add more code to specify the settings on other parameters that may affect your search.
                                Just an FYI.

                                1 Reply Last reply Reply Quote 1
                                • Alan KilbornA
                                  Alan Kilborn @dinkumoil
                                  last edited by Alan Kilborn

                                  @dinkumoil said in How to start a search automatically:

                                  I would like to recommend using the tool grepWin for searching

                                  A nice thing about this is that grepWin uses the Boost regex engine, just like Notepad++, so if you are very familiar with doing “advanced” searching with regular expressions in Notepad++, you’d feel very familiar with doing it in grepWin.

                                  1 Reply Last reply Reply Quote 1
                                  • Alan KilbornA
                                    Alan Kilborn @dinkumoil
                                    last edited by

                                    @dinkumoil said in How to start a search automatically:

                                    (grepWin) can be configured to start Notepad++ or any other text editor by double-clicking its search results. It is even possible to place the cursor in Notepad++ at a certain search result using the appropriate Npp command line options.

                                    I took a deeper look into this, and I didn’t see how to configure these two things as described. Can you elaborate, @dinkumoil ? Note that the double-clicking might be straightforward using a file association, but I don’t like those and would hope that there is another way.

                                    I do realize that this isn’t a grepWin support forum, but we are talking about “integration” with Notepad++, so I think it is OK.

                                    Alan KilbornA 1 Reply Last reply Reply Quote 0
                                    • Alan KilbornA
                                      Alan Kilborn @Alan Kilborn
                                      last edited by Alan Kilborn

                                      @Alan-Kilborn said in How to start a search automatically:

                                      I took a deeper look into this, and I didn’t see how to configure these two things as described.

                                      @dinkumoil

                                      Never mind; I see it now.
                                      It could have been I was using an older version at first where this wasn’t supported. (Or I just missed it)
                                      On the latest (2.0.4) now and this configuration is available by pressing the Settings button on the grepWin UI.
                                      Nothing about it in the Help stuff that I saw, though.

                                      It can only be configured to jump to the line of the match; having a column jump would make this truly useful.

                                      dinkumoilD 1 Reply Last reply Reply Quote 1
                                      • pbarneyP
                                        pbarney @kaifuzi
                                        last edited by

                                        @kaifuzi Another approach that might provide more flexibility is to use a third-party macro program like AutoHotKey, which is a standard for power users. You can create macros that can automate things in any program you use, including the operating system.

                                        Not sure if links are allowed here, but it should be pretty easy to find. There are a million different examples on the web for how to do something like this, but I think your task would be incredibly easy.

                                        dinkumoilD 1 Reply Last reply Reply Quote 1
                                        • dinkumoilD
                                          dinkumoil @Alan Kilborn
                                          last edited by

                                          @Alan-Kilborn

                                          Nice to hear that you were successful to configure grepWin in a way that it integrates with Notepad++.

                                          Indeed, I was too “enthusiastic” about grepWin in my statement above ;-). Sadly it provides no means to navigate to the column of a search result.

                                          1 Reply Last reply Reply Quote 1
                                          • dinkumoilD
                                            dinkumoil @pbarney
                                            last edited by

                                            @pbarney said in How to start a search automatically:

                                            Not sure if links are allowed here

                                            Yes, they are - as long as the links point to valuable content connected with the topic of the issue. You can even use the Markdown syntax for embedding links into floating text.

                                            This is a [link to Npp's homepage](https://notepad-plus-plus.org/).
                                            

                                            becomes

                                            This is a link to Npp’s homepage.

                                            1 Reply Last reply Reply Quote 3
                                            • Alan KilbornA Alan Kilborn referenced this topic on
                                            • Alan KilbornA Alan Kilborn referenced this topic on
                                            • First post
                                              Last post
                                            The Community of users of the Notepad++ text editor.
                                            Powered by NodeBB | Contributors