Clicker Counter Lag - c#

So I've just started coding and i'm probably using a very rudimentary method to do this but i wanted to make a click counter so i can make a 'Drop Log' for Runescape. Everything's going smooth however I've noticed that after X amount of Clicker counters programmed the buttons start to lag? Debug isn't popping up with any issues, its kind of just getting stuck?
Here's my code:
namespace DropLog_v2
{
public partial class Form1 : Form
{
//Pernix Counts
int CountP = 0;
int CountP2 = 0;
int CountP3 = 0;
int CountP4 = 0;
int CountP5 = 0;
int CountP6 = 0;
//Virtus Counts
int CountV = 0;
int CountV2 = 0;
int CountV3 = 0;
int CountV4 = 0;
int CountV5 = 0;
int CountV6 = 0;
int CountV7 = 0;
//Torva Counts
int CountM = 0;
public Form1()
{
InitializeComponent();
}
//Move Window
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Left += e.X - lastPoint.X;
this.Top += e.Y - lastPoint.Y;
}
}
Point lastPoint;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
lastPoint = new Point(e.X, e.Y);
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Left += e.X - lastPoint.X;
this.Top += e.Y - lastPoint.Y;
}
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
lastPoint = new Point(e.X, e.Y);
}
//Close Window
private void button1_Click(object sender, EventArgs e)
{
Close();
}
//Pernix Buttons
private void Pernix1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
CountP++;
PernixL1.Text = CountP.ToString();
}
else if(e.Button==MouseButtons.Right)
{
if (CountP > 0)
CountP--;
PernixL1.Text = CountP.ToString();
}
}
private void Pernix2_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
CountP2++;
PernixL2.Text = CountP2.ToString();
}
else if (e.Button == MouseButtons.Right)
{
if (CountP2 > 0)
CountP2--;
PernixL2.Text = CountP2.ToString();
}
}
private void Pernix3_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button==MouseButtons.Left)
{
CountP3++;
PernixL3.Text = CountP3.ToString();
}
else if(e.Button==MouseButtons.Right)
{
if (CountP3 > 0)
CountP3--;
PernixL3.Text = CountP3.ToString();
}
}
private void Pernix4_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button==MouseButtons.Left)
{
CountP4++;
PernixL4.Text = CountP4.ToString();
}
else if(e.Button==MouseButtons.Right)
{
if (CountP4 > 0)
CountP4--;
PernixL4.Text = CountP4.ToString();
}
}
private void Pernix5_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button==MouseButtons.Left)
{
CountP5++;
PernixL5.Text = CountP5.ToString();
}
else if(e.Button==MouseButtons.Right)
{
if (CountP5 > 0)
CountP5--;
PernixL5.Text = CountP5.ToString();
}
}
private void Pernix6_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button==MouseButtons.Left)
{
CountP6++;
PernixL6.Text = CountP6.ToString();
}
else if(e.Button==MouseButtons.Right)
{
if (CountP6 > 0)
CountP6--;
PernixL6.Text = CountP6.ToString();
}
}
//Virtus Buttons
private void Virtus1_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button==MouseButtons.Left)
{
CountV++;
VirtusL1.Text = CountV.ToString();
}
else if(e.Button==MouseButtons.Right)
{
if (CountV > 0)
CountV--;
VirtusL1.Text = CountV.ToString();
}
}
private void Virtus2_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button==MouseButtons.Left)
{
CountV2++;
VirtusL2.Text = CountV2.ToString();
}
else if(e.Button==MouseButtons.Right)
{
if (CountV2 > 0)
CountV2--;
VirtusL2.Text = CountV2.ToString();
}
}
private void Virtus3_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button==MouseButtons.Left)
{
CountV3++;
VirtusL3.Text = CountV3.ToString();
}
else if(e.Button==MouseButtons.Right)
{
if (CountV3 > 0)
CountV3--;
VirtusL3.Text = CountV3.ToString();
}
}
private void Virtus4_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button==MouseButtons.Left)
{
CountV4++;
VirtusL4.Text = CountV4.ToString();
}
else if(e.Button==MouseButtons.Right)
{
if (CountV4 > 0)
CountV4--;
VirtusL4.Text = CountV4.ToString();
}
}
private void Virtus5_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button==MouseButtons.Left)
{
CountV5++;
VirtusL5.Text = CountV5.ToString();
}
else if(e.Button==MouseButtons.Right)
{
if (CountV5 > 0)
CountV5--;
VirtusL5.Text = CountV5.ToString();
}
}
private void Virtus6_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button==MouseButtons.Left)
{
CountV6++;
VirtusL6.Text = CountV6.ToString();
}
else if(e.Button==MouseButtons.Right)
{
if (CountV6 > 0)
CountV6--;
VirtusL6.Text = CountV6.ToString();
}
}
private void Virtus7_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button==MouseButtons.Left)
{
CountV7++;
VirtusL7.Text = CountV7.ToString();
}
else if(e.Button==MouseButtons.Right)
{
if (CountV7 > 0)
CountV7--;
VirtusL7.Text = CountV7.ToString();
}
}
//Torva Buttons
private void Torva1_MouseMove(object sender, MouseEventArgs e)
{
if(e.Button==MouseButtons.Left)
{
CountM++;
TorvaL1.Text = CountM.ToString();
}
else if(e.Button==MouseButtons.Right)
{
if (CountM > 0)
CountM--;
TorvaL1.Text = CountM.ToString();
}
}
}
}
All was working Great until i tried doing the next line of code for the first 'torva' clicker and now its become laggy..
All help is appreciated (Novice, so please go easy)

