fru*_*cup 1 c# visual-studio-2010 winforms
我有一个(我认为是)非常简单的问题,但我正在扯掉我的头发:
在我的主类中,在main方法中,我有以下代码:
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();
Run Code Online (Sandbox Code Playgroud)
这是尝试使用背景图像创建无边框窗口.我运行代码并且没有错误 - 但是没有表单出现.
我究竟做错了什么?
您的代码不起作用,因为您尚未启动事件循环以保持进程运行.让代码工作就像改变一样简单
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();
Run Code Online (Sandbox Code Playgroud)
至
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);
Run Code Online (Sandbox Code Playgroud)
Application.Run的添加启动了一个消息处理循环,以便您的应用程序现在等待事件处理,直到您运行Application.Exit.将窗口发送到此命令可确保在关闭表单时自动运行退出,这样您就不会意外地让进程在后台运行.无需运行表单show方法,因为Application.Run会自动为您显示.
这就是说我仍然建议使用类似于LarsTech发布的方法,因为它解决了一些额外的问题.
[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);
}
Run Code Online (Sandbox Code Playgroud)
[STAThread]将表单的线程模型限制在单个线程中.这有助于防止一些可能破坏您的表单的边缘案例线程问题.
Application.EnableVisualStyles告诉您的应用程序默认使用您的操作系统级别使用的样式.
自Visual Studio 2005以来, Application.SetCompatibleTextRenderingDefault在新表单项目中默认为false.您应该没有理由更改它,因为您显然正在进行新的开发.