I have a control derived from System.Windows.Forms.UserControl which is placed in a customized grid. When the first column of the grid is frozen and the grid is scrolled by a few pixels, the second column becomes partially visible such that the control in this column also becomes partially visible.
To achieve this I want to be able to display only the part of the control that is visible either by hiding/clipping the left portion of the control.
I don’t want to modify OnPaint of the control as I don’t have access to the control’s code. I can change the code in the grid where the control is displayed.
I tried the following which didn’t work:
1. Invalidating only part of the control that has to be visible.
2. Using GraphicsPath as follows:
System.Drawing.Drawing2D.GraphicsPath controlPath = new System.Drawing.Drawing2D.GraphicsPath();
controlPath.AddRectangle(visibleRect);
editControl.Region = new Region(controlPath);
Any other ideas?
I got this working solution:
Using GraphicsPath worked. I wasn’t setting the clip rectangle properly due to which I couldn’t see the clipping happen. Instead of setting the xOffset of the clip region wrt to the Grid I had to set the clip region wrt to the control.
The window region is a collection of pixels within the window where the operating system permits drawing. The operating system does not display any portion of a window that lies outside of the window region. The coordinates of a control’s region are relative to the upper-left corner of the control, not the client area of the control( http://msdn.microsoft.com/en-us/library/ddhy6052(vs.71).aspx)