working on it ...

Filters

snippets
37
followers
7
Published by qxshower

Useful C# stuff by qxshower

the projects include ○ Sqlcetoolbox ○ Codemaid ○ SassyStudio ○ VSSpellCHecker ○ GitHubExtension ○ MultiEdit
Sort by

Found 37 snippets

    public by qxshower  1730  2  7  1

    GetEmbeddedStringResource

    Gets the embedded file identified by the resource name, and converts the file into a string.
    using EnvDTE;
    using EnvDTE80;
    using Microsoft.VisualStudio;
    using Microsoft.VisualStudio.Shell.Interop;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Microsoft.VSSDK.Tools.VsIdeTesting;
    using SteveCadwallader.CodeMaid.Helpers;
    using System;
    using System.ComponentModel.Design;
    using System.Diagnostics;
    using System.IO;
    using System.Reflection;
    using System.Text;
    
    #region Methods: Handling embedded resources
    
    /// <summary>
    /// Gets the embedded file identified by the resource name, and converts the file into a string.
    /// </summary>
    /// <param name="assembly"></param>
    /// <param name="resourceName">In VS, is DefaultNamespace.FileName?</param>
    /// <returns></returns>
    public static string GetEmbeddedStringResource(Assembly assembly, string resourceName)
    {
        string result = null;
    
        // Use the .NET procedure for loading a file embedded in the assembly
        Stream stream = assembly.GetManifestResourceStream(resourceName);
        if (stream != null)
        {
            // Convert bytes to string
            byte[] fileContentsAsBytes = new byte[stream.Length];
            stream.Read(fileContentsAsBytes, 0, (int)stream.Length);
            result = Encoding.Default.GetString(fileContentsAsBytes);
        }
        else
        {
            // Embedded resource not found - list available resources
            Debug.WriteLine("Unable to find the embedded resource file '" + resourceName + "'.");
            Debug.WriteLine("  Available resources:");
            foreach (string aResourceName in assembly.GetManifestResourceNames())
            {
                Debug.WriteLine("    " + aResourceName);
            }
        }
    
        return result;
    }                                    

    public by qxshower  1404  0  6  0

    HandleDragScrolling

    Handles scrolling the specified scroll viewer if the drag event indicates the drag operation is nearing the scroll viewers top or bottom boundaries.
    using SteveCadwallader.CodeMaid.Helpers;
    using SteveCadwallader.CodeMaid.Integration;
    using SteveCadwallader.CodeMaid.Logic.Reorganizing;
    using SteveCadwallader.CodeMaid.Model.CodeItems;
    using SteveCadwallader.CodeMaid.Properties;
    using SteveCadwallader.CodeMaid.UI.Enumerations;
    using System;
    using System.ComponentModel.Design;
    using System.Linq;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    
    /// <summary>
    /// Handles scrolling the specified scroll viewer if the drag event indicates the drag
    /// operation is nearing the scroll viewers top or bottom boundaries.
    /// </summary>
    /// <param name="scrollViewer">The scroll viewer.</param>
    /// <param name="e">
    /// The <see cref="System.Windows.DragEventArgs" /> instance containing the event data.
    /// </param>
    private static void HandleDragScrolling(ScrollViewer scrollViewer, DragEventArgs e)
    {
        const int threshold = 20;
        const int offset = 10;
    
        var mousePoint = e.GetPosition(scrollViewer);
    
        if (mousePoint.Y < threshold)
        {
            scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset - offset);
        }
        else if (mousePoint.Y > (scrollViewer.ActualHeight - threshold))
        {
            scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + offset);
        }
    }            

    public by qxshower  1517  1  6  0

    FindMatches

    Finds all matches of the specified pattern within the specified text document.
    using EnvDTE;
    using SteveCadwallader.CodeMaid.Model.CodeItems;
    using System;
    using System.Collections.Generic;
    using System.Text.RegularExpressions;
    
    /// <summary>
    /// The common set of options to be used for find and replace patterns.
    /// </summary>
    internal const int StandardFindOptions = (int)(vsFindOptions.vsFindOptionsRegularExpression |
                                                           vsFindOptions.vsFindOptionsMatchInHiddenText);
    
    #endregion Internal Constants
    
    #region Internal Methods
    
    /// <summary>
    /// Finds all matches of the specified pattern within the specified text document.
    /// </summary>
    /// <param name="textDocument">The text document.</param>
    /// <param name="patternString">The pattern string.</param>
    /// <returns>The set of matches.</returns>
    internal static IEnumerable<EditPoint> FindMatches(TextDocument textDocument, string patternString)
    {
        var matches = new List<EditPoint>();
        var cursor = textDocument.StartPoint.CreateEditPoint();
        EditPoint end = null;
        TextRanges dummy = null;
    
        while (cursor != null && cursor.FindPattern(patternString, StandardFindOptions, ref end, ref dummy))
        {
            matches.Add(cursor.CreateEditPoint());
            cursor = end;
        }
    
        return matches;
    }            

    public by qxshower  135624  2  5  0

    MoveToCodeItem

    Attempts to move the cursor to the specified code item.
    using EnvDTE;
    using SteveCadwallader.CodeMaid.Model.CodeItems;
    using System;
    using System.Collections.Generic;
    using System.Text.RegularExpressions;
    
    /// <summary>
    /// Attempts to move the cursor to the specified code item.
    /// </summary>
    /// <param name="document">The document.</param>
    /// <param name="codeItem">The code item.</param>
    /// <param name="centerOnWhole">True if the whole element should be used for centering.</param>
    internal static void MoveToCodeItem(Document document, BaseCodeItem codeItem, bool centerOnWhole)
    {
        var textDocument = document.GetTextDocument();
        if (textDocument == null) return;
    
        try
        {
            object viewRangeEnd = null;
            TextPoint navigatePoint = null;
    
            codeItem.RefreshCachedPositionAndName();
            textDocument.Selection.MoveToPoint(codeItem.StartPoint, false);
    
            if (centerOnWhole)
            {
                viewRangeEnd = codeItem.EndPoint;
            }
    
            var codeItemElement = codeItem as BaseCodeItemElement;
            if (codeItemElement != null)
            {
                navigatePoint = codeItemElement.CodeElement.GetStartPoint(vsCMPart.vsCMPartNavigate);
            }
    
            textDocument.Selection.AnchorPoint.TryToShow(vsPaneShowHow.vsPaneShowCentered, viewRangeEnd);
    
            if (navigatePoint != null)
            {
                textDocument.Selection.MoveToPoint(navigatePoint, false);
            }
            else
            {
                textDocument.Selection.FindText(codeItem.Name, (int)vsFindOptions.vsFindOptionsMatchInHiddenText);
                textDocument.Selection.MoveToPoint(textDocument.Selection.AnchorPoint, false);
            }
        }
        catch (Exception)
        {
            // Move operation may fail if element is no longer available.
        }
        finally
        {
            // Always set focus within the code editor window.
            document.Activate();
        }
    }            

    public by qxshower  1349  0  5  0

    GetEditPointAtCursor

    Gets an edit point at the cursor for the specified text document.
    using EnvDTE;
    
    /// <summary>
    /// Gets an edit point at the cursor for the specified text document.
    /// </summary>
    /// <param name="textDocument">The text document.</param>
    /// <returns>An edit point at the cursor.</returns>
    internal static EditPoint GetEditPointAtCursor(this TextDocument textDocument)
    {
        var cursor = textDocument.CreateEditPoint();
        cursor.MoveToPoint(textDocument.Selection.ActivePoint);
    
        return cursor;
    }                        

    public by qxshower  1442  0  4  0

    LineMatchesRegex

    using EnvDTE;
    using SteveCadwallader.CodeMaid.Model.Comments;
    using System.Diagnostics;
    using System.Text.RegularExpressions;
    
    internal static Match LineMatchesRegex(EditPoint point, Regex regex)
    {
        var line = point.GetLine();
        var match = regex.Match(line);
        return match;
    }            

    public by qxshower  1410  0  5  0

    GetColorFromUInt

    A simple converter for turning a "uint" into a "Color".
    using EnvDTE80;
    using SteveCadwallader.CodeMaid.Helpers;
    using SteveCadwallader.CodeMaid.Properties;
    using SteveCadwallader.CodeMaid.UI.Enumerations;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows;
    using System.Windows.Media;
    
    /// <summary>
    /// A simple converter for turning a <see cref="uint" /> into a <see cref="Color" />.
    /// </summary>
    /// <param name="number">The number to convert.</param>
    /// <returns>The color.</returns>
    private static Color GetColorFromUInt(uint number)
    {
        return Color.FromRgb((byte)(number >> 16),
                             (byte)(number >> 8),
                             (byte)(number >> 0));
    }                        

    public by qxshower  1470  1  5  0

    FindMessageBoxString

    Finds a messagebox string on a messagebox.
    using Microsoft.VisualStudio.Shell;
    using Microsoft.VisualStudio.Shell.Interop;
    using System;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading;
    
    /// <summary>
    /// Finds a messagebox string on a messagebox.
    /// </summary>
    /// <param name="hwnd">The windows handle of the dialog</param>
    /// <param name="unmanagedMemoryLocation">
    /// A pointer to the memorylocation the string will be written to
    /// </param>
    /// <returns>True if found.</returns>
    private static bool FindMessageBoxString(IntPtr hwnd, IntPtr unmanagedMemoryLocation)
    {
        StringBuilder sb = new StringBuilder(512);
        NativeMethods.GetClassName(hwnd, sb, sb.Capacity);
    
        if (sb.ToString().ToLower().Contains("static"))
        {
            StringBuilder windowText = new StringBuilder(2048);
            NativeMethods.GetWindowText(hwnd, windowText, windowText.Capacity);
    
            if (windowText.Length > 0)
            {
                IntPtr stringAsPtr = IntPtr.Zero;
                try
                {
                    stringAsPtr = Marshal.StringToHGlobalAnsi(windowText.ToString());
                    char[] stringAsArray = windowText.ToString().ToCharArray();
    
                    // Since unicode characters are copied check if we are out of the allocated
                    // length. If not add the end terminating zero.
                    if ((2 * stringAsArray.Length) + 1 < 2048)
                    {
                        Marshal.Copy(stringAsArray, 0, unmanagedMemoryLocation, stringAsArray.Length);
                        Marshal.WriteInt32(unmanagedMemoryLocation, 2 * stringAsArray.Length, 0);
                    }
                }
                finally
                {
                    if (stringAsPtr != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(stringAsPtr);
                    }
                }
                return false;
            }
        }
    
        return true;
    }            

    public by qxshower  1358  1  5  0

    CreateXmlOpenTag

    Creates the XML open tag string for an XElement.
    using EnvDTE;
    using SteveCadwallader.CodeMaid.Model.Comments;
    using System.Diagnostics;
    using System.Text.RegularExpressions;
    
    public const char Spacer = ' ';
    
    /// <summary>
    /// Creates the XML open tag string for an XElement.
    /// </summary>
    /// <param name="element">The element.</param>
    /// <param name="options">The comment options used to contruct the tag.</param>
    /// <returns>The XML open tag. In case of an element without value, the tag is self-closing.</returns>
    internal static string CreateXmlOpenTag(System.Xml.Linq.XElement element, CodeCommentOptions options)
    {
        var builder = new System.Text.StringBuilder();
        builder.Append("<");
        var name = element.Name.LocalName;
        builder.Append(options.XmlTagsToLowerCase ? name.ToLowerInvariant() : name);
    
        if (element.HasAttributes)
        {
            foreach (var attr in element.Attributes())
            {
                builder.Append(CodeCommentHelper.Spacer);
                builder.Append(attr.ToString());
            }
        }
    
        if (element.IsEmpty)
        {
            if (options.XmlSpaceSingleTags)
            {
                builder.Append(CodeCommentHelper.Spacer);
            }
    
            builder.Append("/");
        }
    
        builder.Append(">");
    
        var result = builder.ToString();
    
        return options.XmlKeepTagsTogether ? SpaceToFake(result) : result;
    }                        

    public by qxshower  87443  1  5  0

    CreateXmlCloseTag

    Creates the XML close tag string for an XElement.
    using EnvDTE;
    using SteveCadwallader.CodeMaid.Model.Comments;
    using System.Diagnostics;
    using System.Text.RegularExpressions;
    
    /// <summary>
    /// Creates the XML close tag string for an XElement.
    /// </summary>
    /// <param name="element">The element.</param>
    /// <param name="options">The comment options used to contruct the tag.</param>
    /// <returns>
    /// The XML close tag, or <c>null</c> if the element has no value and is a self-closing tag.
    /// </returns>
    internal static string CreateXmlCloseTag(System.Xml.Linq.XElement element, CodeCommentOptions options)
    {
        if (element.IsEmpty)
        {
            return null;
        }
    
        var name = element.Name.LocalName;
    
        var result = string.Format("</{0}>", options.XmlTagsToLowerCase ? name.ToLowerInvariant() : name);
    
        return options.XmlKeepTagsTogether ? SpaceToFake(result) : result;
    }                        
    • Public Snippets
    • Channels Snippets