You have MouseMove instead of MouseDown

Related

Customization censored in textBox

I have problem, which consists in aesthetic sense, correctly - There is textBox to which i apply true condition of UseSystemPasswordChar.. It's work! But i get bold points. Try to change font size - decreases textbox's field. Below is the code (although why is it here?). Can anyone help, thank you in advance)
public partial class frmRegistr : Form
{
public frmRegistr()
{
InitializeComponent();
}
int counter = 0;
int a = 0;
string b;
private void frmRegistr_Load(object sender, EventArgs e)
{
b = label1.Text;
a = b.Length;
label1.Text = "";
timer1.Start();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
if (counter < a)
{
counter++;
label1.Text = b.Substring(0, counter);
}
else
{
timer1.Stop();
}
}
private void label4_Click(object sender, EventArgs e)
{
timer3.Start();
}
private void label4_MouseHover(object sender, EventArgs e)
{
//if this.MouseLeave
label4.BackColor = Color.FromArgb(((int)(((byte)(154)))), ((int)(((byte)(181)))), ((int)(((byte)(101)))));
}
private void timer2_Tick(object sender, EventArgs e)
{
if (Opacity == 1)
{
timer2.Stop();
}
Opacity += .2;
}
private void timer3_Tick(object sender, EventArgs e)
{
if (Opacity <= 0)
{
this.Close();
}
Opacity -= .2;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
textBox2.UseSystemPasswordChar = true;
}
}
}
If you want to define your own password character, use property TextBox.PasswordChar. If you want this in a certain font, use Control.Font
As you only have to do this once, do this in the constructor:
public MyForm : Form
{
InitializeComponents(),
this.textBox1.PasswordChar = '_';
this.textBox11.Font = new Font(...)
};
You can also decide to do this using the visual studio designer.
You can setup this in VisualStudio designer, but this is code:
textBox1.PasswordChar = '*';
//* = password character

How to make disabled control allow to be draggable or move by mouse?

I use dragging control by mouse
Problem is if Control.Enabled = false. Dragging can't work? Is there any method to overload to prevent this behavior?
static void control_MouseEnter(object sender, EventArgs e)
{
(sender as Control).Enabled = false;
(sender as Control).Cursor = Cursors.SizeAll;
}
static void control_MouseLeave(object sender, EventArgs e)
{
(sender as Control).Enabled = true;
// (sender as Control).Cursor = Cursors.Default;
}
static void control_MouseDown(object sender, MouseEventArgs e)
{
mouseLocation = e.Location;
// turning on dragging
draggables[(Control)sender] = true;
}
static void control_MouseUp(object sender, MouseEventArgs e)
{
// turning off dragging
draggables[(Control)sender] = false;
}
static void control_MouseMove(object sender, MouseEventArgs e)
{
// only if dragging is turned on
if (draggables[(Control)sender] == true)
{
var control = (sender as Control);
control.Left = e.X + control.Left - mouseLocation.X;
control.Top = e.Y + control.Top - mouseLocation.Y;
}
}
You could use IMessageFilter to trap WM_MOUSEMOVE.
Here I'm changing the caption of the form when the mouse is moved within pictureBox1. It also tells you whether the left mouse button is down:
public partial class Form1 : Form
{
private MyFilter mf;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Enabled = false;
mf = new MyFilter();
mf.PreFilterMouseMove += Mf_PreFilterMouseMove;
Application.AddMessageFilter(mf);
}
private void Mf_PreFilterMouseMove()
{
Point pt = pictureBox1.PointToClient(Cursor.Position);
if (pictureBox1.ClientRectangle.Contains(pt))
{
bool leftDown = (Control.MouseButtons == MouseButtons.Left);
this.Text = "leftDown = " + leftDown + " : position = " + pt.ToString();
}
else
{
this.Text = "...not within pictureBox1...";
}
}
}
public class MyFilter : IMessageFilter
{
private const int WM_MOUSEMOVE = 0x200;
public delegate void dlgMouseMove();
public event dlgMouseMove PreFilterMouseMove;
public bool PreFilterMessage(ref Message m)
{
switch (m.Msg)
{
case WM_MOUSEMOVE:
if (PreFilterMouseMove != null)
{
PreFilterMouseMove();
}
break;
}
return false;
}
}

