我发现了如何在我的Windows应用程序中禁用"OK“按钮(通过将ControlBox
设置为false)。
我遇到的问题是,我可以通过按这个"OK“按钮来关闭我的应用程序,但是没有触发FormClosing、FormClosed事件,而且最令人担忧的是,也没有调用表单的most ()方法。这使得清理像线程和其他资源这样的事情变得相当困难。
现在,我可以强制用户使用我自己的“退出”按钮,这些方法都如我所期望的那样被执行。
问题:为什么Windows应用程序中的"OK“按钮在绕过我提到的方法时关闭应用程序?
发布于 2012-06-26 13:55:32
当您为FormClosing
和FormClosed
事件编写代码时,您记得将实际的表单连接起来使用这些表单吗?
我有一些我维护的Windows应用程序,它们调用我为它们创建的方法。
我经常忘记设置控件来使用我为它们编写的代码,所以这是我想到的第一件事。
编辑:我不使用微软的OK
按钮,而是使用带有退出菜单项的菜单。
在我的主程序执行之前,我还通过P/调用Program.cs
文件中的"coredll“文件来关闭软输入面板(SIP)和任务栏。
这可能是你的解决办法。如果是这样的话,这应该是我所使用的所有代码。一定要测试它,如果缺少什么东西,让我知道,我会更新它。
const string COREDLL = "coredll.dll";
[DllImport(COREDLL, EntryPoint = "FindWindowW", SetLastError = true)]
public static extern IntPtr FindWindowCE(string lpClassName, string lpWindowName);
[DllImport(COREDLL, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);
private static Form1 objForm = null;
private static IntPtr _taskBar = IntPtr.Zero;
private static IntPtr _sipButton = IntPtr.Zero;
[MTAThread]
static void Main() {
ShowWindowsMenu(false);
try {
objForm = new Form1();
Application.Run(objForm);
} catch (Exception err) {
objForm.DisableTimer();
if (!String.IsNullOrEmpty(err.Message)) {
ErrorWrapper("AcpWM5 Form (Program)", err);
}
} finally {
ShowWindowsMenu(true); // turns the menu back on
}
}
private static void ShowWindowsMenu(bool enable) {
try {
if (enable) {
if (_taskBar != IntPtr.Zero) {
SetWindowPos(_taskBar, IntPtr.Zero, 0, 0, 240, 26, (int)WindowPosition.SWP_SHOWWINDOW); // display the start bar
}
} else {
_taskBar = FindWindowCE("HHTaskBar", null); // Find the handle to the Start Bar
if (_taskBar != IntPtr.Zero) { // If the handle is found then hide the start bar
SetWindowPos(_taskBar, IntPtr.Zero, 0, 0, 0, 0, (int)WindowPosition.SWP_HIDEWINDOW); // Hide the start bar
}
}
} catch (Exception err) {
ErrorWrapper(enable ? "Show Start" : "Hide Start", err);
}
try {
if (enable) {
if (_sipButton != IntPtr.Zero) { // If the handle is found then hide the start bar
SetWindowPos(_sipButton, IntPtr.Zero, 0, 0, 240, 26, (int)WindowPosition.SWP_SHOWWINDOW); // display the start bar
}
} else {
_sipButton = FindWindowCE("MS_SIPBUTTON", "MS_SIPBUTTON");
if (_sipButton != IntPtr.Zero) { // If the handle is found then hide the start bar
SetWindowPos(_sipButton, IntPtr.Zero, 0, 0, 0, 0, (int)WindowPosition.SWP_HIDEWINDOW); // Hide the start bar
}
}
} catch (Exception err) {
ErrorWrapper(enable ? "Show SIP" : "Hide SIP", err);
}
}
https://stackoverflow.com/questions/11200390
复制相似问题