Visual C#: CmdDepot – small clipboard utility

by Kliment Andreev
0 comment
Reading Time: 2 minutes

While I was playing with docker, I needed some 10-15 commands executed repeatedly. At first, I was using the cursor up to search through the history, but at point there were so menu of them that I got lost. In bash, you can also use the command history and then execute the command from it if you know the line number by putting ! as a prefix, e.g. !20, but that didn’t work too. So, I’ve opened a notepad on a side and pasted my most used commands in a separate line. This worked better, but then while I was selecting and copying the lines, I would make mistakes and copy the line with a part of the next line or with the line feed character, so it executed immediately when pasted. I needed a small text edit where each line can be copied to the clipboard with a click of a button and can stay on top of the other windows. In addition, I needed many of these, so I needed something to spawn a new instance from the program itself. So, I made this utility that looks like this.

The program’s caption bar has been minimized so the program doesn’t take much real estate on the screen. The green bar on top is used to move the window. The buttons have captions to quickly display the function. Form left to right, button C copies from the text box to the clipboard, button P paste the text from clipboard into the text box, N means a new window – the program will start another instance of itself, the T button is a toggle – to stay on top of the other windows or not (if you have one monitor, this is very useful), and the last two buttons are minimize and exit. On the right bottom size there is a grip to resize the instance. This is how I use my program.

Anytime I need something, I just click on the C button and then right-click-Paste in my shell. No need to search through the history of your bash.
The source and the exe are at github.

git clone https://www.github.com/klimenta/cmddepot.git

The EXE is under cmddepot\CmdDepot\bin\Release folder.
The main source. Click to expand.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace CmdDepot
{
    public partial class CmdDepot : Form
    {
        public CmdDepot()
        {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.None;
            this.DoubleBuffered = true;
            this.SetStyle(ControlStyles.ResizeRedraw, true);
        }
        private const int cGrip = 16;      // Grip size
        private const int cCaption = 5;   // Caption bar height;

        protected override void OnPaint(PaintEventArgs e)
        {
            Rectangle rc = new Rectangle(this.ClientSize.Width - cGrip, this.ClientSize.Height - cGrip, cGrip, cGrip);
            ControlPaint.DrawSizeGrip(e.Graphics, this.BackColor, rc);
            rc = new Rectangle(0, 0, this.ClientSize.Width, cCaption);
            e.Graphics.FillRectangle(Brushes.Chartreuse, rc);
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x84)
            {  // Trap WM_NCHITTEST
                Point pos = new Point(m.LParam.ToInt32());
                pos = this.PointToClient(pos);
                if (pos.Y < cCaption)
                {
                    m.Result = (IntPtr)2;  // HTCAPTION
                    return;
                }
                if (pos.X >= this.ClientSize.Width - cGrip && pos.Y >= this.ClientSize.Height - cGrip)
                {
                    m.Result = (IntPtr)17; // HTBOTTOMRIGHT
                    return;
                }
            }
            base.WndProc(ref m);
        }
    
        private void btnClose_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.Application.Exit();
        }

        private void btnMinimize_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void btnPaste_Click(object sender, EventArgs e)
        {
            tbCmd.Paste();
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(tbCmd.Text);
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
        }

        private void btnStayOnTop_Click(object sender, EventArgs e)
        {
            this.TopMost = !this.TopMost;
            if (this.TopMost) {
                btnStayOnTop.BackColor = Color.Tomato;
            } else
            {
                btnStayOnTop.BackColor = btnCopy.BackColor;
            }            
        }
    }
}

Related Articles

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More