DrawReversibleFrame?
-
Hi, Should this be enough to draw a dotted rectangle on my form?
private void Form1_click(object sender, System.EventArgs e) { Rectangle r = new Rectangle(5,5,15,15); ControlPaint.DrawReversibleFrame(r, Color.Red, FrameStyle.Dashed); }
If not, what am I missing? Thanks! Mel -
Hi, Should this be enough to draw a dotted rectangle on my form?
private void Form1_click(object sender, System.EventArgs e) { Rectangle r = new Rectangle(5,5,15,15); ControlPaint.DrawReversibleFrame(r, Color.Red, FrameStyle.Dashed); }
If not, what am I missing? Thanks! Mel -
Only if your form happens to be where you draw the rectangle on the screen. Perhaps you should ask about what you are trying to accomplish, instead. --- b { font-weight: normal; }
Thank you, that makes sense. :) (I wasn't thinking and expected to see it at 5,5,15,15 on the form rather than the screen.) Another question though. I'd actually like to use
DrawFocusRectangle
on a button, and did get it working yesterday, but after I changed a couple properties of my button (like tabstop #, size, and location), it stopped working. I added aMessageBox
just before theDrawFocusRectangle
call and after I hit the ok button, the focus rectangle did draw (code below). I also added a Click event (on some label) and moved the code below (without theMessageBox
) to the event, and then it worked too.button2.Focus(); int x = button2.ClientRectangle.Location.X + 3; int y = button2.ClientRectangle.Location.Y + 3; int w = button2.ClientRectangle.Width - 6; int h = button2.ClientRectangle.Height - 6; Rectangle r = new Rectangle(x,y,w,h); MessageBox.Show(""); ControlPaint.DrawFocusRectangle(Graphics.FromHwnd(button2.Handle), r, Color.Black, Color.Black);
Anyone know why it won't work without some external help? Thanks again!!! Mel :confused: -
Thank you, that makes sense. :) (I wasn't thinking and expected to see it at 5,5,15,15 on the form rather than the screen.) Another question though. I'd actually like to use
DrawFocusRectangle
on a button, and did get it working yesterday, but after I changed a couple properties of my button (like tabstop #, size, and location), it stopped working. I added aMessageBox
just before theDrawFocusRectangle
call and after I hit the ok button, the focus rectangle did draw (code below). I also added a Click event (on some label) and moved the code below (without theMessageBox
) to the event, and then it worked too.button2.Focus(); int x = button2.ClientRectangle.Location.X + 3; int y = button2.ClientRectangle.Location.Y + 3; int w = button2.ClientRectangle.Width - 6; int h = button2.ClientRectangle.Height - 6; Rectangle r = new Rectangle(x,y,w,h); MessageBox.Show(""); ControlPaint.DrawFocusRectangle(Graphics.FromHwnd(button2.Handle), r, Color.Black, Color.Black);
Anyone know why it won't work without some external help? Thanks again!!! Mel :confused:You are calling the Focus event, I assume that this will redraw the control to reflect the changed status. That redraw will however not take place until you returned control to the system. Without the message box that will not happen until after you have drawn the rectangle, so it would just be drawn over. When you show a message box that gives the control to the system. Calling DoEvents will have the same result. However, just drawing on top of a control is not the well behaved way to change the apperance of the control. You should subscribe to the OnPaint event to do it properly. Then you don't have the problem that the rectangle disappears whenever the control is redrawn. --- b { font-weight: normal; }
-
You are calling the Focus event, I assume that this will redraw the control to reflect the changed status. That redraw will however not take place until you returned control to the system. Without the message box that will not happen until after you have drawn the rectangle, so it would just be drawn over. When you show a message box that gives the control to the system. Calling DoEvents will have the same result. However, just drawing on top of a control is not the well behaved way to change the apperance of the control. You should subscribe to the OnPaint event to do it properly. Then you don't have the problem that the rectangle disappears whenever the control is redrawn. --- b { font-weight: normal; }
Thank you very much for that explanation! That clears up one of the mysteries. What my problem has been from the start is that when I load a tabpage, if the first control is one of my buttons, it doesn't highlight and you can't tell that it has focus. What I've been doing to get around this is to focus on the control that's one tabstop before my button and
SendKeys.Send("{Tab}");
which does highlight the button. As soon as a button on the tabpage has been highlighted, all buttons will highlight when focused on until I leave the tabpage and return again. So I've deleted the focus call since the button has tabstop 0. I want the rectangle to disappear when focus is lost, so if it would just draw this first time, I'd be happy (is there a way to tell the system that it has control without having to call anything?). But nothing at all draws after deleting thebutton2.Focus()
, regardless of whether there's a MessageBox before, after, or not at all. So I tried adding the PaintEventArg for the button and moved the code into it. I also added a bool called focus since I only want the button to have the rectangle when it's in focus.private void focusPaint(object sender, System.Windows.Forms.PaintEventArgs e) { if (focus == true) { int x = toggleLength.ClientRectangle.Location.X + 3; int y = toggleLength.ClientRectangle.Location.Y + 3; int w = toggleLength.ClientRectangle.Width - 6; int h = toggleLength.ClientRectangle.Height - 6; Rectangle r = new Rectangle(x,y,w,h); ControlPaint.DrawFocusRectangle(e.Graphics, r, Color.Red, Color.Red); focus = false; } }
This results in me never being able to move to another control (using the up/down arrow keys - only with the mouse). When I returned control to the system (by adding a Messagebox above theif (focus == true)
), I could move without problem. I also noticed that if I replace thee.Graphics
with my originalGraphics.FromHwnd(toggleLength.Handle)
, it wouldn't draw with or without system control, which I don't understand. (Maybe if I get that working I wouldn't need to add a Paint event?) I'm not sure what else to try. Thanks so much for any thoughts!!!!!! Mel -- modified at 15:59 Friday 28th April, 2006