• Login
Community
  • Login

How to use StyleSetBack properly

Scheduled Pinned Locked Moved Notepad++ & Plugin Development
5 Posts 3 Posters 444 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.
  • M
    Michael Leung
    last edited by Aug 7, 2023, 6:05 AM

    Hi everyone,

    I am new to programming, and I want to mess around with Notepad plugin because I use it alot in my work. I am using C# and want to change the first letter background to blue background. This is my code

      scint = new ScintillaGateway(PluginBase.GetCurrentScintilla());
        int style = 50;
        scint.StyleSetBack(style, new Colour(127, 218, 219));
        scint.StartStyling(0, 0);
        scint.SetStyling(1, style);
    

    It’s not changing any colours though, so I appreciate any insight. Thanks for all your help!

    E 1 Reply Last reply Aug 7, 2023, 6:38 AM Reply Quote 0
    • E
      Ekopalypse @Michael Leung
      last edited by Aug 7, 2023, 6:38 AM

      @Michael-Leung

      This looks ok to me. A similar code in python works.

      ddc1b537-ef5a-4dfe-b66d-e925d9bd3a21-image.png

      but note that it can be overlaid if your current line is the same.

      27167ef5-229d-4248-87c4-310c992388f1-image.png

      M 1 Reply Last reply Aug 7, 2023, 6:54 AM Reply Quote 1
      • M
        Michael Leung @Ekopalypse
        last edited by Aug 7, 2023, 6:54 AM

        @Ekopalypse Thanks for the reply. However, I tested it again but it still doesn’t work on my end. This is the full code

        using Kbg.NppPluginNET.PluginInfrastructure;
        using System;
        using System.Diagnostics;
        using System.Runtime.InteropServices;
        using System.Threading;
        using static System.Windows.Forms.VisualStyles.VisualStyleElement;
        
        namespace Kbg.NppPluginNET
        {
            class Main
            {
                internal const string PluginName = "Test";
        
                public static void OnNotification(ScNotification notification)
                {
                    // This method is invoked whenever something is happening in notepad++
                    // use eg. as
                    // if (notification.Header.Code == (uint)NppMsg.NPPN_xxx)
                    // { ... }
                    // or
                    //
                    // if (notification.Header.Code == (uint)SciMsg.SCNxxx)
                    // { ... }
                }
        
                internal static void CommandMenuInit()
                {
        
                    PluginBase.SetCommand(0, "Start", start);
                }
        
                internal static void SetToolBarIcon()
                {
                    
                }
        
                internal static void PluginCleanUp()
                {
                    
                }
        
                internal static void start()
                {
        
                    int style = 50;
                    var Window = new ScintillaGateway(PluginBase.GetCurrentScintilla());
                    Window.StyleSetBack(style, new Colour(127, 218, 219));
                    Window.StartStyling(0, 0);
                    Window.SetStyling(1, style);
                }
        
            }
        
        }
        

        Is there any settings I have to set?

        R 1 Reply Last reply Aug 7, 2023, 12:51 PM Reply Quote 0
        • R
          rdipardo @Michael Leung
          last edited by rdipardo Aug 7, 2023, 12:56 PM Aug 7, 2023, 12:51 PM

          @Michael-Leung said in How to use StyleSetBack properly:

          I tested it again but it still doesn’t work on my end.

          Like @Ekopalypse tried to explain, the style you are setting is probably hidden by the caret line background (a horizontal bar spanning the current line), which is opaque by default.

          Your method could try moving the caret to the next line (assuming there’s at least two lines in the document), like this:

          + private static void GoToNextLine()
          + {
          +   IntPtr lineEndCurrent, lineStartNext;
          +   var hwnd = PluginBase.GetCurrentScintilla();
          +   var editor = new ScintillaGateway(hwnd);
          +   lineEndCurrent = Win32.SendMessage(hwnd, SciMsg.SCI_GETLINEENDPOSITION, editor.GetCurrentLineNumber(), 0);
          +   lineStartNext = Win32.SendMessage(hwnd, SciMsg.SCI_POSITIONAFTER, lineEndCurrent, 0);
          +   editor.SetEmptySelection((int)lineStartNext);
          + }
          
            internal static void start()
            {
          
              int style = 50;
              var Window = new ScintillaGateway(PluginBase.GetCurrentScintilla());
              Window.StyleSetBack(style, new Colour(127, 218, 219));
              Window.StartStyling(0, 0);
              Window.SetStyling(1, style);
          +   GoToNextLine();
            }
          

          StyleSetBack

          M 1 Reply Last reply Aug 7, 2023, 5:12 PM Reply Quote 3
          • M
            Michael Leung @rdipardo
            last edited by Aug 7, 2023, 5:12 PM

            @rdipardo Oh wow that worked great!

            Really appreciate the time you guys took to help me, thanks!

            1 Reply Last reply Reply Quote 0
            1 out of 5
            • First post
              1/5
              Last post
            The Community of users of the Notepad++ text editor.
            Powered by NodeBB | Contributors