WinAPI - Ecke
Man kann in der WinAPI nicht nur schöne Funktionen schreiben, sondern auch Klassen. Dieses erspart dem Programmierer oft sehr viel Schreibarbeit. Ich zeige auf dieser Seite nur die public-Abschnitte der Klassen, da nur diese für den Programmierer von Bedeutung sind. Den Download-ZIPs sind Textdateien als Dokumentation der jeweiligen Klasse beigelegt.


1. Klasse CFont
2. Klasse CTrayIcon
3. Klasse CRegistry



Klasse CFont
//---------------------------------------------------------------------------
#ifndef CFontH
#define CFontH
//---------------------------------------------------------------------------
#include 
//---------------------------------------------------------------------------

// Font Styles
#define FS_DEFAULT    0x0000
#define FS_ITALIC     0x0001
#define FS_BOLD       0x0010
#define FS_UNDERLINE  0x0100
#define FS_STRIKEOUT  0x1000
//---------------------------------------------------------------------------

class CFont
{
public:
    // Constructors
    CFont();
    CFont(HFONT hFont);
    CFont(LPCTSTR lpszFontName, UINT uiSize, WORD wFontStyle);

    // Destructor
    ~CFont();

    // Get Functions
    HFONT GetHandle();
    WORD  GetStyle();
    UINT  GetSize();
    VOID  GetName(LPTSTR lpszFontName, UINT uiBufSize);

    // Set Functions
    VOID SetName(LPCTSTR lpszFontName);
    VOID SetSize(UINT uiFontSize);
    VOID SetStyle(WORD wFontStyle);

    // Other methods
    VOID Assign(HFONT hFont);
    VOID Assign(CFont* font);
    INT  AssignToWindow(HWND hwnd, BOOL fRedraw);
};
//---------------------------------------------------------------------------
#endif
Erkl�rung: CFont ist eine kleine Klasse, die das Arbeiten mit Schriftarten in der Windows32 API kapselt. Mit CFont kann man so vorgehen wie z.B. in MS Word, wo man einen Namen, eine Schriftgröße und einen Stil für eine Schriftart angibt.
Download:
CFont.ZIP
NACH OBEN




Klasse CTrayIcon
//---------------------------------------------------------------------------
#ifndef CTrayIconH
#define CTrayIconH
//---------------------------------------------------------------------------
#define _WIN32_IE 0x0601
#include <windows.h>
//---------------------------------------------------------------------------
// Popup Reaction
#define PR_NONE        0
#define PR_RBUTTON     1
#define PR_LBUTTON     2
#define PR_BOTH        3
//---------------------------------------------------------------------------
// Icon constants for balloons
#define NIIF_NONE      0x0
#define NIIF_INFO      0x1
#define NIIF_WARNING   0x2
#define NIIF_ERROR     0x3
#define NIIF_GUID      0x5
#define NIIF_QUESTION  0x7
#define NIIF_ICON_MASK 0xF
#define NIIF_NOSOUND   0x10
#define NIIF_USER      0x14
//---------------------------------------------------------------------------

class CTrayIcon
{
public:
    CTrayIcon(HINSTANCE hInstance, HWND hWnd, HICON hIcon, LPTSTR lpszTip);
    CTrayIcon(HINSTANCE hInstance, HWND hWnd, WORD wIconID, LPTSTR lpszTip);
    CTrayIcon(HINSTANCE hInstance, WNDPROC WndProc, HICON hIcon, LPTSTR lpszTip);
    CTrayIcon(HINSTANCE hInstance, WNDPROC WndProc, WORD wIconID, LPTSTR lpszTip);
    ~CTrayIcon();

    VOID    Show();
    VOID    Hide();
    BOOL    IsVisible();
    VOID    DoPopup();    
    VOID    SetToolTip(LPTSTR lpszTip);
    UINT    GetToolTip(LPTSTR lpszBuf, UINT uiBufSize);
    VOID    SetIcon(HICON hIcon);
    HICON   GetIcon();
    VOID    SetPopupMenu(HMENU hMenu, INT index);
    VOID    SetPopupMenu(WORD wMenuID, INT index);
    HMENU   GetPopupMenu();
    BOOL    SetPopupReaction(WORD wPR);
    WORD    GetPopupReaction();
    VOID    WaitForDoubleClick(BOOL bWait);
    BOOL    GetWaitForDoubleClick();
    HWND    GetMainWindow();
    WNDPROC GetWindowProc();
    VOID    ShowBalloon(LPTSTR szTitle, LPTSTR szInfo, UINT uiTimeout, DWORD dwIcon);
};
//---------------------------------------------------------------------------
#endif
Erkl�rung: Mit CTrayIcon ist man in der Lage, seinem Programm auf besonders einfache Art und Weise ein TrayIcon hinzuzufügen. Solche Icons erscheinen in dem Bereich rechts unten in der Taskleiste. Die wichtigsten Elemente eines CTrayIcon-Objektes sind natürlich ein Icon in Form eines HICON-Objektes, ein Hilfstext, der beim Verharren des Mauscursors über dem TrayIcon erscheint und ein optionales PopupMenu in Form eines HMENU-Objektes. Bei welcher Mausaktion das PopupMenu aufpoppen soll ist über die Funktion CTrayIcon::SetPopupReaction() steuerbar. Weiterhin kann man über CTrayIcon::WaitForDoubleClick() angeben, dass ein Linksklick auf das Icon erst nach einer halben Sekunde ausgeführt werden soll, um auf einen zweiten Klick zu warten, der einen Doppelklick auslöst. In der neusten Version kann man (unter XP) auch mit ShowBalloon() eine Sprechblase mit Informationen aufpoppen lassen.
Download:
CTrayIcon.ZIP
NACH OBEN




