Home   Index   About
Ultimate Pack


Custom Search
Using Owner-Drawn Buttons

The parent window of an owner-drawn button typically responds to at least three messages for the button: WM_INITDIALOG, WM_COMMAND, and WM_DRAWITEM. It is not necessary to process the WM_MEASUREITEM message for owner-drawn buttons.

When you must paint an owner-drawn button, Windows sends the parent window a WM_DRAWITEM message whose lParam points to a DRAWITEMSTRUCT structure. Use this structure with all owner-drawn controls to provide the application with the information it requires to paint the control. The itemAction and itemState members of the DRAWITEMSTRUCT structure define how to paint an owner-drawn button.

The following example shows how to process WM_INITDIALOG, WM_DRAWITEM, and WM_COMMAND messages for owner-drawn buttons. This example demonstrates how to draw one of two bitmaps for a control, depending on whether the control is selected. You would typically use the wParam parameter of the WM_DRAWITEM message to identify the control; in this example, only one control is assumed.

LRESULT APIENTRY OwnDrawProc(hDlg, message, wParam, lParam)

HWND hDlg; // window handle of dialog box

UINT message; // type of message

UINT wParam; // message-specific information

LONG lParam;

{

HDC hdcMem;

LPDRAWITEMSTRUCT lpdis;

switch (message) {

case WM_INITDIALOG:

// hinst, hbm1 and hbm2 are defined globally.

hbm1 = LoadBitmap((HANDLE) hinst, "OwnBit1");

hbm2 = LoadBitmap((HANDLE) hinst, "OwnBit2");

return TRUE;

case WM_DRAWITEM:

lpdis = (LPDRAWITEMSTRUCT) lParam;

hdcMem = CreateCompatibleDC(lpdis->hDC);

if (lpdis->itemState & ODS_SELECTED) // if selected

SelectObject(hdcMem, hbm2);

else

SelectObject(hdcMem, hbm1);

// Destination

StretchBlt(

lpdis->hDC, // destination DC

lpdis->rcItem.left, // x upper left

lpdis->rcItem.top, // y upper left

// The next two lines specify the width and

// height.

lpdis->rcItem.right - lpdis->rcItem.left,

lpdis->rcItem.bottom - lpdis->rcItem.top,

hdcMem, // source device context

0, 0, // x and y upper left

32, // source bitmap width

32, // source bitmap height

SRCCOPY); // raster operation

DeleteDC(hdcMem);

return TRUE;

case WM_COMMAND:

if (wParam == IDOK

|| wParam == IDCANCEL) {

EndDialog(hDlg, TRUE);

return TRUE;

}

if (HIWORD(wParam) == BN_CLICKED) {

switch (LOWORD(wParam)) {

case IDB_OWNERDRAW:

.

. // application-defined processing

.

break;

}

}

break;

case WM_DESTROY:

DeleteObject(hbm1); // delete bitmaps

DeleteObject(hbm2);

break;

}

return FALSE;

UNREFERENCED_PARAMETER(lParam);

}


Last news from Greatis Software

Nostalgia .Net     Nostalgia .Net     .Net is powerful, but not all-powerful, so sometimes we need to use Win32 API for our .Net applications. It's simple enough with Platform Invoke if you have Win32 skill, but we do not always have time to dig the ancient documentation, declare the special types that are compatible with Win32, find the values of the Win32's constants and so on. Nostalgia .Net offers several simple-to-use classes, and components that will allow you to forget about the headache of Win32 and just use the power of Win32 in your application the same way as you use the native. Net classes.  More »

Recommended software for developers

Ultimate Pack for Delphi and C++ Builder     Ultimate Pack     Component pack for Delphi and C++ Builder that contains runtime form designer, runtime object inspector, print suite and much more for the very special price.  More »

Form Designer .Net     Form Designer .Net     Unique runtime form design solution that allows to edit any form in .Net WinForms application at runtime with full source codes for only 300 euro!  More »

Print Suite .Net     Print Suite .Net     Print Suite .Net is a set of components for easy printing texts, images and grids from your WinForms applications. Full C# source codes are available  More »

Gradient Controls .Net     Gradient Controls .Net     Gradient Controls .Net offers controls with gradient background feature. Labels, panels and so on... Full C# source codes are available  More »

iGrid     Greatis iGrid     iGrid plots drawing grid right over your desktop, so you can use it everywhere, with any drawing application without any special plugins for different graphic editors.  More »


All the contacts and projects

Dmitry Vasiliev (just.dmitry)

Related Links

Software for Visual Studio .NET developers
Software for Delphi and C++ Builder developers
Software for Visual Basic 6 developers
Delphi Tips&Tricks
MegaDetailed.NET

More Online Helps

Win32 Programmer's Reference
Win32 Multimedia Programmer's Reference
OLE Programmer's Reference
Microsoft Windows Pen API Programmer's Reference
Microsoft Windows Sockets 2 Reference
Microsoft Windows Telephony API (TAPI) Programmer's Reference
Unix Manual Pages

Free Tech Secrets ;) Copyright © 2008-2012 Free Tech Secrets ;) greatis just4fun network just4fun