2

我发现了如何禁用 Windows Mobile 应用程序中的“确定”按钮(通过设置ControlBoxfalse)。

我遇到的问题是,我可以通过按下这个“确定”按钮来关闭我的应用程序,但是 FormClosing、FormClosed 事件没有被触发,最令人担忧的是,表单的 Dispose() 方法也没有被调用。这使得清理线程和其他资源之类的东西变得非常困难。

现在我可以强制用户使用我自己的“退出”按钮,这些方法都会按我的预期执行。

问题:为什么Windows Mobile 应用程序中的“确定”按钮会在绕过我提到的方法时关闭应用程序?

4

1 回答 1

0

FormClosing当您为和事件编写代码时FormClosed,您是否记得将实际表单连接起来以使用它们?

我有几个我维护的 Windows Mobile 应用程序,它们调用我为它们创建的方法。

我经常忘记设置控件以使用我为它们编写的代码,所以这是我想到的第一件事。

编辑:我不使用 Microsoft 的OK按钮,而是使用具有 EXIT 菜单项的菜单。

图形用户界面中的 Wm5

Program.cs在我的主程序执行之前,我还通过 P/Invoking 文件中的“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);
  }
}
于 2012-06-26T13:55:32.420 回答