我有一个(我认为)非常简单的问题,但我正在为它撕裂我的头发:
在我的主类中,在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();这是一个用背景图像制作无边框窗口的尝试。我运行代码,没有任何错误-但没有出现任何表单。
我做错了什么?
发布于 2012-08-01 10:59:12
您的代码无法工作,因为您尚未启动事件循环来保持进程运行。让您的代码正常工作就像更改
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();至
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);添加Application.Run将启动一个消息处理循环,以便应用程序在运行Application.Exit之前等待要处理的事件。将窗口发送到此命令可确保在关闭窗体时自动运行exit,这样就不会意外地让进程在后台运行。不需要运行您的窗体显示方法,因为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);
}STAThread将窗体的线程模型限制在单个线程中。这有助于防止一些边缘情况线程问题,这可能会破坏您的表单。
Application.EnableVisualStyles告诉您的应用程序默认使用您的操作系统级别使用的样式。
从Visual Studio2005开始,在新的窗体项目中,默认设置为false。你应该没有理由改变它,因为你显然是在做新的开发。
发布于 2012-08-01 10:24:35
确保有一个消息泵在运行。
类似于:
[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);
}另一件要确保的事情是你的bounds矩形实际上在屏幕尺寸之内。
https://stackoverflow.com/questions/11751749
复制相似问题