 |
Club Builder Foro de C++Builder en español
|
| Ver tema anterior :: Ver tema siguiente |
| Autor |
Mensaje |
edwinhp2 Lt JG

Registrado: 19 Nov 2005Mensajes: 67
|
Publicado: Vie 26 Ene, 2007 6:31 pm Asunto: Componente TListView con Ordenación por columna |
|
|
Hola Compañeros,
Aquí molestándolos un poco con su conocimiento.
Estuve navegando por la web y recolectando pedazos de código del TListView me estoy aventurando para hacer un componente en Builder que ordene las columnas y les ponga una imágen a la columna ordenada.
El ordenamiento funciona pero al momento de colocar la imagen, truena.
Le agrego al proyecto un archivo de recursos ( SortList.rc) para que pueda colocar unas imagenes por defecto, pero el problema que tengo es que al momento de cargarlas con me dice que no se tiene los recursos.
Aquí les dejo el código para ver si alguno me puede ir ayudando.
Saludos !!!
| Código: |
//---------------------------------------------------------------------------
#include <vcl>
#include <DateUtils>
#include <shlwapi>
#pragma hdrstop
#include "SortListView.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//
bool Ascending;
static inline void ValidCtrCheck(TSortListView *)
{
new TSortListView(NULL);
}
//---------------------------------------------------------------------------
__fastcall TSortListView::~TSortListView()
{
if ( !BMP_Up )
delete BMP_Up;
if ( !BMP_Down )
delete BMP_Down;
}
__fastcall TSortListView::TSortListView(TComponent* Owner)
: TListView(Owner)
{
LastSortedColumn = -1;
Ascending = true;
FOrdenarColumnas = false;
BMP_Up = new Graphics::TBitmap();
BMP_Down = new Graphics::TBitmap();
ValidaBMP();
}
//---------------------------------------------------------------------------
void __fastcall TSortListView::ValidaBMP(void)
{
if ( BMP_Up && BMP_Down )
{
try
{
BMP_Up->LoadFromResourceID ( (unsigned int) HInstance,10000);
BMP_Down->LoadFromResourceID( (unsigned int) HInstance,20000);
}
catch(...)
{
throw Exception("Problemas al cargar las imágenes");
}
}
}
void __fastcall DrawTransparentBitmap(HDC DC,
HBITMAP hBmp,
int xStart,
int yStart,
COLORREF cTransparentColor)
{
BITMAP bm;
COLORREF cColor;
HBITMAP bmAndBack, bmAndObject, bmAndMem, bmSave;
HBITMAP bmBackOld, bmObjectOld, bmMemOld, bmSaveOld;
HDC hdcMem, hdcBack, hdcObject, hdcTemp, hdcSave;
TPoint ptSize;
hdcTemp = CreateCompatibleDC(DC);
SelectObject(hdcTemp, hBmp); // Select the bitmap
GetObject(hBmp, sizeof(BITMAP), &bm);
ptSize.x = bm.bmWidth; // Get width of bitmap
ptSize.y = bm.bmHeight; // Get height of bitmap
DPtoLP(hdcTemp, &ptSize, 1); // Convert from device
// to logical points
// Create some DCs to hold temporary data.
hdcBack = CreateCompatibleDC(DC);
hdcObject = CreateCompatibleDC(DC);
hdcMem = CreateCompatibleDC(DC);
hdcSave = CreateCompatibleDC(DC);
// Create a bitmap for each DC. DCs are required for a number of
// GDI functions.
// Monochrome DC
bmAndBack = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);
// Monochrome DC
bmAndObject = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);
bmAndMem = CreateCompatibleBitmap(DC, ptSize.x, ptSize.y);
bmSave = CreateCompatibleBitmap(DC, ptSize.x, ptSize.y);
// Each DC must select a bitmap object to store pixel data.
bmBackOld = SelectObject(hdcBack, bmAndBack);
bmObjectOld = SelectObject(hdcObject, bmAndObject);
bmMemOld = SelectObject(hdcMem, bmAndMem);
bmSaveOld = SelectObject(hdcSave, bmSave);
// Set proper mapping mode.
SetMapMode(hdcTemp, GetMapMode(DC));
// Save the bitmap sent here, because it will be overwritten.
BitBlt(hdcSave, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY);
// Set the background color of the source DC to the color.
// contained in the parts of the bitmap that should be transparent
cColor = SetBkColor(hdcTemp, cTransparentColor);
// Create the object mask for the bitmap by performing a BitBlt
// from the source bitmap to a monochrome bitmap.
BitBlt(hdcObject, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0,SRCCOPY);
// Set the background color of the source DC back to the original
// color.
SetBkColor(hdcTemp, cColor);
// Create the inverse of the object mask.
BitBlt(hdcBack, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0,NOTSRCCOPY);
// Copy the background of the main DC to the destination.
BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, DC, xStart, yStart,SRCCOPY);
// Mask out the places where the bitmap will be placed.
BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, SRCAND);
// Mask out the transparent colored pixels on the bitmap.
BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcBack, 0, 0, SRCAND);
// XOR the bitmap with the background on the destination DC.
BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCPAINT);
// Copy the destination to the screen.
BitBlt(DC, xStart, yStart, ptSize.x, ptSize.y, hdcMem, 0, 0,SRCCOPY);
// Place the original bitmap back into the bitmap sent here.
BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcSave, 0, 0, SRCCOPY);
// Delete the memory bitmaps.
DeleteObject(SelectObject(hdcBack, bmBackOld));
DeleteObject(SelectObject(hdcObject, bmObjectOld));
DeleteObject(SelectObject(hdcMem, bmMemOld));
DeleteObject(SelectObject(hdcSave, bmSaveOld));
// Delete the memory DCs.
DeleteDC(hdcMem);
DeleteDC(hdcBack);
DeleteDC(hdcObject);
DeleteDC(hdcSave);
DeleteDC(hdcTemp);
}
int __stdcall SortByColumn(long Item1,long Item2, long Data)
{
TListItem* item1 = reinterpret_cast<TListItem>(Item1);
TListItem* item2 = reinterpret_cast<TListItem>(Item2);
TListView* lv = static_cast<TListView>(item1->ListView);
TListColumn* lc = static_cast<TListColumn>(lv->Columns->FindItemID(Data));
AnsiString valor1;
AnsiString valor2;
TDateTime fecha1;
TDateTime fecha2;
AnsiString str1;
AnsiString str2;
TReplaceFlags Remplaza;
int resp;
Remplaza <<rfReplaceAll>Caption;
valor2 = item2->Caption;
}
else
{
valor1 = item1->SubItems->Strings[Data-1];
valor2 = item2->SubItems->Strings[Data-1];
}
switch( lc->Tag )
{
case srtText:
/* Cadenas */
resp = AnsiCompareText(valor1,valor2);
break;
case srtNumbers:
/* Números enteros o decimales */
try
{
valor1.ToDouble();
}
catch(...)
{
throw Exception( AnsiString().sprintf("El valor %s no se puede convertir a número.",valor1.c_str()));
}
try
{
valor2.ToDouble();
}
catch(...)
{
throw Exception( AnsiString().sprintf("El valor %s no se puede convertir a número.",valor2.c_str()));
}
resp = valor1.ToDouble() - valor2.ToDouble();
break;
case srtDates:
/* TDateTime's */
if ( !TryStrToDateTime(valor1,fecha1) )
throw Exception( AnsiString().sprintf("El valor %s no se puede convertir a Fecha.",valor1.c_str()));
if ( !TryStrToDateTime(valor2,fecha2) )
throw Exception( AnsiString().sprintf("El valor %s no se puede convertir a Fecha.",valor2.c_str()));
resp = CompareDateTime(fecha1,fecha2);
break;
case strCurrency:
/* Números con formato de monedas : $5,987.26 */
str1 = StringReplace(valor1,ThousandSeparator,"",Remplaza);
str1 = StringReplace(str1,"$","",Remplaza);
str2 = StringReplace(valor2,ThousandSeparator,"",Remplaza);
str2 = StringReplace(str2,"$","",Remplaza);
valor1 = str1;
valor2 = str2;
try
{
valor1.ToDouble();
}
catch(...)
{
throw Exception( AnsiString().sprintf("El valor %s no se puede convertir a número.",valor1.c_str()));
}
try
{
valor2.ToDouble();
}
catch(...)
{
throw Exception( AnsiString().sprintf("El valor %s no se puede convertir a número.",valor2.c_str()));
}
resp = valor1.ToDouble() - valor2.ToDouble();
break;
}
return Ascending ? resp : -resp;
}
void __fastcall TSortListView::ColClick(TListColumn* Column)
{
if ( FOrdenarColumnas )
{
if ( Column->Index == LastSortedColumn )
Ascending = !Ascending;
else
LastSortedColumn = Column->Index;
CustomSort(SortByColumn, Column->Index);
}
if ( FOnColumnClick )
FOnColumnClick(this,Column);
}
void __fastcall TSortListView::WMErase(TMessage &Message)
{
DefaultHandler(&Message);
}
void __fastcall TSortListView::WMNotifica(TMessage &Message)
{
NMHDR *pnmhr = (NMHDR*)Message.LParam;
int ColNo;
TRect Rect;
HDC DC;
int X,Y;
switch(pnmhr->code)
{
case NM_CUSTOMDRAW:
LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)Message.LParam;
switch(lplvcd->nmcd.dwDrawStage)
{
/* notify the list view about the painting operation */
case CDDS_PREPAINT:
Message.Result = CDRF_NOTIFYITEMDRAW;
break;
/* paint only if the item mach specified criteria */
case CDDS_ITEMPREPAINT:
/*
Si queremos cambiarle el color de la letra de la columna
lplvcd->clrText = RGB(255,255,255);
lplvcd->clrTextBk = RGB(255,0,0);
*/
break;
case CDDS_ITEMPOSTPAINT:
ColNo = lplvcd->nmcd.dwItemSpec;
if ( FOrdenarColumnas )
ValidaBMP();
if (LastSortedColumn == ColNo && FOrdenarColumnas )
{
Rect = lplvcd->nmcd.rc;
DC = lplvcd->nmcd.hdc;
if ( Ascending )
{
X = Rect.Right - (BMP_Up->Width + 4);
Y = ( ((Rect.Bottom-Rect.Top)- BMP_Up->Height) / 2);
if (lplvcd->nmcd.uItemState == (CDIS_FOCUS | CDIS_SELECTED) )
{
X+=2;
Y+=2;
}
DrawTransparentBitmap(DC,BMP_Up->Handle,X,Y,clFuchsia);
}
else
{
X = Rect.Right- (BMP_Down->Width + 4);
Y = ( ((Rect.Bottom-Rect.Top)- BMP_Down->Height)/2);
if (lplvcd->nmcd.uItemState == (CDIS_FOCUS | CDIS_SELECTED) )
{
X+=2;
Y+=2;
}
DrawTransparentBitmap(DC,BMP_Down->Handle,X,Y,clFuchsia);
}
}
}
Message.Result = CDRF_NOTIFYSUBITEMDRAW;
break;
break;
}
}
namespace Sortlistview
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TSortListView)};
RegisterComponents("General", classes, 0);
}
}
//---------------------------------------------------------------------------
|
Este es el código del archivo H
| Código: |
//---------------------------------------------------------------------------
#ifndef SortListViewH
#define SortListViewH
//---------------------------------------------------------------------------
#include <SysUtils>
#include <Classes>
#include <ComCtrls>
#include <Controls>
#include <Graphics>
//---------------------------------------------------------------------------
typedef enum{srtText, srtNumbers, srtDates, strCurrency} TSortColumn;
class PACKAGE TSortListView : public TListView
{
private:
int LastSortedColumn;
bool FOrdenarColumnas;
TLVColumnClickEvent FOnColumnClick;
Graphics::TBitmap* BMP_Up;
Graphics::TBitmap* BMP_Down;
void __fastcall ValidaBMP(void);
protected:
DYNAMIC void __fastcall ColClick(TListColumn* Column);
void __fastcall WMErase(TMessage &Message);
void __fastcall WMNotifica(TMessage &Message);
BEGIN_MESSAGE_MAP
VCL_MESSAGE_HANDLER(WM_NOTIFY, TMessage, WMNotifica);
VCL_MESSAGE_HANDLER(WM_ERASEBKGND, TMessage, WMErase);
END_MESSAGE_MAP(TListView);
public:
__fastcall TSortListView(TComponent* Owner);
__fastcall ~TSortListView();
__published:
__property bool OrdenarColumnas = {read=FOrdenarColumnas, write=FOrdenarColumnas, default=false};
__property TLVColumnClickEvent OnColumnClick = {read=FOnColumnClick, write=FOnColumnClick};
//__property Graphics::TBitmap* ImagenAscendente = {read=};
};
//---------------------------------------------------------------------------
#endif
|
el archivo RC es el siguiente
| Código: |
10000 BITMAP "up.bmp"
20000 BITMAP "down.bmp" |
|
|
| Volver arriba |
|
 |
