Problem: Drawing under a custom control

G

Guest

Hi everyone,

First of all, why am I having this problem... I need a REAL transparent
label control. My custom label control needs to be able to be the child of
any other control and must only display text, not a background image or color.

So I created a control derived from System.Windows.Control, the problem is
that the parent control doesn't draw under my custom control, probably
because the parent thinks that the child will do it. However, I need the
parent to completely draw itself. Is there a way to do it?

Any help is appreciated, thanks!

Sylvain Fortin

public class ctrlLabel : Control
{
protected Brush m_brsBlack = null;

....

// OnPaintBackground
protected override void OnPaintBackground(PaintEventArgs pe)
{
}

// OnPaint
protected override void OnPaint(PaintEventArgs pe)
{
RectangleF rcArea = RectangleF.Empty;

// Calling base class OnPaint
base.OnPaint(pe);

try
{
if (m_brsBlack == null)
m_brsBlack = new SolidBrush(Color.Black);

rcArea = new RectangleF(0, 0, this.Width, this.Height);
pe.Graphics.DrawString(this.Text, this.Font, m_brsBlack, rcArea);
}
catch (Exception)
{
}
}
}
 
A

Alex Feinman [MVP]

Haven't tried it, but what if you set the parent form class bits to
~WS_CLIPCHILDREN?
 
G

Guest

Hi Alex,

Thanks for the quick reply. This is a very good idea and I didn't think
about it. However, I tried it and it didn't work. I tired it on the custom
control parent (which is another custom control) and I also tried on the main
form (the only form in the application).

Here is my code sample, just in case I made a mistake.

Thanks for your help,

Sylvain Fortin

....

// Retrieve the main form HWND
Capture = true;
IntPtr hWnd = MyUtils.GetCapture();
Capture = false;

// Get the current window style
lWindowStyle = MyUtils.GetWindowLong(hWnd, -16/*GWL_STYLE*/);

// Change the style of the main form
nReturn = MyUtils.SetWindowLong(hWnd, -16/*GWL_STYLE*/, (int)(lWindowStyle &
~0x02000000L)/*WS_CLIPCHILDREN*/);

....
 
G

Guest

Hah... been there, done that... Not possible. The transparency is not
supported on the system level...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top