( Index )
Month

Brief Information about the May 2005 CSIG Meeting

Df.exe - An Introduction to Microsoft GDI+, GDIPLUS --- Windows API

by Bruce Arnold

3rd Tuesday of the month. 7:30PM - C/C++ Group, SPRS

Disk Free Application

Welcome to the C++ Users Group. This group is a "Special Interest Group" of the ACGNJ devoted to discussing programming languages in general and C, C++, and C++ for Windows programming in particular. Each month a small but hopefully useful program (complete with source code) is presented for discussion.

Object of the Disk Free Space Program: To present a small utility which draws a Pie Chart showing the free space on the main hard drive, usually "C:". It demonstrates a new system of graphic programming objects introduced by Microsoft called "GDIPLUS", or "GDI+".

GDI+

Purpose

GDI+ is a class-based application programming interface (API) for C/C++ programmers. It enables applications to use graphics and formatted text on both the video display and the printer.

Where Applicable

GDI+ can be used in all Windows-based applications. GDI+ is new technology that is included in the Microsoft Windows XP and Windows .NET Server operating systems. It is required as a redistributable for applications that run on the Windows NT 4.0 SP6, Windows 2000, Windows 98, and Windows Millennium Edition operating systems.

Developer Audience

The GDI+ C++ class-based interface is designed for use by C/C++ programmers.

Run-time Requirements

The Gdiplus.dll must be copied to the system directory of the user's computer. GDI+ is available as a redistributable for Windows NT 4.0 SP6, Windows 2000, Windows 98, and Windows Me. To download the latest redistributable, go to http://www.microsoft.com/msdownload/platformsdk/sdkupdate/psdkredist.htm. (The GDI download is about 1 MegaByte in size.) See Also http://msdn.microsoft.com/library/en-us/gdicpp/gdiplus/gdiplus.asp

At the meeting on Tuesday night we will be discussing the utility and the source code.


SAMPLE CODE
===========
void CPie1Dlg::DrawResults(HDC hdc)
    {
    RECT rr;
    GetClientRect(&rr);

    Graphics graphics(hdc);

   // Create a SolidBrush object.
    SolidBrush blackBrush(Color(255, 0, 0, 0));
    SolidBrush greyBrush(Color(255, 224, 224, 224));
    SolidBrush usedBrush(Color(255, 0, 0, 0));

    if      (fPercentUsed >= 95.)
                   usedBrush.SetColor(Color(255, 255, 0, 0));       // red
    else if (fPercentUsed >= 75.)
                   usedBrush.SetColor(Color(255, 255, 192, 0));     // yellow
    else
                   usedBrush.SetColor(Color(255, 0, 255, 0));       // green

    // Create a Pen object.
    Pen blackPen(Color(255, 0, 0, 0), 3);
    blackPen.SetDashStyle(DashStyleDot);    // Make it dotted instead of solid.


    // Define the pie.
    Rect ellipseRect(0, 0, rr.right, rr.bottom);
    ellipseRect.Inflate(-3,-3);
    REAL startAngle = 0.0f;
    REAL sweepAngle = 3.6f * (100.0f - fPercentUsed);

    // Draw the pie.
    graphics.FillPie(&greyBrush, ellipseRect, startAngle, sweepAngle);
    graphics.FillPie(&usedBrush, ellipseRect, sweepAngle, 360 - sweepAngle);
    graphics.DrawPie(&blackPen, ellipseRect, startAngle, sweepAngle);
    graphics.DrawPie(&blackPen, ellipseRect, sweepAngle, 360 - sweepAngle);

       // Create a string.
    char text[256];
    sprintf(text, "FREE\n%.1f%%\n%s\n%ld MB", 
        100.0-fPercentUsed, sTargetDrive.GetBuffer(100), ulFreeMegs);

    WCHAR freetext[256];    // convert string to wide-characters
    ZeroMemory(freetext, sizeof(freetext));
    mbstowcs(freetext, text, strlen(text));

    // Initialize arguments and then draw string inside box.
    REAL x, y, width, height;
    x = (float) (3*rr.right)/4;
    y = (float) rr.bottom/2;
    width = (float) (rr.right - x);
    height = (float)(rr.bottom - y);
    RectF layoutRect(x, y, width, height);      // rectangle defines boundary of string.

    Font myFont(L"Arial", 16, FontStyleBold);   // define font.
    StringFormat format;
    format.SetAlignment(StringAlignmentNear);   // align it near the x,y origin.

    // Draw string.
    graphics.DrawString(freetext, -1, &myFont, layoutRect, &format, &blackBrush);
    }

"Random Access" questions start at 7:30 Tuesday night. See you there.

SOURCE CODE

Source Code Files

For help, email me at b a r n o l d @ i e e e . o r g
Back to C++ Main Page