How to use StyleSetBack properly
-
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!
-
This looks ok to me. A similar code in python works.
but note that it can be overlaid if your current line is the same.
-
@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?
-
@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(); }
-
@rdipardo Oh wow that worked great!
Really appreciate the time you guys took to help me, thanks!