c++ draw something with mouseclick on picturebox - winforms

I have two functions for picurebox. I want to draw something with mouseclick on picturebox.
private: System::Void pictureBox1_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) {
int Curx = e->X;
int Cury = e->Y;
}
and
private: System::Void pictureBox1_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e) {
e->Graphics->DrawEllipse(Pens::Blue, 200,200, 1, 1);
}
I want to use one function in another.

In the private section of the code where you have the picture box defined add two variables for the position, x and y as:
private: System::Windows::Forms::PictureBox^ pictureBox1;
int mousex;
int mousey;
Set your MouseClick event to save the coordinates to those variables and force a repaint by calling Refresh():
private: System::Void pictureBox1_MouseClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
{
mousex = e->X;
mousey = e->Y;
pictureBox1->Refresh();
}
In the Paint event, draw your ellipse at the coordinates you have saved in mousex and mousey:
private: System::Void pictureBox1_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e)
{
e->Graphics->DrawEllipse(Pens::Blue, mousex, mousey, 60, 60);
}
Adjust the width and height of the ellipse, currently 60 each to your choice.

Related

C++/CLI Winform - Is it possible to have 1 event handler for multiple textboxes

Im trying to limit the user input to float or integer by using this function I found from another post.
private: System::Void keypressValidation(System::Windows::Forms::TextBox^ textBox, System::Windows::Forms::KeyPressEventArgs^ e) {
// Only allow 1 decimal point
if (e->KeyChar == '.')
{
if (textBox->Text->Contains(".") && !textBox->SelectedText->Contains("."))
{
e->Handled = true;
}
}
// Accepts only digits and Backspace keypress
else if (!Char::IsDigit(e->KeyChar) && e->KeyChar != 0x08)
{
e->Handled = true;
}
}
Right now I have 8 textboxes on my UI and I created 8 different keypress event handler for every individual textbox.
private: System::Void txtN3_KeyPress(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e) {
keypressValidation(txtN3, e);
}
private: System::Void txtN2_KeyPress(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e) {
keypressValidation(txtN2, e);
}
private: System::Void txtN1_KeyPress(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e) {
keypressValidation(txtN1, e);
}
private: System::Void txtN0_KeyPress(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e) {
keypressValidation(txtN0, e);
}
private: System::Void txtD3_KeyPress(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e) {
keypressValidation(txtD3, e);
}
private: System::Void txtD2_KeyPress(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e) {
keypressValidation(txtD2, e);
}
private: System::Void txtD1_KeyPress(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e) {
keypressValidation(txtD1, e);
}
private: System::Void txtD0_KeyPress(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e) {
keypressValidation(txtD0, e);
}
But later on in my project I will have around 64 textboxes and I find it too tedious to have an event handler for every textboxes.
Is there a method which will make this more compact such as only having one event handler for multiple textboxes?
You absolutely can do this with only a single handler function. First you must deal with the small difference between the functions:
void txtGeneric_KeyPress(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e)
{
keypressValidation(safe_cast<TextBox^>(sender), e);
}
Then it is a simple matter in the designer of wiring up the event on every textbox to this unified handler. Instead of double-clicking on the handler entry in the event list, use the drop-down to select the existing function.
Another option is to remove all the handlers in the designer and wire them up at runtime, as suggested by #EylM. Note that his loop will only work if every single textbox on the form is treated the same way, if you have even one or two that aren't part of the pattern, you'll need more complicated filtering.
My preferred way would be to iterate over all textboxes in the Form and bind a single event handler.
Here is the sample code in C++/CLI:
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
for each (Control ^ ctrl in this->Controls)
{
if (ctrl->GetType() == TextBox::typeid)
{
ctrl->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &Form1::textBox1_KeyPress);
}
}
}
private: System::Void textBox1_KeyPress(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e)
{
}

Polygon Mouse Left Button Down Not Firing and Polygon child possible or not?

