From 11dab2e63ec47ff7866716bf36fa488793de6f9c Mon Sep 17 00:00:00 2001 From: "Valeriano A.R" Date: Sun, 5 Feb 2017 01:07:58 +0100 Subject: [PATCH] DoubleBufferPanel: Panel with double buffer and utility functions. --- CsvView.csproj | 3 +++ DoubleBufferPanel.cs | 41 ++++++++++++++++++++++++++++++++++++++++ FrmCsvViewer.Designer.cs | 8 ++++---- FrmCsvViewer.cs | 33 +++++++++++++++++++++----------- 4 files changed, 70 insertions(+), 15 deletions(-) create mode 100644 DoubleBufferPanel.cs diff --git a/CsvView.csproj b/CsvView.csproj index 1c73f06..e2abc3f 100644 --- a/CsvView.csproj +++ b/CsvView.csproj @@ -50,6 +50,9 @@ Component + + Component + Form diff --git a/DoubleBufferPanel.cs b/DoubleBufferPanel.cs new file mode 100644 index 0000000..531c0ed --- /dev/null +++ b/DoubleBufferPanel.cs @@ -0,0 +1,41 @@ +using System; +using System.Runtime.InteropServices; +using System.Windows.Forms; + +namespace CsvView +{ + public class DoubleBufferPanel : Panel + { + public DoubleBufferPanel() + { + DoubleBuffered = true; + SetStyle(ControlStyles.UserPaint | + ControlStyles.AllPaintingInWmPaint | + ControlStyles.ResizeRedraw | + ControlStyles.ContainerControl | + ControlStyles.OptimizedDoubleBuffer | + ControlStyles.SupportsTransparentBackColor + , true); + } + + + [DllImport("user32.dll", CharSet = CharSet.Auto)] + public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam); + + private const int WM_SetRedraw = 0XB; + + public void SuspendDrawing() + { + SuspendLayout(); + SendMessage(Handle, WM_SetRedraw, false, 0); + } + + public void ResumeDrawing() + { + ResumeLayout(true); + SendMessage(Handle, WM_SetRedraw, true, 0); + Refresh(); + } + + } +} diff --git a/FrmCsvViewer.Designer.cs b/FrmCsvViewer.Designer.cs index 50b6561..c01265e 100644 --- a/FrmCsvViewer.Designer.cs +++ b/FrmCsvViewer.Designer.cs @@ -37,8 +37,8 @@ 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.pnlReg = new CsvView.DoubleBufferPanel(); + this.pnlData = new CsvView.DoubleBufferPanel(); this.tblRegNumbers.SuspendLayout(); this.pnlReg.SuspendLayout(); this.SuspendLayout(); @@ -203,8 +203,8 @@ 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; + private CsvView.DoubleBufferPanel pnlReg; + private CsvView.DoubleBufferPanel pnlData; } } diff --git a/FrmCsvViewer.cs b/FrmCsvViewer.cs index a9d896d..148adf7 100644 --- a/FrmCsvViewer.cs +++ b/FrmCsvViewer.cs @@ -123,17 +123,17 @@ namespace CsvView return csvParser.Data[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; - + bool _rendering = false; private void RenderReg(long currentReg) { if (_index == null || _index.Count <= 0) { RenderRegClean(); } + + if (_rendering) { return; } + _rendering = true; + pnlReg.Enabled = true; bool first = false; bool last = false; @@ -148,6 +148,9 @@ namespace CsvView last = true; } + pnlData.SuspendDrawing(); + + pnlData.Controls.Clear(); _currentReg = currentReg; txtCurrentReg.Text = Convert.ToString(currentReg); txtTotalRegs.Text = Convert.ToString(_totalRegs); @@ -158,9 +161,7 @@ namespace CsvView btnNextReg.Enabled = (last == false); List currentData = Index_LoadReg((int)currentReg); - - pnlData.Visible = false; - pnlData.Controls.Clear(); + int y = 0; const int TexboxPadding = 5; const int Padding = 9; @@ -171,7 +172,9 @@ namespace CsvView pnlData.Controls.Add(txtValue); y += txtValue.Height + Padding; } - pnlData.Visible = true; + + pnlData.ResumeDrawing(); + _rendering = false; } private TextBox RenderValue(string value, int y, int TexboxPadding, int Padding, int LineHeight) @@ -199,12 +202,20 @@ namespace CsvView private void RenderRegClean() { + if (_rendering) { return; } + _rendering = true; + + pnlData.SuspendDrawing(); + + pnlData.Controls.Clear(); pnlReg.Enabled = false; txtCurrentReg.Text = string.Empty; txtTotalRegs.Text = string.Empty; - pnlData.Controls.Clear(); - } + pnlData.ResumeDrawing(); + _rendering = false; + } + private void btnFirstReg_Click(object sender, EventArgs e) { RenderReg(0);