C# version here.
The following code uses a single Paint event handler to draw the same "picture" on every control on a form. Specifically it draws a line from the top, left corner of the form to the bottom, right corner and "over" any controls that happen to be on that diagonal.You can use a similar pattern to draw any "picture" you like on your form. The important part is the use of PointToScreen and PointToClient to convert coordinates from the form's frame of reference to that of the control currently being painted.VB.NET Code:
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Dim ctl As Control = Me.GetNextControl(Me, True) Do Until ctl Is Nothing AddHandler ctl.Paint, AddressOf Form1_Paint ctl = Me.GetNextControl(ctl, True) Loop End Sub Private Sub Form1_Paint(ByVal sender As Object, _ ByVal e As PaintEventArgs) Handles Me.Paint 'Get the points relative to the form. Dim start As New Point(0, 0) Dim [end] As New Point(Me.ClientSize) Dim paintee As Control = DirectCast(sender, Control) 'Translate the points relative to the control being painted. start = paintee.PointToClient(Me.PointToScreen(start)) [end] = paintee.PointToClient(Me.PointToScreen([end])) e.Graphics.DrawLine(Pens.Black, start, [end]) End Sub End Class
Note also that you can attach the event handler to your controls any way you like. The code I've included in the Load event handler there means that any controls you add in the designer will be included. Alternatively you could include the other controls in the method's Handles clause along with the form. You can also use the AddHandler statement to include controls added at run time.




Reply With Quote