DrawGrabHandle

  • Thread starter Thread starter Kate Luu
  • Start date Start date
K

Kate Luu

I did create a custom control, which inherit from picturebox control.
OnClick method, I have put this line of code:

Graphics gfx = this.createGraphics();

ControlPaint.DrawGrabHandle(gfx , this.ClientRectangle, true,true );

this.Invalidate();

but nothing happen.



Does any body know what's the problem? Thanks you so much in advance, and
I'm very appreciated for your help.
 
Kate,

If you want to apply any custom painting to a control, you should
override the OnPaint method, or you should add an event handler to your
control to handle the Paint event.

In your paint event, you should custom paint the control to display
itself as you wish. What you paint, however, would be determined by state
that is held elsewhere in your control. So, in your case, when you click
the button, you set a flag indicating that the rectangle will be painted.
Then, you call invalidate, which will cause a repaint of your control, and
the rectangle to be painted.

The reason for all of this is that the painting of a control is handled
in a loop, just like any other action in the UI. The message pump is called
over and over. The paint event gets called a number of times, and you have
to be ready to paint at any time (due to a number of other things that could
be going on).

Hope this helps.
 
Kate,
Drawing code generally should not be in the OnClick method, it really should
be in the OnPaint method. As the OnPaint will be called when you Invalidate
the control, or the system invalidates it. For example, you had another
window in the fore ground, then came back to this window.

The argument to OnPaint method has a Graphics property that gives you a
Graphics object you can use.

If you call CreateGraphics, be certain to call graphics.Dispose when you are
done with it. The graphics passed to the OnPaint is disposed of for you.

This is you "real" problem.
this.Invalidate();

You draw on the control, then you invalidate the displayed area making the
OnPaint method to be called. If you want to see what was drawn during the
OnClick event do not invalidate the Window.

Hope this helps
Jay
 
Thanks you so much for your all help, Nicholas and Jay. I'm deeply
appreciated. So, you guy should be PRO because the instruction from two of
you are the same. I did follow the instruction but I cannot make it work,
because nothing happen. I didn't see any sizing drag around the control. Do
I missed something...

Here how I do it:

private void UserControl1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)

{

bMouse_Down = true;

this.Invalidate();

}



private void UserControl1_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)

{

if( bMouse_Down == true )

{

//ControlPaint.DrawSizeGrip( e.Graphics, Color.Red,
this.ClientRectangle);

ControlPaint.DrawGrabHandle( e.Graphics,
this.ClientRectangle, true,true );

this.Invalidate();

}

}
 
Thanks you so much Jay. I did try it without any "invalidate" but I still
not work. I might be missed something but I don't know what do I miss. Once
again, thanks you a lots for your help. Have a nice weekend :)

Kate
 
Yes, I did. I was removed the mouse down flag at OnMouseUp event. But seem
like unluck to get it work. Might be this is Microsoft bug, who's know,
right. Thanks you so much to follow my problem, Jay. I'm deeply appreciated
of your help. I owed you...

Kate
 
Kate,
Are you still having problems? Can you post the entire source for the
control that is having a problem?

I don't really see anything close to a Microsoft Bug here.

Hope this helps
Jay
 
Thanks you so much, Jay.

Here is the entire source code:

**********************************************

using System;

using System.Collections;

using System.ComponentModel;

using System.Drawing;

using System.Data;

using System.Windows.Forms;

namespace PictureBox_Extension

{

/// <summary>

/// Summary description for UserControl1.

/// </summary>

public class UserControl1 : System.Windows.Forms.PictureBox

{

/// <summary>

/// Required designer variable.

/// </summary>

private bool bMouse_Down = false;

private System.ComponentModel.Container components = null;

public UserControl1()

{

// This call is required by the Windows.Forms Form Designer.

InitializeComponent();

// TODO: Add any initialization after the InitForm call

}

/// <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 Component 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()

{

//

// UserControl1

//

this.Name = "UserControl1";

this.Size = new System.Drawing.Size(340, 310);

this.MouseUp += new
System.Windows.Forms.MouseEventHandler(this.UserControl1_MouseUp);

this.Paint += new
System.Windows.Forms.PaintEventHandler(this.UserControl1_Paint);

this.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.UserControl1_MouseDown);

}

#endregion

private void UserControl1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)

{

bMouse_Down = true;

this.Invalidate();

}

private void UserControl1_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)

{

if( bMouse_Down )

{

bMouse_Down = false;

this.Invalidate();

}

}

private void UserControl1_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)

{

if( bMouse_Down )

{

ControlPaint.DrawGrabHandle( e.Graphics, this.ClientRectangle, true, true );

}

}

}

}


**********************************************
 
Kate,
If I put your control on a form, then run the form. When I press down on the
mouse the control changes color, when I release the mouse the control
returns to the original color.

Remember in the UserControl designer no code will run for this control, only
for base controls, in the forms designer (on a form you put your control)
the mouse is used by the designer, your code cannot run.

Hope this helps
Jay

kate-luu said:
Thanks you so much, Jay.

Here is the entire source code:
<<snip>>
 
Good Morning Jay,

