Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   C++ Builder (https://www.clubdelphi.com/foros/forumdisplay.php?f=13)
-   -   Error al mover componentes en tiempo de ejecución (https://www.clubdelphi.com/foros/showthread.php?t=82559)

DSK25 19-03-2013 14:45:52

Error al mover componentes en tiempo de ejecución
 
Hola, sucede que en una aplicación quiero que el usuario pueda mover los componentes, asi que use el siguiente código:

Código:

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
bool Mover = false;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ListBox1MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
  Mover = true; 
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ListBox1MouseUp(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
  Mover = false; 
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ListBox1MouseMove(TObject *Sender,
      TShiftState Shift, int X, int Y)
{
  if(Mover == true) {
      TPoint *nPoint2;
      GetCursorPos(nPoint2);
      ListBox1->Left = (nPoint2->x - Form1->Left) - ListBox1->Width/2;
      ListBox1->Top = (nPoint2->y - Form1->Top) - ListBox1->Width/2;
  };
}
//---------------------------------------------------------------------------

Ahí trato de mover un ListBox pero me manda el error "Access violation at address ...", alguien sabe como solucionarlo :)

ecfisa 19-03-2013 17:47:54

Hola DSK25.

El error es por que estas haciendo mal la creación del puntero nPoint2. No probé el código (no sé si hace lo que buscas) pero debería ser:

Código:

void __fastcall TForm1::ListBox1MouseMove(TObject *Sender,
  TShiftState Shift, int X, int Y)
{
  if(Mover == true) {
      TPoint *nPoint2 = new TPoint;
      __try {
        GetCursorPos(nPoint2);
        ListBox1->Left = (nPoint2->x - Form1->Left) - ListBox1->Width/2;
        ListBox1->Top = (nPoint2->y - Form1->Top) - ListBox1->Width/2;
      }
      __finally {
        delete nPoint2;
      }
  };
}

De todos modos tal vez te interese probar este código que hace lo que buscas:
Código:

void __fastcall TForm1::ListBox1MouseDown(TObject *Sender,
  TMouseButton Button, TShiftState Shift, int X, int Y)
{
  ReleaseCapture;
  SendMessage(ListBox1->Handle, WM_SYSCOMMAND, 0xF012, 0);
}

Saludos.

DSK25 19-03-2013 18:24:45

Gracias ecfisa, me funciono ^\||/.


La franja horaria es GMT +2. Ahora son las 23:00:00.

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