2010-11-10 1 views
2

У меня есть следующий код, который добавляет элемент «Всегда поверх» в контекстное меню системы, как показано на окне хром. Он работает правильно, но я бы хотел, чтобы он отображал галочку или аналогичную информацию, указывающую, включен ли он вкл/выкл.Создание элемента контекстного меню системы WPF toggleable

Любая идея, как я могу это сделать?

public RibbonShell() 
{ 
    InitializeComponent(); 

    Loaded += (s,e) => 
       { 
        // Get the Handle for the Forms System Menu 
        var systemMenuHandle = GetSystemMenu(Handle, false); 

        // Create our new System Menu items just before the Close menu item 
        InsertMenu(systemMenuHandle, 5, MfByposition | MfSeparator, 0, string.Empty); // <-- Add a menu seperator 
        InsertMenu(systemMenuHandle, 6, MfByposition, SettingsSysMenuId, "Always on Top"); 

        // Attach our WindowCommandHandler handler to this Window 
        var source = HwndSource.FromHwnd(Handle); 
        source.AddHook(WindowCommandHandler); 
       }; 
} 

#region Win32 API Stuff 

// Define the Win32 API methods we are going to use 
[DllImport("user32.dll")] 
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert); 

[DllImport("user32.dll")] 
private static extern bool InsertMenu(IntPtr hMenu, Int32 wPosition, Int32 wFlags, Int32 wIDNewItem, string lpNewItem); 

/// Define our Constants we will use 
private const int WmSyscommand = 0x112; 
private const int MfSeparator = 0x800; 
private const int MfByposition = 0x400; 

#endregion 

// The constants we'll use to identify our custom system menu items 
private const int SettingsSysMenuId = 1000; 

/// <summary> 
/// This is the Win32 Interop Handle for this Window 
/// </summary> 
public IntPtr Handle 
{ 
    get { return new WindowInteropHelper(this).Handle; } 
} 

private IntPtr WindowCommandHandler(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) 
{ 
    // Check if a System Command has been executed 
    if (msg == WmSyscommand && wParam.ToInt32() == SettingsSysMenuId) 
    { 
     Topmost = !Topmost; 
     handled = true; 
    } 

    return IntPtr.Zero; 
} 

ответ

4

Вы должны позвонить CheckMenuItem всякий раз, когда вы меняете Topmost. Подробности см. На странице CheckMenuItem documentaton. Вот P/Invoke подпись и константы вам нужно:

[DllImport("user32.dll")] 
private static extern bool CheckMenuItem(IntPtr hMenu, Int32 uIDCheckItem, Int32 uCheck); 

private const int MfChecked = 8; 
private const int MfUnchecked = 0; 

Теперь, чтобы проверить деталь, просто:

CheckMenuItem(systemMenuHandle, SettingsSysMenuId, MfChecked); 

и снимите:

CheckMenuItem(systemMenuHandle, SettingsSysMenuId, MfUnchecked); 

 Смежные вопросы

  • Нет связанных вопросов^_^