I have 2 TDBGrids: dbgAnexos and dbgDespesas.
My goal is to click on 1 or more records from dbgAnexos and drag them onto a record from dbgDespesas that corresponds to the position of the mouse relative to it in order to link dbgAnexos's records's data to dbgDespesa's record, but dbgAnexos's records themselves won't be added to dbgDespesas. Both DBGrids will then refresh and dbgAnexos's selected records will disappear, for it only loads records that aren't linked to dbgDespesas.
Here's the code for dbgDespesas's onDragDrop event:
procedure TfrmDespesaLista.dbgDespesasDragDrop(Sender, Source: TObject; X,
Y: Integer);
var
i, j: Integer;
DataSet : TDataSet;
oAnexo: TAnexo;
Pt : TPoint;
begin
try
GetCursorPos(pt);
Pt.x := Round(Pt.x * (65535 / Screen.Width));
Pt.y := Round(Pt.y * (65535 / Screen.Height));
{Move o mouse}
Mouse_Event(MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_MOVE, Pt.x, Pt.y, 0, 0);
{Simula o pressionamento do botão esquerdo do mouse}
Mouse_Event(MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_LEFTDOWN, Pt.x, Pt.y, 0, 0);
{ Simula soltando o botão esquerdo do mouse }
Mouse_Event(MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_LEFTUP, Pt.x, Pt.y, 0, 0);
Application.ProcessMessages;
dbgAnexos.SelectedRows.CurrentRowSelected := True;
if (not queDespesas.Eof) and
(MessageDlg(
'Confirmar os anexos para a Despesa: '+queDespesas.fieldByName('CPN_CODIGO').AsString+'?',
mtConfirmation,
[mbYes,mbNo],
0) = mrYes) then
begin
DataSet := dbgAnexos.DataSource.DataSet;
try
dbgAnexos.BeginUpdate;
DataSet.DisableControls;
for i:=0 to dbgAnexos.SelectedRows.Count-1 do
begin
DataSet.GotoBookmark(Pointer(dbgAnexos.SelectedRows.Items[i]));
oAnexo := TAnexo.Create(oAnexo);
oAnexo.getById({queAnexos}DataSet.FieldByName('ANX_CODIGO').AsInteger);
oAnexo.updateTipoAnexo(taDespesa, queDespesas.FieldByName('CPN_CODIGO').AsInteger);
end;
finally
DataSet.EnableControls;
dbgAnexos.EndUpdate;
end;
end;
dbgAnexos.EndDrag(True);
except on E:Exception do
end;
end;
I thought simulating a mouse click and calling Application.ProcessMessages during this event would be a good way to select the record from dbgDespesas which the mouse is over.
But Application.ProcessMessages returns an access violation somewhere in the code after the onDragDrop event ends. I tried debugging to get answers, but the only thing coming before the violation were assembly codes. Do you have any idea of the reason behind this error?