I have the following code that I am trying to figure out but I am completely stumped. I am adding the progressbar into the listview, but I really don't know how to access each progressbar to update the progress values.
public ProgressBar LvAddProgB(ListView LV, int LVII, int LVColI, string lvName)
{
Rectangle SizeR = default(Rectangle);
ProgressBar ProgBar = new ProgressBar();
SizeR = LV.Items[LVII].Bounds;
SizeR.Width = LV.Columns[LVColI].Width;
if (LVColI > 0)
{
SizeR.X = SizeR.X + LV.Columns[LVColI - 1].Width;
}
ProgBar.Parent = LV;
ProgBar.Name = lvName;
ProgBar.SetBounds(SizeR.X, SizeR.Y, SizeR.Width, SizeR.Height);
ProgBar.Visible = true;
ProgBar.Maximum = 1000;
ProgBar.Step = 1;
return ProgBar;
}
private void button1_Click(object sender, EventArgs e)
{
for (int x = 0; x < 3; ++x)
{
ListViewItem item = new ListViewItem();
item.Text = "d.Name";
item.SubItems.Add(" ");
listView1.Items.Add(item);
LvAddProgB(listView1, x, 1, "Lview" + x.ToString());
}
}
If you use a key of some sort, you can fish it back out of the Controls collection to update. Since each is displayed as if it was part of the ListView, it seems like there is some sort of linkage between the two. A key will also provide a way to link the item and related ProgressBar.
Assuming your ListView is Details view, just add a subitem at the end, without a related ColumnHeader. The data will not show, but will still be related to the Item. Use the same text as the ProgressBar name and it is easy to find.
My ListView has 3 columns: {Item, Name, Completion}, but the code will add a 4th subitem to store the key:
private void AddLVItem(string key, string name, int value)
{
ListViewItem lvi = new ListViewItem();
ProgressBar pb = new ProgressBar();
lvi.SubItems[0].Text = name;
lvi.SubItems.Add(value.ToString());
lvi.SubItems.Add("");
lvi.SubItems.Add(key); // LV has 3 cols; this wont show
lv.Items.Add(lvi);
Rectangle r = lvi.SubItems[2].Bounds;
pb.SetBounds(r.X, r.Y, r.Width, r.Height);
pb.Minimum = 1;
pb.Maximum = 10;
pb.Value = value;
pb.Name = key; // use the key as the name
lv.Controls.Add(pb);
}
Then, a method to update the Value and Progress bar for a given key:
private void UpdateItemValue(string key, int value)
{
ListViewItem lvi;
ProgressBar pb;
// find the LVI based on the "key" in
lvi = lv.Items.Cast<ListViewItem>().FirstOrDefault(q => q.SubItems[3].Text == key);
if (lvi != null)
lvi.SubItems[1].Text = value.ToString();
pb = lv.Controls.OfType<ProgressBar>().FirstOrDefault(q => q.Name == key);
if (pb != null)
pb.Value = value;
}
usage:
// add some data
AddLVItem("A", "Ziggy", 1);
AddLVItem("B", "Zacky", 1);
AddLVItem("C", "Zoey", 1);
AddLVItem("D", "Zeke", 1);
// update the displayed value and progressbar using the key:
UpdateItemValue("A", 6);
UpdateItemValue("B", 5);
UpdateItemValue("C", 8);
UpdateItemValue("D", 2);
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.listView1 = new System.Windows.Forms.ListView();
this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.columnHeader2 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.columnHeader3 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(12, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// listView1
//
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader1,
this.columnHeader2,
this.columnHeader3});
this.listView1.GridLines = true;
this.listView1.Location = new System.Drawing.Point(12, 64);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(504, 164);
this.listView1.TabIndex = 1;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.Details;
//
// columnHeader1
//
this.columnHeader1.Width = 99;
//
// columnHeader2
//
this.columnHeader2.Width = 117;
//
// columnHeader3
//
this.columnHeader3.Width = 117;
//
// Form1
//
this.ClientSize = new System.Drawing.Size(528, 261);
this.Controls.Add(this.listView1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.VScrollBar vScrollBar1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
private System.Windows.Forms.ColumnHeader columnHeader3;
public ProgressBar LvAddProgB(ListView LV, int X, int Y, string lvName)
{
ProgressBar ProgBar = new ProgressBar();
ProgBar.Parent = LV;
ProgBar.Name = lvName;
ProgBar.Location = new Point(X, Y);
ProgBar.Visible = true;
ProgBar.Maximum = 1000;
ProgBar.Step = 1;
return ProgBar;
}
private void button1_Click(object sender, EventArgs e)
{
for (int x = 0; x < 3; ++x)
{
ListViewItem item = new ListViewItem();
item.Text = "d.Name";
item.SubItems.Add(" ");
listView1.Items.Add(item);
listView1.Controls.Add(LvAddProgB(listView1, item.Position.X + item.Bounds.Width, item.Position.Y, "Lview" + x.ToString()));
}
}
Related
I have a tabControl in a Winform application. The user is able to change settings that will impact each tab visually, and I need to get each tabs as images.
I'm trying to understand why/how some controls are updating even if not visible and some other don't.
Is there a way to force an existing UserControl to update/redraw even if not visible so that Control.DrawToBitmap(...) gets a valid image?
I created an example with a Chart and a FlowLayoutPanel to explain what I mean and to make sure I could reproduce the issue I have.
In the example below:
The image created from the FlowLayoutPanel is only correct if it was updated and visible before DrawToBitmap is called.
The image from the Chart is alway up to date even if updated when not visible.
Questions:
How does it work with one control and not the other?
How could I ensure the FlowLayoutPanel has the same behavior as the Chart?
(EDIT) I have tried a few things without success:
Suspend/Resume layout when updating
call Refresh / Update / Invalidate on the layout that do not update as expected
Here is a gif showing that the chart get updated even when not visible but not the FlowLayoutPanel:
Here is the code of this example:
using LiveChartsCore.SkiaSharpView;
using System.Collections.ObjectModel;
namespace WinFormsDrawToBitmapTest
{
public partial class Form1 : Form
{
int legendCount = 1;
ViewModel viewModel;
public Form1()
{
InitializeComponent();
viewModel = new ViewModel();
cartesianChart1.Series = viewModel.Series;
updateLegend(this.flowLayoutPanel1);
}
private async void button1_Click(object sender, EventArgs e)
{
Bitmap bitmap = await Task.Run(() => getImg3());
this.pictureBox1.Image = bitmap;
this.pictureBox1.Invoke((MethodInvoker)delegate
{ this.pictureBox1.Image = bitmap; });
}
private Bitmap getImg3()
{
Bitmap bitmap = new Bitmap(this.cartesianChart1.Width, this.cartesianChart1.Height + this.flowLayoutPanel1.Height);
Bitmap chart = new Bitmap(this.cartesianChart1.Width, this.cartesianChart1.Height);
Bitmap legend = new Bitmap(this.flowLayoutPanel1.Width, this.flowLayoutPanel1.Height);
this.cartesianChart1.Invoke((MethodInvoker)delegate
{
this.cartesianChart1.DrawToBitmap(chart, new Rectangle(new Point(0, 0), this.cartesianChart1.Size));
});
this.flowLayoutPanel1.Invoke((MethodInvoker)delegate
{
this.flowLayoutPanel1.DrawToBitmap(legend, new Rectangle(new Point(0, 0), this.flowLayoutPanel1.Size));
});
using (Graphics g = Graphics.FromImage(bitmap))
{
g.DrawImage(legend, 0, 0);
g.DrawImage(chart, 0, legend.Height);
}
return bitmap;
}
private void button2_Click(object sender, EventArgs e)
{
Random r = new Random();
viewModel.Data.Add(r.Next(0, 10));
legendCount++;
updateLegend(this.flowLayoutPanel1);
}
private void updateLegend(FlowLayoutPanel flow)
{
flow.Controls.Clear();
Color[] colorList = new Color[]
{
Color.FromArgb(15, 1, 215),
Color.FromArgb(255, 0, 0),
Color.FromArgb(0, 176, 80),
Color.FromArgb(112, 48, 160)
};
for (int i = 0; i < legendCount; i++)
{
//flow.Controls.Add(new LegendLabel($"Label {i}", colorList[i % colorList.Length]));
Button btn = new Button();
btn.Name = $"Button {i}";
btn.TabIndex = 0;
btn.Text = $"Button {i}";
flow.Controls.Add(btn);
}
}
}
public partial class ViewModel
{
public ObservableCollection<double> Data { get; set; }
public List<LineSeries<double>> Series { get; set; }
public ViewModel()
{
Data = new ObservableCollection<double>();
Data.Add(1); Data.Add(2); Data.Add(5); Data.Add(4);
Series = new List<LineSeries<double>>();
Series.Add(new LineSeries<double> { Values = Data, Fill = null });
}
}
}
With the associated .Designer.cs in case it helps
namespace WinFormsDrawToBitmapTest
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.panel2 = new System.Windows.Forms.Panel();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.cartesianChart1 = new LiveChartsCore.SkiaSharpView.WinForms.CartesianChart();
this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
this.tabPage2 = new System.Windows.Forms.TabPage();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.panel2.SuspendLayout();
this.tabControl1.SuspendLayout();
this.tabPage1.SuspendLayout();
this.tabPage2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// panel2
//
this.panel2.Controls.Add(this.button2);
this.panel2.Controls.Add(this.button1);
this.panel2.Dock = System.Windows.Forms.DockStyle.Top;
this.panel2.Location = new System.Drawing.Point(0, 0);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(1042, 24);
this.panel2.TabIndex = 1;
//
// button2
//
this.button2.Location = new System.Drawing.Point(167, 0);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(86, 23);
this.button2.TabIndex = 1;
this.button2.Text = "Change Tab1";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(0, 0);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(161, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Get Tab1 as Image in Tab 2";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// tabControl1
//
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Controls.Add(this.tabPage2);
this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tabControl1.Location = new System.Drawing.Point(0, 24);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(1042, 585);
this.tabControl1.TabIndex = 2;
//
// tabPage1
//
this.tabPage1.Controls.Add(this.cartesianChart1);
this.tabPage1.Controls.Add(this.flowLayoutPanel1);
this.tabPage1.Location = new System.Drawing.Point(4, 24);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
this.tabPage1.Size = new System.Drawing.Size(1034, 557);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "tabPage1";
this.tabPage1.UseVisualStyleBackColor = true;
//
// cartesianChart1
//
this.cartesianChart1.Location = new System.Drawing.Point(8, 116);
this.cartesianChart1.Name = "cartesianChart1";
this.cartesianChart1.Size = new System.Drawing.Size(1018, 435);
this.cartesianChart1.TabIndex = 4;
//
// flowLayoutPanel1
//
this.flowLayoutPanel1.Location = new System.Drawing.Point(8, 6);
this.flowLayoutPanel1.Name = "flowLayoutPanel1";
this.flowLayoutPanel1.Size = new System.Drawing.Size(1018, 100);
this.flowLayoutPanel1.TabIndex = 3;
//
// tabPage2
//
this.tabPage2.Controls.Add(this.pictureBox1);
this.tabPage2.Location = new System.Drawing.Point(4, 24);
this.tabPage2.Name = "tabPage2";
this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
this.tabPage2.Size = new System.Drawing.Size(1034, 557);
this.tabPage2.TabIndex = 1;
this.tabPage2.Text = "tabPage2";
this.tabPage2.UseVisualStyleBackColor = true;
//
// pictureBox1
//
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox1.Location = new System.Drawing.Point(3, 3);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(1028, 551);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1042, 609);
this.Controls.Add(this.tabControl1);
this.Controls.Add(this.panel2);
this.Name = "Form1";
this.Text = "Form1";
this.panel2.ResumeLayout(false);
this.tabControl1.ResumeLayout(false);
this.tabPage1.ResumeLayout(false);
this.tabPage2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private Panel panel2;
private Button button1;
private TabControl tabControl1;
private TabPage tabPage1;
private TabPage tabPage2;
private PictureBox pictureBox1;
private FlowLayoutPanel flowLayoutPanel1;
private LiveChartsCore.SkiaSharpView.WinForms.CartesianChart cartesianChart1;
private Button button2;
}
}
Thank you for your help with this.
I am using teechart cursor tool and using its "change" event, which gets call each and every time when we move cursor of chart, but I want a event/function which gets call when we release cursor after moving.
this.cursorTool1.Change += new Steema.TeeChart.Tools.CursorChangeEventHandler(this.cursorTool1_Change);
Sample code :-
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace Steema.TeeChart.Samples
{
public class Tool_Cursor : Samples.BaseForm
{
private Steema.TeeChart.Styles.Line lineSeries1;
private Steema.TeeChart.Styles.Line lineSeries2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.CheckBox checkBox2;
private System.Windows.Forms.Button button1;
private Steema.TeeChart.Axis axis1;
private Steema.TeeChart.Tools.CursorTool cursorTool1;
private Steema.TeeChart.Tools.CursorTool cursorTool2;
private System.ComponentModel.IContainer components = null;
private bool flag;
public Tool_Cursor()
{
// This call is required by the Windows Form Designer.
InitializeComponent();
this.lineSeries1.FillSampleValues(100);
this.lineSeries2.FillSampleValues(100);
this.tChart1.Axes.Left.AutomaticMinimum = false;
this.tChart1.Axes.Left.Minimum = 0.0;
this.lineSeries2.CustomVertAxis = this.axis1;
this.tChart1.BeforeDrawSeries += new PaintChartEventHandler(tChart1_BeforeDrawSeries);
flag = true;
this.cursorTool1.Pen.Color = Color.Navy;
this.cursorTool1.Pen.Style = System.Drawing.Drawing2D.DashStyle.Dash;
this.cursorTool1.Pen.Width = 2;
this.cursorTool2.Pen.Color = Color.Plum;
this.cursorTool2.Pen.Style = System.Drawing.Drawing2D.DashStyle.Dot;
this.cursorTool2.Pen.Width = 2;
}
void tChart1_BeforeDrawSeries(object sender, Steema.TeeChart.Drawing.Graphics3D g)
{
if (flag)
{
// place upper series cursor in the middle
this.cursorTool1.XValue = 0.5 * (this.lineSeries1.XValues.Maximum + this.lineSeries1.XValues.Minimum);
flag = false;
}
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Tool_Cursor));
this.lineSeries1 = new Steema.TeeChart.Styles.Line();
this.lineSeries2 = new Steema.TeeChart.Styles.Line();
this.axis1 = new Steema.TeeChart.Axis(this.components);
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.checkBox2 = new System.Windows.Forms.CheckBox();
this.button1 = new System.Windows.Forms.Button();
this.cursorTool1 = new Steema.TeeChart.Tools.CursorTool();
this.cursorTool2 = new Steema.TeeChart.Tools.CursorTool();
this.panel1.SuspendLayout();
this.chartContainer.SuspendLayout();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Size = new System.Drawing.Size(406, 56);
this.textBox1.Text = "Cursor Tool is used to display vertical and / or horizontal lines on top of chart" +
"s. Cursors can be dragged by mouse or by code at runtime. They notify position c" +
"hanges with the OnChange event.";
//
// panel1
//
this.panel1.Controls.Add(this.button1);
this.panel1.Controls.Add(this.checkBox2);
this.panel1.Controls.Add(this.checkBox1);
this.panel1.Controls.Add(this.label2);
this.panel1.Controls.Add(this.label1);
this.panel1.Location = new System.Drawing.Point(0, 56);
this.panel1.Size = new System.Drawing.Size(406, 52);
//
// tChart1
//
//
//
//
this.tChart1.Aspect.View3D = false;
this.tChart1.Aspect.ZOffset = 0;
//
//
//
this.tChart1.Axes.Custom.Add(this.axis1);
//
//
//
this.tChart1.Axes.Left.EndPosition = 50;
//
//
//
//
//
//
//
//
//
this.tChart1.Axes.Left.Labels.Font.Brush.Color = System.Drawing.Color.Red;
this.tChart1.Axes.Left.Labels.Font.Size = 7;
this.tChart1.Axes.Left.Labels.Font.SizeFloat = 7F;
this.tChart1.Axes.Left.LogarithmicBase = 2;
//
//
//
this.tChart1.Header.Lines = new string[] {
"Cursor tool example"};
this.tChart1.Header.Visible = false;
//
//
//
//
//
//
this.tChart1.Panel.Brush.Color = System.Drawing.Color.FromArgb(((int)(((byte)(254)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));
this.tChart1.Series.Add(this.lineSeries1);
this.tChart1.Series.Add(this.lineSeries2);
this.tChart1.Size = new System.Drawing.Size(406, 155);
this.tChart1.Tools.Add(this.cursorTool1);
this.tChart1.Tools.Add(this.cursorTool2);
this.tChart1.AfterDraw += new Steema.TeeChart.PaintChartEventHandler(this.tChart1_AfterDraw);
//
// chartContainer
//
this.chartContainer.Location = new System.Drawing.Point(0, 108);
this.chartContainer.Size = new System.Drawing.Size(406, 155);
//
// lineSeries1
//
//
//
//
this.lineSeries1.Brush.Color = System.Drawing.Color.FromArgb(((int)(((byte)(68)))), ((int)(((byte)(102)))), ((int)(((byte)(163)))));
this.lineSeries1.Color = System.Drawing.Color.FromArgb(((int)(((byte)(68)))), ((int)(((byte)(102)))), ((int)(((byte)(163)))));
this.lineSeries1.ColorEach = false;
//
//
//
this.lineSeries1.LinePen.Color = System.Drawing.Color.FromArgb(((int)(((byte)(41)))), ((int)(((byte)(61)))), ((int)(((byte)(98)))));
//
//
//
//
//
//
this.lineSeries1.Marks.Callout.ArrowHead = Steema.TeeChart.Styles.ArrowHeadStyles.None;
this.lineSeries1.Marks.Callout.ArrowHeadSize = 8;
//
//
//
this.lineSeries1.Marks.Callout.Brush.Color = System.Drawing.Color.Black;
this.lineSeries1.Marks.Callout.Distance = 0;
this.lineSeries1.Marks.Callout.Draw3D = false;
this.lineSeries1.Marks.Callout.Length = 10;
this.lineSeries1.Marks.Callout.Style = Steema.TeeChart.Styles.PointerStyles.Rectangle;
this.lineSeries1.Marks.Callout.Visible = false;
//
//
//
this.lineSeries1.Pointer.Style = Steema.TeeChart.Styles.PointerStyles.Rectangle;
this.lineSeries1.Title = "line1";
//
//
//
this.lineSeries1.XValues.DataMember = "X";
this.lineSeries1.XValues.Order = Steema.TeeChart.Styles.ValueListOrder.Ascending;
//
//
//
this.lineSeries1.YValues.DataMember = "Y";
//
// lineSeries2
//
//
//
//
this.lineSeries2.Brush.Color = System.Drawing.Color.FromArgb(((int)(((byte)(243)))), ((int)(((byte)(156)))), ((int)(((byte)(53)))));
this.lineSeries2.Color = System.Drawing.Color.FromArgb(((int)(((byte)(243)))), ((int)(((byte)(156)))), ((int)(((byte)(53)))));
this.lineSeries2.ColorEach = false;
this.lineSeries2.CustomVertAxis = this.axis1;
//
//
//
this.lineSeries2.LinePen.Color = System.Drawing.Color.FromArgb(((int)(((byte)(146)))), ((int)(((byte)(94)))), ((int)(((byte)(32)))));
//
//
//
//
//
//
this.lineSeries2.Marks.Callout.ArrowHead = Steema.TeeChart.Styles.ArrowHeadStyles.None;
this.lineSeries2.Marks.Callout.ArrowHeadSize = 8;
//
//
//
this.lineSeries2.Marks.Callout.Brush.Color = System.Drawing.Color.Black;
this.lineSeries2.Marks.Callout.Distance = 0;
this.lineSeries2.Marks.Callout.Draw3D = false;
this.lineSeries2.Marks.Callout.Length = 10;
this.lineSeries2.Marks.Callout.Style = Steema.TeeChart.Styles.PointerStyles.Rectangle;
this.lineSeries2.Marks.Callout.Visible = false;
//
//
//
this.lineSeries2.Pointer.Style = Steema.TeeChart.Styles.PointerStyles.Rectangle;
this.lineSeries2.Title = "line2";
this.lineSeries2.VertAxis = Steema.TeeChart.Styles.VerticalAxis.Custom;
//
//
//
this.lineSeries2.XValues.DataMember = "X";
this.lineSeries2.XValues.Order = Steema.TeeChart.Styles.ValueListOrder.Ascending;
//
//
//
this.lineSeries2.YValues.DataMember = "Y";
//
// axis1
//
this.axis1.Horizontal = false;
//
//
//
//
//
//
//
//
//
this.axis1.Labels.Font.Brush.Color = System.Drawing.Color.Green;
this.axis1.Labels.Font.Size = 7;
this.axis1.Labels.Font.SizeFloat = 7F;
this.axis1.OtherSide = false;
this.axis1.StartPosition = 50;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(5, 7);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(0, 13);
this.label1.TabIndex = 0;
this.label1.UseMnemonic = false;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(5, 26);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(0, 13);
this.label2.TabIndex = 1;
this.label2.UseMnemonic = false;
//
// checkBox1
//
this.checkBox1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.checkBox1.Location = new System.Drawing.Point(107, 12);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(53, 23);
this.checkBox1.TabIndex = 2;
this.checkBox1.Text = "&Snap";
this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
//
// checkBox2
//
this.checkBox2.Checked = true;
this.checkBox2.CheckState = System.Windows.Forms.CheckState.Checked;
this.checkBox2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.checkBox2.Location = new System.Drawing.Point(167, 12);
this.checkBox2.Name = "checkBox2";
this.checkBox2.Size = new System.Drawing.Size(72, 23);
this.checkBox2.TabIndex = 3;
this.checkBox2.Text = "&Active";
this.checkBox2.CheckedChanged += new System.EventHandler(this.checkBox2_CheckedChanged);
//
// button1
//
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Location = new System.Drawing.Point(247, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 4;
this.button1.Text = "&Edit...";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// cursorTool1
//
this.cursorTool1.Series = this.lineSeries1;
this.cursorTool1.Style = Steema.TeeChart.Tools.CursorToolStyles.Vertical;
this.cursorTool1.Change += new Steema.TeeChart.Tools.CursorChangeEventHandler(this.cursorTool1_Change);
//
// cursorTool2
//
this.cursorTool2.FollowMouse = true;
this.cursorTool2.Series = this.lineSeries2;
this.cursorTool2.Change += new Steema.TeeChart.Tools.CursorChangeEventHandler(this.cursorTool2_Change);
//
// Tool_Cursor
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(406, 263);
this.Name = "Tool_Cursor";
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.chartContainer.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private void checkBox1_CheckedChanged(object sender, System.EventArgs e)
{
this.cursorTool1.Snap = this.checkBox1.Checked;
this.cursorTool1.Style = Steema.TeeChart.Tools.CursorToolStyles.Vertical;
}
private void checkBox2_CheckedChanged(object sender, System.EventArgs e)
{
this.cursorTool1.Active = this.checkBox2.Checked;
}
private void tChart1_AfterDraw(object sender, Steema.TeeChart.Drawing.Graphics3D g)
{
Steema.TeeChart.Drawing.Graphics3D gr = this.tChart1.Graphics3D;
// TODO : add custom horizontal line where the axes meet
}
private void cursorTool1_Change(object sender, Steema.TeeChart.Tools.CursorChangeEventArgs e)
{
// show the cursor values...
this.label1.Text = "X="+e.XValue.ToString("#.00");
}
private void cursorTool2_Change(object sender, Steema.TeeChart.Tools.CursorChangeEventArgs e)
{
// show the cursor values...
this.label2.Text = "X="+e.XValue.ToString("#.00")+"; Y="+e.YValue.ToString("#.00");
}
private void button1_Click(object sender, System.EventArgs e)
{
TeeChart.Editors.Tools.ToolsEditor.ShowEditor(this.cursorTool1);
}
}
}
You could try adding in a MouseUp event and checked if the CursorTool was clicked, e.g.
this.cursorTool1.Pen.Width = 2;
tChart1.MouseUp += TChart1_MouseUp;
this.cursorTool2.Pen.Color = Color.Plum;
this.cursorTool2.Pen.Style = System.Drawing.Drawing2D.DashStyle.Dot;
this.cursorTool2.Pen.Width = 2;
}
private void TChart1_MouseUp(object sender, MouseEventArgs e)
{
var clicked = cursorTool1.Clicked(e.X, e.Y);
if(clicked == Tools.CursorClicked.Vertical)
{
MessageBox.Show("The Cursed has been released!");
}
}
I have a checkedListBox in a TabControl
What I want is to create a label and NumericUpDown dynamically, when User check an item of checkedListBox it will show the new label and NumericUpDown
Then , when it Unchecked this item ,The numericUpDown will be clear (empty).
Conclusion: As many checked items , as many w've create labels and NumericUpDowns.
Please, how will I do that ??
For each checkbox item in your checkedListBox in properties switch to events and create subscriber checkBoxName_CheckStateChanged for event CheckStateChanged.
The code in the sucriber can be like this:
private void checkBox1_CheckStateChanged(object sender, EventArgs e)
{
var source = sender as CheckBox;
if (source.Checked == true)
{
this.numericUpDown1.Text = "TextWhenChecked";
this.labelAtTheNumericUpDown.Text = "TextWhenChecked";
}
else
{
this.numericUpDown1.Text = "TextWhenUnchecked";
this.label1.Text = "TextWhenUnchecked";
}
}
You fill the strings as you want. These are only examples.
To have only checkBox checked at a time look at here: https://stackoverflow.com/a/24693858/6650581.
What you need to do is creating Label and NumericUpDown manually and show it by adding to Controls collection. A TableLayoutPanel can help you arranging controls without setting Size and calculate Location manually.
Here is an example:
public class MainForm : Form
{
private CheckedListBox checkedListBox;
private TableLayoutPanel tableLayoutPanel;
public MainForm()
{
InitializeComponent();
//Fill checkedListBox and create controls
for( int i = 0; i <= 5; i++ )
{
checkedListBox.Items.Add( i.ToString() );
Label lbl = new Label()
{
Name = "lbl" + i,
Text = "Label " + i,
Visible = false
};
NumericUpDown num = new NumericUpDown()
{
Name = "num" + i,
Value = i,
Visible = false
};
tableLayoutPanel.Controls.Add( lbl, 0, i );
tableLayoutPanel.Controls.Add( num, 1, i );
}
}
private void checkedListBox_ItemCheck( object sender, ItemCheckEventArgs e )
{
if( e.NewValue == CheckState.Checked )
{
tableLayoutPanel.Controls["lbl" + e.Index].Visible = true;
tableLayoutPanel.Controls["num" + e.Index].Visible = true;
}
else
{
tableLayoutPanel.Controls["lbl" + e.Index].Visible = false;
((NumericUpDown)tableLayoutPanel.Controls["num" + e.Index]).Value = 0M;
}
}
private void InitializeComponent()
{
this.checkedListBox = new System.Windows.Forms.CheckedListBox();
this.tableLayoutPanel = new System.Windows.Forms.TableLayoutPanel();
this.SuspendLayout();
//
// checkedListBox
//
this.checkedListBox.Location = new System.Drawing.Point(8, 8);
this.checkedListBox.Name = "checkedListBox";
this.checkedListBox.Size = new System.Drawing.Size(200, 100);
this.checkedListBox.TabIndex = 1;
this.checkedListBox.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.checkedListBox_ItemCheck);
//
// tableLayoutPanel
//
this.tableLayoutPanel.AutoScroll = true;
this.tableLayoutPanel.ColumnCount = 2;
this.tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel.Location = new System.Drawing.Point(8, 112);
this.tableLayoutPanel.Name = "tableLayoutPanel";
this.tableLayoutPanel.Size = new System.Drawing.Size(200, 100);
this.tableLayoutPanel.TabIndex = 2;
//
// MainForm
//
this.ClientSize = new System.Drawing.Size(223, 227);
this.Controls.Add(this.tableLayoutPanel);
this.Controls.Add(this.checkedListBox);
this.Name = "MainForm";
this.ResumeLayout(false);
}
}
I have a window, which shows always smaller than in the designer. No matter how big it is in the designer and how big numbers I put in size in the properties, it always shows small. I tried to put maximum and minimum size, tried with autosize, autovalidate, autoscale options. Despite that, it was always small and the same size. I solved it by putting form.width and form.height during initialization. But I wonder what happened and how to prevent it in the future. It happened second time for me, and I do not know why. Any tips what to check more?
c# designer
namespace WindowsFormsApplication3
{
partial class Form1
{
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.newToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.saveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.label7 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.pictureBox2 = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.menuStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
this.splitContainer1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.button1.Location = new System.Drawing.Point(77, 187);
this.button1.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(200, 55);
this.button1.TabIndex = 0;
this.button1.Text = "Segment";
this.button1.UseVisualStyleBackColor = false;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.button2.Location = new System.Drawing.Point(77, 309);
this.button2.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(200, 55);
this.button2.TabIndex = 1;
this.button2.Text = "Triangle";
this.button2.UseVisualStyleBackColor = false;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.button3.Location = new System.Drawing.Point(77, 433);
this.button3.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(200, 55);
this.button3.TabIndex = 2;
this.button3.Text = "Quadrangle";
this.button3.UseVisualStyleBackColor = false;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.button4.Location = new System.Drawing.Point(77, 559);
this.button4.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(200, 55);
this.button4.TabIndex = 3;
this.button4.Text = "Hexagon";
this.button4.UseVisualStyleBackColor = false;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// pictureBox1
//
this.pictureBox1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.pictureBox1.Location = new System.Drawing.Point(101, 1027);
this.pictureBox1.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(176, 119);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(320, 193);
this.label1.Margin = new System.Windows.Forms.Padding(8, 0, 8, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(0, 32);
this.label1.TabIndex = 5;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(320, 315);
this.label2.Margin = new System.Windows.Forms.Padding(8, 0, 8, 0);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(0, 32);
this.label2.TabIndex = 6;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(320, 439);
this.label3.Margin = new System.Windows.Forms.Padding(8, 0, 8, 0);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(0, 32);
this.label3.TabIndex = 7;
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(320, 565);
this.label4.Margin = new System.Windows.Forms.Padding(8, 0, 8, 0);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(0, 32);
this.label4.TabIndex = 8;
//
// menuStrip1
//
this.menuStrip1.ImageScalingSize = new System.Drawing.Size(40, 40);
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.fileToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(2419, 49);
this.menuStrip1.TabIndex = 10;
this.menuStrip1.Text = "menuStrip1";
//
// fileToolStripMenuItem
//
this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.newToolStripMenuItem,
this.saveToolStripMenuItem});
this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
this.fileToolStripMenuItem.Size = new System.Drawing.Size(75, 45);
this.fileToolStripMenuItem.Text = "File";
//
// newToolStripMenuItem
//
this.newToolStripMenuItem.Name = "newToolStripMenuItem";
this.newToolStripMenuItem.Size = new System.Drawing.Size(194, 46);
this.newToolStripMenuItem.Text = "New";
this.newToolStripMenuItem.Click += new System.EventHandler(this.newToolStripMenuItem_Click);
//
// saveToolStripMenuItem
//
this.saveToolStripMenuItem.Name = "saveToolStripMenuItem";
this.saveToolStripMenuItem.Size = new System.Drawing.Size(194, 46);
this.saveToolStripMenuItem.Text = "Save";
this.saveToolStripMenuItem.Click += new System.EventHandler(this.saveToolStripMenuItem_Click);
//
// splitContainer1
//
this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.Location = new System.Drawing.Point(0, 49);
this.splitContainer1.Name = "splitContainer1";
//
// splitContainer1.Panel1
//
this.splitContainer1.Panel1.Controls.Add(this.label7);
this.splitContainer1.Panel1.Controls.Add(this.label6);
this.splitContainer1.Panel1.Controls.Add(this.button4);
this.splitContainer1.Panel1.Controls.Add(this.button1);
this.splitContainer1.Panel1.Controls.Add(this.label4);
this.splitContainer1.Panel1.Controls.Add(this.button2);
this.splitContainer1.Panel1.Controls.Add(this.label3);
this.splitContainer1.Panel1.Controls.Add(this.button3);
this.splitContainer1.Panel1.Controls.Add(this.label2);
this.splitContainer1.Panel1.Controls.Add(this.pictureBox1);
this.splitContainer1.Panel1.Controls.Add(this.label1);
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.Controls.Add(this.pictureBox2);
this.splitContainer1.Size = new System.Drawing.Size(2419, 1402);
this.splitContainer1.SplitterDistance = 621;
this.splitContainer1.TabIndex = 11;
//
// label7
//
this.label7.AutoSize = true;
this.label7.Location = new System.Drawing.Point(95, 927);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(309, 32);
this.label7.TabIndex = 10;
this.label7.Text = "A color for new shapes:";
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(71, 98);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(120, 32);
this.label6.TabIndex = 9;
this.label6.Text = "Shapes:";
//
// pictureBox2
//
this.pictureBox2.BackColor = System.Drawing.Color.White;
this.pictureBox2.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox2.Location = new System.Drawing.Point(0, 0);
this.pictureBox2.Name = "pictureBox2";
this.pictureBox2.Size = new System.Drawing.Size(1794, 1402);
this.pictureBox2.TabIndex = 9;
this.pictureBox2.TabStop = false;
this.pictureBox2.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.pictureBox2_MouseDoubleClick);
this.pictureBox2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox2_MouseDown);
this.pictureBox2.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox2_MouseMove);
this.pictureBox2.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox2_MouseUp);
this.pictureBox2.Resize += new System.EventHandler(this.pictureBox2_Resize);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(16F, 31F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoValidate = System.Windows.Forms.AutoValidate.EnablePreventFocusChange;
this.ClientSize = new System.Drawing.Size(2419, 1451);
this.Controls.Add(this.splitContainer1);
this.Controls.Add(this.menuStrip1);
this.MainMenuStrip = this.menuStrip1;
this.Margin = new System.Windows.Forms.Padding(8, 7, 8, 7);
this.Name = "Form1";
this.Text = "Simple Paint";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.splitContainer1.Panel1.ResumeLayout(false);
this.splitContainer1.Panel1.PerformLayout();
this.splitContainer1.Panel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
this.splitContainer1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem newToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem saveToolStripMenuItem;
private System.Windows.Forms.SplitContainer splitContainer1;
private System.Windows.Forms.PictureBox pictureBox2;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label7;
}
}
Form.cs deleted one function because i reached the limit.
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
private bool isStrip = false;
private string NameString;
private string StorePath;
private int VertexCatch = -1;
private int button = -1;
private int checker = 0;
private int dblclck = -1;
private Bitmap graphic;
private Graphics g;
private Name NameForm;
private Point[] points;
private Pen p = new Pen(Color.Black);
private ColorDialog colorDialog1 = new ColorDialog();
private List<Label> LabelList = new List<Label>();
private List<Button> ButtonList = new List<Button>();
private List<Point[]> PointsList = new List<Point[]>();
private List<Brush> BrushList = new List<Brush>();
public Form1()
{
InitializeComponent();
graphic = new Bitmap(pictureBox2.Width, pictureBox2.Height);
g = Graphics.FromImage(graphic);
LabelList.Add(label1);
LabelList.Add(label2);
LabelList.Add(label3);
LabelList.Add(label4);
ButtonList.Add(button1);
ButtonList.Add(button2);
ButtonList.Add(button3);
ButtonList.Add(button4);
this.Width = 1200;
this.Height = 600;
}
private void DrawFullList()
{
graphic = new Bitmap(pictureBox2.Width, pictureBox2.Height + 100);
g = Graphics.FromImage(graphic);
pictureBox2.Image = graphic;
for (int i = 0; i < PointsList.Count; i++)
{
bool yellowframe = false;
if (i == dblclck)
yellowframe = true;
Draw(BrushList[i], PointsList[i], yellowframe);
}
}
private void Draw(Brush brush, Point[] points, bool yellowframe)
{
Pen PathPen = new Pen(brush);
if (yellowframe)
PathPen = new Pen(Color.Yellow);
PathPen.Width = 3;
g.DrawPolygon(PathPen, points);
g.FillPolygon(brush, points);
pictureBox2.Image = graphic;
}
private int showNumberForButton(int button)
{
if (button == -1)
return 0;
else if (button > 2)
return 6;
else
return button + 2;
}
private void ButtonClick(int buttonint)
{
button = buttonint;
LabelList[button].Text = "Choose " + (showNumberForButton(button)) + " more points\nto construct shape"; ;
if(showNumberForButton(button) == 2)
points = new Point[4];
else
points = new Point[showNumberForButton(button)];
for (int i = 0; i < 4; i++)
ButtonList[i].Enabled = false;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
DialogResult result = colorDialog1.ShowDialog();
if (result == DialogResult.OK)
{
p.Color = colorDialog1.Color;
pictureBox1.BackColor = colorDialog1.Color;
}
}
private void button1_Click(object sender, EventArgs e)
{
ButtonClick(0);
}
private void button2_Click(object sender, EventArgs e)
{
ButtonClick(1);
}
private void button3_Click(object sender, EventArgs e)
{
ButtonClick(2);
}
private void button4_Click(object sender, EventArgs e)
{
ButtonClick(3);
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
DialogResult result = dialog.ShowDialog();
if (result == DialogResult.OK)
{
StorePath = dialog.SelectedPath;
NameForm = new Name();
NameForm.FormClosed += new FormClosedEventHandler(ChildFormClosed);
NameForm.ShowDialog();
}
else
MessageBox.Show("You canceled the saving prodedure.\nThe image is not saved.", "Image saving canceled", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void ChildFormClosed(object sender, FormClosedEventArgs e)
{
NameString = NameForm.PassName();
if (NameString != null && StorePath != null)
graphic.Save(StorePath + "\\" + NameString + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
else
MessageBox.Show("You canceled the saving prodedure.\nThe image is not saved.", "Image saving canceled", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
{
for (int i = 0; i < PointsList.Count; i++)
{
if (IsPointInPolygon4(PointsList[i], pictureBox2.PointToClient(Cursor.Position)))
{
dblclck = -1;
DrawFullList();
}
}
if (checker < showNumberForButton(button))
{
points[checker++] = pictureBox2.PointToClient(Cursor.Position);
if (showNumberForButton(button) == 2 && checker == 2)
{
points[2] = new Point(points[1].X + 3, points[1].Y + 3);
points[3] = new Point(points[0].X + 3, points[0].Y + 3);
}
if (checker == showNumberForButton(button))
{
Brush brush = new SolidBrush(colorDialog1.Color);
Draw(brush, points, false);
PointsList.Add(points);
BrushList.Add(brush);
checker = 0;
button = -1;
for (int i = 0; i < 4; i++)
{
ButtonList[i].Enabled = true;
LabelList[i].Text = string.Empty;
}
}
else
LabelList[button].Text = "Choose " + (showNumberForButton(button) - checker) + " more points\nto construct shape";
}
Point MouseCatch = pictureBox2.PointToClient(Cursor.Position);
for (int i = 0; i < PointsList.Count; i++)
{
if (IsPointInPolygon4(PointsList[i], MouseCatch))
{
for (int j = 0; j < PointsList[i].Length; j++)
{
if (MouseCatch.X >= PointsList[i][j].X - 20 && MouseCatch.X <= PointsList[i][j].X + 20 && MouseCatch.Y >= PointsList[i][j].Y - 20 && MouseCatch.Y <= PointsList[i][j].Y + 20)
{
VertexCatch = j;
Point[] temp = PointsList[i];
Brush tempB = BrushList[i];
PointsList.Remove(PointsList[i]);
BrushList.Remove(BrushList[i]);
PointsList.Add(temp);
BrushList.Add(tempB);
if (isItStrip(PointsList[PointsList.Count - 1]))
isStrip = true;
break;
}
}
}
}
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
graphic = new Bitmap(pictureBox2.Width, pictureBox2.Height);
PointsList.Clear();
BrushList.Clear();
checker = 0;
button = -1;
for (int i = 0; i < 4; i++)
{
ButtonList[i].Enabled = true;
LabelList[i].Text = string.Empty;
}
VertexCatch = -1;
g = Graphics.FromImage(graphic);
pictureBox2.Image = graphic;
}
private bool isItStrip(Point[] Points)
{
if (Points.Length == 4 && (points[2].X == (points[1].X + 3)) && (points[2].Y == points[1].Y + 3) && (points[3].X == (points[0].X + 3)) && (points[0].Y + 3 == points[3].Y))
return true;
return false;
}
private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
{
if (VertexCatch > -1)
{
Point k = pictureBox2.PointToClient(Cursor.Position);
PointsList[PointsList.Count - 1][VertexCatch] = k;
if (isStrip)
{
if (VertexCatch == 0)
PointsList[PointsList.Count - 1][VertexCatch + 3] = new Point (k.X + 3,k.Y + 3);
else if(VertexCatch == 1)
PointsList[PointsList.Count - 1][VertexCatch + 1] = new Point(k.X + 3, k.Y + 3);
else if (VertexCatch == 2)
PointsList[PointsList.Count - 1][VertexCatch - 1] = new Point(k.X - 3, k.Y + 3);
else if (VertexCatch == 3)
PointsList[PointsList.Count - 1][VertexCatch - 3] = new Point(k.X - 3, k.Y + 3);
}
DrawFullList();
}
}
private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
{
isStrip = false;
VertexCatch = -1;
}
private void pictureBox2_MouseDoubleClick(object sender, MouseEventArgs e)
{
Point MouseCatch = pictureBox2.PointToClient(Cursor.Position);
for (int i = PointsList.Count-1; i >=0 ; i--)
{
if (IsPointInPolygon4(PointsList[i], MouseCatch))
{
dblclck = i;
break;
}
}
DrawFullList();
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (dblclck != -1) {
switch (keyData) {
case Keys.Left:
for (int i = 0; i < PointsList[dblclck].Length; i++)
PointsList[dblclck][i].X -= 20;
break;
case Keys.Right:
for (int i = 0; i < PointsList[dblclck].Length; i++)
PointsList[dblclck][i].X += 20;
break;
case Keys.Up:
for (int i = 0; i < PointsList[dblclck].Length; i++)
PointsList[dblclck][i].Y -= 20;
break;
case Keys.Down:
for (int i = 0; i < PointsList[dblclck].Length; i++)
PointsList[dblclck][i].Y += 20;
break;
case Keys.Enter:
dblclck = -1;
break;
default:
return base.ProcessCmdKey(ref msg, keyData);
}
DrawFullList();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
private void pictureBox2_Resize(object sender, EventArgs e)
{
DrawFullList();
}
}
}
The form in the designer is set 2419 x 1451 pixels... if this is larger than the screen WinForms ignores the setting of that property (check this.ClientSize = new System.Drawing.Size(2419, 1451); in your Form1.designer.cs file), so the window gets a "default" size.
Set your form to a reasonable size within the designer (1200 x 600, for example, like you are doing on your constructor).
If you are setting it to 1200x600 but still is getting serialized as 2419x1451, then you may have hit one of the MANY problems the winform designer has with high-dpi displays. Set your AutoscaleMode to None and it should not try to adjust the properties to the "current" dpi upon serializing.
I'm looking for the fastest way to refresh an image (and draw some shapes) on a Winform. I'm using a PictureBox now, but I'm open for suggestions.
The way I'm getting the max FPS possible I took from here: "My last post on render loops (hopefully)"
This is a game loop pattern, but it can also be used to other purpose, like to display live images acquired from a camera.
I'm drawing a red circle in different positions.
As you can see, the FPS drops a lot when a Bitmap is loaded at the PictureBox. Especially in the SizeMode Zoom (the one I want to use).
My PC values for FPS:
Form size (start size)
Bitmap ON: 140 (Zoom); 1000 (Normal); 135 (Stretch); 880 (Auto); 1000 (Center)
Bitmap OFF: 3400 !!
Form size (after Zoom 100% click)
Bitmap ON: 40 (Zoom); 150 (Normal); 65 (Stretch); 150 (Auto); 150 (Center)
Bitmap OFF: 540 !!
Edit 1:
Well, this results was for a 1024x1024 image. But I realizy that the form Height was not getting the right value, I found out that it is because the form size is limited by the Screen resolution. So I've edited the code to load a 800x600 Bitmap. Now the Zoom 100% button works. As you can see, at zoom 100% the FPS is 350, but if you increase the size of the PictureBox one more pixel, the FPS drops to 35, about 10x slower ¬¬ !
The big question is: How can I improve the FPS, specially in zoom mode?
PS: I know WinForm is not the best way to go, I also know WPF. But for now I'm looking the solution using WinForm. Ok? The reason is that I have a big scary project using it. I'm planing to move to WPF, but I'm still learning it. :)
Here is the full code, so you can test it by yourself.
Form1.cs:
//.NET 4.0 Client Profile
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;
//You need to add System.Activities.Presentation.dll
using System.Activities.Presentation.Hosting;
namespace WindowsFormsApplication2
{
public enum DrawSource
{
None = 0,
PictureBox = 1,
Grapghics = 2,
}
public partial class Form1 : Form
{
private readonly Timer _updateFPSTimer;
private bool _isIdle;
private Point _location;
private double _updateCount;
private readonly Bitmap _backImage;
public Form1()
{
InitializeComponent();
this.DoubleBuffered = this.ckbDoubleBufferForm.Checked;
this.pictureBox1.DoubleBuffered = this.ckbDoubleBufferPictureBox.Checked;
_backImage = new Bitmap(800, 600, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(_backImage);
int penWidth = 10;
Pen pen = new Pen(Brushes.Blue, penWidth);
g.DrawRectangle(pen, new Rectangle(new Point(penWidth, penWidth), new Size(_backImage.Size.Width - 2 * penWidth, _backImage.Size.Height - 2 * penWidth)));
this.cbxSizeMode.DataSource = Enum.GetValues(typeof(PictureBoxSizeMode));
this.cbxSizeMode.SelectedItem = PictureBoxSizeMode.Zoom;
this.cbxBitmapDrawSource.DataSource = Enum.GetValues(typeof(DrawSource));
this.cbxBitmapDrawSource.SelectedItem = DrawSource.PictureBox;
this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
Application.Idle += Application_Idle;
_updateFPSTimer = new Timer();
_updateFPSTimer.Tick += UpdateFPSTimer_Tick;
_updateFPSTimer.Interval = Convert.ToInt32(1000.0 / 5); //Period in s = 1 s / 5 Hz
_updateFPSTimer.Start();
}
private void Application_Idle(object sender, EventArgs e)
{
while (this.ckbRun.Checked && IsAppStillIdle())
this.pictureBox1.Refresh();
}
void UpdateFPSTimer_Tick(object sender, EventArgs e)
{
GetFPS();
}
private void GetFPS()
{
double fps = _updateCount / (Convert.ToDouble(_updateFPSTimer.Interval) / 1000.0);
_updateCount = 0;
lblFps.Text = fps.ToString("0.00");
}
private void PictureBox1_Paint(object sender, PaintEventArgs e)
{
DrawCircle(e.Graphics);
}
private void DrawCircle(Graphics g)
{
if (this.pictureBox1.Image == null && ((DrawSource)this.cbxBitmapDrawSource.SelectedItem == DrawSource.Grapghics))
g.DrawImage(_backImage, 0, 0, g.ClipBounds.Width, g.ClipBounds.Height);
var rect = new Rectangle(_location, new Size(30, 30));
g.DrawEllipse(Pens.Red, rect);
_location.X += 1;
_location.Y += 1;
if (_location.X > g.ClipBounds.Width)
_location.X = 0;
if (_location.Y > g.ClipBounds.Height)
_location.Y = 0;
_updateCount++;
}
/// <summary>
/// Gets if the app still idle.
/// http://blogs.msdn.com/b/tmiller/archive/2005/05/05/415008.aspx
/// </summary>
/// <returns></returns>
private bool IsAppStillIdle()
{
Message msg;
return !PeekMessage(out msg, IntPtr.Zero, 0, 0, 0);
}
#region Unmanaged Get PeekMessage
// http://blogs.msdn.com/b/tmiller/archive/2005/05/05/415008.aspx
[System.Security.SuppressUnmanagedCodeSecurity] // We won't use this maliciously
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern bool PeekMessage(out Message msg, IntPtr hWnd, uint messageFilterMin, uint messageFilterMax, uint flags);
#endregion
private void btnZoomReset_Click(object sender, EventArgs e)
{
if (_backImage != null)
{
//Any smarter way to do this?
//Note that the maximum Form size is limmited by designe by the Screen resolution.
//Rectangle screenRectangle = RectangleToScreen(this.ClientRectangle);
//int titleHeight = screenRectangle.Top - this.Top;
//Borders
Size border = new Size();
border.Width = this.Width - this.pictureBox1.Width;
border.Height = this.Height - this.pictureBox1.Height;
this.Width = border.Width + _backImage.Width;
this.Height = border.Height + _backImage.Height;
Console.WriteLine("PictureBox size: " + this.pictureBox1.Size.ToString());
}
}
private void SizeMode_SelectedIndexChanged(object sender, EventArgs e)
{
PictureBoxSizeMode mode;
Enum.TryParse<PictureBoxSizeMode>(cbxSizeMode.SelectedValue.ToString(), out mode);
this.pictureBox1.SizeMode = mode;
}
private void DoubleBufferForm_CheckedChanged(object sender, EventArgs e)
{
this.DoubleBuffered = this.ckbDoubleBufferForm.Checked;
}
private void DoubleBufferPictureBox_CheckedChanged(object sender, EventArgs e)
{
this.pictureBox1.DoubleBuffered = this.ckbDoubleBufferPictureBox.Checked;
}
private void BitmapDrawSource_SelectedIndexChanged(object sender, EventArgs e)
{
if ((DrawSource)this.cbxBitmapDrawSource.SelectedItem == DrawSource.PictureBox)
this.pictureBox1.Image = _backImage;
else
this.pictureBox1.Image = null;
}
}
}
Designer.CS:
namespace WindowsFormsApplication2
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.pictureBox1 = new WindowsFormsApplication2.PictureBoxDoubleBuffer();
this.label1 = new System.Windows.Forms.Label();
this.lblFps = new System.Windows.Forms.Label();
this.ckbRun = new System.Windows.Forms.CheckBox();
this.btnZoomReset = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.cbxSizeMode = new System.Windows.Forms.ComboBox();
this.gpbDoubleBuffer = new System.Windows.Forms.GroupBox();
this.ckbDoubleBufferPictureBox = new System.Windows.Forms.CheckBox();
this.ckbDoubleBufferForm = new System.Windows.Forms.CheckBox();
this.cbxBitmapDrawSource = new System.Windows.Forms.ComboBox();
this.label3 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.gpbDoubleBuffer.SuspendLayout();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.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.pictureBox1.BackColor = System.Drawing.SystemColors.ControlDarkDark;
this.pictureBox1.DoubleBuffered = true;
this.pictureBox1.Location = new System.Drawing.Point(8, 84);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(347, 215);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.PictureBox1_Paint);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(134, 13);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(30, 13);
this.label1.TabIndex = 3;
this.label1.Text = "FPS:";
//
// lblFps
//
this.lblFps.AutoSize = true;
this.lblFps.Location = new System.Drawing.Point(160, 13);
this.lblFps.Name = "lblFps";
this.lblFps.Size = new System.Drawing.Size(10, 13);
this.lblFps.TabIndex = 4;
this.lblFps.Text = "-";
//
// ckbRun
//
this.ckbRun.Appearance = System.Windows.Forms.Appearance.Button;
this.ckbRun.AutoSize = true;
this.ckbRun.Checked = true;
this.ckbRun.CheckState = System.Windows.Forms.CheckState.Checked;
this.ckbRun.Location = new System.Drawing.Point(8, 8);
this.ckbRun.Name = "ckbRun";
this.ckbRun.Size = new System.Drawing.Size(37, 23);
this.ckbRun.TabIndex = 5;
this.ckbRun.Text = "Run";
this.ckbRun.UseVisualStyleBackColor = true;
//
// btnZoomReset
//
this.btnZoomReset.Location = new System.Drawing.Point(51, 8);
this.btnZoomReset.Name = "btnZoomReset";
this.btnZoomReset.Size = new System.Drawing.Size(74, 23);
this.btnZoomReset.TabIndex = 7;
this.btnZoomReset.Text = "Zoom 100%";
this.btnZoomReset.UseVisualStyleBackColor = true;
this.btnZoomReset.Click += new System.EventHandler(this.btnZoomReset_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(5, 37);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(57, 13);
this.label2.TabIndex = 8;
this.label2.Text = "SizeMode:";
//
// cbxSizeMode
//
this.cbxSizeMode.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cbxSizeMode.FormattingEnabled = true;
this.cbxSizeMode.Location = new System.Drawing.Point(73, 34);
this.cbxSizeMode.Name = "cbxSizeMode";
this.cbxSizeMode.Size = new System.Drawing.Size(84, 21);
this.cbxSizeMode.TabIndex = 9;
this.cbxSizeMode.SelectedIndexChanged += new System.EventHandler(this.SizeMode_SelectedIndexChanged);
//
// gpbDoubleBuffer
//
this.gpbDoubleBuffer.Controls.Add(this.ckbDoubleBufferPictureBox);
this.gpbDoubleBuffer.Controls.Add(this.ckbDoubleBufferForm);
this.gpbDoubleBuffer.Location = new System.Drawing.Point(221, 8);
this.gpbDoubleBuffer.Name = "gpbDoubleBuffer";
this.gpbDoubleBuffer.Size = new System.Drawing.Size(99, 65);
this.gpbDoubleBuffer.TabIndex = 10;
this.gpbDoubleBuffer.TabStop = false;
this.gpbDoubleBuffer.Text = "Double Buffer";
//
// ckbDoubleBufferPictureBox
//
this.ckbDoubleBufferPictureBox.AutoSize = true;
this.ckbDoubleBufferPictureBox.Checked = true;
this.ckbDoubleBufferPictureBox.CheckState = System.Windows.Forms.CheckState.Checked;
this.ckbDoubleBufferPictureBox.Location = new System.Drawing.Point(9, 38);
this.ckbDoubleBufferPictureBox.Name = "ckbDoubleBufferPictureBox";
this.ckbDoubleBufferPictureBox.Size = new System.Drawing.Size(77, 17);
this.ckbDoubleBufferPictureBox.TabIndex = 8;
this.ckbDoubleBufferPictureBox.Text = "PictureBox";
this.ckbDoubleBufferPictureBox.UseVisualStyleBackColor = true;
this.ckbDoubleBufferPictureBox.CheckedChanged += new System.EventHandler(this.DoubleBufferPictureBox_CheckedChanged);
//
// ckbDoubleBufferForm
//
this.ckbDoubleBufferForm.AutoSize = true;
this.ckbDoubleBufferForm.Checked = true;
this.ckbDoubleBufferForm.CheckState = System.Windows.Forms.CheckState.Checked;
this.ckbDoubleBufferForm.Location = new System.Drawing.Point(9, 15);
this.ckbDoubleBufferForm.Name = "ckbDoubleBufferForm";
this.ckbDoubleBufferForm.Size = new System.Drawing.Size(49, 17);
this.ckbDoubleBufferForm.TabIndex = 7;
this.ckbDoubleBufferForm.Text = "Form";
this.ckbDoubleBufferForm.UseVisualStyleBackColor = true;
this.ckbDoubleBufferForm.CheckedChanged += new System.EventHandler(this.DoubleBufferForm_CheckedChanged);
//
// cbxBitmapDrawSource
//
this.cbxBitmapDrawSource.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cbxBitmapDrawSource.FormattingEnabled = true;
this.cbxBitmapDrawSource.Items.AddRange(new object[] {
"PictureBox",
"Graphics"});
this.cbxBitmapDrawSource.Location = new System.Drawing.Point(73, 57);
this.cbxBitmapDrawSource.Name = "cbxBitmapDrawSource";
this.cbxBitmapDrawSource.Size = new System.Drawing.Size(84, 21);
this.cbxBitmapDrawSource.TabIndex = 12;
this.cbxBitmapDrawSource.SelectedIndexChanged += new System.EventHandler(this.BitmapDrawSource_SelectedIndexChanged);
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(5, 60);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(68, 13);
this.label3.TabIndex = 11;
this.label3.Text = "Bitmap draw:";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(361, 304);
this.Controls.Add(this.cbxBitmapDrawSource);
this.Controls.Add(this.label3);
this.Controls.Add(this.gpbDoubleBuffer);
this.Controls.Add(this.cbxSizeMode);
this.Controls.Add(this.label2);
this.Controls.Add(this.btnZoomReset);
this.Controls.Add(this.ckbRun);
this.Controls.Add(this.lblFps);
this.Controls.Add(this.label1);
this.Controls.Add(this.pictureBox1);
this.DoubleBuffered = true;
this.Name = "Form1";
this.Text = "Max FPS tester";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.gpbDoubleBuffer.ResumeLayout(false);
this.gpbDoubleBuffer.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
PictureBoxDoubleBuffer pictureBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label lblFps;
private System.Windows.Forms.CheckBox ckbRun;
private System.Windows.Forms.Button btnZoomReset;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.ComboBox cbxSizeMode;
private System.Windows.Forms.GroupBox gpbDoubleBuffer;
private System.Windows.Forms.CheckBox ckbDoubleBufferPictureBox;
private System.Windows.Forms.CheckBox ckbDoubleBufferForm;
private System.Windows.Forms.ComboBox cbxBitmapDrawSource;
private System.Windows.Forms.Label label3;
}
}
PictureBoxDoubleBuffer.cs:
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public class PictureBoxDoubleBuffer : PictureBox
{
public bool DoubleBuffered
{
get { return base.DoubleBuffered; }
set { base.DoubleBuffered = value; }
}
public PictureBoxDoubleBuffer()
{
base.DoubleBuffered = true;
}
}
}
"The big question is: How can I improve the FPS, specially in zoom mode?"
In Zoom (and Stretch) mode the Image() is probably being resized for each Refresh()...a very expensive operation. You could roll your own "zoom" mode that creates a new image that is already zoomed and assign that instead, leaving the mode at "Normal".
Here's a quick example using a cheat zoom with another PictureBox (obviously you could do the zoom mathematically by computing aspect ratio, etc...but that hurts my brain in the morning):
private void cbxSizeMode_SelectedIndexChanged(object sender, EventArgs e)
{
PictureBoxSizeMode mode;
Enum.TryParse<PictureBoxSizeMode>(cbxSizeMode.SelectedValue.ToString(), out mode);
this.pictureBox1.SizeMode = mode;
if (this.pictureBox1.SizeMode == PictureBoxSizeMode.Zoom)
{
using (PictureBox pb = new PictureBox())
{
pb.Size = pictureBox1.Size;
pb.SizeMode = PictureBoxSizeMode.Zoom;
pb.Image = _backImage;
Bitmap bmp = new Bitmap(pb.Size.Width, pb.Size.Height);
pb.DrawToBitmap(bmp, pb.ClientRectangle);
this.pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
this.pictureBox1.Image = bmp;
}
}
else
{
pictureBox1.Image = _backImage;
}
}
You'll need to call this method when the form first runs if Zoom is the default selection, otherwise it won't have custom zoom image.
Now your Zoom mode should zoom along! (sorry, couldn't resist doing that)