⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hexedit.cs

📁 ASCII HEX Editor 國外源碼可參考一下
💻 CS
📖 第 1 页 / 共 4 页
字号:
    ///</summary>
    ///<remarks>
    ///</remarks>
    ///
    public class HexEditBox : HexEditBase
    {
        /*
        ** Class Local Variables
        */
        ///<summary>used for backup, so the position alignment will not keep if from moving</summary>
        protected bool      m_bIgnorePart       = false;

        ///<summary>This is the list of characters allowed in the hex window</summary>
        protected string    m_strAllowed        = "1234567890ABCDEF";

        ///<summary>This is the array where the data is stored</summary>
        protected byte[]    m_abyData           = null;

        ///<summary>ensures we dont get a Selection Change while we are processing one already</summary>
        protected bool      m_bSelChangeProcess = false;

        ///<summary>This is a reference to the linked display (if there is one)</summary>
        protected LinkedBox m_rtbLink           = null;

        ///<summary>This is our menuItem for copy</summary>
        protected MenuItem  m_miSelectAll       = null;

        ///<summary>This is our menuItem for copy</summary>
        protected MenuItem  m_miCopy            = null;

        ///<summary>This is our menuItem for Paste</summary>
        protected MenuItem  m_miPasteASCII      = null;

        ///<summary>This is our menuItem for Paste</summary>
        protected MenuItem  m_miPasteBytes      = null;

        ///<summary>This is our menuItem for copy to ASCII</summary>
        protected MenuItem  m_miCopyASCII       = null;

        ///<summary>This is our menuItem for copy bytes</summary>
        protected MenuItem  m_miCopyBytes       = null;


        /*
        ***************************************************************************
        ** 
        ** Function: InitializeComponent
        */
        ///<summary>
        /// This is the custom initialization we set our menu and font here
        ///</summary>
        ///<returns>void</returns>
        ///<exception cref="System.Exception">Thrown</exception>
        ///<remarks>
        ///</remarks>
        ///<example>How to use this function
        ///<code>
        ///</code>
        ///</example>
        ///
        override public void InitializeComponent()
        {
            /*
            ** Create the Context menu
            */    
            m_menuContext   = new ContextMenu();
            m_miCopy        = new MenuItem();
            m_miPasteASCII  = new MenuItem();
            m_miPasteBytes  = new MenuItem();
            m_miCopyASCII   = new MenuItem();
            m_miCopyBytes   = new MenuItem();
            m_miSelectAll   = new MenuItem();

            /*
            ** Add the Pop Message
            */
            m_menuContext.Popup += new System.EventHandler(MenuPopup);

            /*
            ** miSelectAll
            ** 
            */
            m_miSelectAll.Text = "Select All";
            m_miSelectAll.Click += new System.EventHandler(Menu_SelectAll);

            /*
            ** miCopyBytes
            ** 
            */
            m_miCopyBytes.Text = "Copy Data Bytes";
            m_miCopyBytes.Click += new System.EventHandler(Menu_CopyBytes);

            /*
            ** miCopyASCII
            ** 
            */
            m_miCopyASCII.Text = "Copy Bytes to ASCII";
            m_miCopyASCII.Click += new System.EventHandler(Menu_CopyASCII);

            /*
            ** miCopy
            ** 
            */
            m_miCopy.Text = "Copy Hex as ASCII";
            m_miCopy.Click += new System.EventHandler(Menu_Copy);

            /*
            ** miPasteASCII
            ** 
            */
            m_miPasteASCII.Text = "Paste ASCII";
            m_miPasteASCII.Click += new System.EventHandler(Menu_PasteASCII);

            /*
            ** miPasteBytes
            ** 
            */
            m_miPasteBytes.Text = "Paste Bytes";
            m_miPasteBytes.Click += new System.EventHandler(Menu_PasteBytes);

            // 
            // rtbHex
            // 
            this.AcceptsTab = true;
            this.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Name = "rtbHex";
            this.TabIndex = 1;
            this.ContextMenu = m_menuContext;
            this.Text = "";
            this.WordWrap = false;
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.OnKeyDown);
            this.SelectionChanged += new System.EventHandler(this.OnSelectionChange);
            this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.OnKeyPress);
            this.Resize += new System.EventHandler(this.OnResizeBox);

        }


        /*
        ***************************************************************************
        ** 
        ** Function: MenuPopup
        */
        ///<summary>
        ///
        ///</summary>
        ///<param name="sender" type="object"></param>
        ///<param name="e" type="System.EventArgs"></param>
        ///<returns>void</returns>
        ///
        protected void MenuPopup(object sender, System.EventArgs e)
        {
            /*
            ** Local Variables
            */
            IDataObject DataObj = Clipboard.GetDataObject();
            int         iIndex  = 0;

            /*
            ** Clear the Items
            */
            m_menuContext.MenuItems.Clear();

            /*
            ** Add Items to the Context Menu
            */
            m_menuContext.MenuItems.Add(iIndex++, m_miSelectAll);

            /*
            ** Add in the Copy Stuff is there is a Selection
            */
            if(SelectionLength > 0)
            {
                m_menuContext.MenuItems.Add(iIndex++, new MenuItem("-"));
                m_menuContext.MenuItems.Add(iIndex++, m_miCopyBytes);
                m_menuContext.MenuItems.Add(iIndex++, m_miCopyASCII);
                m_menuContext.MenuItems.Add(iIndex++, m_miCopy);
            }
 
            /*
            ** Determines whether the data is in a format you can use.
            */
            if(DataObj.GetDataPresent(m_abyData.GetType()))
            {
                m_menuContext.MenuItems.Add(iIndex++, new MenuItem("-"));
                m_menuContext.MenuItems.Add(iIndex++, m_miPasteBytes);
                if(DataObj.GetDataPresent(DataFormats.Text))
                {
                    m_menuContext.MenuItems.Add(iIndex++, m_miPasteASCII);
                }
            }
            else
            {
                if(DataObj.GetDataPresent(DataFormats.Text))
                {
                    m_menuContext.MenuItems.Add(iIndex++, new MenuItem("-"));
                    m_menuContext.MenuItems.Add(iIndex++, m_miPasteASCII);
                }
            }
        }

        /*
        ***************************************************************************
        ** 
        ** Function: Menu_SelectAll
        */
        ///<summary>
        ///Select all
        ///</summary>
        ///<param name="sender" type="object"></param>
        ///<param name="e" type="System.EventArgs"></param>
        ///<returns>void</returns>
        ///
        protected void Menu_SelectAll(object sender, System.EventArgs e)
        {
            SelectAll();
        }
        
        /*
        ***************************************************************************
        ** 
        ** Function: Menu_Copy
        */
        ///<summary>
        /// Process the Copy Menu Item
        ///</summary>
        ///<param name="sender" type="object"></param>
        ///<param name="e" type="System.EventArgs"></param>
        ///<returns>void</returns>
        ///
        protected void Menu_Copy(object sender, System.EventArgs e)
        {   
            Copy();
        }

        /*
        ***************************************************************************
        ** 
        ** Function: Menu_CopyASCII
        */
        ///<summary>
        ///
        ///</summary>
        ///<param name="sender" type="object"></param>
        ///<param name="e" type="System.EventArgs"></param>
        ///<returns>void</returns>
        ///
        protected void Menu_CopyASCII(object sender, System.EventArgs e)
        {    
            /*
            ** Local Variables
            */
            int             iStart  = (SelectionStart / 3);
            int             iLen    = (SelectionLength / 3);
            StringBuilder   sbVar   = new StringBuilder(iLen);

            /*
            ** Make sure our size is only as much as we have
            */
            if(iLen > m_abyData.Length)
            {
                iLen = m_abyData.Length;
            }

            for(int i = iStart; i < iLen; i++)
            {
                char    cData = GetDisplayChar(m_abyData[i]);
                sbVar.Append(cData);
            }

            /*
            ** Copy the bitmap to the clipboard.
            */
            Clipboard.SetDataObject(sbVar.ToString());
        }

        /*
        ***************************************************************************
        ** 
        ** Function: Menu_CopyBytes
        */
        ///<summary>
        /// Process the Menu Copy Bytes (Converts the hex to Asc in the Hex window)
        ///</summary>
        ///<param name="sender" type="object"></param>
        ///<param name="e" type="System.EventArgs"></param>
        ///<returns>void</returns>
        ///
        protected void Menu_CopyBytes(object sender, System.EventArgs e)
        {    
            /*
            ** Local Variables
            */
            int         iStart  = (SelectionStart / 3);
            int         iLen    = (SelectionLength / 3);
            byte[]      abyCopy = new byte[iLen];

            /*
            ** Make sure our size is only as much as we have
            */
            if(iLen > m_abyData.Length)
            {
                iLen = m_abyData.Length;
            }

            /*
            ** Get the Bytes we want to mess with
            */
            Array.Copy(m_abyData, iStart, abyCopy, 0, iLen);

            /*
            ** Copy the bitmap to the clipboard.
            */
            Clipboard.SetDataObject(abyCopy);
        }

        /*
        ***************************************************************************
        ** 
        ** Function: Menu_PasteASCII
        */
        ///<summary>
        /// Processes the paste ASCII bytes to the hex winedow
        ///</summary>
        ///<remarks>
        ///This is marked internal so that the linked window can call it
        ///</remarks>
        ///<param name="sender" type="object"></param>
        ///<param name="e" type="System.EventArgs"></param>
        ///<returns>void</returns>
        ///
        internal void Menu_PasteASCII(object sender, System.EventArgs e)
        {    
            /*
            ** Save our starting position to restore after we work
            */
            int     iSave = SelectionStart;

            /*
            ** Declares an IDataObject to hold the data returned from the clipboard.
            ** Retrieves the data from the clipboard.
            */
            IDataObject DataObj = Clipboard.GetDataObject();
 
            /*
            ** Determines whether the data is in a format you can use.
            */
            if(DataObj.GetDataPresent(DataFormats.Text)) 
            {
                /*
                ** Local Variables
                */
                string          strText = (string)DataObj.GetData(DataFormats.Text);
                int             iPos    = (SelectionStart / 3);
                int             iLen    = (strText.Length < (m_abyData.Length - iPos)) ? strText.Length : (m_abyData.Length - iPos);

                /*
                ** Yes it is, so display it in a text box.
                */
                for(int i = 0; i < iLen; i++)
                {
                    m_abyData[i + iPos] = (byte)strText[i];
                }

                UpdateDisplay();

            }
            else
            {
                MessageBox.Show("Nothing to paste");
            }
            
            /*
            ** Restore our original position
            */
            SelectionStart = iSave;
        }

        /*
        ***************************************************************************
        ** 
        ** Function: Menu_PasteBytes
        */
        ///<summary>
        ///
        ///</summary>
        ///<param name="sender" type="object"></param>
        ///<param name="e" type="System.EventArgs"></param>
        ///<returns>void</returns>
        ///
        internal void Menu_PasteBytes(object sender, System.EventArgs e)
        {    
            /*
            ** Save our starting position to restore after we work
            */
            int     iSave = SelectionStart;

            /*
            ** Declares an IDataObject to hold the data returned from the clipboard.
            ** Retrieves the data from the clipboard.
            */
            IDataObject DataObj = Clipboard.GetDataObject();
 
            /*
            ** This is a Byte Paste
            */
            if(DataObj.GetDataPresent(m_abyData.GetType()))
            {
                /*

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -