Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can't break out of my ClipRectangle and I want to cry

When overriding the OnPaint method of a custom control I'm supplied with a PaintEventArgs, which contains a Graphics object and a ClipRectangle. The ClipRectangle is too small for my tastes and so I want to either extend it by say, 100 pixels, or ignore it completely.

No matter what I do, I can't seem to break out of the ClipRectangle. So far I've tried:

e.ClipRectangle.Inflate(100, 0);
e.Graphics.ResetClip();
e.Graphics.SetClip(new Rectangle(x, y, w, h));

The parent control is calling Invalidate(true) (to force an invalidation of all the childs' child controls).

I've also done some googling and looked on bob powell's site but can't find nuffin.

ResetClip resets the clipping region for the Graphics object to an infinitely large Rectangle, but the Graphics object cheerfully continues to use the provided ClipRectangle.

Help.

like image 904
Matt Jacobsen Avatar asked Apr 26 '11 09:04

Matt Jacobsen


People also ask

How to cry when you can’t Cry?

The following are the processes you need to do to cry when you can’t cry: You need to find a comfortable place for you to cry in. You can use your bedroom for your crying session. You can also use your bathroom to get your crying out in this private room.

Why can’t I keep myself from crying?

Eventually, you may not even have to make an effort to keep yourself from crying — it just doesn’t happen. People often start to see crying as a sign of weakness when other people, including parents, siblings, and peers, shame them for crying in childhood. An inability to cry can also develop as a learned behavior.

How to stop crying on the beach when traveling?

When you are having your vacation, you can find a quiet spot in the beach which can get you to release your crying. You need to clear your head from distractions that might stop you from the crying you need to release. You can do this by going to that special place where you can have your privacy to cry your eyes out.

Why don’t my tears come out when I Cry?

Even if something deeply upsetting happens, you might not display much of a reaction. There’s nothing affecting your physical ability to cry, but the tears just don’t come. If you believe crying exposes your vulnerability or suggests weakness, you might hold back your tears intentionally.


2 Answers

Recap: It sounds like you have a child control with a custom paint method. You want to paint the control in an area that is larger than the control's bounds itself. For instance if you place your control at (10, 10, 100, 100) you want to draw the area at (0, 10, 110, 100).

You can't exactly do that, the control's HDC that is passed to paint method is for that control's native window handle. It is clipped by the OS and AFAIK there is no way to draw outside of it with the HDC given to the paint method.

What you can do: One option is to override the parent window's create method and remove the window style WS_CLIPCHILDREN (0x02000000), then draw the child in the parent's paint method.

Another option is to just expand the area of the child window so that it encompasses the area you want to draw. If you also override the OnPaintBackground method of your control you can prevent the background paint from clearing the parent's rendering. This is problematic though since the parent will clip the area of the child and not refresh it. Thus you still need to remove the parent's WS_CLIPCHILDREN for it to work.

I'm sure there are other possibilities all of which are basically the same result, you can't do that.

like image 114
csharptest.net Avatar answered Nov 15 '22 15:11

csharptest.net


Rectangle is a struct, not a class - it's passed as value so calling e.ClipRectangle.Inflate(100, 0); can't help even theoretically.

Next thing you have to know is that e.Graphics is related to hardware device context, so Windows won't let you expand painting area. Solution is to get device context of the parent window and then paint wherever you want to. This is how to do it:

[DllImport("User32.dll")]
static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("User32.dll")]
static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDC);

IntPtr dcPtr = GetDC(this.ParentForm.Handle);
Graphics g = Graphics.FromHdc(dcPtr);

// your code here

// don't forget to release it
g.Dispose();
ReleaseDC(this.ParentForm.Handle, dcPtr);
like image 20
rotman Avatar answered Nov 15 '22 15:11

rotman