【问题标题】:Windows Mobile 'OK' buttonWindows Mobile 的“确定”按钮
【发布时间】:2012-06-27 02:41:00
【问题描述】:

我发现如何在我的 Windows Mobile 应用程序中禁用“确定”按钮(通过将 ControlBox 设置为 false)。

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

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

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

【问题讨论】:

  • 您是指“OK”按钮还是“X”,IIRC 当您点击“X”按钮时,您的应用程序不会关闭,它只是被隐藏了,所以当用户再次运行它时刚刚回到顶部。我认为“确定”按钮仅在对话框窗口中显示。
  • IIRC
  • 这是“确定”按钮。我的应用程序不是模态的。
  • 因为这个问题在试图弄清楚如何摆脱 ok 按钮时出现我想说的是,通过将 controlbox 属性设置为 false 你可以摆脱它

标签: c# windows-mobile


【解决方案1】:

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

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

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

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

在我的主程序执行之前,我还通过 P/Invoking 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);
  }
}

【讨论】:

  • 是的,事件已关联。当我使用 Close() 关闭表单时,它们会正确执行。
  • 所以是微软的小OK按钮没有触发任何Windows Mobile能够传递的东西。我做了一些改变,可能会让你“陷入困境”。
  • 这已经不是问题了,现在我已经隐藏了确定按钮设置ControlBox = false。我的问题与会导致这种行为的情况更相关。对我来说,允许用户看似关闭程序而不实际关闭它似乎是一个主要的 UI 错误。马特上面的评论似乎是一个很好的答案 - 好的只是隐藏窗口。以前从未使用过这个平台,并且在同一个地方看到 OK 按钮,我希望有一个 X 按钮 - 你可以看到我的困惑出现在哪里。
  • (ok) 关闭表单,(X) 最小化它。显示哪个取决于窗体的 ShowMinimizeButton 属性。如果您完全移除控制框,则只能转移焦点(无需连接您自己的按钮或菜单,在 Form 实例本身上调用 Close)。
  • @CharlieSalts:这是设计使然。由于没有硬盘驱动器,所有程序实际上都安装在 RAM 中。因此,决定(我相信)关闭应用程序而不是简单地最小化它没有任何好处。随着电池电量耗尽,移动设备上的应用程序应该响应低电量通知并保存其数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-08
  • 1970-01-01
  • 1970-01-01
  • 2011-02-07
  • 1970-01-01
相关资源
最近更新 更多