Klasse CRegistry
//---------------------------------------------------------------------------
#ifndef CRegistryH
#define CRegistryH
//---------------------------------------------------------------------------
#include 
//---------------------------------------------------------------------------

// Possible types for values:
//--------------------------------
// REG_BINARY	            Binary data in any form.
// REG_DWORD	            A 32-bit number.
// REG_DWORD_LITTLE_ENDIAN  A 32-bit number in little-endian format (same as REG_DWORD). In little-endian format, the most significant byte of a word is the high-order byte. This is the most common format for computers running Windows NT and Windows 95.
// REG_DWORD_BIG_ENDIAN	    A 32-bit number in big-endian format. In big-endian format, the most significant byte of a word is the low-order byte.
// REG_EXPAND_SZ	    A null-terminated string that contains unexpanded references to environment variables (for example, �%PATH%�). It will be a Unicode or ANSI string depending on whether you use the Unicode or ANSI functions.
// REG_LINK	            A Unicode symbolic link.
// REG_MULTI_SZ	            An array of null-terminated strings, terminated by two null characters.
// REG_RESOURCE_LIST	    A device-driver resource list.
// REG_SZ	            A null-terminated string. It will be a Unicode or ANSI string depending on whether you use the Unicode or ANSI functions.

// Notes on the write-functions:
//-----------------------------------
// - they overwrite any value careless of which type it may be
// - they act as creating functions if the specified value does not exist
//   therefore there is no need for functions to create values

class CRegistry
{
public:
    CRegistry();
    CRegistry(HKEY hRootKey);
    ~CRegistry();
    BOOL SetRoot(HKEY hRootKey);
    // Path functions
    BOOL OpenPath(LPTSTR szPath, BOOL bCreate);
    BOOL ClosePath();
    BOOL PathExists(LPTSTR szPath);
    BOOL CreatePath(LPTSTR szPath);
    BOOL DeleteLastPathKey(LPTSTR szPath); // Deletes last key in szPath
    // Value functions
    BOOL ValueExists(LPTSTR szValName);
    BOOL GetValueSize(LPTSTR szValName, LPDWORD lpdwSize);
    BOOL GetValueType(LPTSTR szValName, LPDWORD lpdwType);
    BOOL WriteString(LPTSTR szValName, LPTSTR szValue);
    BOOL WriteDWORD(LPTSTR szValName, DWORD dwValue);
    BOOL WriteBinary(LPTSTR szValName, LPBYTE lpValue, DWORD dwNumBytes);
    BOOL ReadString(LPTSTR szValName, LPTSTR lpszRetVal, DWORD dwBufSize);
    BOOL ReadDWORD(LPTSTR szValName, LPDWORD lpdwRetVal);
    BOOL ReadBinary(LPTSTR szValName, LPBYTE lpRetVal, DWORD dwBufSize);
    // Other functions
    LPTSTR GetLastError();
};
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
Erkl�rung: Die Klasse CRegistry kapselt die elementaren Registry-Funktionen der WinAPI. Der Code wird mit Verwendung der Klasse k�rzer und �berschaubarer. Die Funktionen sind selbsterkl�rend. Ein Beispiel:
CRegistry reg;
reg.SetRoot(HKEY_LOCAL_MACHINE);
reg.OpenPath( TEXT("Software//MyCompany//MyProg"), TRUE );
reg.WriteString( TEXT("InstallPath"), TEXT("C:\\Programme\\MyProg") );
reg.WriteDWORD( TEXT("ButtonCount"), 12 );
reg.ClosePath();
Mit der Funktion GetLastError() kann man Informationen zum letzten aufgetretenen Fehler erlangen.
Diese Version beinhaltet bisher nur das Speichern und Lesen von Strings und DWORD-Werten. Es ist geplant, weitere Funktionen einzuf�gen.
Download:
CRegistry.ZIP
NACH OBEN
Hosted by www.Geocities.ws

1