473,748 Members | 9,933 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# application - system transparent control inside user drawn control...

2 New Member
This one's a hairy one to explain. What I've got is a class called "VistaGroup Box" that I put some nice-looking Vista-style gradients in the background and made to look like the ribbon categories you see in Office 2007. I got it working great, and created a few more controls, including a "VistaButto n", which is primarily transparent when the cursor does not hover over it. However, when I added a standard Panel in the VistaGroupBox and put my button inside the Panel (I want the Panel to auto-scroll), the VistaButton's transparent background started as though it were in the upper-left-hand corner of the VistaGroupBox, which was not where the button actually was at all! The button draws correctly when outside the Panel.

So, what I've got is this:
Expand|Select|Wrap|Line Numbers
  1. VistaGroupBox
  2.  +-Panel
  3.     +-VistaButton
In order to draw my transparent controls, I've made a function that does this: (Some controls, like Button, don't have transparency support at all - I got this from the Mono implementation)
Expand|Select|Wrap|Line Numbers
  1.         public delegate void InvokePaintDelegate(Control c, PaintEventArgs e);
  2.         public delegate void InvokePaintBackgroundDelegate(Control c, PaintEventArgs e);
  3.         /// <summary>
  4.         /// This function looks like a mess primarily because of the #if ... #endifs.  The problem is, the "BROKEN WINDOWS"
  5.         /// section is how WINDOWS does it, which is a problem for going through multiple transparent areas for whatever reason.
  6.         /// MY_CORRECT works if you're only going from user drawn transparency to user drawn transparency - that is, none of your
  7.         /// controls use Control.OnPaintBackground (like a Transparent Panel).  WINDOWS_HACK works in all conditions - too bad it uses
  8.         /// a bit more memory due to the image creation.
  9.         /// </summary>
  10.         /// <param name="c">The control to paint the parent to</param>
  11.         /// <param name="pevent">The PaintEventArgs from the OnPaintBackground call</param>
  12.         /// <param name="InvokePaint">The control's InvokePaint method - because it is protected.</param>
  13.         /// <param name="InvokePaintBackground">The control's InvokePaintBackground method - because it is protected.</param>
  14.         public static void DrawParent(Control c, PaintEventArgs pevent, InvokePaintDelegate InvokePaint, InvokePaintBackgroundDelegate InvokePaintBackground)
  15.         {
  16.             PaintEventArgs parent_pe;
  17.             GraphicsState state;
  18.             Rectangle clipRect;
  19.  
  20.             Graphics g;
  21. #if BROKEN_WINDOWS
  22.             IntPtr hdc = pevent.Graphics.GetHdc();
  23.             g = Graphics.FromHdc(hdc);
  24.             if (c.Region != null)
  25.                 g.Clip = c.Region;
  26.             else
  27.                 g.Clip = new Region(c.ClientRectangle);
  28.             clipRect = new Rectangle(pevent.ClipRectangle.X + c.Left, pevent.ClipRectangle.Y + c.Top, pevent.ClipRectangle.Width, pevent.ClipRectangle.Height);
  29. #elif MY_CORRECT
  30.             g = pevent.Graphics;
  31.             clipRect = new Rectangle(pevent.ClipRectangle.X + c.Left, pevent.ClipRectangle.Y + c.Top, pevent.ClipRectangle.Width, pevent.ClipRectangle.Height);
  32. #elif WINDOWS_HACK
  33.             Image img = new Bitmap(c.Parent.Width, c.Parent.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
  34.             g = Graphics.FromImage(img);
  35.             clipRect = new Rectangle(c.Location, c.Size);
  36.             g.Clip = new Region(clipRect);
  37. #endif
  38.  
  39.             parent_pe = new PaintEventArgs(g, clipRect);
  40.  
  41.             state = g.Save();
  42. #if !WINDOWS_HACK
  43.             g.TranslateTransform(-c.Left, -c.Top);
  44. #endif
  45.             InvokePaintBackground(c.Parent, parent_pe);
  46.             g.Restore(state);
  47.  
  48.             state = g.Save();
  49. #if !WINDOWS_HACK
  50.             g.TranslateTransform(-c.Left, -c.Top);
  51. #endif
  52.             InvokePaint(c.Parent, parent_pe);
  53.             g.Restore(state);
  54.  
  55. #if BROKEN_WINDOWS
  56.             g.Dispose();
  57.             pevent.Graphics.ReleaseHdc(hdc);
  58. #elif WINDOWS_HACK
  59.             g.Dispose();
  60.             pevent.Graphics.DrawImage(img, new Rectangle(new Point(0,0), c.Size), new Rectangle(c.Location, c.Size), GraphicsUnit.Pixel);
  61.             img.Dispose();
  62. #endif
  63.         }
  64.  
You can see that I've got 3 preprocessor directives here, each should be used exclusively. BROKEN_WINDOWS behaves exactly like the Windows default, WINDOWS_HACK works properly, except I'm creating images, and so it tends to be a bit slow.

Anyone got some time to help me figure this one out?
Sep 30 '07 #1
0 2870

Sign in to post your reply or Sign up for a free account.

Similar topics

3
3103
by: Steve Koon | last post by:
Any thoughts on getting this project to work or suggesting another method would be appreciated. Steve ========================================== Project: A Windows Form which acts as a Whiteboard. The Form contains 3
3
9834
by: Sushil Srivastava | last post by:
Hi Guys, Would you be able to help me using C# GUI (with user interface component) in my MFC application. I have used managed extension, COM-interops, etc but problem is this C# component has user interface. how should have get window handle from managed windows? Thanks in advance. Sushil
5
6461
by: Mark Deibert | last post by:
I'm a former VB6 coder. Quit a few years ago. Now I'm back and trying to teach myself VB.NET. I don't remember having this much difficulty learning VB6. I'm totally stuck on something and need your expert guidance. I just want to load a GIF with a transparent background into a container with a transparent background. So I can move the GIFs over and under each other. You know, as in basic game stuff. I can load the transparent GIF no...
5
2159
by: Brian Henry | last post by:
Hello, I want to make a control that has a scrollable Gantt type display (not really one but will look similar to one) this is going to be custom drawn (i already have the drawing procedure completed for the overriden onpaint event) but the problem is this can be a very long scrollable viewport (part that scrolls inside the control) while the control will be relatively 800x600 in size)... i tried to do this with a panel scrolling inside...
4
1486
by: Adam Maltby | last post by:
Hi, I need to do a transparent label but with a difference. I have drawn on my form a custom background using CreateGraphics as I needed an ellipse looking fill in a rectangle - so the actual background colour of the form is still set to "control" colour. If I use the background transparent setting instead of seeing my drawn on background and it's gradient i see the forms background colour. If the label was on a solid coloured bit of...
1
1596
by: Lucile | last post by:
Hi All, I am trying to obtain some sort of a "Model" based on horizontal & vertical lines (like a frame) drawn on a form or on a PictureBox. For frames configuration purposes, I created a white background image; and that works fine. But the "Model" will be used to cut off pieces of a given image with MS Office. I would therefore like that the lines be appearent on the image to be cut; but the the underlying control be transparent (so as...
4
2878
by: Adam Clauss | last post by:
I have a completely user-drawn user control in which I enabled transparency (follownig suggestions at http://www.c-sharpcorner.com/UploadFile/Nildo%20Soares%20de%20Araujo/TransparentControls11152005074108AM/TransparentControls.aspx) The user control does not contain any other controls; I manually draw contents in the OnPaint override I created. My problem is that the control does not always draw itself properly. Sometimes NONE of the...
3
4873
by: NickP | last post by:
Hi there, I have a usercontrol that inherits from Windows.Forms.Button. In the OnCreateControl method I set the style of the control so that it supports transparent backcolor and set the background color to transparent. ..... Protected Overrides Sub OnCreateControl()
1
3020
by: Kevinst | last post by:
Hello there, I have got a problem and several approaches but none is really staisfying: Lets assume we have a form. On its surface we have got several animations (like a blinking light). This form is not owner drawn. Everything is drawn by child controls. Now we want to go ahead and create an animation that is on top of everything of that form. Lets assume this animation was an arrow that is moving from the top left corner to the...
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9325
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9249
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8244
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4607
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4876
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3315
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.