Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   C++ Builder (https://www.clubdelphi.com/foros/forumdisplay.php?f=13)
-   -   Ayuda dinámica (https://www.clubdelphi.com/foros/showthread.php?t=91356)

Angel.Matilla 13-01-2017 12:36:54

Ayuda dinámica
 
Tengo este formulario

El cuadro gris que se ve abajo a la derecha es un TListBox que sea crea de forma dinámica al pulsar el BitBtn que se ve a la izquierda. Par ello uso este código:
Código:

void __fastcall TfTablas::BotAyu(TObject *Sender)
{
    TBitBtn *Boton = dynamic_cast<TBitBtn *>(Sender);
    TComponent *Source;
    POINT P1, P2;

    for (nItem = 0; nItem < this->ComponentCount; nItem ++)
    {
          Source = this->FindComponent("AyuLis");
          if (Source != NULL)
              return;
    }

    for (nItem = 0; nItem < this->ComponentCount; nItem ++)
    {
          Source = this->FindComponent(Boton->Name.SubString(2, Boton->Name.Length()));
          if (Source != NULL)
              if (Source->ClassNameIs("TEdit") || Source->ClassNameIs("TLabeledEdit"))
              {
                    AyuEdit = static_cast<TCustomEdit *>(Source);
                    break;
              }
    }

    if (Source == NULL)
          return;

    P1.x = AyuEdit->Parent->Left + AyuEdit->Left;
    P1.y = AyuEdit->Parent->Top + AyuEdit->Top + AyuEdit->Height;
    P2  = ClientToScreen(P1);

    AyuLis              = new TListBox(this);
    AyuLis->Name        = "AyuLis";
    AyuLis->Parent      = this;
    AyuLis->BevelInner  = bvNone;
    AyuLis->BevelKind  = bkFlat;
    AyuLis->BevelOuter  = bvLowered;
    AyuLis->BorderStyle = bsNone;
    AyuLis->Color      = clSilver;
    AyuLis->Height      = 10 * AyuLis->ItemHeight;
    AyuLis->Left        = P2.x;
    AyuLis->Top        = P2.y;
    AyuLis->Width      = AyuEdit->Width + Boton->Width;
    AyuLis->OnClick    = AyuLisClick;
}

La idea, lógicamente, es que dicha ayuda aparezca justo debajo del TEdit correspondiente pero no sé como convertir esas coordenadas obtenidas con ClientToScreen de forma que se muestre como deseo.

Angel.Matilla 13-01-2017 19:12:48

¡Encontré la solución!
 
La he tenido todo el tiempo delante y no me había dado cuenta :oops:

El código ha quedado así:
Código:

void __fastcall TfTablas::BotAyu(TObject *Sender)
{
    TBitBtn *Boton = dynamic_cast<TBitBtn>(Sender);
    TComponent *Source;
    POINT P1, P2;

    for (nItem = 0; nItem <this>ComponentCount; nItem ++)
    {
          Source = this->FindComponent("AyuLis");
          if (Source != NULL)
              return;
    }

    for (nItem = 0; nItem <this>ComponentCount; nItem ++)
    {
          Source = this->FindComponent(Boton->Name.SubString(2, Boton->Name.Length()));
          if (Source != NULL)
              if (Source->ClassNameIs("TEdit") || Source->ClassNameIs("TLabeledEdit"))
              {
                    AyuEdit = static_cast<TCustomEdit>(Source);
                    break;
              }
    }

    if (Source == NULL)
          return;

    P1.x = AyuEdit->Left;
    P1.y = AyuEdit->Top;
    P2 = this->ScreenToClient(AyuEdit->Parent->ClientToScreen(P1));

    AyuLis              = new TListBox(this);
    AyuLis->Name        = "AyuLis";
    AyuLis->Parent      = this;
    AyuLis->BevelInner  = bvNone;
    AyuLis->BevelKind  = bkFlat;
    AyuLis->BevelOuter  = bvLowered;
    AyuLis->BorderStyle = bsNone;
    AyuLis->Height      = 10 * AyuLis->ItemHeight;
    AyuLis->Left        = P2.x;
    AyuLis->Top        = P2.y + AyuEdit->Height;
    AyuLis->Width      = AyuEdit->Width + Boton->Width;
    AyuLis->OnClick    = AyuLisClick;
}

Hay que usar ClientToScreen para sacar la posición absoluta en píxeles del objeto y convertirla luego a posición relativa referida al formulario con ScreenToClient.


La franja horaria es GMT +2. Ahora son las 15:26:32.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi