[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 729
  • Last Modified:

Nutty Custom-Shaped WinForm BG

I have a lovely WinForm that uses a custom shape.  The bitmap used in its BackgroundImage property has a transparent color, and the WinForm has that TransparencyKey property and I have all that set correctly.  Works great.

But what I really want my form to be able to do is to set its own BackgroundImage property dynamically, using an on-board ImageList control.

Problem is, when I dynamically have the form set its own background image, I lose that wonderful transparency.  Suddenly, the entire image that I use to create my custom-shaped form is visible, in all its ugliness.  The color that should be transparent (white) is suddenly opaque (black).  All because rather than setting my form's BackgroundImage property at design-time and compiling it in, I'm trying to dig it out of an ImageList.

Any ideas on how I can get dynamic background images to work here?  I don't want to load them from a subfolder on the fly or anything kooky like that.  The images need to be on-board.

Thanks.
0
bryker
Asked:
bryker
  • 2
1 Solution
 
cybercrewCommented:
Have you tried embedding the images in a resource file?  One other thing you might try is calling this.Refresh() after changing the background.

1.  In your Solution Explorer expand the Properties folder.
2. Double click on  Resources.resx
3. In Resource Designer click on  Add Resource.
4. The select "Add Existing File"
5. Locate your image files and them.
6. For each image check the properties window to make sure they are embedded into the resource file and not linked.
7.  You can now access your images by typing <your project namespace>.Properties.Resources.<name of Image>

Example:  (Blue_Lace_16 and Feather_Texture are the names of image files embedded in my Resources.resx file)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ResourceFiles
{
    public partial class Form1 : Form
    {
        private bool toggle = false;
        public Form1()
        {
            InitializeComponent();
           
           
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.BackgroundImage = ResourceFiles.Properties.Resources.Blue_Lace_16;
           
        }


        private void Form1_DoubleClick(object sender, EventArgs e)
        {
            if (toggle == false)
            {
                toggle = true;
                this.BackgroundImage = ResourceFiles.Properties.Resources.FeatherTexture;
            }
            else{
                toggle = false;
                this.BackgroundImage = ResourceFiles.Properties.Resources.Blue_Lace_16;}
            this.Refresh();
        }
    }
}
0
 
brykerAuthor Commented:
Sorry, Cyber.  I really am going to get to this in the next few days, then I'll be able to grade your suggestion.  Thanks for your patience.
0
 
brykerAuthor Commented:
Okay, Cyber, I haven't gotten around to testing this, but I can't leave you hanging any longer in good conscience.  I'm assuming it's going to work for me.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now