Пример #1
0
        private void Init(RulerInfo rulerInfo)
        {
            // Set fields
            this.toolTip = new ToolTip
            {
                AutoPopDelay = 10000,
                InitialDelay = 1
            };

            this.resizeRegion      = ResizeRegion.None;
            this.resizeBorderWidth = 5;

            // Form setup ------------------
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.UpdateStyles();

            ResourceManager resources = new ResourceManager(typeof(MainForm));

            this.Icon            = (Icon)resources.GetObject("$this.Icon");
            this.Opacity         = rulerInfo.Opacity;
            this.FormBorderStyle = FormBorderStyle.None;
            this.Font            = new Font("Tahoma", 10);
            this.Text            = "Ruler";
            this.BackColor       = Color.White;

            // Create menu
            this.CreateMenuItems(rulerInfo);

            RulerInfo.CopyInto(rulerInfo, this);
            this.doLockRulerResizeOnMove = false;

            this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
        }
Пример #2
0
        public static RulerInfo GetDefaultRulerInfo()
        {
            RulerInfo ret = new RulerInfo();

            Func <string, bool> isFlag =
                flagName =>
                (Properties.Settings.Default.Properties[flagName]?.DefaultValue.ToString() == "True");

            ret.IsVertical    = isFlag("IsVertical");
            ret.IsLocked      = isFlag("IsLocked");
            ret.ShowToolTip   = isFlag("ShowToolTip");
            ret.ShowUpTicks   = isFlag("ShowUpTicks");
            ret.ShowDownTicks = isFlag("ShowDownTicks");
            ret.TopMost       = isFlag("TopMost");
            ret.IsFlipped     = isFlag("IsFlipped");

            ret.Length    = int.Parse(Properties.Settings.Default.Properties["Length"]?.DefaultValue.ToString() ?? string.Empty);
            ret.Thickness = int.Parse(Properties.Settings.Default.Properties["Thickness"]?.DefaultValue.ToString() ?? string.Empty);
            ret.Opacity   = double.Parse(Properties.Settings.Default.Properties["Opacity"]?.DefaultValue.ToString() ?? string.Empty);

            string defaultColor = Properties.Settings.Default.Properties["BackColor"]?.DefaultValue.ToString() ?? string.Empty;

            ret.BackColor = Colors.ContainsKey(defaultColor) ? Colors[defaultColor] : ColorTranslator.FromHtml(defaultColor);

            return(ret);
        }
Пример #3
0
        public RulerForm(RulerInfo rulerInfo)
        {
            this.Init(rulerInfo);
            RulerApplicationContext context = RulerApplicationContext.CurrentContext;

            context.RegisterRuler(this);
        }
Пример #4
0
        private void Init(RulerInfo rulerInfo)
        {
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.UpdateStyles();

            ResourceManager resources = new ResourceManager(typeof(MainForm));

            this.Icon = ((Icon)(resources.GetObject("$this.Icon")));

            this.SetUpMenu();

            this.Text      = "Ruler";
            this.BackColor = Color.White;

            rulerInfo.CopyInto(this);

            this.FormBorderStyle = FormBorderStyle.None;

            this.ContextMenu = _menu;
            this.Font        = new Font("Tahoma", 10);

            this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);

            MouseDoubleClick += (sender, args) =>
            { IsVertical = !IsVertical; };

            _toolTipMenuItem.Checked   = true;
            this._toolTip.AutoPopDelay = 9999;
        }
Пример #5
0
        private RulerInfo GetRulerInfo()
        {
            RulerInfo rulerInfo = new RulerInfo();

            RulerInfo.CopyInto(this, rulerInfo);

            return(rulerInfo);
        }
Пример #6
0
        public MainForm()
        {
            this.InitLocalVars();

            RulerInfo rulerInfo = RulerInfo.GetDefaultRulerInfo();

            this.Init(rulerInfo);
        }
