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

📄 fshedview.cpp

📁 HEX编辑器
💻 CPP
📖 第 1 页 / 共 5 页
字号:
			linbuf[m++] = c;
			c = (Doc->DataArray[i] & 0x0f);
			if( c < 10 )
				c += '0';
			else
				c = c - 10 + 'a';
			linbuf[m++] = c;
			linbuf[m++] = ' ';
		}
	}
	// Write spaces for non-existant bytes.
	if (endpos-startpos < App->m_iBytesPerLine-1)
	{
		for (i=0; i<App->m_iBytesPerLine-1-(endpos-startpos); i++)
		{
			linbuf[m++] = ' ';
			linbuf[m++] = ' ';
			linbuf[m++] = ' ';
		}
	}

	// Write offset to chars.
	for (i=0; i<m_stScreenSettings.iCharSpace; i++)
		linbuf[m++] = ' ';
	
	// Write ASCIIs.
	for (i=startpos; i<=endpos; i++)
	{
		if (i == Doc->DataArray.GetLength())
		{
			linbuf[m++] = ' ';
		}
		else if (App->m_iCharacterSet == OEM_FIXED_FONT && Doc->DataArray[i]!=0)
		{
			linbuf[m++] = Doc->DataArray[i];
		}
		else if ((Doc->DataArray[i]>=32 && Doc->DataArray[i]<=126) || (Doc->DataArray[i]>=160 && Doc->DataArray[i]<=255) || (Doc->DataArray[i]>=145 && Doc->DataArray[i]<=146))
		{
			linbuf[m++] = Doc->DataArray[i];
		}
		else
		{
			linbuf[m++] = '.';
		}
	}

	// Write spaces for nonexisting chars.
	if (endpos-startpos < App->m_iBytesPerLine-1)
		for (i=0; i<App->m_iBytesPerLine-1-(endpos-startpos); i++)
			linbuf[m++] = ' ';
	
