Tag Archives: Interop

Windows 7 API Code Pack v1.0 released

My new best friend, the Windows 7 Code Pack, has been released.  From Charlie Calvert’s Community Blog:

The Windows® API Code Pack for Microsoft® .NET Framework provides support for various features of Windows 7 and previous releases of that operating system. The Code Pack has  reached version 1.0 and has been published on Code Gallery:

Here are some of the features you can from managed code using the Code Pack:

  • Windows 7 Taskbar Jump Lists, Icon Overlay, Progress Bar, Tabbed Thumbnails, and Thumbnail Toolbars.
  • Windows 7 Libraries, Known Folders, non-file system containers.
  • Windows Shell Search API support, a hierarchy of Shell Namespace entities, and Drag and Drop functionality for Shell Objects.
  • Explorer Browser Control.
  • Shell property system.
  • Windows Vista and Windows 7 Common File Dialogs, including custom controls.
  • Windows Vista and Windows 7 Task Dialogs.
  • Direct3D 11.0, Direct3D 10.1/10.0, DXGI 1.0/1.1, Direct2D 1.0, DirectWrite, Windows Imaging Component (WIC) APIs. (DirectWrite and WIC have partial support)
  • Sensor Platform APIs
  • Extended Linguistic Services APIs
  • Power Management APIs
  • Application Restart and Recovery APIs
  • Network List Manager APIs
  • Command Link control and System defined Shell icons.

Disabling the Maximize button in WPF

I am writing a WPF application which runs as a tool-style dialog on the desktop, and needs to be able to be minimized but not maximised.  It seems that this is somthing the WPF designers didn’t quite anticipate, despite it having been pretty easy to achieve under Windows Forms.

After searching around the ‘net, I finally found some code that disabled both the minimize & maximize buttons, but took a couple shortcuts that I didn’t like (and besides, it was in VB…).

A bit of searching and a quick cleanup left me with a working result.  Catch the Loaded event of your window, and put the following code into the handler:

// Import some references
using System.Interop;
using System.Runtime.InteropServices;

// Place these inside the class definition...
[DllImport("user32.dll")]
private extern static Int32 SetWindowLong(IntPtr hWnd, Int32 nIndex, Int32 dwNewLong);
[DllImport("user32.dll")]
private extern static Int32 GetWindowLong(IntPtr hWnd, Int32 nIndex);

private const Int32 GWL_STYLE = -16;
private const Int32 WS_MAXIMIZEBOX = 0x10000;
private const Int32 WS_MINIMIZEBOX = 0x20000;

// And finally the event handler...
private void Window_Loaded(object sender, RoutedEventArgs e)
{
    IntPtr hWnd = new WindowInteropHelper(this).Handle;
    Int32 windowLong = GetWindowLong(hWnd, GWL_STYLE);
    windowLong = windowLong & ~WS_MAXIMIZEBOX;
    SetWindowLong(hWnd, GWL_STYLE, windowLong);
}

I have included the value for WS_MINIMIZEBOX in case you need it.