From cd5a7ad5840b1b6d5ae8b73d02ae294295f47532 Mon Sep 17 00:00:00 2001 From: "Valeriano A.R" Date: Mon, 30 Jan 2017 01:24:01 +0100 Subject: [PATCH] Initial commit. + CsvParser: CSV Parser with indexing. + CTextBox: CustomTextBox. + FrmCsvViewer: Basic form to view CSV files, as forms. --- .gitignore | 27 +++++ CTextBox.cs | 35 ++++++ CsvParser.cs | 192 +++++++++++++++++++++++++++++++ CsvView.csproj | 74 ++++++++++++ CsvView.sln | 22 ++++ FrmCsvViewer.Designer.cs | 210 ++++++++++++++++++++++++++++++++++ FrmCsvViewer.cs | 169 +++++++++++++++++++++++++++ FrmCsvViewer.resx | 120 +++++++++++++++++++ MessageBoxEx.cs | 229 +++++++++++++++++++++++++++++++++++++ Program.cs | 22 ++++ Properties/AssemblyInfo.cs | 14 +++ 11 files changed, 1114 insertions(+) create mode 100644 .gitignore create mode 100644 CTextBox.cs create mode 100644 CsvParser.cs create mode 100644 CsvView.csproj create mode 100644 CsvView.sln create mode 100644 FrmCsvViewer.Designer.cs create mode 100644 FrmCsvViewer.cs create mode 100644 FrmCsvViewer.resx create mode 100644 MessageBoxEx.cs create mode 100644 Program.cs create mode 100644 Properties/AssemblyInfo.cs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9b2de5b --- /dev/null +++ b/.gitignore @@ -0,0 +1,27 @@ +#ignorar miniaturas creadas por windows +Thumbs.db +#Ignorar archivos construidos por Visual Studio +*.obj +*.exe +*.pdb +*.user +*.aps +*.pch +*.vspscc +*_i.c +*_p.c +*.ncb +*.suo +*.tlb +*.tlh +*.bak +*.cache +*.ilk +*.log +[Bb]in +[Dd]ebug*/ +*.lib +*.sbr +obj/ +[Rr]elease*/ +_ReSharper*/ diff --git a/CTextBox.cs b/CTextBox.cs new file mode 100644 index 0000000..504d638 --- /dev/null +++ b/CTextBox.cs @@ -0,0 +1,35 @@ +using System; +using System.Windows.Forms; +using System.Runtime.InteropServices; +using System.Drawing; + +namespace CsvView +{ + public class CTextBox : TextBox + { + public CTextBox() + { + Multiline = true; + WordWrap = false; + ScrollBars = ScrollBars.Horizontal; + Font = new Font("Courier New", 9, FontStyle.Regular, GraphicsUnit.Point, 0); + } + + [DllImport("user32.dll")] + private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp); + + private const int WM_MOUSEWHEEL = 0x20a; + + protected override void WndProc(ref Message m) + { + if (m.Msg == WM_MOUSEWHEEL) + { + SendMessage(Parent.Handle, m.Msg, m.WParam, m.LParam); + } + else + { + base.WndProc(ref m); + } + } + } +} diff --git a/CsvParser.cs b/CsvParser.cs new file mode 100644 index 0000000..5a71114 --- /dev/null +++ b/CsvParser.cs @@ -0,0 +1,192 @@ +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace CsvView +{ + public class CsvParser + { + private bool _insideString = false; + + private char _separator = ','; + private char _quoteChar = '"'; + private char _escapeChar = '\\'; + + public CsvParser(char separator = ',', char quoteChar = '"', char escapeChar = '\\') + { + _separator = separator; + _quoteChar = quoteChar; + _escapeChar = escapeChar; + } + + private List> _data = new List>(); + + private List _currentReg = null; + StringBuilder _currentCell = null; + + public List> Data + { + get { return _data; } + } + + public void ParseLine(string line) + { + if(_currentReg == null) + { + _currentReg = new List(); + } + if(_currentCell == null) + { + _currentCell = new StringBuilder(); + } + + for (int i = 0; i < line.Length; i++) + { + char c = line[i]; + if (c == _separator && _insideString == false) + { + _currentReg.Add(_currentCell.ToString()); + _currentCell.Clear(); + continue; + } + if (c == _quoteChar && _insideString == false) + { + _insideString = true; + continue; + } + if (c == _quoteChar && _insideString == true) + { + _insideString = false; + continue; + } + if (c == _escapeChar && _insideString) + { + i++; + if (i == line.Length) { break; } + c = line[i]; + } + + _currentCell.Append(c); + } + + + if (_insideString) + { + _currentCell.Append('\n'); + } + else + { + _currentReg.Add(_currentCell.ToString()); + _currentCell.Clear(); + _data.Add(_currentReg); + _currentReg = null; + } + } + + public void ParseFile(string file, long offset = 0, int count = 0) + { + _insideString = false; + _data = new List>(); + _currentReg = null; + FileStream stream = new FileStream(file, FileMode.Open); + stream.Seek(offset, SeekOrigin.Begin); + using (StreamReader reader = new StreamReader(stream, Encoding.Default, true, 4096)) + { + string currentLine; + while ((currentLine = reader.ReadLine()) != null) + { + ParseLine(currentLine); + if(count>0 && Data.Count== count) + { + break; + } + } + } + stream.Close(); + } + + private List _index = new List(); + + public List Index { get { return _index; } } + + private void DummyParser(string line) + { + for (int i = 0; i < line.Length; i++) + { + char c = line[i]; + if (c == _separator && _insideString == false) + { + continue; + } + if (c == _quoteChar && _insideString == false) + { + _insideString = true; + continue; + } + if (c == _quoteChar && _insideString == true) + { + _insideString = false; + continue; + } + if (c == _escapeChar && _insideString) + { + i++; + c = line[i]; + } + } + } + + private class TrackingTextReader : TextReader + { + private TextReader _baseReader; + private int _position; + + public TrackingTextReader(TextReader baseReader) + { + _baseReader = baseReader; + } + + public override int Read() + { + _position++; + return _baseReader.Read(); + } + + public override int Peek() + { + return _baseReader.Peek(); + } + + public int Position + { + get { return _position; } + } + } + + public void GenerateIndex(string file) + { + _insideString = false; + _index.Clear(); + FileStream stream = new FileStream(file, FileMode.Open); + using (StreamReader streamReader = new StreamReader(stream, Encoding.Default, true, 4096)) + using (TrackingTextReader reader = new TrackingTextReader(streamReader)) + { + string currentLine; + if (_insideString == false) + { + _index.Add(reader.Position); + } + while ((currentLine = reader.ReadLine()) != null) + { + DummyParser(currentLine); + if (_insideString == false) + { + _index.Add(reader.Position); + } + } + } + stream.Close(); + } + + } +} diff --git a/CsvView.csproj b/CsvView.csproj new file mode 100644 index 0000000..1c73f06 --- /dev/null +++ b/CsvView.csproj @@ -0,0 +1,74 @@ + + + + + Debug + AnyCPU + {587169B9-7891-4A2F-8537-9196898B86AF} + WinExe + Properties + CsvView + CsvView + v4.6.1 + 512 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + Component + + + Form + + + FrmCsvViewer.cs + + + + + + FrmCsvViewer.cs + + + + + \ No newline at end of file diff --git a/CsvView.sln b/CsvView.sln new file mode 100644 index 0000000..c82c2b7 --- /dev/null +++ b/CsvView.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.25420.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CsvView", "CsvView.csproj", "{587169B9-7891-4A2F-8537-9196898B86AF}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {587169B9-7891-4A2F-8537-9196898B86AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {587169B9-7891-4A2F-8537-9196898B86AF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {587169B9-7891-4A2F-8537-9196898B86AF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {587169B9-7891-4A2F-8537-9196898B86AF}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/FrmCsvViewer.Designer.cs b/FrmCsvViewer.Designer.cs new file mode 100644 index 0000000..50b6561 --- /dev/null +++ b/FrmCsvViewer.Designer.cs @@ -0,0 +1,210 @@ +namespace CsvView +{ + partial class FrmCsvViewer + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.txtPath = new System.Windows.Forms.TextBox(); + this.btnLoad = new System.Windows.Forms.Button(); + this.btnFirstReg = new System.Windows.Forms.Button(); + this.btnPrevReg = new System.Windows.Forms.Button(); + this.btnNextReg = new System.Windows.Forms.Button(); + this.btnLastReg = new System.Windows.Forms.Button(); + this.txtCurrentReg = new System.Windows.Forms.TextBox(); + this.txtTotalRegs = new System.Windows.Forms.TextBox(); + this.tblRegNumbers = new System.Windows.Forms.TableLayoutPanel(); + this.pnlReg = new System.Windows.Forms.Panel(); + this.pnlData = new System.Windows.Forms.Panel(); + this.tblRegNumbers.SuspendLayout(); + this.pnlReg.SuspendLayout(); + this.SuspendLayout(); + // + // txtPath + // + this.txtPath.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.txtPath.Location = new System.Drawing.Point(12, 14); + this.txtPath.Name = "txtPath"; + this.txtPath.Size = new System.Drawing.Size(457, 20); + this.txtPath.TabIndex = 0; + this.txtPath.DoubleClick += new System.EventHandler(this.txtPath_DoubleClick); + // + // btnLoad + // + this.btnLoad.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.btnLoad.Location = new System.Drawing.Point(475, 11); + this.btnLoad.Name = "btnLoad"; + this.btnLoad.Size = new System.Drawing.Size(45, 23); + this.btnLoad.TabIndex = 3; + this.btnLoad.Text = "Load"; + this.btnLoad.UseVisualStyleBackColor = true; + this.btnLoad.Click += new System.EventHandler(this.btnLoad_Click); + // + // btnFirstReg + // + this.btnFirstReg.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btnFirstReg.Location = new System.Drawing.Point(0, 0); + this.btnFirstReg.Name = "btnFirstReg"; + this.btnFirstReg.Size = new System.Drawing.Size(36, 30); + this.btnFirstReg.TabIndex = 4; + this.btnFirstReg.Text = "|<"; + this.btnFirstReg.UseVisualStyleBackColor = true; + this.btnFirstReg.Click += new System.EventHandler(this.btnFirstReg_Click); + // + // btnPrevReg + // + this.btnPrevReg.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btnPrevReg.Location = new System.Drawing.Point(42, 0); + this.btnPrevReg.Name = "btnPrevReg"; + this.btnPrevReg.Size = new System.Drawing.Size(36, 30); + this.btnPrevReg.TabIndex = 5; + this.btnPrevReg.Text = "<"; + this.btnPrevReg.UseVisualStyleBackColor = true; + this.btnPrevReg.Click += new System.EventHandler(this.btnPrevReg_Click); + // + // btnNextReg + // + this.btnNextReg.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.btnNextReg.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btnNextReg.Location = new System.Drawing.Point(430, 0); + this.btnNextReg.Name = "btnNextReg"; + this.btnNextReg.Size = new System.Drawing.Size(36, 30); + this.btnNextReg.TabIndex = 6; + this.btnNextReg.Text = ">"; + this.btnNextReg.UseVisualStyleBackColor = true; + this.btnNextReg.Click += new System.EventHandler(this.btnNextReg_Click); + // + // btnLastReg + // + this.btnLastReg.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.btnLastReg.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btnLastReg.Location = new System.Drawing.Point(472, 0); + this.btnLastReg.Name = "btnLastReg"; + this.btnLastReg.Size = new System.Drawing.Size(36, 30); + this.btnLastReg.TabIndex = 7; + this.btnLastReg.Text = ">|"; + this.btnLastReg.UseVisualStyleBackColor = true; + this.btnLastReg.Click += new System.EventHandler(this.btnLastReg_Click); + // + // txtCurrentReg + // + this.txtCurrentReg.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.txtCurrentReg.Location = new System.Drawing.Point(3, 3); + this.txtCurrentReg.Name = "txtCurrentReg"; + this.txtCurrentReg.Size = new System.Drawing.Size(164, 20); + this.txtCurrentReg.TabIndex = 8; + this.txtCurrentReg.TextChanged += new System.EventHandler(this.txtCurrentReg_TextChanged); + // + // txtTotalRegs + // + this.txtTotalRegs.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.txtTotalRegs.Location = new System.Drawing.Point(173, 3); + this.txtTotalRegs.Name = "txtTotalRegs"; + this.txtTotalRegs.ReadOnly = true; + this.txtTotalRegs.Size = new System.Drawing.Size(164, 20); + this.txtTotalRegs.TabIndex = 9; + // + // tblRegNumbers + // + this.tblRegNumbers.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.tblRegNumbers.ColumnCount = 2; + this.tblRegNumbers.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F)); + this.tblRegNumbers.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F)); + this.tblRegNumbers.Controls.Add(this.txtTotalRegs, 1, 0); + this.tblRegNumbers.Controls.Add(this.txtCurrentReg, 0, 0); + this.tblRegNumbers.Location = new System.Drawing.Point(84, 0); + this.tblRegNumbers.Name = "tblRegNumbers"; + this.tblRegNumbers.RowCount = 1; + this.tblRegNumbers.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F)); + this.tblRegNumbers.Size = new System.Drawing.Size(340, 30); + this.tblRegNumbers.TabIndex = 10; + // + // pnlReg + // + this.pnlReg.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) + | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.pnlReg.Controls.Add(this.pnlData); + this.pnlReg.Controls.Add(this.btnPrevReg); + this.pnlReg.Controls.Add(this.tblRegNumbers); + this.pnlReg.Controls.Add(this.btnFirstReg); + this.pnlReg.Controls.Add(this.btnLastReg); + this.pnlReg.Controls.Add(this.btnNextReg); + this.pnlReg.Enabled = false; + this.pnlReg.Location = new System.Drawing.Point(12, 40); + this.pnlReg.Name = "pnlReg"; + this.pnlReg.Size = new System.Drawing.Size(508, 469); + this.pnlReg.TabIndex = 11; + // + // pnlData + // + this.pnlData.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) + | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.pnlData.AutoScroll = true; + this.pnlData.Location = new System.Drawing.Point(0, 37); + this.pnlData.Name = "pnlData"; + this.pnlData.Size = new System.Drawing.Size(508, 432); + this.pnlData.TabIndex = 11; + // + // FrmCsvViewer + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(532, 521); + this.Controls.Add(this.pnlReg); + this.Controls.Add(this.btnLoad); + this.Controls.Add(this.txtPath); + this.Name = "FrmCsvViewer"; + this.Text = "CsvViewer"; + this.tblRegNumbers.ResumeLayout(false); + this.tblRegNumbers.PerformLayout(); + this.pnlReg.ResumeLayout(false); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.TextBox txtPath; + private System.Windows.Forms.Button btnLoad; + private System.Windows.Forms.Button btnFirstReg; + private System.Windows.Forms.Button btnPrevReg; + private System.Windows.Forms.Button btnNextReg; + private System.Windows.Forms.Button btnLastReg; + private System.Windows.Forms.TextBox txtCurrentReg; + private System.Windows.Forms.TextBox txtTotalRegs; + private System.Windows.Forms.TableLayoutPanel tblRegNumbers; + private System.Windows.Forms.Panel pnlReg; + private System.Windows.Forms.Panel pnlData; + } +} + diff --git a/FrmCsvViewer.cs b/FrmCsvViewer.cs new file mode 100644 index 0000000..469a9b3 --- /dev/null +++ b/FrmCsvViewer.cs @@ -0,0 +1,169 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Runtime.InteropServices; +using System.Windows.Forms; + +namespace CsvView +{ + public partial class FrmCsvViewer : Form + { + public FrmCsvViewer() + { + InitializeComponent(); + } + + private void txtPath_DoubleClick(object sender, EventArgs e) + { + OpenFileDialog loadDialog = new OpenFileDialog(); + DialogResult result = loadDialog.ShowDialog(); + if (result == DialogResult.OK) + { + txtPath.Text = loadDialog.FileName; + } + } + + private string _loadedFile = string.Empty; + private long _currentReg = 0; + private long _totalRegs = 0; + private List> _data = null; + + private void btnLoad_Click(object sender, EventArgs e) + { + if (File.Exists(txtPath.Text) == false) + { + RenderRegClean(); + _loadedFile = null; + _totalRegs = 0; + _data = null; + MessageBoxEx.Show(this, "FileNotFound"); + return; + } + + _loadedFile = txtPath.Text; + + var csvParser = new CsvParser(); + csvParser.ParseFile(_loadedFile); + + _totalRegs = csvParser.Data.Count; + _data = csvParser.Data; + + RenderReg(0); + } + + [DllImport("user32.dll", CharSet = CharSet.Auto)] + private static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam); + + private const int WM_SetRedraw = 0XB; + + private void RenderReg(long currentReg) + { + if (_data == null || _data.Count <= 0) + { + RenderRegClean(); + } + pnlReg.Enabled = true; + bool first = false; + bool last = false; + if (currentReg <= 0) + { + currentReg = 0; + first = true; + } + if (currentReg >= (_totalRegs - 1)) + { + currentReg = _totalRegs - 1; + last = true; + } + + _currentReg = currentReg; + txtCurrentReg.Text = Convert.ToString(currentReg); + txtTotalRegs.Text = Convert.ToString(_totalRegs); + + btnFirstReg.Enabled = (first == false); + btnPrevReg.Enabled = (first == false); + btnLastReg.Enabled = (last == false); + btnNextReg.Enabled = (last == false); + + List currentData = _data[(int)currentReg]; + + pnlData.Visible = false; + pnlData.Controls.Clear(); + int y = 0; + const int TexboxPadding = 5; + const int Padding = 9; + const int LineHeight = 15; + for (int i = 0; i < currentData.Count; i++) + { + TextBox txtValue = RenderValue(currentData[i], y, TexboxPadding, Padding, LineHeight); + pnlData.Controls.Add(txtValue); + y += txtValue.Height + Padding; + } + pnlData.Visible = true; + } + + private TextBox RenderValue(string value, int y, int TexboxPadding, int Padding, int LineHeight) + { + string[] valueLines = value.Split('\n'); + CTextBox txtValue = new CTextBox() + { + Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right), + Width = pnlData.Width - Padding, + Height = (valueLines.Length * LineHeight) + TexboxPadding, + Top = y, + Left = 0, + ReadOnly = true, + }; + for (int j = 0; j < valueLines.Length; j++) + { + if (j > 0) + { + txtValue.AppendText("\n"); + } + txtValue.AppendText(valueLines[j]); + } + return txtValue; + } + + private void RenderRegClean() + { + pnlReg.Enabled = false; + txtCurrentReg.Text = string.Empty; + txtTotalRegs.Text = string.Empty; + pnlData.Controls.Clear(); + } + + private void btnFirstReg_Click(object sender, EventArgs e) + { + RenderReg(0); + } + + private void btnPrevReg_Click(object sender, EventArgs e) + { + RenderReg(_currentReg - 1); + } + + private void btnNextReg_Click(object sender, EventArgs e) + { + RenderReg(_currentReg + 1); + } + + private void btnLastReg_Click(object sender, EventArgs e) + { + RenderReg(_totalRegs - 1); + } + + private void txtCurrentReg_TextChanged(object sender, EventArgs e) + { + int newReg = 0; + if (int.TryParse(txtCurrentReg.Text, out newReg)) + { + RenderReg(newReg); + } + else + { + RenderReg(_currentReg); + } + } + } +} diff --git a/FrmCsvViewer.resx b/FrmCsvViewer.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/FrmCsvViewer.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/MessageBoxEx.cs b/MessageBoxEx.cs new file mode 100644 index 0000000..7269b58 --- /dev/null +++ b/MessageBoxEx.cs @@ -0,0 +1,229 @@ +using System; +using System.Drawing; +using System.Runtime.InteropServices; +using System.Text; +using System.Windows.Forms; + +namespace CsvView +{ + public class MessageBoxEx + { + private static IWin32Window _owner; + private static HookProc _hookProc; + private static IntPtr _hHook; + + public static DialogResult Show(string text) + { + Initialize(); + return MessageBox.Show(text); + } + + public static DialogResult Show(string text, string caption) + { + Initialize(); + return MessageBox.Show(text, caption); + } + + public static DialogResult Show(string text, string caption, MessageBoxButtons buttons) + { + Initialize(); + return MessageBox.Show(text, caption, buttons); + } + + public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon) + { + Initialize(); + return MessageBox.Show(text, caption, buttons, icon); + } + + public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defButton) + { + Initialize(); + return MessageBox.Show(text, caption, buttons, icon, defButton); + } + + public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defButton, MessageBoxOptions options) + { + Initialize(); + return MessageBox.Show(text, caption, buttons, icon, defButton, options); + } + + public static DialogResult Show(IWin32Window owner, string text) + { + _owner = owner; + Initialize(); + return MessageBox.Show(owner, text); + } + + public static DialogResult Show(IWin32Window owner, string text, string caption) + { + _owner = owner; + Initialize(); + return MessageBox.Show(owner, text, caption); + } + + public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons) + { + _owner = owner; + Initialize(); + return MessageBox.Show(owner, text, caption, buttons); + } + + public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon) + { + _owner = owner; + Initialize(); + return MessageBox.Show(owner, text, caption, buttons, icon); + } + + public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defButton) + { + _owner = owner; + Initialize(); + return MessageBox.Show(owner, text, caption, buttons, icon, defButton); + } + + public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defButton, MessageBoxOptions options) + { + _owner = owner; + Initialize(); + return MessageBox.Show(owner, text, caption, buttons, icon, + defButton, options); + } + + public delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam); + + public delegate void TimerProc(IntPtr hWnd, uint uMsg, UIntPtr nIDEvent, uint dwTime); + + public const int WH_CALLWNDPROCRET = 12; + + public enum CbtHookAction : int + { + HCBT_MOVESIZE = 0, + HCBT_MINMAX = 1, + HCBT_QS = 2, + HCBT_CREATEWND = 3, + HCBT_DESTROYWND = 4, + HCBT_ACTIVATE = 5, + HCBT_CLICKSKIPPED = 6, + HCBT_KEYSKIPPED = 7, + HCBT_SYSCOMMAND = 8, + HCBT_SETFOCUS = 9 + } + [DllImport("kernel32.dll")] + private static extern uint GetCurrentThreadId(); + + [DllImport("user32.dll")] + private static extern bool GetWindowRect(IntPtr hWnd, ref Rectangle lpRect); + + [DllImport("user32.dll")] + private static extern int MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); + + [DllImport("User32.dll")] + public static extern UIntPtr SetTimer(IntPtr hWnd, UIntPtr nIDEvent, uint uElapse, TimerProc lpTimerFunc); + + [DllImport("User32.dll")] + public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam); + + [DllImport("user32.dll")] + public static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId); + + [DllImport("user32.dll")] + public static extern int UnhookWindowsHookEx(IntPtr idHook); + + [DllImport("user32.dll")] + public static extern IntPtr CallNextHookEx(IntPtr idHook, int nCode, IntPtr wParam, IntPtr lParam); + + [DllImport("user32.dll")] + public static extern int GetWindowTextLength(IntPtr hWnd); + + [DllImport("user32.dll")] + public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int maxLength); + + [DllImport("user32.dll")] + public static extern int EndDialog(IntPtr hDlg, IntPtr nResult); + + [StructLayout(LayoutKind.Sequential)] + public struct CWPRETSTRUCT + { + public IntPtr lResult; + public IntPtr lParam; + public IntPtr wParam; + public uint message; + public IntPtr hwnd; + }; + + static MessageBoxEx() + { + _hookProc = new HookProc(MessageBoxHookProc); + _hHook = IntPtr.Zero; + } + + private static void Initialize() + { + if (_hHook != IntPtr.Zero) + { + throw new NotSupportedException("multiple calls are not supported"); + } + + if (_owner != null) + { + _hHook = SetWindowsHookEx(WH_CALLWNDPROCRET, _hookProc, IntPtr.Zero, (int)GetCurrentThreadId()); + } + } + + private static IntPtr MessageBoxHookProc(int nCode, IntPtr wParam, IntPtr lParam) + { + if (nCode < 0) + { + return CallNextHookEx(_hHook, nCode, wParam, lParam); + } + + CWPRETSTRUCT msg = (CWPRETSTRUCT)Marshal.PtrToStructure(lParam, typeof(CWPRETSTRUCT)); + IntPtr hook = _hHook; + + if (msg.message == (int)CbtHookAction.HCBT_ACTIVATE) + { + try + { + CenterWindow(msg.hwnd); + } + finally + { + UnhookWindowsHookEx(_hHook); + _hHook = IntPtr.Zero; + } + } + + return CallNextHookEx(hook, nCode, wParam, lParam); + } + + private static void CenterWindow(IntPtr hChildWnd) + { + Rectangle recChild = new Rectangle(0, 0, 0, 0); + bool success = GetWindowRect(hChildWnd, ref recChild); + + int width = recChild.Width - recChild.X; + int height = recChild.Height - recChild.Y; + + Rectangle recParent = new Rectangle(0, 0, 0, 0); + success = GetWindowRect(_owner.Handle, ref recParent); + + Point ptCenter = new Point(0, 0); + ptCenter.X = recParent.X + ((recParent.Width - recParent.X) / 2); + ptCenter.Y = recParent.Y + ((recParent.Height - recParent.Y) / 2); + + + Point ptStart = new Point(0, 0); + ptStart.X = (ptCenter.X - (width / 2)); + ptStart.Y = (ptCenter.Y - (height / 2)); + + ptStart.X = (ptStart.X < 0) ? 0 : ptStart.X; + ptStart.Y = (ptStart.Y < 0) ? 0 : ptStart.Y; + + int result = MoveWindow(hChildWnd, ptStart.X, ptStart.Y, width, + height, false); + } + + } +} \ No newline at end of file diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..1db4a7f --- /dev/null +++ b/Program.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace CsvView.Net +{ + static class Program + { + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main() + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new FrmCsvViewer()); + } + } +} diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..0636657 --- /dev/null +++ b/Properties/AssemblyInfo.cs @@ -0,0 +1,14 @@ +using System.Reflection; +using System.Runtime.InteropServices; + +[assembly: AssemblyTitle("CSVView.Net")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("CSVView.Net")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] +[assembly: ComVisible(false)] +[assembly: Guid("587169b9-7891-4a2f-8537-9196898b86af")] +[assembly: AssemblyVersion("1.0.*")]