How to identify the color of a shape using EmguCV

I am developing an application which can identify basic shapes and their colors. Identifying shape part is done Now I want to identify the color of that shapes.I am using EmguCV libraries. Is there any one help me?
Shape Detection and Color Filtering in Emgu CV
This is the edited code in C# (According to the previously mentioned video in visual basic)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.UI;
namespace EmguCVShapeDetector
{
public partial class Form1 : Form
{
//---Variables--//
bool blnFirstTimeInResizeEvent = true;
int OriginalFormWidth;
int OriginalFormHeight;
int OriginalTableLayoutPanelWidth;
int OriginalTableLayoutPanelHeight;
Capture capwebcam;
bool webCamCapturingInProcess = false;
Image<Bgr, Byte> imgOriginal;
Image<Bgr, Byte> imgSmoothed;
Image<Gray, Byte> imgGrayColorFiltered;
Image<Gray, Byte> imgCanny;
Image<Bgr, Byte> imgCircles;
Image<Bgr, Byte> imgLines;
Image<Bgr, Byte> imgTrisRectsPolys;
Double dbMinBlue = 0.0;
double dbMinGreen = 0.0;
double dbMinRed = 0.0;
double dbMaxBlue = 0.0;
double dbMaxGreen = 0.0;
double dbMaxRed = 0.0;
public Form1()
{
InitializeComponent();
OriginalFormWidth = this.Width;
OriginalFormHeight = this.Height;
OriginalTableLayoutPanelWidth = tlpLebelsAndImageBoxes.Width;
OriginalTableLayoutPanelHeight = tlpLebelsAndImageBoxes.Height;
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i <= 255; i++)
{
cmbMinBlue.Items.Add(i);
cmbMinGreen.Items.Add(i);
cmbMinRed.Items.Add(i);
cmbMaxBlue.Items.Add(i+1);
cmbMaxGreen.Items.Add(i+1);
cmbMaxRed.Items.Add(i+1);
}
cmbMinBlue.Text = "0";
cmbMinGreen.Text = "0";
cmbMinRed.Text = "0";
cmbMaxBlue.Text = "1";
cmbMaxGreen.Text = "1";
cmbMaxRed.Text = "1";
}
private void Form1_Resize(object sender, EventArgs e)
{
if(blnFirstTimeInResizeEvent == true)
{
blnFirstTimeInResizeEvent = false;
}
else
{
tlpLebelsAndImageBoxes.Width = this.Width - (OriginalTableLayoutPanelWidth);
tlpLebelsAndImageBoxes.Height = this.Height - (OriginalTableLayoutPanelHeight);
}
}
private void radImageFile_CheckedChanged(object sender, EventArgs e)
{
if(radImageFile.Checked == true)
{
if(webCamCapturingInProcess == true)
{
Application.Idle -= ProcessImageAndUpdateGUI;
webCamCapturingInProcess = false;
}
ibOriginal.Image = null;
ibGrayColorFilter.Image = null;
ibCanny.Image = null;
ibCircles.Image = null;
ibLines.Image = null;
ibTrianglesAndPolys.Image = null;
lblFile.Visible = true;
txtFile.Visible = true;
btnFile.Visible = true;
}
}
private void radWebCam_CheckedChanged(object sender, EventArgs e)
{
if(radWebCam.Checked == true)
{
try
{
capwebcam = new Capture();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
Application.Idle += ProcessImageAndUpdateGUI;
webCamCapturingInProcess = true;
lblFile.Visible = false;
txtFile.Visible = false;
btnFile.Visible = false;
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if(capwebcam != null)
{
capwebcam.Dispose();
}
}
private void btnFile_Click(object sender, EventArgs e)
{
if (ofdFile.ShowDialog() == DialogResult.OK)
{
Image inputImage = Image.FromFile(ofdFile.FileName);
txtFile.Text = ofdFile.FileName.ToString();
}
}
private void chbDrawCirclesOnOriginalImage_CheckedChanged(object sender, EventArgs e)
{
if(webCamCapturingInProcess == false)
{
ProcessImageAndUpdateGUI(new object(), new EventArgs());
}
}
private void chbDrawLinesOnOriginalImage_CheckedChanged(object sender, EventArgs e)
{
if (webCamCapturingInProcess == false)
{
ProcessImageAndUpdateGUI(new object(), new EventArgs());
}
}
private void chbDrawTrianglesAndPolygansOnOriginalImage_CheckedChanged(object sender, EventArgs e)
{
if (webCamCapturingInProcess == false)
{
ProcessImageAndUpdateGUI(new object(), new EventArgs());
}
}
private void chbFillterOnColor_CheckedChanged(object sender, EventArgs e)
{
if(chbFillterOnColor.Checked == true)
{
lblBlue.Visible = true;
lblGreen.Visible = true;
lblRed.Visible = true;
lblMin.Visible = true;
lblMax.Visible = true;
cmbMinBlue.Visible = true;
cmbMinGreen.Visible = true;
cmbMinRed.Visible = true;
cmbMaxBlue.Visible = true;
cmbMaxGreen.Visible = true;
cmbMaxRed.Visible = true;
lblGrayColorFilter.Text = "gray (color filtered):";
ProcessImageAndUpdateGUI(new object(), new EventArgs());
}
else if (chbFillterOnColor.Checked == false)
{
lblBlue.Visible = false;
lblGreen.Visible = false;
lblRed.Visible = false;
lblMin.Visible = false;
lblMax.Visible = false;
cmbMinBlue.Visible = false;
cmbMinGreen.Visible = false;
cmbMinRed.Visible = false;
cmbMaxBlue.Visible = false;
cmbMaxGreen.Visible = false;
cmbMaxRed.Visible = false;
lblGrayColorFilter.Text = "gray:";
ProcessImageAndUpdateGUI(new object(), new EventArgs());
}
}
private void txtFile_TextChanged(object sender, EventArgs e)
{
txtFile.SelectionStart = txtFile.Text.Length;
}
private void cmbMinBlue_SelectedIndexChanged(object sender, EventArgs e)
{
if(chbFillterOnColor.Checked == true && txtFile.Text != "")
{
dbMinBlue = Convert.ToDouble(cmbMinBlue.Text);
ProcessImageAndUpdateGUI(new object(), new EventArgs());
}
}
private void cmbMinGreen_SelectedIndexChanged(object sender, EventArgs e)
{
if (chbFillterOnColor.Checked == true && txtFile.Text != "")
{
dbMinGreen = Convert.ToDouble(cmbMinGreen.Text);
ProcessImageAndUpdateGUI(new object(), new EventArgs());
}
}
private void cmbMinRed_SelectedIndexChanged(object sender, EventArgs e)
{
if (chbFillterOnColor.Checked == true && txtFile.Text != "")
{
dbMinRed = Convert.ToDouble(cmbMinRed.Text);
ProcessImageAndUpdateGUI(new object(), new EventArgs());
}
}
private void cmbMaxBlue_SelectedIndexChanged(object sender, EventArgs e)
{
if (chbFillterOnColor.Checked == true && txtFile.Text != "")
{
dbMaxBlue = Convert.ToDouble(cmbMaxBlue.Text);
ProcessImageAndUpdateGUI(new object(), new EventArgs());
}
}
private void cmbMaxGreen_SelectedIndexChanged(object sender, EventArgs e)
{
if (chbFillterOnColor.Checked == true && txtFile.Text != "")
{
dbMaxGreen = Convert.ToDouble(cmbMaxGreen.Text);
ProcessImageAndUpdateGUI(new object(), new EventArgs());
}
}
private void cmbMaxRed_SelectedIndexChanged(object sender, EventArgs e)
{
if (chbFillterOnColor.Checked == true && txtFile.Text != "")
{
dbMaxRed = Convert.ToDouble(cmbMaxRed.Text);
ProcessImageAndUpdateGUI(new object(), new EventArgs());
}
}
private void cmbMinBlue_Leave(object sender, EventArgs e)
{
if (Convert.ToInt32(cmbMinBlue.Text) < 0 || Convert.ToInt32(cmbMinBlue.Text) > 255)
{
cmbMinBlue.Text = "0";
}
cmbMinBlue_SelectedIndexChanged(new object(), new EventArgs());
}
private void cmbMinGreen_Leave(object sender, EventArgs e)
{
if (Convert.ToInt32(cmbMinGreen.Text) < 0 || Convert.ToInt32(cmbMinGreen.Text) > 255)
{
cmbMinGreen.Text = "0";
}
cmbMinGreen_SelectedIndexChanged(new object(), new EventArgs());
}
private void cmbMinRed_Leave(object sender, EventArgs e)
{
if (Convert.ToInt32(cmbMinRed.Text) < 0 || Convert.ToInt32(cmbMinRed.Text) > 255)
{
cmbMinRed.Text = "0";
}
cmbMinRed_SelectedIndexChanged(new object(), new EventArgs());
}
private void cmbMaxBlue_Leave(object sender, EventArgs e)
{
if (Convert.ToInt32(cmbMaxBlue.Text) < 1 || Convert.ToInt32(cmbMaxBlue.Text) > 256)
{
cmbMaxBlue.Text = "1";
}
cmbMaxBlue_SelectedIndexChanged(new object(), new EventArgs());
}
private void cmbMaxGreen_Leave(object sender, EventArgs e)
{
if (Convert.ToInt32(cmbMaxGreen.Text) < 1 || Convert.ToInt32(cmbMaxGreen.Text) > 256)
{
cmbMaxGreen.Text = "1";
}
cmbMaxGreen_SelectedIndexChanged(new object(), new EventArgs());
}
private void cmbMaxRed_Leave(object sender, EventArgs e)
{
if (Convert.ToInt32(cmbMaxRed.Text) < 1 || Convert.ToInt32(cmbMaxRed.Text) > 256)
{
cmbMaxRed.Text = "1";
}
cmbMaxRed_SelectedIndexChanged(new object(), new EventArgs());
}
private void cmbMinBlue_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode.Equals(Keys.Enter) || e.KeyCode.Equals(Keys.Return))
{
e.SuppressKeyPress = true;
lblOriginal.Focus();
}
}
private void cmbMinGreen_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode.Equals(Keys.Enter) || e.KeyCode.Equals(Keys.Return))
{
e.SuppressKeyPress = true;
lblOriginal.Focus();
}
}
private void cmbMinRed_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode.Equals(Keys.Enter) || e.KeyCode.Equals(Keys.Return))
{
e.SuppressKeyPress = true;
lblOriginal.Focus();
}
}
private void cmbMaxBlue_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode.Equals(Keys.Enter) || e.KeyCode.Equals(Keys.Return))
{
e.SuppressKeyPress = true;
lblOriginal.Focus();
}
}
private void cmbMaxGreen_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode.Equals(Keys.Enter) || e.KeyCode.Equals(Keys.Return))
{
e.SuppressKeyPress = true;
lblOriginal.Focus();
}
}
private void cmbMaxRed_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode.Equals(Keys.Enter) || e.KeyCode.Equals(Keys.Return))
{
e.SuppressKeyPress = true;
lblOriginal.Focus();
}
}
void ProcessImageAndUpdateGUI(object sender, EventArgs e)
{
if(radImageFile.Checked == true)
{
try
{
imgOriginal = new Image<Bgr,Byte>(txtFile.Text);
}
catch (Exception ex)
{
return;
}
}
else if(radWebCam.Checked == true)
{
try
{
imgOriginal = capwebcam.QueryFrame();
}
catch (Exception ex)
{
return;
}
}
if(imgOriginal == null)
{
return;
}
imgSmoothed = imgOriginal.PyrDown().PyrUp();
imgSmoothed._SmoothGaussian(3);
if(chbFillterOnColor.Checked == true)
{
imgGrayColorFiltered = imgSmoothed.InRange(new Bgr(dbMinBlue,dbMinGreen,dbMinRed),new Bgr(dbMaxBlue,dbMaxGreen,dbMaxRed));
imgGrayColorFiltered = imgGrayColorFiltered.PyrDown().PyrUp();
imgGrayColorFiltered._SmoothGaussian(3);
}
else if(chbFillterOnColor.Checked == false)
{
imgGrayColorFiltered = imgSmoothed.Convert<Gray, Byte>();
}
Gray grayCannyThreshold = new Gray(160);
Gray grayCircleAccumThreshold = new Gray(100);
Gray grayThreshLinking = new Gray(80);
imgCanny = imgGrayColorFiltered.Canny(grayCannyThreshold, grayThreshLinking);
imgCircles = imgOriginal.CopyBlank();
imgLines = imgOriginal.CopyBlank();
imgTrisRectsPolys = imgOriginal.CopyBlank();
Double dblAccumRes = 2.0;
Double dblMinDistBetweenCircles = imgGrayColorFiltered.Height / 4;
int intMinRadius = 10;
int intMaxRadius = 400;
CircleF[] circles = imgGrayColorFiltered.HoughCircles(grayCannyThreshold, grayCircleAccumThreshold, dblAccumRes, dblMinDistBetweenCircles, intMinRadius, intMaxRadius)[0];
foreach(CircleF circle in circles)
{
imgCircles.Draw(circle, new Bgr(Color.Red),2);
if(chbDrawCirclesOnOriginalImage.Checked == true)
{
imgCircles.Draw(circle, new Bgr(Color.Red), 2);
}
}
Double dblRhoRes = 1.0;
Double dblThetaRes = 4.0 *(Math.PI/180.0);
int intThreshold = 20;
Double dblMinLineWidth = 30.0;
Double dblMinGapBetweenLines = 10.0;
LineSegment2D[] lines = imgCanny.Clone().HoughLinesBinary(dblRhoRes, dblThetaRes, intThreshold, dblMinLineWidth, dblMinGapBetweenLines)[0];
foreach(LineSegment2D line in lines)
{
imgLines.Draw(line, new Bgr(Color.DarkGreen),2);
if(chbDrawLinesOnOriginalImage.Checked == true)
{
imgOriginal.Draw(line, new Bgr(Color.DarkGreen),2);
}
}
Contour<Point> contours = imgCanny.FindContours();
List<Triangle2DF> lstTreangles = new List<Triangle2DF>();
List<MCvBox2D> lstRectangles = new List<MCvBox2D>();
List<Contour<Point>> lstPoluhons = new List<Contour<Point>>();
while(contours != null)
{
Contour<Point> contour = contours.ApproxPoly(10.0);
if(contour.Area > 250.0)
{
if(contour.Total == 3)
{
Point[] ptPoints = contour.ToArray();
lstTreangles.Add(new Triangle2DF(ptPoints[0],ptPoints[1],ptPoints[2]));
}
else if(contour.Total >= 4 && contour.Total <= 6)
{
Point[] ptPoints = contour.ToArray();
Boolean blnIsRectangle = true;
if(contour.Total == 4)
{
LineSegment2D[] ls2dEdges = PointCollection.PolyLine(ptPoints, true);
for(int i = 0; i< ls2dEdges.Length -1; i++)
{
Double dblAngle = Math.Abs(ls2dEdges[(i+1) % ls2dEdges.Length].GetExteriorAngleDegree(ls2dEdges[i]));
if(dblAngle < 80.0 || dblAngle > 100.0)
{
blnIsRectangle = false;
}
}
}
else
{
blnIsRectangle = false;
}
if(blnIsRectangle)
{
lstRectangles.Add(contour.GetMinAreaRect());
}
else
{
lstPoluhons.Add(contour);
}
}
}
contours = contours.HNext;
}
foreach(Triangle2DF triangle in lstTreangles)
{
imgTrisRectsPolys.Draw(triangle, new Bgr(Color.Yellow),2);
if(chbDrawTrianglesAndPolygansOnOriginalImage.Checked == true)
{
imgOriginal.Draw(triangle, new Bgr(Color.Yellow),2);
}
}
foreach(MCvBox2D rect in lstRectangles)
{
imgTrisRectsPolys.Draw(rect, new Bgr(Color.Blue),2);
if(chbDrawTrianglesAndPolygansOnOriginalImage.Checked == true)
{
imgOriginal.Draw(rect, new Bgr(Color.Blue),2);
}
}
foreach(Contour<Point> contPoly in lstPoluhons)
{
imgTrisRectsPolys.Draw(contPoly, new Bgr(Color.Gray),2);
if(chbDrawTrianglesAndPolygansOnOriginalImage.Checked == true)
{
imgOriginal.Draw(contPoly, new Bgr(Color.Gray),2);
}
}
ibOriginal.Image = imgOriginal;
ibGrayColorFilter.Image = imgGrayColorFiltered;
ibCanny.Image = imgCanny;
ibLines.Image = imgLines;
ibTrianglesAndPolys.Image = imgTrisRectsPolys;
}
}
}
I think this code will helpful to your project.