Cesar C. Salazar Gonzalez Master


Registrado: 01 Oct 2002Mensajes: 2155Ubicación: San Cristóbal de Las Casas, Chiapas
|
Publicado: Vie 26 Ene, 2007 8:22 pm Asunto: |
|
|
Disculpa entendi mal tu codigo, puedes darnos un ejemplo un pequeño proyecto para reproducirlo y detectar el fallo.
Gracias!
un saludo _________________

Ultima edición por Cesar C. Salazar Gonzalez el Dom 28 Ene, 2007 9:36 pm, editado 1 vez |
|
| Volver arriba |
|
 |
edwinhp2 Lt JG

Registrado: 19 Nov 2005Mensajes: 67
|
Publicado: Dom 28 Ene, 2007 8:15 pm Asunto: |
|
|
Compañeros,
Estuve chambeandole en el componente, lo más que he podido hacer es que al momento de asignarle la imagen en tiempo de diseño, al momento de correr el ejecutable, solamente me sale la imagen en la primera columna la imagen de arriba, de abajo y ya no vuelve a salir, se desaparece todo.
Aquí les pongo el código
_Leo, pon tu dedo en el código, please !!! (jejejeje)
| Código: |
//---------------------------------------------------------------------------
#include <vcl>
#include <DateUtils>
#include <shlwapi>
#pragma hdrstop
#include "SortListView.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//
#ifndef HDF_SORTUP
#define HDF_SORTUP 0x0400
#endif
#ifndef HDF_SORTDOWN
#define HDF_SORTDOWN 0x0200
#endif
#ifndef IDB_UPARROW
#define IDB_UPARROW 10000
#endif
#ifndef IDB_DOWNARROW
#define IDB_DOWNARROW 20000
#endif
bool Ascending;
static inline void ValidCtrCheck(TSortListView *)
{
new TSortListView(NULL);
}
//---------------------------------------------------------------------------
void __fastcall TSortListView::SetImagenUp(Graphics::TBitmap* Imagen)
{
if ( !FImagenUp->Empty )
FImagenUp->FreeImage();
FImagenUp->Assign(Imagen);
FImagenUp->TransparentColor = clFuchsia;
FImagenUp->Transparent = true;
}
void __fastcall TSortListView::SetImagenDown(Graphics::TBitmap* Imagen)
{
if ( !FImagenDown->Empty )
FImagenDown->FreeImage();
FImagenDown->Assign(Imagen);
FImagenDown->TransparentColor = clFuchsia;
FImagenDown->Transparent = true;
}
__fastcall TSortListView::~TSortListView()
{
if ( !FImagenDown->Empty )
FImagenDown->FreeImage();
if ( !FImagenUp->Empty )
FImagenUp->FreeImage();
delete FImagenUp;
delete FImagenDown;
}
__fastcall TSortListView::TSortListView(TComponent* Owner)
: TListView(Owner)
{
LastSortedColumn = -1;
Ascending = true;
FOrdenarColumnas = false;
FImagenUp = new Graphics::TBitmap();
FImagenDown = new Graphics::TBitmap();
}
//---------------------------------------------------------------------------
BOOL IsCommCtrlVersion6()
{
static BOOL isCommCtrlVersion6 = -1;
if (isCommCtrlVersion6 != -1)
return isCommCtrlVersion6;
//The default value
isCommCtrlVersion6 = FALSE;
HINSTANCE commCtrlDll = LoadLibrary("comctl32.dll");
if(commCtrlDll)
{
DLLGETVERSIONPROC pDllGetVersion;
pDllGetVersion = (DLLGETVERSIONPROC)GetProcAddress(commCtrlDll, "DllGetVersion");
if (pDllGetVersion)
{
DLLVERSIONINFO dvi = {0};
dvi.cbSize = sizeof(DLLVERSIONINFO);
(*pDllGetVersion)(&dvi);
isCommCtrlVersion6 = (dvi.dwMajorVersion == 6);
}
FreeLibrary(commCtrlDll);
}
return isCommCtrlVersion6;
}
void TSortListView::ListView_SetHeaderSortImage(HWND listView, int columnIndex, BOOL isAscending)
{
HWND header = ListView_GetHeader(listView);
BOOL isCommonControlVersion6 = IsCommCtrlVersion6();
int columnCount = Header_GetItemCount(header);
for (int i = 0; i<columnCount>Index == LastSortedColumn )
Ascending = !Ascending;
else
LastSortedColumn = Column->Index;
if ( !FImagenUp->Empty && !FImagenUp->Empty )
ListView_SetHeaderSortImage(this->Handle, LastSortedColumn, Ascending);
CustomSort(SortByColumn, Column->Index);
}
if ( FOnColumnClick )
FOnColumnClick(this,Column);
}
/*void __fastcall TSortListView::WMErase(TMessage &Message)
{
DefaultHandler(&Message);
}*/
namespace Sortlistview
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TSortListView)};
RegisterComponents("General", classes, 0);
}
}
//---------------------------------------------------------------------------
|
archivo H
| Código: |
//---------------------------------------------------------------------------
#ifndef SortListViewH
#define SortListViewH
//---------------------------------------------------------------------------
#include <SysUtils>
#include <Classes>
#include <ComCtrls>
#include <Controls>
#include <Graphics>
//---------------------------------------------------------------------------
typedef enum{srtText, srtNumbers, srtDates, strCurrency} TSortColumn;
class PACKAGE TSortListView : public TListView
{
private:
int LastSortedColumn;
bool FOrdenarColumnas;
TLVColumnClickEvent FOnColumnClick;
void ListView_SetHeaderSortImage(HWND listView, int columnIndex, BOOL isAscending);
Graphics::TBitmap* FImagenDown;
Graphics::TBitmap* FImagenUp;
void __fastcall SetImagenUp(Graphics::TBitmap* Imagen);
void __fastcall SetImagenDown(Graphics::TBitmap* Imagen);
protected:
DYNAMIC void __fastcall ColClick(TListColumn* Column);
//void __fastcall WMErase(TMessage &Message);
/*BEGIN_MESSAGE_MAP
VCL_MESSAGE_HANDLER(WM_ERASEBKGND, TMessage, WMErase);
END_MESSAGE_MAP(TListView);*/
public:
__fastcall TSortListView(TComponent* Owner);
__fastcall ~TSortListView();
__published:
__property bool OrdenarColumnas = {read=FOrdenarColumnas, write=FOrdenarColumnas};
__property TLVColumnClickEvent OnColumnClick = {read=FOnColumnClick, write=FOnColumnClick};
__property Graphics::TBitmap* ImagenArriba = {read=FImagenUp, write=SetImagenUp};
__property Graphics::TBitmap* ImagenAbajo = {read=FImagenDown, write=SetImagenDown};
};
//---------------------------------------------------------------------------
#endif
|
|
|
| Volver arriba |
|
 |
|
|
No puedes publicar temas nuevos en este foro No puedes responder a temas en este foro No puedes editar tus mensajes en este foro No puedes borrar tus mensajes en este foro No puedes votar en las encuestas de este foro
|
© 2002–2026 Club Builder · Funciona con phpBB.
|