ks.drax
September 27th, 2006, 02:12 AM
hello out there,
I have a problem with my ownerdrawn listbox. hope somebody can help.
The listbox is supposed to be an language chooser with bmp instead of text.
(because of ansii -> unicode problems, for example japaneese in the bitmap)
The Imagelistbox works just fine, with one exception. On startup the focus rectangle ist drawn (incorrect ->means not totally around the item) over the first item of the listbox.
If I choose another item there two focus rectangles shown. the old one an the new one (yet correct drawn -> means totally around the item).
After scrolling down and up the list box, the incorrect focus disappears and the listbox works fine. Also the incorrect focus disappears when changing to an other window and change back to my application window.
The selection is always returned correctly (even if the incorrect focus is shown).
I've added a screenshot to the post. Ist taken after selecting an item (directly after startup).
I've tried for hours but can't fix it.
May anybody can help?
ThX
ks.drax
Here's my source code:
(in the resource there's the dialog and the bitmap included)
/*********************************************************************
* imagelistbox.cpp
*
*
* Description - gui element (choose language from bitmaps)
**********************************************************************
*
* 25.09.2006 File created
*
*********************************************************************/
#include <windows.h>
#include "resource.h"
#define XBITMAP 100
#define YBITMAP 20
#define BUFFER MAX_PATH
HBITMAP hbmpPencil, hbmpCrayon, hbmpMarker, hbmpPen, hbmpFork;
HBITMAP hbmpPicture, hbmpOld;
BOOL CALLBACK DialogProc (HWND, UINT, WPARAM, LPARAM);
void AddItem(HWND hwnd, LPSTR lpstr, HBITMAP hbmp);
/*********************************************************************
* FUNCTION : WinMain
* DESCRIPTION: the mainroutine of the application
* PARAMETERS : HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow
* RETURNS : error code
*********************************************************************/
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
return DialogBox (hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, DialogProc);
}
/*********************************************************************
* FUNCTION : DialogProc
* DESCRIPTION: the dialog handling routine
* PARAMETERS : HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam
* RETURNS : true or false
*********************************************************************/
BOOL CALLBACK DialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
HWND hListBox;
HDC hdcMem;
LPMEASUREITEMSTRUCT lpmis;
LPDRAWITEMSTRUCT lpdis;
int lResult;
switch(message)
{
case WM_INITDIALOG:
// Retrieve list box handle.
// Load bitmaps.
hbmpPencil = LoadBitmap(GetModuleHandle(NULL),MAKEINTRESOURCE(103));
hListBox = GetDlgItem(hDlg, IDC_LIST1);
// Initialize the list box text
AddItem(hListBox, "pencil", hbmpPencil);
AddItem(hListBox, "crayon", hbmpPencil);
AddItem(hListBox, "marker", hbmpPencil);
AddItem(hListBox, "marker", hbmpPencil);
AddItem(hListBox, "marker", hbmpPencil);
AddItem(hListBox, "marker", hbmpPencil);
SetFocus(hListBox);
SendMessage(hListBox, LB_SETCURSEL, -1, 0);
return TRUE;
case WM_MEASUREITEM:
lpmis = (LPMEASUREITEMSTRUCT) lParam;
// Set the height of the list box items.
lpmis->itemHeight = 22;
return TRUE;
case WM_DRAWITEM:
lpdis = (LPDRAWITEMSTRUCT) lParam;
// If there are no list box items, skip this message.
if (lpdis->itemID == -1)
{
break;
}
// Draw the bitmap for the list box item.
switch (lpdis->itemAction)
{
case ODA_SELECT:
case ODA_DRAWENTIRE:
// Display the bitmap associated with the item.
hbmpPicture =(HBITMAP)SendMessage(lpdis->hwndItem,
LB_GETITEMDATA, lpdis->itemID, (LPARAM) 0);
hdcMem = CreateCompatibleDC(lpdis->hDC);
hbmpOld = (HBITMAP)SelectObject(hdcMem, hbmpPicture);
BitBlt(lpdis->hDC,
lpdis->rcItem.left, lpdis->rcItem.top,
lpdis->rcItem.right - lpdis->rcItem.left,
lpdis->rcItem.bottom - lpdis->rcItem.top,
hdcMem, 0, 0, SRCCOPY);
SelectObject(hdcMem, hbmpOld);
DeleteDC(hdcMem);
break;
case ODA_FOCUS:
break;
}
break;
case WM_DESTROY:
case WM_CLOSE:
// Hier wird der Dialog geschlossen
EndDialog(hDlg,0);
return (TRUE);
case WM_COMMAND:
switch (HIWORD(wParam))
{
case LBN_SELCHANGE:
hListBox = GetDlgItem(hDlg, IDC_LIST1);
lResult = (int) SendMessage(hListBox,LB_GETCARETINDEX,0,0);
//lResult holds the index of the selected bmp
//MessageBox(hDlg, "Selection changed", "ok", MB_OK);
return (TRUE);
}
return (TRUE);
}
return (FALSE);
}
/*********************************************************************
* FUNCTION : AddItem
* DESCRIPTION: adds an item to the listbox
* PARAMETERS : HWND hwnd, LPSTR lpstr, HBITMAP hbmp
* RETURNS : none
*********************************************************************/
void AddItem(HWND hwnd, LPSTR lpstr, HBITMAP hbmp)
{
int nItem;
nItem = (int)SendMessage(hwnd, LB_ADDSTRING, 0, (LPARAM)lpstr);
SendMessage(hwnd, LB_SETITEMDATA, nItem, (LPARAM)hbmp);
}
I have a problem with my ownerdrawn listbox. hope somebody can help.
The listbox is supposed to be an language chooser with bmp instead of text.
(because of ansii -> unicode problems, for example japaneese in the bitmap)
The Imagelistbox works just fine, with one exception. On startup the focus rectangle ist drawn (incorrect ->means not totally around the item) over the first item of the listbox.
If I choose another item there two focus rectangles shown. the old one an the new one (yet correct drawn -> means totally around the item).
After scrolling down and up the list box, the incorrect focus disappears and the listbox works fine. Also the incorrect focus disappears when changing to an other window and change back to my application window.
The selection is always returned correctly (even if the incorrect focus is shown).
I've added a screenshot to the post. Ist taken after selecting an item (directly after startup).
I've tried for hours but can't fix it.
May anybody can help?
ThX
ks.drax
Here's my source code:
(in the resource there's the dialog and the bitmap included)
/*********************************************************************
* imagelistbox.cpp
*
*
* Description - gui element (choose language from bitmaps)
**********************************************************************
*
* 25.09.2006 File created
*
*********************************************************************/
#include <windows.h>
#include "resource.h"
#define XBITMAP 100
#define YBITMAP 20
#define BUFFER MAX_PATH
HBITMAP hbmpPencil, hbmpCrayon, hbmpMarker, hbmpPen, hbmpFork;
HBITMAP hbmpPicture, hbmpOld;
BOOL CALLBACK DialogProc (HWND, UINT, WPARAM, LPARAM);
void AddItem(HWND hwnd, LPSTR lpstr, HBITMAP hbmp);
/*********************************************************************
* FUNCTION : WinMain
* DESCRIPTION: the mainroutine of the application
* PARAMETERS : HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow
* RETURNS : error code
*********************************************************************/
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
return DialogBox (hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, DialogProc);
}
/*********************************************************************
* FUNCTION : DialogProc
* DESCRIPTION: the dialog handling routine
* PARAMETERS : HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam
* RETURNS : true or false
*********************************************************************/
BOOL CALLBACK DialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
HWND hListBox;
HDC hdcMem;
LPMEASUREITEMSTRUCT lpmis;
LPDRAWITEMSTRUCT lpdis;
int lResult;
switch(message)
{
case WM_INITDIALOG:
// Retrieve list box handle.
// Load bitmaps.
hbmpPencil = LoadBitmap(GetModuleHandle(NULL),MAKEINTRESOURCE(103));
hListBox = GetDlgItem(hDlg, IDC_LIST1);
// Initialize the list box text
AddItem(hListBox, "pencil", hbmpPencil);
AddItem(hListBox, "crayon", hbmpPencil);
AddItem(hListBox, "marker", hbmpPencil);
AddItem(hListBox, "marker", hbmpPencil);
AddItem(hListBox, "marker", hbmpPencil);
AddItem(hListBox, "marker", hbmpPencil);
SetFocus(hListBox);
SendMessage(hListBox, LB_SETCURSEL, -1, 0);
return TRUE;
case WM_MEASUREITEM:
lpmis = (LPMEASUREITEMSTRUCT) lParam;
// Set the height of the list box items.
lpmis->itemHeight = 22;
return TRUE;
case WM_DRAWITEM:
lpdis = (LPDRAWITEMSTRUCT) lParam;
// If there are no list box items, skip this message.
if (lpdis->itemID == -1)
{
break;
}
// Draw the bitmap for the list box item.
switch (lpdis->itemAction)
{
case ODA_SELECT:
case ODA_DRAWENTIRE:
// Display the bitmap associated with the item.
hbmpPicture =(HBITMAP)SendMessage(lpdis->hwndItem,
LB_GETITEMDATA, lpdis->itemID, (LPARAM) 0);
hdcMem = CreateCompatibleDC(lpdis->hDC);
hbmpOld = (HBITMAP)SelectObject(hdcMem, hbmpPicture);
BitBlt(lpdis->hDC,
lpdis->rcItem.left, lpdis->rcItem.top,
lpdis->rcItem.right - lpdis->rcItem.left,
lpdis->rcItem.bottom - lpdis->rcItem.top,
hdcMem, 0, 0, SRCCOPY);
SelectObject(hdcMem, hbmpOld);
DeleteDC(hdcMem);
break;
case ODA_FOCUS:
break;
}
break;
case WM_DESTROY:
case WM_CLOSE:
// Hier wird der Dialog geschlossen
EndDialog(hDlg,0);
return (TRUE);
case WM_COMMAND:
switch (HIWORD(wParam))
{
case LBN_SELCHANGE:
hListBox = GetDlgItem(hDlg, IDC_LIST1);
lResult = (int) SendMessage(hListBox,LB_GETCARETINDEX,0,0);
//lResult holds the index of the selected bmp
//MessageBox(hDlg, "Selection changed", "ok", MB_OK);
return (TRUE);
}
return (TRUE);
}
return (FALSE);
}
/*********************************************************************
* FUNCTION : AddItem
* DESCRIPTION: adds an item to the listbox
* PARAMETERS : HWND hwnd, LPSTR lpstr, HBITMAP hbmp
* RETURNS : none
*********************************************************************/
void AddItem(HWND hwnd, LPSTR lpstr, HBITMAP hbmp)
{
int nItem;
nItem = (int)SendMessage(hwnd, LB_ADDSTRING, 0, (LPARAM)lpstr);
SendMessage(hwnd, LB_SETITEMDATA, nItem, (LPARAM)hbmp);
}