开发者

transparent background for tabcontrol in windows forms ce

开发者 https://www.devze.com 2023-02-28 17:09 出处:网络
I am trying to customize tabcontrol in compact framework but i cannot find solution for set transparent background for this control. I am trying to override\"OnPaintBackground()\" method to begin set

I am trying to customize tabcontrol in compact framework but i cannot find solution for set transparent background for this control. I am trying to override "OnPaintBackground()" method to begin set background on it but this function not called. How i can u开发者_Go百科se this function to be called when the control created?


EDIT: I read your comment about trying to set the background color of the container, not the individual tabs, and I did some experimenting and research.

It seems that the OnDrawItem method of the TabControl class is used to draw the tab "headers" (the part of the control that contain each tab's text, which the user clicks on to select tabs), as well as the background of the container (everything besides the selected tab's contents, which are drawn by the tab itself in its OnPaintBackground method).

You can get the background of the TabControl to be transparent by overriding its OnDrawItem method, but simply filling the bounds passed with the DrawItemEventArgs will also make the tab headers transparent, making them un-clickable (the click will go through the form, onto whatever's behind it).

The way I see it, you've got a couple of options to try to work around this:

  1. Clear the bounds passed to OnDrawItem, and then manually redraw each TabPage's header. This is a pain, because there's no way to get each tab's header without calculating it manually using the page's text, its font size, its borders, and who knows what else. There doesn't seem to be any exposed API for drawing the TabPages' headers separately from the TabControl's background.
  2. Instead of making the TabControl's background and the TabPages' headers completely transparent, only make them semi-transparent, leaving the headers clickable. This might not look as pretty, but it's a lot easier than the first option. To do this, you'd need to set the form's AllowTransparency property to true, and then use the code below:
    class TransparentisTabControl : TabControl
    {
        //Without declaring this as new, you'd probably get a warning saying this property hides a member of the base class.
        //The base class's BackColor property, as I'm sure you've found out,
        //is hidden with attributes anyway, so it doesn't really matter.
        public new Color BackColor {get; set;}

        public TransparentishTabControl(Color backColor)
        {
            if (backColor.A == 0)
                throw new ArgumentException("The alpha component of backColor cannot be zero, or this TransparentisTabControl's tab pages won't be selectable.");
            BackColor = backColor;
        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            base.OnDrawItem(fake);
            e.Graphics.Clear(BackColor);
        }
    }

This code works fine for me, but it might behave differently on your target platform. Let me know if you need any more help/clarification :)

Are you trying to make the tab control transparent, or the individual tab pages? Overriding OnPaintBackground in a TabControl-derrived class isn't enough because each TabPage paints itself as well. You need a custom class that is derived from TabPage and has an override of OnPaintBackground.

    class TransparentTabPage : TabPage
    {
        public TransparentTabPage()
            : base("TransparentTabPage")
        {

        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            base.OnPaintBackground(e);

            Form form = FindForm();

            e.Graphics.CompositingMode = CompositingMode.SourceCopy;
            using (SolidBrush sb = new SolidBrush(form.TransparencyKey))
                e.Graphics.FillRectangle(sb, Bounds);
        }
    }

For this to work, your form needs to have its TransparencyKey set to something, and the value of its AllowTransparency property must be true.


I cannot find ondrowitem in tabcontrol to override it. windows CE environment(smartDevice) not support all functionallity in windows forms

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号