2016年12月24日土曜日

C#でDump

 C#でDumpしてみました。MIDIとかのプログラミングをするときに必要だったからという動機です。

 以前、VBAでも作りましたが、こちらは、ドラッグ&ドロップに対応しているので、少し使いやすいかもしれません。

 ご自由にご利用ください。例によって、動作保証はありませんが。



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

public class Program
{
    [STAThread]

public static void Main()
{
Application.Run( new FormDump());
}
}

public class FormDump : Form
{
private const int VIEW_SIZE = 256;

private byte[] data;

private MenuStrip ms;
private ToolStripMenuItem[][] tsmi;

private Label lbl1;
private TrackBar tbr;
private Label lbl2;
private ComboBox cmb;

private DataGridView dgv;

public FormDump()
{
this.ClientSize = new System.Drawing.Size( 480, 360);

/*--------------------------------------------------*/
this.ms = new MenuStrip();

this.tsmi = new ToolStripMenuItem[1][];
this.tsmi[0] = new ToolStripMenuItem[3];

for( int i = 0; i < this.tsmi.Length; i++)
{
this.tsmi[i][0] = new ToolStripMenuItem();
this.ms.Items.Add( this.tsmi[i][0]);

for( int j = 1; j < this.tsmi[i].Length; j++)
{
this.tsmi[i][j] = new ToolStripMenuItem();
this.tsmi[i][0].DropDownItems.Add( this.tsmi[i][j]);
this.tsmi[i][j].Click += new EventHandler( this.Menu_Click);
}
}

this.tsmi[0][0].Text = "File (&F)";
this.tsmi[0][1].Text = "Open (&O)";
this.tsmi[0][2].Text = "Exit (&X)";

this.Controls.Add( this.ms);
this.MainMenuStrip = this.ms;

/*--------------------------------------------------*/
this.lbl1 = new Label();
this.lbl1.Anchor = AnchorStyles.Left | AnchorStyles.Top;
this.lbl1.Font = new Font( "Arial", 10, FontStyle.Regular);
this.lbl1.TextAlign = ContentAlignment.MiddleRight;
this.lbl1.Text = "Format";
this.lbl1.SetBounds( 0, this.ms.Height, 80, 20);
this.Controls.Add( this.lbl1);

this.cmb = new ComboBox();
this.cmb.Anchor = AnchorStyles.Left | AnchorStyles.Top;
this.cmb.Font = new Font( "Arial", 10, FontStyle.Regular);
this.cmb.DropDownStyle = ComboBoxStyle.DropDownList;

this.cmb.Items.Add( "Hex");
this.cmb.Items.Add( "Dec");
this.cmb.Items.Add( "Asc");
this.cmb.SelectedIndex = 0;

this.cmb.SelectedIndexChanged += new EventHandler( this.cmb_SelectedIndexChanged);
this.cmb.SetBounds( 80, this.ms.Height, 80, 20);
this.Controls.Add( this.cmb);

/*--------------------------------------------------*/
this.lbl2 = new Label();
this.lbl2.Anchor = AnchorStyles.Left | AnchorStyles.Top;
this.lbl2.Font = new Font( "Arial", 10, FontStyle.Regular);
this.lbl2.TextAlign = ContentAlignment.MiddleRight;
this.lbl2.Text = "Offset";
this.lbl2.SetBounds( 160, this.ms.Height, 80, 20);
this.Controls.Add( this.lbl2);

this.tbr = new TrackBar();
this.tbr.Anchor = AnchorStyles.Left | AnchorStyles.Top;
this.tbr.Font = new Font( "Arial", 10, FontStyle.Regular);
this.tbr.AutoSize = false;
this.tbr.ValueChanged += new EventHandler( this.tbr_ValueChanged);
this.tbr.Minimum = 0;
this.tbr.Maximum = 0;
this.tbr.Value = 0;
this.tbr.SmallChange = 1;
this.tbr.LargeChange = 10;
this.tbr.SetBounds( 240, this.ms.Height, 160, 20);
this.Controls.Add( this.tbr);

/*--------------------------------------------------*/
this.dgv = new DataGridView();
this.dgv.SetBounds( 0, this.ms.Height + this.cmb.Height, 480, 360 - this.ms.Height - this.cmb.Height);
this.dgv.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;
this.dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
this.dgv.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
this.dgv.AllowDrop = true;
this.dgv.DragEnter += new DragEventHandler( this.dgv_DragEnter);
this.dgv.DragDrop += new DragEventHandler( this.dgv_DragDrop);
this.Controls.Add( this.dgv);

for( int i = 0; i < 16; i++)
{
this.dgv.Columns.Add( i.ToString( "x"), i.ToString( "x"));
this.dgv.Rows.Add();
}

/*--------------------------------------------------*/
this.data = new byte[0];
this.UpdateView();

/*--------------------------------------------------*/
string[] args = Environment.GetCommandLineArgs();

if( 1 < args.Length)
{
dump( args[1]);
}
}

private void Menu_Click( object sender, EventArgs e)
{
if( sender.ToString() == this.tsmi[0][1].Text)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "file|*.*";

if( ofd.ShowDialog() == DialogResult.OK)
{
dump( ofd.FileName);
}
}
else if( sender.ToString() == this.tsmi[0][2].Text)
{
this.Close();
}
}

private void cmb_SelectedIndexChanged( object sender, EventArgs e)
{
this.UpdateView();
}

private void tbr_ValueChanged( object sender, EventArgs e)
{
this.UpdateView();
}

private void dgv_DragEnter( object sender, DragEventArgs e)
{
if( e.Data.GetDataPresent( DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Move;
}
}

private void dgv_DragDrop( object sender, DragEventArgs e)
{
string[] filename = (string[]) e.Data.GetData( DataFormats.FileDrop, false);

dump( filename[0]);
}

private void dump( string filename)
{
try
{
this.Text = Path.GetFileName( filename);

FileInfo fi = new FileInfo( filename);
this.data = new byte[fi.Length];

this.tbr.Maximum = (int) fi.Length;
this.tbr.LargeChange = (int) fi.Length / 10;

FileStream fs = new FileStream( filename, FileMode.Open, FileAccess.Read);
            fs.Seek( 0, SeekOrigin.Begin);
fs.Read( this.data, 0, this.data.Length);
fs.Close();

this.UpdateView();
}
catch( System.Exception err)
{
MessageBox.Show( err.Message);
}
}

private void UpdateView()
{
int offset = this.tbr.Value;

for( int i = 0; i < FormDump.VIEW_SIZE / 16; i++)
{
for( int j = 0; j < 16; j++)
{
if( i * 16 + j + offset < this.data.Length)
{
if( this.cmb.SelectedIndex == 0)
{
this.dgv[j,i].Value = this.data[i * 16 + j + offset].ToString( "x");
}
else if( this.cmb.SelectedIndex == 1)
{
this.dgv[j,i].Value = this.data[i * 16 + j + offset].ToString();
}
else if( this.cmb.SelectedIndex == 2)
{
this.dgv[j,i].Value = ( (char) this.data[i * 16 + j + offset]).ToString();
}
}
else
{
this.dgv[j,i].Value = null;
}
}

this.dgv.Rows[i].HeaderCell.Value = ( i * 16 + offset).ToString();
}
}
}

0 件のコメント:

コメントを投稿