![]() |
![]() |
| Paypal | FTP | CCD | Buscar | Trucos | Trabajo | Foros |
|
|||||||
| Registrarse | FAQ | Miembros | Calendario | Guía de estilo | Buscar | Temas de Hoy | Marcar Foros Como Leídos |
![]() |
|
|
Herramientas | Buscar en Tema | Desplegado |
|
|
|
#1
|
||||
|
||||
|
¿Margen en celdas de TStringGrid?
Buenas amigos, sigo mejorando el juego de las tablas y ahora le he añadido un ranking y todo va correcto pero me gustaria crear un pequeño margen para que cuando alineo a la derecha los numeros no llegue justo al filo sino que quede un poquitin de espacio a la derecha.
Esto tengo hecho: Código:
void __fastcall TFormRanking::StringGridRankingDrawCell(TObject *Sender,
int ACol, int ARow, TRect &Rect, TGridDrawState State)
{
char *cadena;
cadena = StringGridRanking->Cells[ACol][ARow].c_str();
if (State.Contains(gdFixed))
{
StringGridRanking->Canvas->Brush->Color = clBtnFace;
StringGridRanking->Canvas->Font->Color = clWindowText;
StringGridRanking->Canvas->FillRect(Rect);
HDC dc = StringGridRanking->Canvas->Handle;
DrawText(dc, cadena, strlen(cadena), &Rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
}
else if(State.Contains(gdSelected)) //if is selected use the clAqua color
{
StringGridRanking->Canvas->Brush->Color = clWindow;
StringGridRanking->Canvas->Font->Color = clWindowText;
StringGridRanking->Canvas->FillRect(Rect);
HDC dc = StringGridRanking->Canvas->Handle;
if(ACol == 1)
DrawText(dc, cadena, strlen(cadena), &Rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
else
DrawText(dc, cadena, strlen(cadena), &Rect, DT_RIGHT | DT_VCENTER | DT_SINGLELINE );
}
else if (ARow %2 == 0)
{
StringGridRanking->Canvas->Brush->Color = clSilver;
StringGridRanking->Canvas->Font->Color = clWindowText;
StringGridRanking->Canvas->FillRect(Rect);
HDC dc = StringGridRanking->Canvas->Handle;
if(ACol == 1)
DrawText(dc, cadena, strlen(cadena), &Rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
else
DrawText(dc, cadena, strlen(cadena), &Rect, DT_RIGHT | DT_VCENTER | DT_SINGLELINE );
}
else
{
StringGridRanking->Canvas->Brush->Color = StringGridRanking->Color;
StringGridRanking->Canvas->Font->Color = StringGridRanking->Font->Color;
StringGridRanking->Canvas->FillRect(Rect);
HDC dc = StringGridRanking->Canvas->Handle;
if(ACol == 1)
DrawText(dc, cadena, strlen(cadena), &Rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
else
DrawText(dc, cadena, strlen(cadena), &Rect, DT_RIGHT | DT_VCENTER | DT_SINGLELINE );
}
}
|
|
#2
|
||||
|
||||
|
Al final lo he solucionado concatenando un espacio tras cada cadena que quiero alinear a la derecha. De todos modos si hay algun modo mejor pues me quedo a la espera de sus sugerencias.
|
|
#3
|
||||
|
||||
|
Hola aguml.
A mi tampoco se me ocurre otro medio mas sencillo que agregar un espacio al final. Pero si me permites, yo restructuraría el código de este modo: Código:
void __fastcall TFormRanking::StringGridRankingDrawCell(TObject *Sender, int ACol,
int ARow, TRect &Rect, TGridDrawState State)
{
TStringGrid *SG = static_cast<TStringGrid*>((TStringGrid*)(Sender));
String cad = SG->Cells[ACol][ARow];
HDC dc = StringGrid1->Canvas->Handle;
register int Flags = (ACol == 1 ? DT_CENTER | DT_VCENTER | DT_SINGLELINE :
DT_RIGHT | DT_VCENTER | DT_SINGLELINE);
cad += (ACol > 1 ? " " : "");
if (State.Contains(gdFixed)) {
SG->Canvas->Brush->Color = clBtnFace;
SG->Canvas->Font->Color = clWindowText;
SG->Canvas->FillRect(Rect);
DrawText(dc, cad.c_str(), cad.Length(), &Rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
else if(State.Contains(gdSelected)) {
SG->Canvas->Brush->Color = clAqua;
SG->Canvas->Font->Color = clWindowText;
SG->Canvas->FillRect(Rect);
DrawText(dc, cad.c_str(), cad.Length(), &Rect, Flags);
}
else {
if (ARow % 2 == 0){
SG->Canvas->Brush->Color = clSilver;
SG->Canvas->Font->Color = clWindowText;
}
else {
SG->Canvas->Brush->Color = SG->Color;
SG->Canvas->Font->Color = SG->Font->Color;
}
SG->Canvas->FillRect(Rect);
DrawText(dc, cad.c_str(), cad.Length(), &Rect, Flags);
}
}
![]()
__________________
Daniel Didriksen Guía de estilo - Uso de las etiquetas - La otra guía de estilo .... Última edición por ecfisa fecha: 05-08-2014 a las 22:10:34. |
|
#4
|
||||
|
||||
|
¿podrias explicarme que haces aqui?
Código:
register int Flags = (ACol == 1 ? DT_CENTER | DT_VCENTER | DT_SINGLELINE :
DT_RIGHT | DT_VCENTER | DT_SINGLELINE);
cad += (ACol > 1 ? " " : "");
|
|
#5
|
||||
|
||||
|
Hola aguml.
El símbolo ? es el operador ternario de C y C++, y funciona de este modo: Cita:
Código:
#include <iostream>
using namespace std;
#define MAX(a, b) ((a) > (b) ? (a) : (b))
char *MinMayEqu(const int a, const int b)
{
return (a < b) ? "menor" : ((a > b) ? "mayor" : "igual");
}
int main()
{
int a = 8, b = 7;
cout << MAX(a,b) << endl;
cout << a << " es " << MinMayEqu(a,b) << " a " << b;
cin.get();
return 0;
}
Le indica al compilador que, de ser posible, en lugar del stack utilice los registros del procesador para guardar los valores generando así un código mas rápido. (Solo se aplica a variables automáticas y parámetros) Saludos ![]()
__________________
Daniel Didriksen Guía de estilo - Uso de las etiquetas - La otra guía de estilo .... |
|
#6
|
||||
|
||||
|
Lo he probado y he tenido que modificarlo un poco ya que mi codigo tambien fue modificado un poco para hacer lo que yo queria realmente. Asi ha quedado pero tiene un problemilla:
Código:
void __fastcall TFormRanking::StringGridRankingDrawCell(TObject *Sender, int ACol,
int ARow, TRect &Rect, TGridDrawState State)
{
TStringGrid *SG = static_cast<TStringGrid*>((TStringGrid*)(Sender));
String cad = SG->Cells[ACol][ARow];
HDC dc = StringGridRanking->Canvas->Handle;
register int Flags = (ACol == 1 || State.Contains(gdFixed) ? DT_CENTER | DT_VCENTER | DT_SINGLELINE :
DT_RIGHT | DT_VCENTER | DT_SINGLELINE);
cad += (ACol != 1 && ARow > 0 ? " " : "");
if (State.Contains(gdFixed)) {
SG->Canvas->Brush->Color = clBtnFace;
SG->Canvas->Font->Color = clWindowText;
SG->Canvas->FillRect(Rect);
DrawText(dc, cad.c_str(), cad.Length(), &Rect, Flags);
}
else if(State.Contains(gdSelected)) {
SG->Canvas->Brush->Color = clWindow;
SG->Canvas->Font->Color = clWindowText;
SG->Canvas->FillRect(Rect);
HDC dc = StringGridRanking->Canvas->Handle; //No se porque pero si no pongo esta linea no aparece el texto de la celda seleccionada
DrawText(dc, cad.c_str(), cad.Length(), &Rect, Flags );
}
else {
if (ARow % 2 == 0){
SG->Canvas->Brush->Color = clSilver;
SG->Canvas->Font->Color = clWindowText;
}
else {
SG->Canvas->Brush->Color = SG->Color;
SG->Canvas->Font->Color = SG->Font->Color;
}
SG->Canvas->FillRect(Rect);
DrawText(dc, cad.c_str(), cad.Length(), &Rect, Flags );
}
}
|
|
#7
|
||||
|
||||
|
Hola aguml.
Acabo de probar el codigo que publicaste y me funciona bién, lo que si veo es que me quedó un detalle de la prueba, cambiar la siguiente línea, Código:
HDC dc = StringGrid1->Canvas->Handle; Te pongo el código completo de mi prueba (que funciona correctamente) para que puedas controlar con el tuyo. Código:
/* darle algunos valores a la grilla */
void __fastcall TForm1::FormCreate(TObject *Sender)
{
StringGrid1->Options << TGridOption(goEditing);
for (register int c = 0; c < StringGrid1->ColCount; c++)
for (register int r = 0; r < StringGrid1->RowCount; r++)
StringGrid1->Cells[c][r] = IntToStr(c+r);
}
/* organizar la presentación */
void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
int ARow, TRect &Rect, TGridDrawState State)
{
TStringGrid *SG = static_cast<TStringGrid*>((TStringGrid*)(Sender));
String cad = SG->Cells[ACol][ARow];
HDC dc = SG->Canvas->Handle; // <<<< --- Cambio
register int Flags = (ACol == 1 ? DT_CENTER | DT_VCENTER | DT_SINGLELINE :
DT_RIGHT | DT_VCENTER | DT_SINGLELINE);
cad += (ACol > 1 ? " " : "");
if (State.Contains(gdFixed)) {
SG->Canvas->Brush->Color = clBtnFace;
SG->Canvas->Font->Color = clWindowText;
SG->Canvas->FillRect(Rect);
DrawText(dc, cad.c_str(), cad.Length(), &Rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
else if(State.Contains(gdSelected)) {
SG->Canvas->Brush->Color = clAqua;
SG->Canvas->Font->Color = clWindowText;
SG->Canvas->FillRect(Rect);
DrawText(dc, cad.c_str(), cad.Length(), &Rect, Flags);
}
else {
if (ARow % 2 == 0){
SG->Canvas->Brush->Color = clSilver;
SG->Canvas->Font->Color = clWindowText;
}
else {
SG->Canvas->Brush->Color = SG->Color;
SG->Canvas->Font->Color = SG->Font->Color;
}
SG->Canvas->FillRect(Rect);
DrawText(dc, cad.c_str(), cad.Length(), &Rect, Flags);
}
}
Saludos ![]()
__________________
Daniel Didriksen Guía de estilo - Uso de las etiquetas - La otra guía de estilo .... |
![]() |
| Herramientas | Buscar en Tema |
| Desplegado | |
|
|
Temas Similares
|
||||
| Tema | Autor | Foro | Respuestas | Último mensaje |
| Cambiar color por defecto al seleccionar varias celdas de un TStringGrid | JAI_ME | Varios | 1 | 05-08-2012 18:58:19 |
| Centrar celdas en un TSTringGrid | gluglu | Varios | 3 | 23-01-2005 13:41:44 |
| colorear las celdas de un TstringGrid | mangel | OOP | 2 | 16-12-2003 17:22:47 |
| Celdas de colores diferentes en un TStringGrid | phyera | Gráficos | 2 | 14-09-2003 19:06:26 |
| Justificar Texto en las celdas del TStringGrid | Anabel | Varios | 3 | 19-05-2003 18:20:47 |
|