文件拖入的问题,如何拖入2个文本文件 到2个不同的listbox中?如何识别2个控件? ( 积分: 100 )

  • 主题发起人 主题发起人 xgwzw
  • 开始时间 开始时间
X

xgwzw

Unregistered / Unconfirmed
GUEST, unregistred user!
拖入时如何制定目标组件呢?
procedure TForm1.AppOnMessage(var Msg: TMsg; var Handled: Boolean);
VAR WMD : TWMDropFiles;
BEGIN
IF Msg.message = WM_DROPFILES then
BEGIN
WMD.Msg := Msg.message;
WMD.Drop := Msg.wParam;
WMD.Unused := Msg.lParam;
WMD.Result := 0;
WMDropFiles(WMD);
Handled := TRUE;
END;

end;

procedure TForm1.WMDropFiles(var Msg: TWMDropFiles);
VAR
N : Word;
buffer : ARRAY[0..180] OF Char;
Pt:TPoint;

begin
WITH Msg DO
BEGIN
FOR N := 0 TO DragQueryFile(Drop, $FFFFFFFF, buffer,1)-1 DO
BEGIN
DragQueryFile(Drop, N, Buffer, 80);
if SameText(ExtractFileExt(StrPas(Buffer)),'.txt') then
begin
GetCursorPos (Pt);
//if (pt.x>chklst1.Left) and (pt.x<chklst1.Width+chklst1.Left) and (pt.y>chklst1.Top) and (pt.y<chklst1.Height+chklst1.top)then
// if ControlAtPos(pt,False).Name='chklst1' then
试图得到鼠标位置来判断,我没有通过,可能是我的功力不够把,我想应该可以的!

begin
//ShowMessage('1');
chklst1.Items.Add(StrPas(Buffer));

end;

// if fchklstflag=2 then

if ControlAtPos(pt,False).Name='chklst4' then
begin

chklst5.Items.Add(StrPas(Buffer));


end;

end;
END;
DragFinish(Drop);
END;

END;
 
应该是参考座标的问题。
你需要调用 ClientToScreen(); ScreentToClient();进行TPoint的转换。
这样才能正确判断鼠标位置所在的控件。
 
function ControlAtPos(const Pos: TPoint; AllowDisabled: Boolean, AllowWinControls:Boolean=False): TControl;

Description

Use ControlAtPos to determine which child control is at the specified location within the control. ControlAtPos returns an immediate child of the control; that is, one of the entries of the Controls property, that has this control for its Parent property.

Specify the position in client coordinates (看这里!) as the value of the Pos parameter. Pos can be anywhere within the boundaries of the child control, not just the upper left corner.
 
如何判断一个坐标的位置在一个控件内呢 比如listbox?
 
接受答案了.
 
后退
顶部