I have a polygon which has a MouseLeftButtonDown event. When I click near its edges (say 10 pixes inside from the borders) the event is not called. What could be the problem of this?
Secondly, can we add child of a polygon?
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Polygon p = new Polygon() {
Stroke = Brushes.Black,
StrokeThickness = 2,
Points = new PointCollection() {new Point(10,10), new Point(50,10),
new Point(56, 45) }};
p.MouseLeftButtonDown += new MouseButtonEventHandler(p_MouseLeftButtonDown);
mygrd.Children.Add(p);
}
void p_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("Entered");
}
give the polygon a fill brush and your mouse event fires
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Polygon p = new Polygon() {
Stroke = Brushes.Black,
StrokeThickness = 2,
Fill = Brushes.Transparent, // or something else
Points = new PointCollection() {new Point(10,10), new Point(50,10), new Point(56, 45) }};
p.MouseLeftButtonDown += new MouseButtonEventHandler(p_MouseLeftButtonDown);
mygrd.Children.Add(p);
}

How do I display a tooltip when focus is in a specific textbox?

For a textbox, I want to display a tooltip immediatly when the focus is in on the textbox, and stay there for the duration of the focus - not just when the mouse hovers over the textbox.
How can I do that?
The Enter and Leave events are probably useful here, and show it with a duration of 0 to keep it there.
private ToolTip tt;
private void textBox1_Enter(object sender, EventArgs e) {
tt = new ToolTip();
tt.InitialDelay = 0;
tt.IsBalloon = true;
tt.Show(string.Empty, textBox1);
tt.Show("I need help", textBox1, 0);
}
private void textBox1_Leave(object sender, EventArgs e) {
tt.Dispose();
}
Note: Calling the Show(...) method twice like in my example will force the "pointer" to point correctly to the control.
have tested, the event names:
private void textbox_Enter(object sender, EventArgs e)
{
toolTip1.Show("your tip here", textbox);
}
private void textbox_Leave(object sender, EventArgs e)
{
toolTip1.Hide(textbox);
}
tooltip is a control, needs to be added from toolbox.
using mouse hover and mouse leave events
private void textBox1_MouseHover(object sender, EventArgs e)
{
toolTip1.Show("your tip here", textBox2);
}
private void textBox1_MouseLeave(object sender, EventArgs e)
{
toolTip1.Hide(textBox2);
}
>
Windows Forms
public partial class FormWindow : Form
{
//Constructor
public FormWindow()
{
txtUrl.Text = "Enter text here";
txtUrl.ForeColor = Color.Gray;
txtUrl.GotFocus += TxtUrl_GotFocus;
txtUrl.LostFocus += TxtUrl_LostFocus;
}
private void TxtUrl_GotFocus(object sender, EventArgs e)
{
txtUrl.Text = "";
txtUrl.ForeColor = Color.Black;
}
private void TxtUrl_LostFocus(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(txtUrl.Text))
{
txtUrl.Text = "Enter text here";
txtUrl.ForeColor = Color.Gray;
}
}
}
Use a System.Windows.Forms.ToolTip and show it in textbox GotFocus event and Hide it in LostFocus event:
void textBox_GotFocus(object sender, EventArgs e)
{
toolTip.Show("your tip", textBox);
}
void textBox_LostFocus(object sender, EventArgs e)
{
toolTip.Hide(textBox);
}

How to submit events to InkCanvas in WPF manually?

How would I be able to submit events manually to be received by InkCanvas ?
What I need to do, is to set the mode of InkCanvas to ink mode, and then, send virtual events to InkCanvas so that I get a drawing behavior as if user used the real mouse.
Thanks
The following code snippet shows an example of drawing a shape in InkCanvas:
StylusPointCollection stroke1Points = new StylusPointCollection();
stroke1Points.Add(new StylusPoint(50,10));
stroke1Points.Add(new StylusPoint(90,50));
stroke1Points.Add(new StylusPoint(10,50));
stroke1Points.Add(new StylusPoint(50,10));
Stroke stroke1 = new Stroke(stroke1Points);
canvas.Strokes.Add(stroke1);
Where canvas is of type InkCanvas. The above generates a triangle in the canvas.
"And yes, you may accept the answer if it helps you."
Something like this?
private void inkSurface_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
inkSurface.CaptureMouse();
_inkStroke = new Stroke(
e.StylusDevice.GetStylusPoints(inkSurface));
_inkStroke.DrawingAttributes.Width = 5;
_inkStroke.DrawingAttributes.Height = 5;
_inkStroke.DrawingAttributes.Color = Colors.Black;
inkSurface.Strokes.Add(_inkStroke);
e.Handled = true;
}
private void inkSurface_MouseMove(object sender, MouseEventArgs e)
{
if (_inkStroke != null)
{
_inkStroke.StylusPoints.Add(
e.StylusDevice.GetStylusPoints(inkSurface));
}
e.Handled = true;
}
private void inkSurface_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
inkSurface.ReleaseMouseCapture();
e.Handled = true;
}