C# dynamic table of objects listbox - dragdrop method between listbox1 and dynamic listbox[n]

I have a problem with drag and drop effect. I want to move selected index from listboxListaPrzedmiotow to dynamically allocated listboxA[n]. I don't have any idea to solve the problem of using method on dynamic object. Please help.
Here is my code:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
static decimal kolumny = 3;//numericUpDownKolumny.Value;
static decimal wiersze = 4;//numericUpDownWiersze.Value;
static int n = (int)kolumny * (int)wiersze;
ListBox[] listboxA = new ListBox[n];
public Form1()
{
InitializeComponent();
}
public void AddNewListBox()
{
int i = 0;
for (int y = 0; y < wiersze; y++)
{
for (int x = 0; x < kolumny; x++)
{
//ListBox[] listboxA = new ListBox();
listboxA[i] = new ListBox();
listboxA[i].Left = 9 + 126 * x;
listboxA[i].Top = 64 + 101 * y;
splitContainer1.Panel2.Controls.Add(listboxA[i]);
listboxA[i].AllowDrop = true;
i++;
}
}
}
private void buttonOK_Click(object sender, EventArgs e)
{
AddNewListBox();
}
private void listBoxListaPrzedmiotow_MouseDown(object sender, MouseEventArgs e)
{
listBoxListaPrzedmiotow.DoDragDrop(listBoxListaPrzedmiotow.SelectedIndex, DragDropEffects.Copy);
}
private void listBoxListaPrzedmiotow_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void listBoxListaPrzedmiotow_DragDrop(object sender, DragEventArgs e)
{
listBoxListaPrzedmiotow.Items.Add(e.Data.GetData(DataFormats.Text));
}
private void listBoxA_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
private void listBoxA_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.StringFormat))
{
string str = (string)e.Data.GetData(
DataFormats.StringFormat);
for (int i=0; i < n; i++)
listboxA[i].Items.Add(str);
}
}
}
}

