Archive for Programming

Manage Windows® 7 with Microsoft® .NET Framework

// August 12th, 2009 // 1 Comment » // Programming

Windows® API Code Pack for Microsoft® .NET Framework

The Windows® API Code Pack for Microsoft® .NET Framework provides a source code library that can be used to access some new Windows 7 features (and some existing features of older versions of Windows operating system) from managed code. These Windows features are not available to developers today in the .NET Framework.

The individual features supported in this version (v1.0) of the library are:

  • 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.

Can’t wait to get a copy of the Windows 7 and test this up. Sounds like an opportunity to crank out some utility apps.

Reduce flickers in Window Forms

// June 29th, 2009 // No Comments » // Programming

I know this topic has come up a gazillion times but I’m just gonna write it anyway lest I totally forgot about it. Flickers happen a lot when you try resizing or changing the content of a windows form especially when the background is filled with an image. Most often, it is caused by the background repainting in an uncontrolled manner.

There is a technique called double buffering which we could turn on from Form object itself. However, under certain conditions, this does not work for me. So, after further researching into the topic, I’ve found another solution; That is to implement double buffering myself.

There are two events we can override in the Form object namely, OnPaint and OnPaintBackground. What we are trying to achieve here is to repaint the background ourselves before the Form redraw the child controls. First, we override the OnPaintBackground and make it does absolutely nothing.

protected override void OnPaintBackground(PaintEventArgs e)
{
    // Do Nothing
}

Okay, here comes the interesting part. We will now declare a buffer to keep the background drawing, changing it only when the Form resizes. Needless to say, this would save a lot of reloading and redrawing of the image from file.

private Bitmap _dblBuffer = null;

protected override void OnPaint(PaintEventArgs e)
{
    if (_dblBuffer == null)
    {
        _dblBuffer = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
        Graphics g = Graphics.FromImage(_dblBuffer);
        g.DrawImage(_backgroundImage, 0, 0, this.ClientSize.Width, this.ClientSize.Height);
        g.Flush();
        g.Dispose();
    }

    e.Graphics.DrawImage(_dblBuffer, 0, 0, this.ClientSize.Width, this.ClientSize.Height);

    base.OnPaint(e);
}

protected override void OnSizeChanged(EventArgs e)
{
    if (_dblBuffer != null)
    {
        _dblBuffer.Dispose();
        _dblBuffer = null;
    }

    base.OnSizeChanged(e);
}

That should do it. The flickering should have dramatically improved after this. The code is not perfect, so do post it in the comments if you  have any suggestions.

By the way, if anyone knows how to preserve the codes tabbing with SyntaxHighlighter Plus, please drop me a mail. It seems to remove all my tabs after I save the post.

Update: I’m now using SyntaxHighlighter Evolved to solve the indentation issue.