So I'm creating a custom user control in .NET 2.0 and this user control is basically a combined user control ( it has a picture on it with a label ). And it's function is basically to act like a button, it can be clicked etc.
Now the problem I have is for whatever reason the border style doesn't support 3d borders...
So when the button is unclicked it should have the Border3dStyle.Raised look. Then when it gets pressed it should have the Border3dStyle.Sunken look.
I achieved getting the Border3dStyle.Raised by overriding the OnPaint method. Like so...
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
ControlPaint.DrawBorder3D(e.Graphics, this.ClientRectangle, Border3DStyle.Raised);
}
I have other method I'd like to call when the button itself is clicked this is what I was thinking might work.
private void UserInkControl_Paint(object sender, PaintEventArgs e)
{
Rectangle borderRectangle = this.ClientRectangle;
ControlPaint.DrawB开发者_运维技巧order3D(e.Graphics, borderRectangle, Border3DStyle.Sunken);
}
I registered it in the load event
private void UserInkControl_Load(object sender, EventArgs e)
{
this.Paint += new System.Windows.Forms.PaintEventHandler(this.UserInkControl_Paint);
}
How can I call the UserInkControl_Paint when the click event is fired?
The Click
event won't work for what you're trying to do, because that particular event is only called after the mouse button is held down and then released. You should use the MouseDown
event to set a boolean property (say, _isDown
) to true
and then call .Refresh()
; use the MouseUp
event to set _isDown = false;
, then also just call .Refresh()
.
In the Paint
event, check the _isDown
property and call the DrawBorder3D
method with the appropriate parameters.
public void UserInkControl_Click(object sender, EventArgs ea)
{
UserInkControl.Refresh (); // Causes repainting immediately
// or
UserInkControl.Invalidate (); // Invalidates the whole painting surface,
//so when the message loop catches up, it gets repainted.
// There is also an overload of Invalidate that
// lets you invalidate a particular part of the button,
// So only this area is redrawn. This can reduce flicker.
}
You can call "myControl.Refresh()". It will redraw the whole control.
You're looking for the Invalidate
method.
精彩评论