【问题标题】:How to retain ControlBox UI feel when ControlBox is set to false in COmpact Framework (win mobile)如何在 COMpact Framework 中将 ControlBox 设置为 false 时保留 ControlBox UI 感觉(win mobile)
【发布时间】:2011-12-18 19:37:15
【问题描述】:

我的表单需要删除一些表单上的“x”-(“智能最小化”-实际上并不那么智能)和“确定”按钮。不幸的是,当我这样做时,小键盘输入图标从中间移动到右侧,并且下方的栏变成灰色而不是黑色。

我希望能够删除最小化和确定控件(或只是覆盖它们的处理程序) - 但不幸的是我无法在 CF 中做到这一点。 (大错特错,MS!)

有没有办法恢复一些 UI 外观(如黑条)?

就像我说的那样,理想情况下,我们希望将文本“OK”更改为其他单词,或者只是重载用户启动的最小化(单击 X 或确定)。

(我会尽量放一些屏幕截图来展示我在说什么)

编辑

还要注意,我在表单初始化的主菜单中添加了两项。

    // create three menu items to go at bottom of form/on main menu
    // add new menu items to main menu
    // get rid of 'X' (smart minimize) and OK controls

    menuNext = new System.Windows.Forms.MenuItem();
    ...

    mainMenu.MenuItems.Add(menuPrevious);             
    mainMenu.MenuItems.Add(menuNext);
    mainMenu.MenuItems.Add(menuCancel); 

    MinimizeBox = false;                        
    ControlBox = false;

注意 我以编程方式生成表单和项目 - 而不是使用表单设计器。这是一项要求,因为这些表单是在运行时根据配置文件动态生成的。

【问题讨论】:

  • 我无法使用模拟器重现这一点。我将 ControlBox 和 MinimizeBox 设置为 false,它按预期工作。
  • @yms - 请注意,我还在主菜单中添加了一些菜单项。我认为这也与它有关。但是除非我删除确定按钮/控制框,否则一切看起来都很好
  • 即使添加菜单也无法重现此问题。但是我确实注意到,如果您在设计器上执行相同操作,生成的代码将始终在调用 SuspendLayout() 和调用 ResumeLayout(false) 之间执行所有这些操作。也许你可以尝试做同样的事情......
  • 不幸的是我不能使用设计器。这些表单以编程方式生成。
  • 我打算尝试以编程方式使用 SuspendLayout() 和 ResumeLayout(false)...

标签: c# compact-framework windows-mobile-6


【解决方案1】:

Tim,这里有一些 P/Invoke 电话,我发现它们有助于显示和隐藏 HHTaskBarMS_SIPBUTTON

[DllImport("coredll.dll", 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);

[DllImport("coredll.dll", EntryPoint = "FindWindowW", SetLastError = true)]
public static extern IntPtr FindWindowCE(string lpClassName, string lpWindowName);

public enum WindowPosition {
  SWP_HIDEWINDOW = 0x0080,
  SWP_SHOWWINDOW = 0x0040
}

这是我为它写的包装器:

static IntPtr _taskBar;
static IntPtr _sipButton;
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) {
    // log my Error (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) {
    // log my Error Wrapper(enable ? "Show SIP" : "Hide SIP", err);
  }
}

最后,我是这样使用它的:

/// <summary>
/// The main entry point for the application.
/// </summary>
[MTAThread]
static void Main() {
  ShowWindowsMenu(false);
  try {
    Application.Run(new Form());
  } catch (Exception err) {
    // Log my error
  } finally {
    ShowWindowsMenu(true);
  }
}

【讨论】:

  • 但这并不能保持控制箱的外观和感觉。我的看法是,他想要保留标题栏和 Windows 图标,他只是想要完全移除 (ok) 或 (X) 按钮或覆盖它们的行为。
  • 也许有一些对 (ok) 或 (X) 按钮的字符串引用(如 HHTaskBarMS_SIPBUTTON)。如果他在任务中偶然发现了这些,他可以修改我的ShowWindowsMenu 以显示和隐藏这些。
【解决方案2】:

我遇到了类似的问题,一直在努力寻找答案,直到我偶然发现了以下网址 http://msdn.microsoft.com/en-us/library/bb158579.aspx

它提到了一种名为 WS_NONAVDONEBUTTON 的样式,它似乎适用于这个问题,但这段代码是针对 C++ 的。

看起来有人已经为它写了一个包装器,这解决了我所有的问题。

http://www.koders.com/csharp/fid55A69F22A80DB21F0DB8F0F3EAA3F7D17849142C.aspx?s=button#L8

为了您的信息,我在基本表单的 OnActivated 覆盖中使用了 HideXButton 方法,突然 X 和 Ok 不再可见。

[DllImport("coredll.dll")]
public static extern UInt32 SetWindowLong(
    IntPtr hWnd,
    int nIndex,
    UInt32 dwNewLong);

[DllImport("coredll.dll")]
public static extern UInt32 GetWindowLong(
    IntPtr hWnd,
    int nIndex);

public const int GWL_STYLE = -16;
public const UInt32 WS_NONAVDONEBUTTON = 0x00010000;

public static void HideXButton(IntPtr hWnd)
{
    UInt32 dwStyle = GetWindowLong(hWnd, GWL_STYLE);

    if ((dwStyle & WS_NONAVDONEBUTTON) == 0)
        SetWindowLong(hWnd, GWL_STYLE, dwStyle | WS_NONAVDONEBUTTON);
}

【讨论】:

    猜你喜欢
    • 2014-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-28
    • 2013-05-08
    • 1970-01-01
    • 2011-01-26
    • 2012-01-09
    相关资源
    最近更新 更多