简体   繁体   中英

Windows Mobile 'OK' button

I've discovered how to disable the "OK" button in my Windows Mobile application (by setting ControlBox to false ).

The problem I had was that it was possible for me to close my application by pressing this "OK" button, but the FormClosing, FormClosed events were not fired, and most worrying, the form's Dispose() method was not called either. This made it quite difficult to clean up things like threads and other resources.

Now that I can force the user to use my own "Quit" button, those methods all get executed as I would expect.

Question : why does the "OK" button in a Windows Mobile application close the application while bypassing the methods I mentioned?

When you wrote the code for the FormClosing and FormClosed events, did you remember to wire the actual form up to use these?

I have a few Windows Mobile applications that I maintain, and they call the methods I created for them.

I often forget to set the Controls to use the code that I write for them, so that was the first thing I thought of.

EDIT: I do not use Microsoft's OK button, but instead make use of a Menu that has an EXIT Menu Item.

GUI中的Wm5

I also turn off the Soft Input Panel (SIP) and Task Bar by P/Invoking the "coredll" file in the Program.cs file before my main program executes.

That may be a solution for you. If so, this should be all of the code I use for that. Be sure to test it, and if something is missing, let me know and I'll update it.

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);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM