Question Adding a visible drag handle for splitcontainer in windows form

You can either create your own control and override the OnPaint method or use a standard SplitContainer and handle the Paint event. You would then use e.Graphics to draw your handle, e.g. by calling DrawImage to draw a handle image you created earlier. You can use the SplitterRectangle to determine where to draw.
 
Thanks jmcilhinney,

I have been trying to execute it but I still can't figure a way to add the image on the SplitterRectangle using the DrawImage. I found this code that made use of the SplitterRectangle but my problem is showing the image. Would you be able to give more explanation or sample code?

Many thanks.


Private Sub SplitContainer2_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles SplitContainer2.Paint
Dim mImage As Image = Image.FromFile(mCurrentDirectory & "\drag.jpg")
ControlPaint.DrawGrabHandle(e.Graphics, SplitContainer2.SplitterRectangle, True, True)
End Sub
 
I saw that same code when I searched, but a grab handle is not what you want. Grab handles are the little black or white boxes you see on the corners or edges of controls in the VS designer.

You need to call e.Graphics.DrawImage to draw the Image. You'll also need to perform a bit of maths on the SplitterRectangle because, presumably, you won't want to fill the entire splitter bar with the image.
 
Also, you aren't going to want to create a new Image object from a file on disk every time you paint the SplitContainer, which can happen many times. You should be reading the file into an Image object once and once only, at startup, then using that same Image object each time.
 
Thanks jmcilhinney,

I finally been able to add the image to the splitcontainer with the code below. My problem now is how to center the image in the splitterRectangle area. The images appears in the left of the SplitterRectangle. Any suggestions?

Thanks a lot.
Yuan


Dim mImage As Image = Image.FromFile(mCurrentDirectory & "\drag.png")
Private Sub SplitContainer2_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles SplitContainer2.Paint
e.Graphics.DrawImage(mImage, SplitContainer2.SplitterRectangle, 0, 0, 500, 7, GraphicsUnit.Pixel)
End Sub
 
Thanks jmcilhinney,

I finally been able to add the image to the splitcontainer with the code below. My problem now is how to center the image in the splitterRectangle area. The images appears in the left of the SplitterRectangle. Any suggestions?

Thanks a lot.
Yuan


Dim mImage As Image = Image.FromFile(mCurrentDirectory & "\drag.png")
Private Sub SplitContainer2_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles SplitContainer2.Paint
e.Graphics.DrawImage(mImage, SplitContainer2.SplitterRectangle, 0, 0, 500, 7, GraphicsUnit.Pixel)
End Sub

The second argument to DrawImage is a Rectangle, within which the Image is drawn. You've already got the Rectangle occupied by the entire splitter bar, so it's simple maths to create a new Rectangle with the appropriate size and position relative to that.
 

Latest posts

Back
Top