DoubleBufferPanel: Panel with double buffer and utility functions.

This commit is contained in:
2017-02-05 01:07:58 +01:00
parent 1fbb85c101
commit 11dab2e63e
4 changed files with 70 additions and 15 deletions

View File

@@ -50,6 +50,9 @@
<Compile Include="CTextBox.cs"> <Compile Include="CTextBox.cs">
<SubType>Component</SubType> <SubType>Component</SubType>
</Compile> </Compile>
<Compile Include="DoubleBufferPanel.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="FrmCsvViewer.cs"> <Compile Include="FrmCsvViewer.cs">
<SubType>Form</SubType> <SubType>Form</SubType>
</Compile> </Compile>

41
DoubleBufferPanel.cs Normal file
View File

@@ -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();
}
}
}

View File

@@ -37,8 +37,8 @@
this.txtCurrentReg = new System.Windows.Forms.TextBox(); this.txtCurrentReg = new System.Windows.Forms.TextBox();
this.txtTotalRegs = new System.Windows.Forms.TextBox(); this.txtTotalRegs = new System.Windows.Forms.TextBox();
this.tblRegNumbers = new System.Windows.Forms.TableLayoutPanel(); this.tblRegNumbers = new System.Windows.Forms.TableLayoutPanel();
this.pnlReg = new System.Windows.Forms.Panel(); this.pnlReg = new CsvView.DoubleBufferPanel();
this.pnlData = new System.Windows.Forms.Panel(); this.pnlData = new CsvView.DoubleBufferPanel();
this.tblRegNumbers.SuspendLayout(); this.tblRegNumbers.SuspendLayout();
this.pnlReg.SuspendLayout(); this.pnlReg.SuspendLayout();
this.SuspendLayout(); this.SuspendLayout();
@@ -203,8 +203,8 @@
private System.Windows.Forms.TextBox txtCurrentReg; private System.Windows.Forms.TextBox txtCurrentReg;
private System.Windows.Forms.TextBox txtTotalRegs; private System.Windows.Forms.TextBox txtTotalRegs;
private System.Windows.Forms.TableLayoutPanel tblRegNumbers; private System.Windows.Forms.TableLayoutPanel tblRegNumbers;
private System.Windows.Forms.Panel pnlReg; private CsvView.DoubleBufferPanel pnlReg;
private System.Windows.Forms.Panel pnlData; private CsvView.DoubleBufferPanel pnlData;
} }
} }

View File

@@ -123,17 +123,17 @@ namespace CsvView
return csvParser.Data[0]; return csvParser.Data[0];
} }
[DllImport("user32.dll", CharSet = CharSet.Auto)] bool _rendering = false;
private static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
private const int WM_SetRedraw = 0XB;
private void RenderReg(long currentReg) private void RenderReg(long currentReg)
{ {
if (_index == null || _index.Count <= 0) if (_index == null || _index.Count <= 0)
{ {
RenderRegClean(); RenderRegClean();
} }
if (_rendering) { return; }
_rendering = true;
pnlReg.Enabled = true; pnlReg.Enabled = true;
bool first = false; bool first = false;
bool last = false; bool last = false;
@@ -148,6 +148,9 @@ namespace CsvView
last = true; last = true;
} }
pnlData.SuspendDrawing();
pnlData.Controls.Clear();
_currentReg = currentReg; _currentReg = currentReg;
txtCurrentReg.Text = Convert.ToString(currentReg); txtCurrentReg.Text = Convert.ToString(currentReg);
txtTotalRegs.Text = Convert.ToString(_totalRegs); txtTotalRegs.Text = Convert.ToString(_totalRegs);
@@ -158,9 +161,7 @@ namespace CsvView
btnNextReg.Enabled = (last == false); btnNextReg.Enabled = (last == false);
List<string> currentData = Index_LoadReg((int)currentReg); List<string> currentData = Index_LoadReg((int)currentReg);
pnlData.Visible = false;
pnlData.Controls.Clear();
int y = 0; int y = 0;
const int TexboxPadding = 5; const int TexboxPadding = 5;
const int Padding = 9; const int Padding = 9;
@@ -171,7 +172,9 @@ namespace CsvView
pnlData.Controls.Add(txtValue); pnlData.Controls.Add(txtValue);
y += txtValue.Height + Padding; y += txtValue.Height + Padding;
} }
pnlData.Visible = true;
pnlData.ResumeDrawing();
_rendering = false;
} }
private TextBox RenderValue(string value, int y, int TexboxPadding, int Padding, int LineHeight) private TextBox RenderValue(string value, int y, int TexboxPadding, int Padding, int LineHeight)
@@ -199,12 +202,20 @@ namespace CsvView
private void RenderRegClean() private void RenderRegClean()
{ {
if (_rendering) { return; }
_rendering = true;
pnlData.SuspendDrawing();
pnlData.Controls.Clear();
pnlReg.Enabled = false; pnlReg.Enabled = false;
txtCurrentReg.Text = string.Empty; txtCurrentReg.Text = string.Empty;
txtTotalRegs.Text = string.Empty; txtTotalRegs.Text = string.Empty;
pnlData.Controls.Clear();
}
pnlData.ResumeDrawing();
_rendering = false;
}
private void btnFirstReg_Click(object sender, EventArgs e) private void btnFirstReg_Click(object sender, EventArgs e)
{ {
RenderReg(0); RenderReg(0);