How to make multiselect listbox only with mouse in C# (Winform)?

I have listbox with some items in it, what I'm looking for this:
always previous items keep selected and if i click an item that isn't selected it get selected too but if it's already selected it get's unselected.
I used these code but it doesn't work very well!
here's my code(this one does not work at all):
public partial class Options_Form : Form
{
public Options_Form()
{
InitializeComponent();
}
private void Options_Load(object sender, EventArgs e)
{
AceMP_Class cl = new AceMP_Class();
listBox1.Items.AddRange(cl.SupportedFiles_stringarray());
listBox1.SelectionMode = SelectionMode.MultiExtended;
listBox1.Size = listBox1.PreferredSize;
listboxitemsState_array = new bool[cl.SupportedFiles_stringarray().Length];
}
private void button2_Click(object sender, EventArgs e)
{
listBox1.ClearSelected();
//selecteditemsindex_list.Clear();
}
//List<int> selecteditemsindex_list = new List<int>();
bool[] listboxitemsState_array;
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
for (int i = 0; i < listboxitemsState_array.Length; i++)
{
if (listboxitemsState_array[i])
{
listBox1.SetSelected(i, true);
}
else
{
listBox1.SetSelected(i, false);
}
}
//if (listBox1.GetSelected(listBox1.IndexFromPoint(e.X, e.Y)))
if (listboxitemsState_array[listBox1.IndexFromPoint(e.X, e.Y)])
{
listBox1.SetSelected(listBox1.IndexFromPoint(e.X, e.Y), false);
listboxitemsState_array[listBox1.IndexFromPoint(e.X, e.Y)] = false;
}
else
{
listBox1.SetSelected(listBox1.IndexFromPoint(e.X, e.Y), true);
listboxitemsState_array[listBox1.IndexFromPoint(e.X, e.Y)] = true;
}
}
private void listBox1_MouseUp(object sender, MouseEventArgs e)
{
for (int i = 0; i < listboxitemsState_array.Length; i++)
{
if (listboxitemsState_array[i])
{
listBox1.SetSelected(i, true);
}
else
{
listBox1.SetSelected(i, false);
}
}
//if (listBox1.GetSelected(listBox1.IndexFromPoint(e.X, e.Y)))
if (listboxitemsState_array[listBox1.IndexFromPoint(e.X, e.Y)])
{
listBox1.SetSelected(listBox1.IndexFromPoint(e.X, e.Y), false);
listboxitemsState_array[listBox1.IndexFromPoint(e.X, e.Y)] = false;
}
else
{
listBox1.SetSelected(listBox1.IndexFromPoint(e.X, e.Y), true);
listboxitemsState_array[listBox1.IndexFromPoint(e.X, e.Y)] = true;
}
}
}
but this one works but not very well!
public partial class Options_Form : Form
{
public Options_Form()
{
InitializeComponent();
}
private void Options_Load(object sender, EventArgs e)
{
AceMP_Class cl = new AceMP_Class();
listBox1.Items.AddRange(cl.SupportedFiles_stringarray());
listBox1.SelectionMode = SelectionMode.MultiExtended;
listBox1.Size = listBox1.PreferredSize;
}
private void button2_Click(object sender, EventArgs e)
{
listBox1.ClearSelected();
selecteditemsindex_list.Clear();
}
List<int> selecteditemsindex_list = new List<int>();
private void listBox1_Click(object sender, EventArgs e)
{
listBox1.ClearSelected();
}
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
for (int i = 0; i < selecteditemsindex_list.Count; i++)
{
listBox1.SetSelected(selecteditemsindex_list[i], true);
}
}
private void listBox1_MouseUp(object sender, MouseEventArgs e)
{
for (int i = 0; i < selecteditemsindex_list.Count; i++)
{
listBox1.SetSelected(selecteditemsindex_list[i], true);
}
if (listBox1.GetSelected(listBox1.IndexFromPoint(e.X, e.Y)))
{
listBox1.SetSelected(listBox1.IndexFromPoint(e.X, e.Y), false);
}
else
{
listBox1.SetSelected(listBox1.IndexFromPoint(e.X, e.Y), true);
selecteditemsindex_list.Add(listBox1.IndexFromPoint(e.X, e.Y));
}
}
}
How can I solve that!?
I want to mention that you achieved this feature with SelectionMode.MultiExtended but you need to press CTRL + Click or SHIFT + Click.
To achieve this only with mouse:
1) A better solution comes up as when i discuss with #ACE (he come up with idea to use virtualkey) is to press CTRL key when you make clicks. On MouseDown you press CTRL and on MouseUp release the CTRL key.
public partial class Options_Form : Form
{
public Options_Form()
{
InitializeComponent();
}
private void Options_Load(object sender, EventArgs e)
{
AceMP_Class cl = new AceMP_Class();
listBox1.Items.AddRange(cl.SupportedFiles_stringarray());
listBox1.SelectionMode = SelectionMode.MultiSimple;
listBox1.Size = listBox1.PreferredSize;
}
[DllImport("user32.dll", SetLastError = true)]
static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
public const uint KEYEVENTF_KEYUP = 0x02;
public const uint VK_CONTROL = 0x11;
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
keybd_event(Convert.ToByte(VK_CONTROL), 0, 0, 0);
}
private void listBox1_MouseUp(object sender, MouseEventArgs e)
{
keybd_event(Convert.ToByte(VK_CONTROL), 0, Convert.ToByte(KEYEVENTF_KEYUP), 0);
}
}
2) The easiest solution that comes up in my mind (with some drawbacks if you pres click very fast) is to use on MouseClick event and to have an array of bool that every index from that array tells me if selected or not.
selecteditemsindex_list[selectedIndex] = !selecteditemsindex_list[selectedIndex];
How it works?
Take the current index and negate (!) the value at that index. This way you obtain select/unselect feature with mouse.
Because i work only with the current index the others values from array remains unmodified and i can use SetSelected for all values from array. This way you can make multiselect with mouse.
public partial class Options_Form : Form
{
public Options_Form()
{
InitializeComponent();
}
bool[] selecteditemsindex_list;
private void Options_Load(object sender, EventArgs e)
{
AceMP_Class cl = new AceMP_Class();
listBox1.Items.AddRange(cl.SupportedFiles_stringarray());
listBox1.SelectionMode = SelectionMode.MultiExtended;
listBox1.Size = listBox1.PreferredSize;
selecteditemsindex_list = new bool[listBox1.Items.Count];
}
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
var selectedIndex = listBox1.SelectedIndex;
selecteditemsindex_list[selectedIndex] = !selecteditemsindex_list[selectedIndex];
for (int i = 0; i < selecteditemsindex_list.Count(); i++)
{
listBox1.SetSelected(i, selecteditemsindex_list[i]);
}
}
}
The ListBox control has a property 'SelectionMode'
You can set it to :
One
MultiSimple
MultiExtended
see this link for details

Categories