Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C++ Problem

Options
  • 06-03-2004 8:53pm
    #1
    Moderators, Computer Games Moderators Posts: 4,560 Mod ✭✭✭✭


    Firstly it is for a project but its help I'd be given anyway if I could wait till monday to ask. But I'm horribly stuck and need to finish this up quickly.

    I'm just trying to enable drag'n'drop in a win32 application for an TImage.

    The problem comes because I'm not declaring the image before I excute the program. I'm loading it after its started.

    Anyone any idea how to set the dragmode to automatic in code?

    It is rather puzzling & since its such an obscure problem neither google nor books are of any help :(


Comments

  • Registered Users Posts: 2,145 ✭✭✭dazberry


    Originally posted by Ivan
    It is rather puzzling & since its such an obscure problem neither google nor books are of any help :(

    If its Borland C++ Builder/VCL I can spirit up some code (In Delphi thou' so you'll have to translate it), but give me a better idea what you're trying to do, its not exactly clear to me.

    D.


  • Closed Accounts Posts: 437 ✭✭casper-


    I've got Win32 code (read non-MFC) that'll do all of that. I know it's in C but maybe you could transfer the calls over to Pascal (?) Like daz said..give us some more info :)


  • Moderators, Computer Games Moderators Posts: 4,560 Mod ✭✭✭✭Ivan


    Well its like this:

    I've created a program that begins by asking you to choose an image.

    You open a dialog box, select the image path. Then you press another button that enables the click "event" so when you click on an area it places a copy of the image onto that area.

    Now what I'm trying to do is set the initial property of the imagine I place on the canvas so that its drag and drop'able.

    void __fastcall TfrmMain::btnSelectImageClick(TObject *Sender)
    {
    if (dlgImagePath->Execute())
    {
    txtImagePath->Text = (dlgImagePath->FileName);
    }
    }
    //

    void __fastcall TfrmMain::btnPlaceObjectClick(TObject *Sender)
    {
    Drawing = True;
    }
    //

    void __fastcall TfrmMain::imgMainInterfaceMouseDown(TObject *Sender,
    TMouseButton Button, TShiftState Shift, int X, int Y)
    {
    if (Drawing == True)
    {
    Drawing = false;
    TImage *testimage = new TImage(this);
    testimage->Parent=pnlMainInterface;
    testimage->Picture->LoadFromFile(dlgImagePath->FileName);
    testimage->Left=X;
    testimage->Top=Y;
    testimage->Dragmode=dmAutomatic;
    txtObjectXCoord->Text = X;
    txtObjectYCoord->Text = Y;
    }
    }
    //

    My code is awfully badly written & needs alot of optimization, but feel free to point that out to me if you want :P

    Also I'm trying to enable the click event as well, but I'd image that will be the same solution.

    Thanks for the help guys.


  • Closed Accounts Posts: 437 ✭✭casper-


    Ahh...yes...I take it that must be C++ Build stuff. Completely different to the world of Win32 and/or MFC by the looks of it ;) Hopefully dazberry can shine some light on your issues...

    Looks like this one line
    testimage->Dragmode=dmAutomatic;
    

    might replace about 50 or so lines of Win32 stuff ;)


  • Moderators, Computer Games Moderators Posts: 4,560 Mod ✭✭✭✭Ivan


    yah, except... err... well I meant to mention that
    testimage->Dragmode=dmAutomatic;

    thats where the problem is.

    It doesnt work :(


  • Advertisement
  • Registered Users Posts: 2,145 ✭✭✭dazberry


    I'll post a solution this evening (No boards in work and I gotta go - soon :(), but in the meantime look at the:

    OnDragDrop
    OnDragOver
    OnEndDrag
    OnStartDrag

    events of both the source and target components you wish to use the drag/drop between.

    D.


  • Moderators, Computer Games Moderators Posts: 4,560 Mod ✭✭✭✭Ivan


    From what I can make out from the help file, these just specify what happens, after you've gotten drag and drop enabled.

    I'm trying to GET drag and drop enabled.

    Guess you were just in a rush or is there some super-secret guru information I'm missing?


  • Registered Users Posts: 2,145 ✭✭✭dazberry


    Originally posted by Ivan

    Guess you were just in a rush or is there some super-secret guru information I'm missing?

    :D - nah I was in a rush.

    Ok, basically you've the concept of automatic and manual incorrect me/thinks.

    Manual means you have to decide when to start the drag operation - and call BeginDrag(..), Automatic means that the VCL has detected what it thinks should be a drag operation. Either way you still need to use the OnDrag___ methods in the source and target components to actually support the drag operation. The way you're doing it on the OnMouseDown event its like you should actually be using the manual mode.

    A very basic example (In Delphi - sorry I'm too lazy to convert it this time - its been a long day):
    // Notes: This is based on one form, and one child TImage.
    // with DragMode set to Manual on both. Image is loaded
    // at design time to save a bit of effort.
    
    Type
        TmyDragObject = class(TDragObject)
         aSourceImage : TImage;
        end;
    
    // Mouse down, so start the drag. 
    procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
     TImage(Sender).BeginDrag(True);
    end;
    
    // BeginDrag(..) will call this event handler.
    // Here you must setup the drag operation
    procedure TForm1.Image1StartDrag(Sender: TObject;
      var DragObject: TDragObject);
    var
       MyDragObject : TMyDragObject;
    begin
     MyDragObject:=TMyDragObject.Create;
     MyDragObject.aSourceImage:=TImage(Sender);
     DragObject:=MyDragObject;
    end;
    
    // Important: Since the form is the target of the drag
    // operation, the FormDragOver event must be registered
    // so to accept the drag object (if appropriate).
    procedure TForm1.FormDragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    begin
     Accept:=True;
    end;
    
    // Ok, now the drop has taken place, so lets do
    // something with it.
    procedure TForm1.FormDragDrop(Sender, Source: TObject; X, Y: Integer);
    var
       aImage : TImage;
       MyDragObject : TMyDragObject;
    begin
     aImage:=TImage.Create(Self);
     MyDragObject:=TMyDragObject(Source);
     aImage.Picture.Bitmap.Assign(MyDragObject.aSourceImage.Picture.Bitmap);
     aImage.Parent:=TWinControl(Sender);
     aImage.Top:=Y;
     aImage.Left:=X;
    end;
    
    

    HTH. Post back if you need more help.

    D.


  • Moderators, Computer Games Moderators Posts: 4,560 Mod ✭✭✭✭Ivan


    Well, firstly thanks for the great response, you've no idea how much I appreciate it.

    Secondly, I've done pascal but have never touched delphi. I dont know how different delphi is to C++ but without a frame of reference I really cant convert that myself.

    What I meant by
    From what I can make out from the help file, these just specify what happens, after you've gotten drag and drop enabled.

    I'm trying to GET drag and drop enabled.

    Guess you were just in a rush or is there some super-secret guru information I'm missing?

    is that
    OnDragDrop
    OnDragOver
    OnEndDrag
    OnStartDrag

    all seem to be used after you've made an object dragable?

    I cant really make out precisely what it is that your code does, but I think I get the general idea. However, if, whenever you get a chance mind, you could convert it to C++, I would be greatly appreciative.

    Also I can enable automatic drag and drop on a pre-existing image (I havent really messed around with manual, but from what I understand thats probably what I'm gonna have to use) but can do neither automatic nor manual for an image that I bring into the program after its compiled and executed.

    This, fundamentally, is my problem :(

    Thanks again!


  • Registered Users Posts: 2,145 ✭✭✭dazberry


    Hi Ivan,

    Download this http://www.istare.com/ivan.zip, its the full project source (with a compiled) .exe so you can see what it does. Drag the image in the top left and you can drop (and create) copies on the main form.

    Secondly remember that automatic vs manual is purely related to how the drag operation is started. In the OnMouseDown event, I'm starting a drag operation manually by calling BeginDrag(). If it was automatic I wouldn't have to do this, the VCL would do it for me, but either way, I would STILL need to implement all the events to tell the VCL how to actually start and complete the drag operation. There's no getting away from this.

    The thing that might be a bit confusing in the TMyDragObject class. TDragObject is an abstract class (as such), so for it to be useful I needed to inherit it, because its that class that contains some information about what is actually being dragged/dropped.

    Thirdly, it doesn't matter if the image is preloaded or not. I did this for convenience, it would work either way, thou' you'd need to check if the image was loaded, if it wasn't you would want to refuse the operation (i.e. Image.Empty = True then Refuse).

    I'm gonna play a board's excuse card on doing the translation. Its been about 8 years since I've done any C++, and although I could do the C stuff, I really can't remember all the C++ class stuff at this point, and its nearly 10.30 and I need to get dinner :(.

    But take a look the example. I've a funny feeling that's not what you really want to do, I could be wrong. Let me know, and if it is I'll try a bit of a translation, otherwise we'll see if we can find a way of doing what you want.

    D.


  • Advertisement
Advertisement