//   TRACE("Offset %d to %d\n", startpos, endpos);
//	TRACE("Line Buffer: %s\n", linbuf);
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	int line_len = m; // Length of the line in chars.

	// Set normal text colors.
	iBkColor = PALETTERGB (GetRValue(App->m_clrBk),GetGValue(App->m_clrBk),GetBValue(App->m_clrBk));
	iTextColor = PALETTERGB (GetRValue(App->m_clrText),GetGValue(App->m_clrText),GetBValue(App->m_clrText));
	SetTextColor (hdc, iTextColor);
	SetBkColor (hdc, iBkColor);

	// How much of offset and byte-space is visible? Print it in normal text colors.
	if( m_stScreenSettings.iHscrollPos < App->m_iOffsetLen + m_stScreenSettings.iByteSpace )
	{
		// A part of offset+byte-space is visible.
		// Write offset to screen.
		TextOut( hdc, CLIENT_BORDER_WIDTH, CLIENT_BORDER_WIDTH + ( line - m_stScreenSettings.iCurLine ) * m_stScreenSettings.cyChar + m_stScreenSettings.iToolbarHeight, linbuf + m_stScreenSettings.iHscrollPos, App->m_iOffsetLen + m_stScreenSettings.iByteSpace - m_stScreenSettings.iHscrollPos );
	}

	int iHexStart = App->m_iOffsetLen + m_stScreenSettings.iByteSpace;
	int iHexXStart = iHexStart * m_stScreenSettings.cxChar;

	// Write char-space, if it is visible.
	if( m_stScreenSettings.iHscrollPos < App->m_iOffsetLen + m_stScreenSettings.iByteSpace + App->m_iBytesPerLine * iHexWidth )
	{
		// Char-space is visible.
		TextOut( hdc,
			CLIENT_BORDER_WIDTH + ( iHexStart + App->m_iBytesPerLine * iHexWidth - m_stScreenSettings.iHscrollPos ) * m_stScreenSettings.cxChar,
			CLIENT_BORDER_WIDTH + ( line - m_stScreenSettings.iCurLine ) * m_stScreenSettings.cyChar + m_stScreenSettings.iToolbarHeight,
			linbuf + iHexStart + App->m_iBytesPerLine * iHexWidth,
			m_stScreenSettings.iCharSpace );
	}

	iSelBkColor = PALETTERGB (GetRValue(App->m_clrSelBk),GetGValue(App->m_clrSelBk),GetBValue(App->m_clrSelBk));
	iSelTextColor = PALETTERGB (GetRValue(App->m_clrSelText),GetGValue(App->m_clrSelText),GetBValue(App->m_clrSelText));
	BOOL last_normtext = TRUE;

	int p, el = startpos + App->m_iBytesPerLine - 1 - 1, s, e;
	for( p = startpos; p <= el; p++ )
	{
		// Write hex, if it is visible.
		// s = Position in string of last character of current hex.
		s = App->m_iOffsetLen + m_stScreenSettings.iByteSpace + ( p - startpos + 1 ) * iHexWidth;
		e = s - iHexWidth;
		// Print only if at least a part of the hex is visible.
		if( m_stScreenSettings.iHscrollPos < s && m_stScreenSettings.iHscrollPos + m_stScreenSettings.cxBuffer >= e )
		{
			// Selected bytes must be printed in selection colors.
			if( bSelected && IN_BOUNDS( p, iStartOfSelection, iEndOfSelection ) )
			{
				if( last_normtext )
				{
					// Set selection colors.
					SetTextColor (hdc, iSelTextColor);
					SetBkColor (hdc, iSelBkColor);
					last_normtext = FALSE;
				}
			}
			else
			{
				if( !last_normtext )
				{
					// Set normal text colors.
					SetTextColor (hdc, iTextColor);
					SetBkColor (hdc, iBkColor);
					last_normtext = TRUE;
				}
			}
			// Hex is visible.
			TextOut( hdc,
				CLIENT_BORDER_WIDTH + ( iHexStart + ( p - startpos ) * iHexWidth - m_stScreenSettings.iHscrollPos ) * m_stScreenSettings.cxChar,
				CLIENT_BORDER_WIDTH + ( line - m_stScreenSettings.iCurLine ) * m_stScreenSettings.cyChar + m_stScreenSettings.iToolbarHeight,
				linbuf + iHexStart + ( p - startpos ) * iHexWidth,
				iHexWidth );
		}

		s = App->m_iOffsetLen + m_stScreenSettings.iByteSpace + App->m_iBytesPerLine * iHexWidth + m_stScreenSettings.iCharSpace + ( p - startpos + 1);
		// Write char, if it is visible.
		if( m_stScreenSettings.iHscrollPos < s && m_stScreenSettings.iHscrollPos + m_stScreenSettings.cxBuffer >= s - 1 )
		{
			// Selected bytes must be printed in selection colors.
			if( bSelected && IN_BOUNDS( p, iStartOfSelection, iEndOfSelection ) )
			{
				if( last_normtext )
				{
					// Set selection colors.
					SetTextColor (hdc, iSelTextColor);
					SetBkColor (hdc, iSelBkColor);
					last_normtext = FALSE;
				}
			}
			else
			{
				if( !last_normtext )
				{
					// Set normal text colors.
					SetTextColor (hdc, iTextColor);
					SetBkColor (hdc, iBkColor);
					last_normtext = TRUE;
				}
			}
			// Char is visible.
			TextOut( hdc,
				CLIENT_BORDER_WIDTH + ( iHexStart + App->m_iBytesPerLine * iHexWidth + m_stScreenSettings.iCharSpace + ( p - startpos ) - m_stScreenSettings.iHscrollPos ) * m_stScreenSettings.cxChar,
				CLIENT_BORDER_WIDTH + ( line - m_stScreenSettings.iCurLine ) * m_stScreenSettings.cyChar + m_stScreenSettings.iToolbarHeight,
				linbuf + iHexStart + App->m_iBytesPerLine * iHexWidth + m_stScreenSettings.iCharSpace + ( p - startpos ),
				1 );
		}
	}
	
	// The last hex in the line is not completely in selection colors. It's
	// succeding space must be printed in normal text colors (visually more
	// appealing).
	// Write hex, if it is visible.
	// s = Position in string of last character of current hex.
	s = App->m_iOffsetLen + m_stScreenSettings.iByteSpace + ( p - startpos + 1 ) * iHexWidth;
	e = s - iHexWidth;
	// Print only if at least a part of the hex is visible.
	if( m_stScreenSettings.iHscrollPos < s && m_stScreenSettings.iHscrollPos + m_stScreenSettings.cxBuffer >= e )
	{
		// Selected bytes must be printed in selection colors.
		if( bSelected && IN_BOUNDS( p, iStartOfSelection, iEndOfSelection ) )
		{
			if( last_normtext )
			{
				// Output the last space first.
				TextOut( hdc,
					CLIENT_BORDER_WIDTH + ( iHexStart + ( p - startpos ) * iHexWidth - m_stScreenSettings.iHscrollPos ) * m_stScreenSettings.cxChar + 2 * m_stScreenSettings.cxChar,
					CLIENT_BORDER_WIDTH + ( line - m_stScreenSettings.iCurLine ) * m_stScreenSettings.cyChar + m_stScreenSettings.iToolbarHeight,
					" ",
					1 );
				// Set selection colors.
				SetTextColor (hdc, iSelTextColor);
				SetBkColor (hdc, iSelBkColor);
				last_normtext = FALSE;
				last_normtext = TRUE;
				// Write hex.
				TextOut( hdc,
					CLIENT_BORDER_WIDTH + ( iHexStart + ( p - startpos ) * iHexWidth - m_stScreenSettings.iHscrollPos ) * m_stScreenSettings.cxChar,
					CLIENT_BORDER_WIDTH + ( line - m_stScreenSettings.iCurLine ) * m_stScreenSettings.cyChar + m_stScreenSettings.iToolbarHeight,
					linbuf + iHexStart + ( p - startpos ) * iHexWidth,
					iHexWidth - 1 );
			}
			else
			{
				// Write hex.
				TextOut( hdc,
					CLIENT_BORDER_WIDTH + ( iHexStart + ( p - startpos ) * iHexWidth - m_stScreenSettings.iHscrollPos ) * m_stScreenSettings.cxChar,
					CLIENT_BORDER_WIDTH + ( line - m_stScreenSettings.iCurLine ) * m_stScreenSettings.cyChar + m_stScreenSettings.iToolbarHeight,
					linbuf + iHexStart + ( p - startpos ) * iHexWidth,
					iHexWidth - 1 );
				// Set normal text colors.
				SetTextColor (hdc, iTextColor);
				SetBkColor (hdc, iBkColor);
				last_normtext = TRUE;
				// Output the last space.
				TextOut( hdc,
					CLIENT_BORDER_WIDTH + ( iHexStart + ( p - startpos ) * iHexWidth - m_stScreenSettings.iHscrollPos ) * m_stScreenSettings.cxChar + 2 * m_stScreenSettings.cxChar,
					CLIENT_BORDER_WIDTH + ( line - m_stScreenSettings.iCurLine ) * m_stScreenSettings.cyChar + m_stScreenSettings.iToolbarHeight,
					" ",
					1 );
			}
		}
		else
		{
			// Non-selected hex.
			if( !last_normtext )
			{
				// Set normal text colors.
				SetTextColor (hdc, iTextColor);
				SetBkColor (hdc, iBkColor);
				last_normtext = TRUE;
			}
			// Hex is visible.
			TextOut( hdc,
				CLIENT_BORDER_WIDTH + ( iHexStart + ( p - startpos ) * iHexWidth - m_stScreenSettings.iHscrollPos ) * m_stScreenSettings.cxChar,
				CLIENT_BORDER_WIDTH + ( line - m_stScreenSettings.iCurLine ) * m_stScreenSettings.cyChar + m_stScreenSettings.iToolbarHeight,
				linbuf + iHexStart + ( p - startpos ) * iHexWidth,
				iHexWidth );
		}
	}

	s = App->m_iOffsetLen + m_stScreenSettings.iByteSpace + App->m_iBytesPerLine * iHexWidth + m_stScreenSettings.iCharSpace + ( p - startpos + 1);
	// Write char, if it is visible.
	if( m_stScreenSettings.iHscrollPos < s && m_stScreenSettings.iHscrollPos + m_stScreenSettings.cxBuffer >= s - 1 )
	{
		// Selected bytes must be printed in selection colors.
		if( bSelected && IN_BOUNDS( p, iStartOfSelection, iEndOfSelection ) )
		{
			if( last_normtext )
			{
				// Set selection colors.
				SetTextColor (hdc, iSelTextColor);
				SetBkColor (hdc, iSelBkColor);
			}
		}
		else
		{
			if( !last_normtext )
			{
				// Set normal text colors.
				SetTextColor (hdc, iTextColor);
				SetBkColor (hdc, iBkColor);
			}
		}
		// Char is visible.
		TextOut( hdc,
			CLIENT_BORDER_WIDTH + ( iHexStart + App->m_iBytesPerLine * iHexWidth + m_stScreenSettings.iCharSpace + ( p - startpos ) - m_stScreenSettings.iHscrollPos ) * m_stScreenSettings.cxChar,
			CLIENT_BORDER_WIDTH + ( line - m_stScreenSettings.iCurLine ) * m_stScreenSettings.cyChar + m_stScreenSettings.iToolbarHeight,
			linbuf + iHexStart + App->m_iBytesPerLine * iHexWidth + m_stScreenSettings.iCharSpace + ( p - startpos ),
			1 );
	}

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Separators.
	for (i = 0; i < (App->m_iBytesPerLine / 4) + 1; i++)
	{
		m = (App->m_iOffsetLen+m_stScreenSettings.iByteSpace)*m_stScreenSettings.cxChar - m_stScreenSettings.cxChar/2 + 3*m_stScreenSettings.cxChar*4*i - m_stScreenSettings.cxChar*m_stScreenSettings.iHscrollPos;
		MoveToEx (hdc, CLIENT_BORDER_WIDTH+m, CLIENT_BORDER_WIDTH+(line-m_stScreenSettings.iCurLine)*m_stScreenSettings.cyChar + m_stScreenSettings.iToolbarHeight, NULL);
		LineTo (hdc, CLIENT_BORDER_WIDTH+m, CLIENT_BORDER_WIDTH+(line-m_stScreenSettings.iCurLine+1)*m_stScreenSettings.cyChar + m_stScreenSettings.iToolbarHeight);
	}
	// Separator for chars.
	m = CHARSTART*m_stScreenSettings.cxChar - m_stScreenSettings.cxChar*m_stScreenSettings.iHscrollPos - 2;
	MoveToEx (hdc, CLIENT_BORDER_WIDTH+m, CLIENT_BORDER_WIDTH+(line-m_stScreenSettings.iCurLine)*m_stScreenSettings.cyChar + m_stScreenSettings.iToolbarHeight, NULL);
	LineTo (hdc, CLIENT_BORDER_WIDTH+m, CLIENT_BORDER_WIDTH+(line-m_stScreenSettings.iCurLine+1)*m_stScreenSettings.cyChar + m_stScreenSettings.iToolbarHeight);
	// Second separator.
	MoveToEx (hdc, CLIENT_BORDER_WIDTH+m+2, CLIENT_BORDER_WIDTH+(line-m_stScreenSettings.iCurLine)*m_stScreenSettings.cyChar + m_stScreenSettings.iToolbarHeight, NULL);
	LineTo (hdc, CLIENT_BORDER_WIDTH+m+2, CLIENT_BORDER_WIDTH+(line-m_stScreenSettings.iCurLine+1)*m_stScreenSettings.cyChar + m_stScreenSettings.iToolbarHeight);

	// Print bookmark indicators.
	// Are there bookmarks in this line?
	el = startpos + App->m_iBytesPerLine - 1;
	int chpos;
	RECT r;
	// Brush for bookmark borders.
	for( i = 0; i < iBmkCount; i++ )
	{
		// Print the bookmark if it is within the file.
		if( IN_BOUNDS( pbmkList[i].offset, startpos, el ) && pbmkList[i].offset <= Doc->DataArray.GetUpperBound() )
		{
			// Found a bookmark in this line.
			// Mark hex.
			chpos = App->m_iOffsetLen + m_stScreenSettings.iByteSpace + ( pbmkList[i].offset % App->m_iBytesPerLine )*3 - m_stScreenSettings.iHscrollPos;
			r.left = CLIENT_BORDER_WIDTH + chpos * m_stScreenSettings.cxChar;
			r.top = CLIENT_BORDER_WIDTH + ( pbmkList[i].offset / App->m_iBytesPerLine - m_stScreenSettings.iCurLine ) * m_stScreenSettings.cyChar + m_stScreenSettings.iToolbarHeight;
			r.right = CLIENT_BORDER_WIDTH + r.left + 2*m_stScreenSettings.cxChar;
			r.bottom = CLIENT_BORDER_WIDTH + ( pbmkList[i].offset / App->m_iBytesPerLine - m_stScreenSettings.iCurLine + 1 ) * m_stScreenSettings.cyChar + m_stScreenSettings.iToolbarHeight;
			FrameRect( hdc, &r, hbr );

			// Mark char.
			chpos = App->m_iOffsetLen + m_stScreenSettings.iByteSpace + App->m_iBytesPerLine*3 + m_stScreenSettings.iCharSpace
				+ ( pbmkList[i].offset % App->m_iBytesPerLine ) - m_stScreenSettings.iHscrollPos;
			r.left = CLIENT_BORDER_WIDTH + chpos * m_stScreenSettings.cxChar;
			r.top = CLIENT_BORDER_WIDTH + ( pbmkList[i].offset / App->m_iBytesPerLine - m_stScreenSettings.iCurLine) * m_stScreenSettings.cyChar + m_stScreenSettings.iToolbarHeight;
			r.right = CLIENT_BORDER_WIDTH + ( chpos + 1 ) * m_stScreenSettings.cxChar;
			r.bottom = CLIENT_BORDER_WIDTH + ( pbmkList[i].offset / App->m_iBytesPerLine - m_stScreenSettings.iCurLine + 1 ) * m_stScreenSettings.cyChar + m_stScreenSettings.iToolbarHeight;
			FrameRect( hdc, &r, hbr );
		}
	}
	return;
}