It means that I'm no luck at all. But I'm still think that we must be able
to change the apparent of the control any ways we want at design time and
runtime, and it should be. Any ways, thanks you so much Jay, I'm really
appreciated for your help. Have a nice day...
 
Kate,
How is the designer going to know that you are clicking for the sake of the
designer or the control? The designer assumes you are designing, so it gets
all the mouse clicks. If you are really asking for in place editing, I'm not
sure how to do that, you will need to ask 'down the hall' in the
microsoft.public.dotnet.framework.windowsforms.designtime newsgroup.

If you add a public property (needs to be a property) to your user control
that changes your bMouse_Down variable. You will be able to set the property
in the Properties window of the designer. You control will then change
appearance.

Something like:
[Description("the mouse is down")]
[Category("Appearance")]
public bool Mouse_Down
{
get
{
return bMouse_Down;
}
set
{
bMouse_Down = value;
this.Invalidate();
}
}

Add the above code to your UserControl1, compile your project, then add the
control to a form. changing the above property in the properties window will
change the appearance of the control.

Hope this helps
Jay
 
Thanks you so much Jay, you're so wonderful and kindness person, help me
with all your best, I don't know how to address my appreciated, Jay. All the
thing you did show me are just for design time support only. But, the
feature I want at runtime. So, like you said, I think I have to post my
question at "microsoft.public.dotnet.framework", might be some expert there
like you can give me an answer. Have a nice day...JAY

Kate

Jay B. Harlow said:
Kate,
How is the designer going to know that you are clicking for the sake of the
designer or the control? The designer assumes you are designing, so it gets
all the mouse clicks. If you are really asking for in place editing, I'm not
sure how to do that, you will need to ask 'down the hall' in the
microsoft.public.dotnet.framework.windowsforms.designtime newsgroup.

If you add a public property (needs to be a property) to your user control
that changes your bMouse_Down variable. You will be able to set the property
in the Properties window of the designer. You control will then change
appearance.

Something like:
[Description("the mouse is down")]
[Category("Appearance")]
public bool Mouse_Down
{
get
{
return bMouse_Down;
}
set
{
bMouse_Down = value;
this.Invalidate();
}
}

Add the above code to your UserControl1, compile your project, then add the
control to a form. changing the above property in the properties window will
change the appearance of the control.

Hope this helps
Jay

Kate Luu said:
Good Morning Jay,

It means that I'm no luck at all. But I'm still think that we must be able
to change the apparent of the control any ways we want at design time an d
runtime, and it should be. Any ways, thanks you so much Jay, I'm really
appreciated for your help. Have a nice day...

on
the control,
only
 
Kate,
The sample you gave me works at runtime! (for me)

Hope this helps
Jay

Kate Luu said:
Thanks you so much Jay, you're so wonderful and kindness person, help me
with all your best, I don't know how to address my appreciated, Jay. All the
thing you did show me are just for design time support only. But, the
feature I want at runtime. So, like you said, I think I have to post my
question at "microsoft.public.dotnet.framework", might be some expert there
like you can give me an answer. Have a nice day...JAY

Kate

Kate,
How is the designer going to know that you are clicking for the sake of the
designer or the control? The designer assumes you are designing, so it gets
all the mouse clicks. If you are really asking for in place editing, I'm not
sure how to do that, you will need to ask 'down the hall' in the
microsoft.public.dotnet.framework.windowsforms.designtime newsgroup.

If you add a public property (needs to be a property) to your user control
that changes your bMouse_Down variable. You will be able to set the property
in the Properties window of the designer. You control will then change
appearance.

Something like:
[Description("the mouse is down")]
[Category("Appearance")]
public bool Mouse_Down
{
get
{
return bMouse_Down;
}
set
{
bMouse_Down = value;
this.Invalidate();
}
}

Add the above code to your UserControl1, compile your project, then add the
control to a form. changing the above property in the properties window will
change the appearance of the control.

Hope this helps
Jay

Kate Luu said:
Good Morning Jay,

It means that I'm no luck at all. But I'm still think that we must be able
to change the apparent of the control any ways we want at design time
an
down
 
Kate,
O.K. lets go back to the beginning!

Do you want the control to change colors or do you want functional sizing
handles in the corners?

If you want "drag sizing stuff", you need to manually draw the "drag sizing
stuff". The DrawGrabHandle will draw a single handle. You were drawing that
single handle in the entire client area of your control.

Normally there are 8 handles you need to draw, plus a border. You need to
call DrawGrabHandle 8 times once for each corner & once for each mid-point
on the side.

I use ControlPaint.DrawSelectionFrame to draw the border, draw the border
before you draw the handles.

Then you need to handle the Mouse move, mouse down, mouse up events. To get
the sizing functional

In my project I have code to draw the sizing handles working, I have yet to
finish the code for getting the mouse to work.

A couple projects that may have the code you need includes:
http://www.icsharpcode.net/opensource/sd/
http://windowsforms.net/Default.aspx?tabindex=5&tabid=47

Both have code available.

Hope this helps
Jay


Kate Luu said:
Good Morning Jay,

No way Jay. If it worked for you, it should work for me too. While it works
for you and not for me than. The control just change the color, it never
show the drag sizing stuff. You should be kidding me. Have a nice day...

Kate
<<snip>>
 
Back
Top