1

I have a (what I think to be) very simple issue, but I'm ripping my hair our over it:

In my main class, in the main method, I have the following code:

Form window = new Form(); window.FormBorderStyle = FormBorderStyle.None; window.BackgroundImage = blurred; //blurred is a Bitmap window.SetBounds(bounds.X, bounds.Y, bounds.Width, bounds.Height); //bounds is a Rectangle window.Show(); 

This is an attempt to make a borderless window with a background image. I run the code and there are no errors- yet no form appears.

What am I doing wrong?

0

2 Answers 2

6

Your code doesn't work because you haven't started an event loop to keep the process running. Making your code work is as simple as changing

Form window = new Form(); window.FormBorderStyle = FormBorderStyle.None; window.BackgroundImage = blurred; //blurred is a Bitmap window.SetBounds(bounds.X, bounds.Y, bounds.Width, bounds.Height); //bounds is a Rectangle window.Show(); 

to

Form window = new Form(); window.FormBorderStyle = FormBorderStyle.None; window.BackgroundImage = blurred; //blurred is a Bitmap window.SetBounds(bounds.X, bounds.Y, bounds.Width, bounds.Height); //bounds is a Rectangle Application.Run(window); 

The addition of Application.Run starts a message processing loop so that your application is now waiting for events to process until you run Application.Exit. Sending the window through to this command guarantees that exit is automatically run when you close your form so you don't accidentally leave the process running in the background. There is no need to run your forms show method as Application.Run makes this visible for you automatically.

That said I would still recommend using a method similar to what was posted by LarsTech as it solves some additional problems.

[STAThread] static void main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Form window = new Form(); window.StartPosition = FormStartPosition.Manual; window.FormBorderStyle = FormBorderStyle.None; window.BackgroundImage = blurred; //blurred is a Bitmap window.SetBounds(bounds.X, bounds.Y, bounds.Width, bounds.Height); Application.Run(window); } 

[STAThread] confines your threading model for your forms to a single thread. This helps prevent some edge case threading issues which can break your forms.

Application.EnableVisualStyles tells your application to default to the styles used at your OS level.

Application.SetCompatibleTextRenderingDefault has been defaulted to false in new forms projects since Visual Studio 2005. You should have no reason to change it as you are obviously doing new development.

Sign up to request clarification or add additional context in comments.

Comments

2

Make sure you have a message pump running.

Something like:

[STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Form window = new Form(); window.StartPosition = FormStartPosition.Manual; window.FormBorderStyle = FormBorderStyle.None; window.BackgroundImage = blurred; window.SetBounds(bounds.X, bounds.Y, bounds.Width, bounds.Height); Application.Run(window); } 

The other thing to make sure is that your bounds rectangle is actually within the screen dimensions.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.