|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
GDI QuestionI have a quick question: I am trying to draw a border around a control with GDI to highlight it. Here is my simple test code added onto a new Usercontrol to make it's border turn off and on with a user's click: Public Class UserControl1 Inherits System.Windows.Forms.UserControl Private _draw as Boolean Private _pen as System.Drawing.Pen Private Sub UserControl1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Instantiate pen _pen = New System.Drawing.Pen(System.Drawing.Color.Red, 4) End Sub Private Sub UserControl1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Click 'Invert the draw flag _draw = Not _draw 'Redraw form MyBase.Refresh() End Sub Protected Overreides Sub OnPaint(ByVal e as PaintEventArgs) Dim g As Graphics = e.Graphics If (_draw) Then g.Clear(Me.BackColor) g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality g.DrawRectangle(_pen, MyBase.DisplayRectangle) g.Fluch(Drawing2d.FlushIntention.Sync) End If End Sub End Class The problem? The toggling only works if I click the control and then drag it off the viewable area of the screen, and then drag it back onto the viewable area again. How do I force the rectangle to auto-redraw? Thanks! -Joel Hi,
Invalidate the control in the click event to get it to redraw. Ken -------------------- Show quoteHide quote "Joel Whitehouse" <joelwhiteho***@hotmail.com> wrote in message news:OIid4C8FGHA.1028@TK2MSFTNGP11.phx.gbl... > Hey guys, > > I have a quick question: > > I am trying to draw a border around a control with GDI to highlight it. > Here is my simple test code added onto a new Usercontrol to make it's > border turn off and on with a user's click: > > Public Class UserControl1 > Inherits System.Windows.Forms.UserControl > > Private _draw as Boolean > Private _pen as System.Drawing.Pen > > Private Sub UserControl1_Load(ByVal sender As System.Object, ByVal e As > System.EventArgs) Handles MyBase.Load > > 'Instantiate pen > _pen = New System.Drawing.Pen(System.Drawing.Color.Red, 4) > End Sub > > Private Sub UserControl1_Click(ByVal sender As System.Object, ByVal e As > System.EventArgs) Handles MyBase.Click > > 'Invert the draw flag > _draw = Not _draw > 'Redraw form > MyBase.Refresh() > End Sub > > Protected Overreides Sub OnPaint(ByVal e as PaintEventArgs) > > Dim g As Graphics = e.Graphics > > If (_draw) Then > g.Clear(Me.BackColor) > g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality > g.DrawRectangle(_pen, MyBase.DisplayRectangle) > g.Fluch(Drawing2d.FlushIntention.Sync) > End If > > End Sub > End Class > > The problem? The toggling only works if I click the control and then drag > it off the viewable area of the screen, and then drag it back onto the > viewable area again. How do I force the rectangle to auto-redraw? > > Thanks! > > -Joel Ken Tucker [MVP] wrote:
> Invalidate the control in the click event to get it to redraw. This helps the regularity of the transition a lot, but I have one more question: the rectangle that gets rendered is often choppy and irregular. The lines are of inconsistent width, almost as if they were splintered... What can I do to make sure that the whole rectangle gets drawn? Thanks again! -Joel Hi,
if I leave out the smoothingmode and draw the border like this, then the border isn't choppy or irregular If (_draw) Then g.Clear(Me.BackColor) g.DrawRectangle(_pen, 0, 0, Me.Width, Me.Height) g.Flush(Drawing2D.FlushIntention.Sync) End If Hth Greetz Peter -- Show quoteHide quoteProgramming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. (Rich Cook) "Joel Whitehouse" <joelwhiteho***@hotmail.com> schreef in bericht news:Oktunz8FGHA.2704@TK2MSFTNGP15.phx.gbl... > Ken Tucker [MVP] wrote: > > > > Invalidate the control in the click event to get it to redraw. > > This helps the regularity of the transition a lot, but I have one more > question: the rectangle that gets rendered is often choppy and > irregular. The lines are of inconsistent width, almost as if they were > splintered... What can I do to make sure that the whole rectangle gets > drawn? > > Thanks again! > > -Joel Peter Proost wrote:
> if I leave out the smoothingmode and draw the border like this, then the Thanks Ken and Peter! When I took out the smoothingmode line from the > border isn't choppy or irregular > > If (_draw) Then > g.Clear(Me.BackColor) > g.DrawRectangle(_pen, 0, 0, Me.Width, Me.Height) > g.Flush(Drawing2D.FlushIntention.Sync) > End If > > Hth Greetz Peter > > drawing code, used an inalidation in the flag changing code, and removed the MyBase.Refresh() line, everything worked as I expected. -Joel P.S. I understand why the control invalidaiton is necessary, but why would the smoothingmode line cause so much trouble?
control arrays
Put standard output into file Spell checker howto make a console app start with window minimized How can you add a custom user control to a datagridview? Export from VB.NET 2003 to an Excel spreadsheet To use ShellExecute or not? Pointer in a structure Datagrid Plus Sign Event Getting list of available classes in framework |
|||||||||||||||||||||||