Club Builder - Foros de discusión Club Builder
Foro de C++Builder en español
 
 FAQFAQ   BuscarBuscar   MiembrosMiembros   Grupos de UsuariosGrupos de Usuarios   RegistrarseRegistrarse 
 PerfilPerfil   Entra para ver tus mensajes privadosEntra para ver tus mensajes privados   EntrarEntrar 

MessageBox personalizado

 
Publicar nuevo tema   Responder al tema    Foros de discusión -> Tips & Tricks
Ver tema anterior :: Ver tema siguiente  
Autor Mensaje
Mensaje    Asunto: MessageBox personalizado Responder citando

Primero que nada gracias a escafandra, que me ha ayudado muchísimo para conseguir esto.

Hace falta un formulario que contenga un TImage, un TActionList y dos TLabel. El código es este:
Código:
//---------------------------------------------------------------------------

#include <vcl>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------

#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

extern AnsiString cMensaje;
extern int nIcono;
//---------------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)
     : TForm(Owner)
{
     int nButWidth, nWidth;
     TComponent *Source;
     TStringList *slBoton = new TStringList();

     Caption  = Application->Title;
     ColorForm(this);

     Image1->Picture->Icon->Handle = LoadIcon(0, MAKEINTRESOURCE(nIcono));
     slBoton->Text = cMensaje.SubString(cMensaje.Pos("#") + 1, cMensaje.Length());

     nButWidth = 15;
     Label1->Caption = cMensaje.SubString(1, cMensaje.Pos("#") - 1);
     Label1->Font->Style = TFontStyles() << fsBold;

     for (int nItem = 0; nItem <slBoton>Count; nItem ++)
     {
          Label2->Caption = slBoton->Strings[nItem];
          Label2->Refresh();

          TButton *Boton     = new TButton(Form1);
          Boton->Parent      = Form1;
          Boton->Action      = ActionList->Actions[nItem];
          Boton->Caption     = slBoton->Strings[nItem];
          Boton->Enabled     = true;
          Boton->Font->Style = TFontStyles() <<fsBold>Height      = 25;
          Boton->Left        = nButWidth;
          Boton->Name        = "Button" + FormatFloat("0", nItem + 1);
          Boton->Top         = max(Label1->Height, Image1->Height) + 25;
          Boton->Visible     = true;
          Boton->Width       = max(Label2->Width, 75) + 15;
          nButWidth += Boton->Width + 10;
     }
     nButWidth += 10;

     nWidth = Label1->Left + Label1->Width + 20;

     if (nButWidth > nWidth)
          nWidth = nButWidth;

     Form1->Width  = nWidth;
     Form1->Height = max(Label1->Height, Image1->Height) + 90;
     Form1->Left   = (Screen->Width - Width) / 2;
     Form1->Top    = (Screen->Height - Height) / 2;

     if (slBoton->Count == 1)
          for (int nItem = 0; nItem <ComponentCount>ClassNameIs("TButton"))
               {
                    TButton *Button = static_cast<TButton>(Source);
                    Button->Left = (Width - Button->Width) / 2;
               }
          }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::CreateParams(TCreateParams& Params)
{
     TForm::CreateParams(Params);
     Params.WindowClass.style |= CS_NOCLOSE;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Action1Execute(TObject *Sender)
{
     nIcono = 1;
     Form1->Close();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Action2Execute(TObject *Sender)
{
     nIcono = 2;
     Form1->Close();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Action3Execute(TObject *Sender)
{
     nIcono = 3;
     Form1->Close();
}
//---------------------------------------------------------------------------

En el programa llamo a la función por ejemplo de la siguiente forma:
Código:
void __fastcall TfColegio::Button33Click(TObject *Sender)
{
     cAux = "Sí, CON copia de seguridad\r\nSí, SIN copia de seguridad\r\nNo, continuar trabajando";
     switch (Mensaje(32514, "¿Desea terminar la sesión de trabajo?", cAux))
     {
          case 1:
               Application->CreateForm(__classid(TfSeguridad), &fSeguridad);
               fSeguridad->GroupBox1->Visible = false;
               fSeguridad->Button1Click(NULL);
               delete fSeguridad;
          case 2:
               fColegio->Close();
               return;
          case 3:
               break;
     }
}
//---------------------------------------------------------------------------

En la llamada los parámetros que se pasan son:
- Icono a pintar. Los valores están en http://msdn.microsoft.com/en-us/library/ms648072
- Literal del mensaje
- Botones a mostrar.
El resultado es este:

_________________
Un saludo

Necesitamos tu ayuda: Envía AI al 27595 (Coste 1,20 euros + IVA)
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email
Mensaje    Asunto: Responder citando

Una mejora/simplificación al código anterior que nos permitirá un número indefinido de botones. Partimos de un formulario similar al descrito antes, pero sin TActionList.
Código:
//---------------------------------------------------------------------------

#include <algorithm>
#include <vcl>
#pragma hdrstop

#include "Mensaje.h"
#include "Socios.h"
//---------------------------------------------------------------------------

#pragma package(smart_init)
#pragma resource "*.dfm"
TfMensaje *fMensaje;

extern AnsiString cMensaje;
extern int nIcono;
//---------------------------------------------------------------------------

__fastcall TfMensaje::TfMensaje(TComponent* Owner)
     : TForm(Owner)
{
     int nButWidth, nWidth;
     TComponent *Source;
     TStringList *slBoton = new TStringList();

     Caption = Application->Title;
   fSocios->ImageList->GetIcon(0, Icon);
     Image1->Picture->Icon->Handle = LoadIcon(0, MAKEINTRESOURCE(nIcono));
     slBoton->Text = cMensaje.SubString(cMensaje.Pos("#") + 1, cMensaje.Length());

     Label1->Caption = cMensaje.SubString(1, cMensaje.Pos("#") - 1);
     Label1->Font->Style = TFontStyles() <<fsBold>Caption.IsEmpty())
          nButWidth = Image1->Left + Image1->Width + 10;
     else
          nButWidth = 15;

     for (int nItem = 0; nItem <slBoton>Count; nItem ++)
     {
          Label2->Caption = slBoton->Strings[nItem];
          Label2->Refresh();

          TButton *Boton     = new TButton(this);
          Boton->Parent      = this;
          try
          {
               Boton->OnClick = ButtonClick;
          }
          catch(...)
          {
          }
          Boton->Caption     = slBoton->Strings[nItem];
          Boton->Enabled     = true;
          Boton->Font->Style = TFontStyles() <<fsBold>Height      = 25;
          Boton->Left        = nButWidth;
          Boton->Name        = "Boton" + FormatFloat("0", nItem +1);
          Boton->Tag         = nItem + 1;
          Boton->Width       = std::max(Label2->Width, 75) + 15;
          Boton->Visible     = true;
          if (Label1->Caption.IsEmpty())
               Boton->Top    = Image1->Top + ((Image1->Height - Boton->Height) / 2);
          else
               Boton->Top    = std::max(Label1->Height, Image1->Height) + 25;

          nButWidth += Boton->Width + 10;
     }
     nButWidth += 10;

     nWidth = Label1->Left + Label1->Width + 20;

     if (nButWidth > nWidth)
          nWidth = nButWidth;

     Panel1->Width  = nWidth - 8;
     if (Label1->Caption.IsEmpty())
          Panel1->Height = Image1->Height + 2 * Image1->Top;
     else
          Panel1->Height = std::max(Label1->Height, Image1->Height) + 60;
     fMensaje->Left = (Screen->Width - Width) / 2;
     fMensaje->Top  = (Screen->Height - Height) / 2;

     if (slBoton->Count == 1)
          for (int nItem = 0; nItem <fMensaje>ComponentCount; nItem ++)
          {
           Source = fMensaje->Components[nItem];

           if (Source->ClassNameIs("TButton"))
               {
                    TButton *Button = static_cast<TButton>(Source);
                    Button->Left = (Width - Button->Width) / 2;
               }
          }
}
//---------------------------------------------------------------------------

void __fastcall TfMensaje::ButtonClick(TObject *Sender)
{
     nIcono = ((TButton*)Sender)->Tag;
     fMensaje->Close();
}
//---------------------------------------------------------------------------

void __fastcall TfMensaje::CreateParams(TCreateParams& Params)
{
     TForm::CreateParams(Params);
     Params.WindowClass.style |= CS_NOCLOSE;
}
//---------------------------------------------------------------------------

La diferencia estriba en que en vez de utilizar acciones predefinidas se asigna un valor al Tag de cada botón en función del número de elemento en la definición del mismo. Así con este código:
Código:
Mensaje(2, "¿Generar informe de facturación?", "En PDF\r\nEn XLS\r\nEn RTF\r\nEn pantalla\r\nNo generar")

obtendremos lo que se muestra en esta imagen.

_________________
Un saludo

Necesitamos tu ayuda: Envía AI al 27595 (Coste 1,20 euros + IVA)
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email
Mostrar mensajes de anteriores:   
Cambiar a:  
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


Hosting WordPress Indedmedia desde 3,99 €
© 2002–2026 Club Builder · Funciona con phpBB.