//--------------------------------------------------------------------------------------------
// Highlight (invert) the character/byte at the current offset.
void CFshedView::mark_char (HDC hdc)
{
	if( bDontMarkCurrentPos )
		return;

	if (bSelected)
	{
      ::SetCaretPos (-m_stScreenSettings.cxChar, -m_stScreenSettings.cyChar);
		return;
	}

	int DC_was_allocated = FALSE;
	if (hdc == 0)
	{
      hdc = ::GetDC (m_hWnd);
		DC_was_allocated = TRUE;		
	}

	int chpos;
	RECT r;
	switch (m_iEnteringMode)
	{
	case CHARS:
		chpos = App->m_iOffsetLen + m_stScreenSettings.iByteSpace + (m_stScreenSettings.iCurByte%App->m_iBytesPerLine)*3 - m_stScreenSettings.iHscrollPos;
		r.left = CLIENT_BORDER_WIDTH + chpos * m_stScreenSettings.cxChar;
		r.top = CLIENT_BORDER_WIDTH + (m_stScreenSettings.iCurByte/App->m_iBytesPerLine-m_stScreenSettings.iCurLine)*m_stScreenSettings.cyChar + m_stScreenSettings.iToolbarHeight;
		r.right = CLIENT_BORDER_WIDTH + r.left + 2*m_stScreenSettings.cxChar;
		r.bottom = CLIENT_BORDER_WIDTH + (m_stScreenSettings.iCurByte/App->m_iBytesPerLine-m_stScreenSettings.iCurLine+1)*m_stScreenSettings.cyChar + m_stScreenSettings.iToolbarHeight;
		InvertRect (hdc, &r);
		break;
	case BYTES:
		chpos = App->m_iOffsetLen + m_stScreenSettings.iByteSpace + App->m_iBytesPerLine*3 + m_stScreenSettings.iCharSpace
			+ (m_stScreenSettings.iCurByte % App->m_iBytesPerLine) - m_stScreenSettings.iHscrollPos;
		r.left = CLIENT_BORDER_WIDTH + chpos * m_stScreenSettings.cxChar;
		r.top = CLIENT_BORDER_WIDTH + (m_stScreenSettings.iCurByte/App->m_iBytesPerLine-m_stScreenSettings.iCurLine)*m_stScreenSettings.cyChar + m_stScreenSettings.iToolbarHeight;
		r.right = CLIENT_BORDER_WIDTH + (chpos+1)*m_stScreenSettings.cxChar;
		r.bottom = CLIENT_BORDER_WIDTH + (m_stScreenSettings.iCurByte/App->m_iBytesPerLine-m_stScreenSettings.iCurLine+1)*m_stScreenSettings.cyChar + m_stScreenSettings.iToolbarHeight;
		InvertRect (hdc, &r);
	}

	if (DC_was_allocated)
      ::ReleaseDC (m_hWnd, hdc);
}

