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

📄 hexedit.cs

📁 ASCII HEX Editor 國外源碼可參考一下
💻 CS
📖 第 1 页 / 共 4 页
字号:
                ** Local Variables
                */
                byte[]          abyCopy = (byte[])DataObj.GetData(m_abyData.GetType());
                int             iPos    = (SelectionStart / 3);
                int             iLen    = (abyCopy.Length < (m_abyData.Length - iPos)) ? abyCopy.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] = abyCopy[i];
                }

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

        /*
        ***************************************************************************
        ** 
        ** Function: LinkDisplay
        */
        ///<summary>
        /// This allows you to link a 'ASCII Display' to the Hex Window
        /// So you can see both
        ///</summary>
        ///<param name="rtbLink" type="HexEdit.LinkedBox">null to unlink else a LinkedBox</param>
        ///<returns>void</returns>
        ///
        public void LinkDisplay(LinkedBox rtbLink)
        {
            m_rtbLink = rtbLink;
            m_rtbLink.LinkHex(this);
        }

        /*
        ***************************************************************************
        ** 
        ** Function: GetDisplayForByte
        */
        ///<summary>
        /// Get the Display for a Data Byte
        ///</summary>
        ///<param name="byData" type="byte"></param>
        ///<example>byte = 0x42 = string = "42"
        ///</example>
        ///
        string GetDisplayForByte(byte byData)
        {
            return string.Format("{0:X2}", byData);
        }

        /*
        ***************************************************************************
        ** 
        ** Function: LoadData
        */
        ///<summary>
        /// Load data into the Hex Box
        ///</summary>
        ///<param name="abyData" type="byte[]"></param>
        ///<returns>void</returns>
        ///
        public void LoadData(byte[] abyData)
        {
            m_abyData = new byte[abyData.Length];
            Array.Copy(abyData, 0, m_abyData, 0, abyData.Length);
            UpdateDisplay();        
        }

        /*
        ***************************************************************************
        ** 
        ** Function: NewData
        */
        ///<summary>
        ///Create a New Blank Display
        ///</summary>
        ///<param name="iSize" type="int"></param>
        ///<returns>void</returns>
        ///
        public void NewData(int iSize)
        {
            m_abyData = new byte[iSize];
            UpdateDisplay();        
        }

        /*
        ***************************************************************************
        ** 
        ** Property(byte[]): Array
        */
        ///<summary>
        /// Get the Data that is the current buffer
        ///</summary>
        ///
        public byte[] ArrayData
        {
            get{return m_abyData;}
        }

        /*
        ***************************************************************************
        ** 
        ** Function: UpdateDisplay
        */
        ///<summary>
        /// This updates the display with the data that is in abyData
        /// Can sync up if data has changed in the buffer due to a paste or
        /// like function
        ///</summary>
        ///
        public void UpdateDisplay()
        {
            /*
            ** Local Variables
            */
            int             iLenData    = m_abyData.Length;
            int             iLenDisp    = 0;
            int             iCols       = 0;
            StringBuilder   sbVar       = null;

            iCols = (this.Width / GetFontWidth());
            iCols = (iCols / 3) - 1;

            /*
            ** Get a copy of the Data so we can mess with it
            ** and also have it aligned where we want it
            */
            iLenDisp = ((iLenData + (iCols - 1)) / iCols) * iCols;
            sbVar = new StringBuilder(iLenDisp);

            for(int i = 0; i < iLenDisp; i++)
            {
                byte        byData  = 0;

                /*
                ** See if we are still in the real buffer range
                ** if so display data, else display spaces
                */
                if(i < iLenData)
                {
                    byData = m_abyData[i];
                    sbVar.Append(GetDisplayForByte(byData));
                }
                else
                {
                    sbVar.Append("  ");
                }

                if(0 != ((i+1) % iCols))
                {
                    sbVar.Append(" ");
                }
                else
                {
                    sbVar.Append("\n");
                }
            }

            /*
            ** Remove the extra last char
            ** and display the data
            */
            sbVar.Remove(sbVar.Length - 1, 1);
            Text = sbVar.ToString();

            if(null != m_rtbLink)
            {
                m_rtbLink.UpdateDisplay(m_abyData);
            }
        }

        /*
        ***************************************************************************
        ** 
        ** Function: GetByteAtCurrent
        */
        ///<summary>
        /// Gets the Byte at the current screen location in the hex editor
        ///</summary>
        ///<returns>byte</returns>
        ///
        protected byte GetByteAtCurrent()
        {
            int     iPutBack    = SelectionStart;
            int     iStart      = (SelectionStart-1)/3;
            byte    by1         = 0;
            byte    by2         = 0;

            /*
            ** Set the Current Position to where this byte begins
            */
            SelectionStart = iStart * 3;
            by1 = HexCtoB(GetCharFromPosition(CaretPosition));
            SelectionStart += 1;
            by2 = HexCtoB(GetCharFromPosition(CaretPosition));
            SelectionStart = iPutBack;

            by1 = (byte)(by1 << 4);
            by1 |= by2;

            return by1;
        }

        /*
        ***************************************************************************
        ** 
        ** Function: OnSelectionChange
        */
        ///<summary>
        ///This is our OnSelectionChange Listener
        ///</summary>
        ///<param name="sender" type="object"></param>
        ///<param name="e" type="System.EventArgs"></param>
        ///<returns>void</returns>
        ///<remarks>
        ///here we can get our current position from selection start
        ///and if we are on a boundry (00^) then we skip one
        ///</remarks>
        ///
        protected void OnSelectionChange(object sender, System.EventArgs e)
        {
            if(!m_bSelChangeProcess)
            {
                m_bSelChangeProcess = true;
                int     iPos = ((SelectionStart + 1) % 3);

                if((0 == iPos) && !m_bIgnorePart)
                {
                    iPos      = SelectionStart/3;
                    if(iPos < m_abyData.Length)
                    {
                        m_abyData[iPos] = GetByteAtCurrent();
                        if(null != m_rtbLink)
                        {
                            m_bNoUpdate = true;
                            m_rtbLink.UpdateChar(iPos, (char)m_abyData[iPos]);
                            m_bNoUpdate = false;
                        }
                        SelectionStart += 1;
                    }
                }
                m_bIgnorePart = false;
                m_bSelChangeProcess = false;
            }
        }

        /*
        ***************************************************************************
        ** 
        ** Function: UpdateChar
        */
        ///<summary>
        /// Updates the character at the Position - Used to sync the
        /// LinkedBox and this box
        ///</summary>
        ///<param name="iPosition" type="int">Position of the Data To Update</param>
        ///<param name="cData" type="char">Data to Update with</param>
        ///<returns>void</returns>
        ///
        public void UpdateChar(int iPosition, char cData)
        {
            /*
            ** controled from the call to update to keep
            ** updates from linked window when we update it
            */
            if(!m_bNoUpdate)
            {
                /*
                ** Process any outstanding messages and set the focus to us
                */
                System.Windows.Forms.Application.DoEvents();
                Focus();

                /*
                ** Local Variables
                */
                int     iPos    = (iPosition * 3);
                string  strVar  = null;
                byte    bData   = (byte)cData;
                byte    bHi     = 0;
                byte    bLo     = 0;

                bLo = (byte)(bData & 0xF);
                bHi = (byte)(bData >> 4);

                strVar = string.Format("{0:X}{1:X}", bHi, bLo);
                SelectionStart = iPos;
                SelectionLength = 2;
            
                /*
                ** Send the keys to this window, and then process
                ** any ourstanding messages
                */
                SendKeys.Send(strVar);
                System.Windows.Forms.Application.DoEvents();
            }
        }

        /*
        ***************************************************************************
        ** 
        ** Function: OnResizeBox
        */
        ///<summary>
        ///
        ///</summary>
        ///<param name="sender" type="object"></param>
        ///<param name="e" type="System.EventArgs"></param>
        ///<returns>void</returns>
        ///
        protected void OnResizeBox(object sender, System.EventArgs e)
        {
            if(null != m_abyData)
            {
                UpdateDisplay();
            }
        }

        /*
        ***************************************************************************
        ** 
        ** Function: OnKeyPress
        */
        ///<summary>
        /// KeyPress Listener Used to allow only the acceptable keys to be entered
        ///</summary>
        ///<param name="sender" type="object"></param>
        ///<param name="e" type="System.Windows.Forms.KeyPressEventArgs"></param>
        ///<returns>void</returns>
        ///<exception cref="System.Exception">Thrown</exception>
        ///
        private void OnKeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            /*
            ** see if it is in the allowed list
            */
            if(-1 != m_strAllowed.IndexOf(e.KeyChar))
            {
                /*
                ** Are we still in the real data area,
                ** if so process if no done to it
                */
                if(SelectionStart < (m_abyData.Length * 3))
                {
                    /*
                    ** This forces a Replace instead of a insert
                    */
                    SelectionLength = 1;
                }
                else
                {
                    e.Handled = true;
                }
            }
            else
            {
                /*
                ** Local Variables
                */
                string strVar = string.Format("{0}", e.KeyChar).ToUpper();

                /*
                ** Tell the system not to handle this character
                */
                e.Handled = true;

                /*
                ** Ok lets see if the Upper case is in the list
                ** if it is good send the upper equavelent
                */
                if(-1 != m_strAllowed.IndexOf(strVar))
                {
                    SendKeys.Send(strVar);
                }
                else
                {
                    switch(e.KeyChar)
                    {
                        case (char)0x9:
                            if(null != m_rtbLink)
                            {
                                m_rtbLink.Focus();
                                m_rtbLink.SelectionStart = (SelectionStart / 3);
                            }
                        break;
                    }
                }
            }
        }
            
        /*
        ***************************************************************************
        ** 
        ** Function: OnKeyDown
        */
        ///<summary>
        /// KeyPress Listener Used to disallow special keys from working

⌨️ 快捷键说明

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