Пример #7
0
        static void Main(string[] args)
        {
            RulerInfo info;

            if (args.Length > 0)
            {
                if (args.Contains("--help"))
                {
                    MessageBox.Show(GetHelpText(), "Ruler", MessageBoxButtons.OK, MessageBoxIcon.None);
                    return;
                }

                string error;
                if (!ParseArguments(args, out info, out error))
                {
                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine("Error parsing arguments:");
                    sb.AppendLine("    " + string.Join(" ", args));
                    sb.AppendLine();
                    sb.AppendLine(error);
                    MessageBox.Show(sb.ToString(), "Ruler", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            else
            {
                info = new RulerInfo();
            }

            string guid  = "bafc08a9-6060-4811-b3c7-76be74bd4f25";
            Mutex  mutex = new Mutex(true, guid);

            if (mutex.WaitOne(TimeSpan.Zero, true))
            {
                try
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);

                    Application.Run(new RulerApplicationContext(info, guid));
                }
                finally
                {
                    mutex.Close();
                }
            }
            else
            {
                var pipeFactory =
                    new ChannelFactory <ISingleInstanceService>(
                        new NetNamedPipeBinding(),
                        new EndpointAddress("net.pipe://localhost/" + guid));

                ISingleInstanceService service = pipeFactory.CreateChannel();
                service.StartNewRuler(info);
            }
        }
Пример #8
0
        public static string GetHelpText()
        {
            RulerInfo defaultValues    = RulerInfo.GetDefaultRulerInfo();
            string    defaultLength    = defaultValues.Length.ToString();
            string    defaultThickness = defaultValues.Thickness.ToString();

            string defaultColor   = RulerInfo.GetNameFromColor(defaultValues.BackColor);
            string defaultOpacity = defaultValues.Opacity.ToString();

            Func <bool, string> flagSign = b => b ? "+" : "-";
            string defaultDownTicks      = flagSign(defaultValues.ShowDownTicks) + "d";
            string defaultFlipped        = flagSign(defaultValues.IsFlipped) + "f";
            string defaultLock           = flagSign(defaultValues.IsLocked) + "l";
            string defaultTopMost        = flagSign(defaultValues.TopMost) + "m";
            string defaultToolTop        = flagSign(defaultValues.ShowToolTip) + "t";
            string defaultUpTicks        = flagSign(defaultValues.ShowUpTicks) + "u";
            string defaultVertical       = flagSign(defaultValues.IsVertical) + "v";

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("Usage: ruler");
            sb.AppendLine("    or:    ruler [OPTIONS | FLAGS] LENGTH [THICKNESS]");
            sb.AppendLine();
            sb.AppendLine("Display a ruler on screen");
            sb.AppendLine();

            sb.AppendLine("LENGTH\t\tThe length of the ruler, in pixels (default: " + defaultLength + ")");
            sb.AppendLine("THICKNESS\tThe thickness of the ruler, in pixels (default: " + defaultThickness + ")");
            sb.AppendLine();
            sb.AppendLine("OPTIONS");
            sb.AppendLine("========");

            // Colors fanciness
            sb.AppendLine("--color=COLOR\tThe color of the ruler. One of:");
            sb.Append("\t\t     ");
            string[] colorNames = RulerInfo.Colors.Keys.OrderBy(c => c).ToArray();
            sb.Append(String.Join(", ", colorNames.Take(colorNames.Length - 1)));
            sb.Append(" or ");
            sb.Append(colorNames[colorNames.Length - 1]);
            sb.AppendLine(" (default: " + defaultColor + ")");

            sb.AppendLine("--opacity=OPACITY\tThe opacity of the ruler in a [0.1, 1.0] range (default: " + defaultOpacity + ")");
            sb.AppendLine("--usedefaults\tUse default values for all unspecified values");

            sb.AppendLine();
            sb.AppendLine("FLAGS");
            sb.AppendLine("========");
            sb.AppendLine("[+|-]d\t\tShow / hide down ticks (default: " + defaultDownTicks + ")");
            sb.AppendLine("[+|-]f\t\tFlipped / unflipped the ruler (default: " + defaultFlipped + ")");
            sb.AppendLine("[+|-]l\t\tLock / unlock the ruler (default: " + defaultLock + ")");
            sb.AppendLine("[+|-]m\t\tMake / do not make top most (default: " + defaultTopMost + ")");
            sb.AppendLine("[+|-]t\t\tShow / hide the tooltip (default: " + defaultToolTop + ")");
            sb.AppendLine("[+|-]u\t\tShow / hide up ticks (default: " + defaultUpTicks + ")");
            sb.AppendLine("[+|-]v\t\tShow the ruler vertically / horizontally (default: " + defaultVertical + ")");
            return(sb.ToString());
        }
Пример #9
0
        private void DuplicateHandler(object sender, EventArgs e)
        {
            string exe = System.Reflection.Assembly.GetExecutingAssembly().Location;

            RulerInfo rulerInfo = this.GetRulerInfo();

            ProcessStartInfo startInfo = new ProcessStartInfo(exe, rulerInfo.ConvertToParameters());

            Process process = new Process();

            process.StartInfo = startInfo;
            process.Start();
        }
Пример #10
0
        public static RulerInfo GetDefaultRulerInfo()
        {
            RulerInfo rulerInfo = new RulerInfo();

            rulerInfo.Width       = 400;
            rulerInfo.Height      = 75;
            rulerInfo.Opacity     = 0.60;
            rulerInfo.ShowToolTip = false;
            rulerInfo.IsLocked    = false;
            rulerInfo.IsVertical  = false;
            rulerInfo.TopMost     = false;

            return(rulerInfo);
        }
Пример #11
0
        public static RulerInfo GetDefaultRulerInfo()
        {
            RulerInfo rulerInfo = new RulerInfo
            {
                Width       = 400,
                Height      = 75,
                Opacity     = 0.60,
                ShowToolTip = false,
                IsLocked    = false,
                IsVertical  = false,
                TopMost     = true
            };

            return(rulerInfo);
        }
Пример #12
0
 public void StartNewRuler(RulerInfo info)
 {
     lock (RulerApplicationContext.CurrentContext)
     {
         // Get the main form so we can start the new ruler on the main (UI) thread
         if (RulerApplicationContext.CurrentContext.MainForm != null)
         {
             RulerApplicationContext.CurrentContext.MainForm.Invoke((MethodInvoker) delegate
             {
                 var newRuler = new RulerForm(info);
                 newRuler.Show();
             });
         }
     }
 }
Пример #13
0
        public static void SaveInfo(this IRulerInfo ruler)
        {
            Properties.Settings.Default.IsVertical    = ruler.IsVertical;
            Properties.Settings.Default.Length        = ruler.Length;
            Properties.Settings.Default.Thickness     = ruler.Thickness;
            Properties.Settings.Default.Opacity       = ruler.Opacity;
            Properties.Settings.Default.ShowToolTip   = ruler.ShowToolTip;
            Properties.Settings.Default.IsLocked      = ruler.IsLocked;
            Properties.Settings.Default.TopMost       = ruler.TopMost;
            Properties.Settings.Default.BackColor     = RulerInfo.GetNameFromColor(ruler.BackColor) == "CUSTOM"?RulerInfo.GetHexFromColor(ruler.BackColor):RulerInfo.GetNameFromColor(ruler.BackColor);
            Properties.Settings.Default.ShowUpTicks   = ruler.ShowUpTicks;
            Properties.Settings.Default.ShowDownTicks = ruler.ShowDownTicks;
            Properties.Settings.Default.IsFlipped     = ruler.IsFlipped;

            Properties.Settings.Default.Save();
        }
Пример #14
0
 public static Config Load()
 {
     var config = new Config();
     var lines = File.Exists(ConfigPath) ? File.ReadAllLines(ConfigPath) : new string[0];
     if (lines.Length == 2)
     {
         config.HorizontalRulerInfo = RulerInfo.CovertToRulerInfo(lines[0].Split(' '));
         config.VerticalRulerInfo = RulerInfo.CovertToRulerInfo(lines[1].Split(' '));
     }
     else
     {
         config.HorizontalRulerInfo = RulerInfo.GetDefaultRulerInfo();
         config.VerticalRulerInfo = RulerInfo.GetDefaultRulerInfo(true);
     }
     return config;
 }
Пример #15
0
        public MainForm()
            : this(RulerInfo.GetDefaultRulerInfo())
        {
            using (var desktopHwnd = Graphics.FromHwnd(IntPtr.Zero))
            {
                var hdc = desktopHwnd.GetHdc();

                try
                {
                    _dpiX = GetDeviceCaps(hdc, (int)DeviceCap.LOGPIXELSX);
                    _dpiY = GetDeviceCaps(hdc, (int)DeviceCap.LOGPIXELSY);
                }
                finally
                {
                    desktopHwnd.ReleaseHdc(hdc);
                }
            }
        }
Пример #16
0
        private static void Main(params string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            MainForm mainForm;

            if (args.Length == 0)
            {
                mainForm = new MainForm();
            }
            else
            {
                mainForm = new MainForm(RulerInfo.CovertToRulerInfo(args));
            }

            Application.Run(mainForm);
        }
Пример #17
0
        private void CreateMenuItems(RulerInfo rulerInfo)
        {
            this.ContextMenu = new ContextMenu();

            var list = new List <MenuItemHolder>()
            {
                new MenuItemHolder(MenuItemEnum.TopMost, "Stay On Top", this.TopMostHandler, rulerInfo.TopMost),
                new MenuItemHolder(MenuItemEnum.Vertical, "Vertical", this.VerticalHandler, rulerInfo.IsVertical),
                new MenuItemHolder(MenuItemEnum.ShowToolTip, "Tool Tip", this.ShowToolTipHandler, rulerInfo.ShowToolTip),
                new MenuItemHolder(MenuItemEnum.Opacity, "Opacity", null, false),
                new MenuItemHolder(MenuItemEnum.LockResize, "Lock Resizing", this.LockResizeHandler, rulerInfo.IsLocked),
                new MenuItemHolder(MenuItemEnum.SetSize, "Set size...", this.SetSizeHandler, false),
                new MenuItemHolder(MenuItemEnum.Duplicate, "Duplicate", this.DuplicateHandler, false),
                MenuItemHolder.Separator,
                new MenuItemHolder(MenuItemEnum.Reset, "Reset To Default", this.ResetToDefaulHandler, false),
                MenuItemHolder.Separator,
                new MenuItemHolder(MenuItemEnum.About, "About...", this.AboutHandler, false),
                MenuItemHolder.Separator,
#if DEBUG
                new MenuItemHolder(MenuItemEnum.RulerInfo, "Copy RulerInfo", this.CopyRulerInfo, false),
                MenuItemHolder.Separator,
#endif
                new MenuItemHolder(MenuItemEnum.Exit, "Exit", this.ExitHandler, false)
            };

            // Build opacity menu
            MenuItem opacityMenuItem = list.Find(m => m.MenuItemEnum == MenuItemEnum.Opacity).MenuItem;

            for (int i = 10; i <= 100; i += 10)
            {
                MenuItem subMenu = new MenuItem(i + "%", this.OpacityMenuHandler)
                {
                    Checked = i == rulerInfo.Opacity * 100
                };
                opacityMenuItem.MenuItems.Add(subMenu);
            }

            // Build main context menu
            list.ForEach(mh => this.ContextMenu.MenuItems.Add(mh.MenuItem));

            this.menuItemList = list;
        }
Пример #18
0
        private void Init(RulerInfo rulerInfo)
        {
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.UpdateStyles();

            this.Icon = GetIcon("$this.Icon");

            this.SetUpMenu();

            this.Text      = "Ruler";
            this.BackColor = Color.LightYellow;

            rulerInfo.CopyInto(this);

            this.FormBorderStyle = FormBorderStyle.None;

            this.ContextMenu = _menu;
            this.Font        = new Font("Segoe UI", 9, FontStyle.Bold);

            this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
        }
Пример #19
0
        public static RulerInfo CovertToRulerInfo(string[] args)
        {
            string width       = args[0];
            string height      = args[1];
            string isVertical  = args[2];
            string opacity     = args[3];
            string showToolTip = args[4];
            string isLocked    = args[5];
            string topMost     = args[6];

            RulerInfo rulerInfo = new RulerInfo();

            rulerInfo.Width       = int.Parse(width);
            rulerInfo.Height      = int.Parse(height);
            rulerInfo.IsVertical  = bool.Parse(isVertical);
            rulerInfo.Opacity     = double.Parse(opacity);
            rulerInfo.ShowToolTip = bool.Parse(showToolTip);
            rulerInfo.IsLocked    = bool.Parse(isLocked);
            rulerInfo.TopMost     = bool.Parse(topMost);

            return(rulerInfo);
        }
Пример #20
0
        private void Init(RulerInfo rulerInfo)
        {
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.UpdateStyles();

            ResourceManager resources = new ResourceManager(typeof(MainForm));

            this.Icon = ((Icon)(resources.GetObject("$this.Icon")));

            this.SetUpMenu();

            this.Text      = "Ruler";
            this.BackColor = Color.White;

            rulerInfo.CopyInto(this);

            this.FormBorderStyle = FormBorderStyle.None;

            this.ContextMenu = _menu;
            this.Font        = new Font("Tahoma", 10);

            this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
        }
Пример #21
0
 public RulerForm(RulerApplicationContext context)
 {
     _context = context;
     _info    = RulerInfo.GetDefaultRulerInfo();
     this.Init();
 }
Пример #22
0
 public MainForm(RulerInfo rulerInfo)
 {
     this.InitializeComponent();
     this.initRulerInfo = rulerInfo;
 }
Пример #23
0
 public MainForm()
     : this(RulerInfo.GetDefaultRulerInfo())
 {
 }
Пример #24
0
 private void ResetToDefaulHandler(object sender, EventArgs e)
 {
     RulerInfo.CopyInto(RulerInfo.GetDefaultRulerInfo(), this);
 }
Пример #25
0
        public MainForm(RulerInfo rulerInfo)
        {
            this.InitLocalVars();

            this.Init(rulerInfo);
        }
Пример #26
0
 public RulerForm(RulerApplicationContext context, RulerInfo rulerInfo)
 {
     _context = context;
     _info    = rulerInfo;
     this.Init();
 }
Пример #27
0
        public static bool ParseArguments(string[] args, out RulerInfo info, out string error)
        {
            info  = null;
            error = string.Empty;
            RulerInfo ret = args.Contains("--defaultvalues") ? RulerInfo.GetDefaultRulerInfo() : new RulerInfo();

            int numCnt = 0;

            int[] nums = new int[3];
            // Parse numerical arguments that should be at the end, if any.
            for (int i = args.Length - 1; i >= 0; i--)
            {
                if (int.TryParse(args[i], out nums[numCnt]))
                {
                    numCnt++;
                    if (numCnt > 2)
                    {
                        numCnt--;
                        error = "Too many numerical arguments";
                    }
                }
            }

            if (error != string.Empty)
            {
                return(false);
            }

            if (numCnt == 1)
            {
                ret.Length = nums[0];
            }
            else if (numCnt == 2)
            {
                ret.Length    = nums[1];
                ret.Thickness = nums[0];
            }

            for (int i = 0; i < args.Length - numCnt; i++)
            {
                if (args[i].StartsWith("--"))
                {
                    if (args[i].StartsWith("--color="))
                    {
                        string colorName = args[i].Substring(8);
                        if (!RulerInfo.Colors.Keys.Contains(colorName))
                        {
                            error = "Unknown color: " + colorName;
                            return(false);
                        }
                        ret.BackColor = RulerInfo.Colors[colorName];
                        continue;
                    }

                    if (args[i].StartsWith("--opacity="))
                    {
                        string opacityString = args[i].Substring(10);
                        double opacity;
                        if (!double.TryParse(opacityString, out opacity))
                        {
                            error = "Don't know how to parse opacity: " + opacityString;
                            return(false);
                        }
                        // need to clamp in [0.1 - 1.0] range and in 0.1 increments.
                        ret.Opacity = opacity;
                        continue;
                    }

                    if (args[i].Equals("--defaultvalues"))
                    {
                        continue;
                    }

                    error = "Unknown option: " + args[i];
                    return(false);
                }

                if (args[i].StartsWith("-") || args[i].StartsWith("+"))
                {
                    if (args[i].Length != 2)
                    {
                        error = "Unknown flag: " + args[i];
                        return(false);
                    }

                    bool enable = args[i][0] == '+';
                    char rest   = args[i][1];
                    switch (rest)
                    {
                    case 'd':
                        ret.ShowDownTicks = enable;
                        break;

                    case 'f':
                        ret.IsFlipped = enable;
                        break;

                    case 'l':
                        ret.IsLocked = enable;
                        break;

                    case 'm':
                        ret.TopMost = enable;
                        break;

                    case 't':
                        ret.ShowToolTip = enable;
                        break;

                    case 'u':
                        ret.ShowUpTicks = enable;
                        break;

                    case 'v':
                        ret.IsVertical = enable;
                        break;

                    default:
                        error = "Unknown flag: " + args[i];
                        return(false);
                    }
                    continue;
                }

                error = "Unknown option: " + args[i];
                return(false);
            }

            info = ret;

            return(true);
        }
Пример #28
0
 public MainForm(RulerInfo rulerInfo)
 {
     this.Init(rulerInfo);
 }
Пример #29
0
        public MainForm()
        {
            RulerInfo rulerInfo = RulerInfo.GetDefaultRulerInfo();

            this.Init(rulerInfo);
        }