Resizing custom forms (with drop shadow effect) and controls on mouse drag event in c sharp?

In my application I have to resize forms and all its control on mouse drag effect and forms should have drop shadow effect the problem is that all my forms are custom one (with no boarder).
Thanks in advance
i think u have to implement by yourself
on mouse down start bind on mouse drag + change cursor to resize icon
on mouse drag, just simply reduce your form size
on mouse up unbind mouse drag event
the reason i suggest dynamic event binding so u can specified which control or area should have mouse down
I'm not sure about the drop shadow effect, but you should be able to resize a form by placing a button in the bottom right corner with some appropriate icon. When the user clicks and drags this button, it resizes the form. Here's some example code:
public partial class Form1 : Form
{
private int bottomBorder;
private int rightBorder;
private Point mouseStart;
private bool isResizing = false;
public Form1()
{
InitializeComponent();
}
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (isResizing)
{
var newLocation = button1.Location;
newLocation.Offset(
e.X - mouseStart.X,
e.Y - mouseStart.Y);
button1.Location = newLocation;
this.Height = button1.Bottom + bottomBorder;
this.Width = button1.Right + rightBorder;
button1.Refresh();
}
}
private void button1_MouseDown(object sender, MouseEventArgs e)
{
isResizing = true;
mouseStart = e.Location;
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
isResizing = false;
}
private void Form1_Load(object sender, EventArgs e)
{
bottomBorder = this.Height - button1.Bottom;
rightBorder = this.Width - button1.Right;
}
}
Without a border (or some control), how do you intend to resize? Figure that part out, then try this code in your form:
public class CustomForm : Form
{
private const int WmNcLButtonDown = 0xA1;
private const int HtBottomRight = 17;
[DllImport("user32.dll")]
private static extern int ReleaseCapture();
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hwnd, int msg, int wparam, int lparam);
// elsewhere
void ResizeForm()
{
ReleaseCapture();
SendMessage(this.Handle, WmNcLButtonDown, HtBottomRight, 0);
}
}
This code will resize your form as though the bottom right corner was used. Look up HT_BOTTOMRIGHT and other HT_ constants for different locations for resizing.
I used the solutions by Don Kirkby and Matthew Ferreira and created my own solution combining the two. I added a StatusStrip named "resizeHandle", made it's size 20x20 pixels and listened to it's events.
public class CustomForm : Form
{
private const int WmNcLButtonDown = 0xA1;
private const int HtBottomRight = 17;
private const int wmNcLButtonUp = 0xA2;
private bool isResizing = false;
[DllImport("user32.dll")]
private static extern int ReleaseCapture();
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hwnd, int msg, int wparam, int lparam);
private void resizeHandle_MouseDown(object sender, MouseEventArgs e)
{
isResizing = true;
}
private void resizeHandle_MouseMove(object sender, MouseEventArgs e)
{
if (isResizing)
{
// Check if we have released the Left mouse button
isResizing = (e.Button == MouseButtons.Left);
ReleaseCapture();
if (isResizing)
{
SendMessage(Handle, wmNcLButtonDown, HtBottomRight, 0);
}
else
{
// Left Mouse button was released, end resizing.
SendMessage(Handle, wmNcLButtonUp, HtBottomRight, 0);
}
}
}

Resources