//-------------------------------------------------------------------
void CFshedView::draw_client_border (HDC hdc)
{
	// Border for client-area.
	int DC_was_allocated = FALSE;
	if (hdc == 0)
	{
      hdc = ::GetDC (m_hWnd);
		DC_was_allocated = TRUE;
	}
	RECT r;
	r.left = 0;
	r.top =  m_stScreenSettings.iToolbarHeight;
	r.right = m_stScreenSettings.cxClient;
	r.bottom = m_stScreenSettings.cyClient + m_stScreenSettings.iToolbarHeight;
	DrawEdge (hdc, &r, EDGE_SUNKEN, BF_RECT);
	if (DC_was_allocated)
	{
      ::ReleaseDC (m_hWnd, hdc);
	}
}

//--------------------------------------------------------------------------------------------
// Handler for character keys.
int CFshedView::character (char ch)
{
	// If there is selection return now.
	if (bSelected)
		return 0;

	// If we are in read-only mode, give a warning and return,
	// except if TAB was pressed.
	if( Doc->bReadOnly && ch != '\t' )
	{
      ::MessageBox( m_hWnd, "Can't change file because read-only mode is engaged!", "Keyboard editing", MB_OK | MB_ICONERROR );
		return 0;
	}

	char x, c = tolower (ch);
	if (ch == '\t') // TAB => change EnteringMode.
	{
		if (m_iEnteringMode == BYTES)
			m_iEnteringMode = CHARS;
		else
			m_iEnteringMode = BYTES;
		
		int log_column;
		if (m_

⌨️ 快捷键说明

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