Click here to Skip to main content
15,922,894 members
Home / Discussions / C#
   

C#

 
GeneralRe: Completely delete a file Pin
Johan Martensson4-Jan-08 21:48
Johan Martensson4-Jan-08 21:48 
GeneralRe: Completely delete a file Pin
PIEBALDconsult5-Jan-08 4:47
mvePIEBALDconsult5-Jan-08 4:47 
GeneralRe: Completely delete a file Pin
Johan Martensson6-Jan-08 11:51
Johan Martensson6-Jan-08 11:51 
GeneralRe: Completely delete a file Pin
PIEBALDconsult6-Jan-08 13:45
mvePIEBALDconsult6-Jan-08 13:45 
GeneralRe: Completely delete a file Pin
Johan Martensson7-Jan-08 1:12
Johan Martensson7-Jan-08 1:12 
GeneralCalling InitializeFromBitmap fails with exception "The method or operation is not implemented" Pin
KeesVer3-Jan-08 2:29
KeesVer3-Jan-08 2:29 
GeneralRe: Calling InitializeFromBitmap fails with exception "The method or operation is not implemented" Pin
KaptinKrunch3-Jan-08 16:29
KaptinKrunch3-Jan-08 16:29 
GeneralRe: Calling InitializeFromBitmap fails with exception "The method or operation is not implemented" Pin
KeesVer3-Jan-08 23:31
KeesVer3-Jan-08 23:31 
Thanks for your reply.

Meanwhile I've come a lot further. The problem was that methods SetData and GetData where not implemented. Therefore I created a new DataObject class which re-implements these methods. This is the code I have so far which is working but needs some adjustments here and there. Also I'm not sure whether all allocated objects are released correctly.

Regards,

Kees

public partial class CustomControl1 : Control
{
DragDropHelper Helper;

public CustomControl1()
{
InitializeComponent();
this.AllowDrop = true;
}

protected override void OnPaint(PaintEventArgs pe)
{
Brush B = new SolidBrush(Color.White);

pe.Graphics.FillRectangle(B, pe.ClipRectangle);
}

protected override void OnMouseDown(MouseEventArgs e)
{
DataFormats.Format ganttbarFormat = DataFormats.GetFormat("Ganttbar");

object ganttbar = new object();

DataObject dragObject = new DataObjectEx(ganttbarFormat.Name, ganttbar);

DoDragDrop(dragObject, DragDropEffects.All);
}

private void UpdateDragImage(System.Runtime.InteropServices.ComTypes.IDataObject dragObject)
{
if (Helper == null)
{
Helper = new DragDropHelper();
}

IDragSourceHelper Drag = (IDragSourceHelper)Helper;

SHDRAGIMAGE tt = new SHDRAGIMAGE();

Bitmap img = new Bitmap(@"C:\Temp\DragImage.bmp");

tt.ptOffset.x = 0;
tt.ptOffset.y = 0;
tt.sizeDragImage.x = img.Width;
tt.sizeDragImage.y = img.Height;
tt.hbmpDragImage = img.GetHbitmap();
tt.crColorKey = 0;

Drag.InitializeFromBitmap(ref tt, dragObject);
}

protected override void OnDragOver(DragEventArgs drgevent)
{
IDropTargetHelper H = (IDropTargetHelper)Helper;

POINT p = new POINT();
p.x = drgevent.X;
p.y = drgevent.Y;

H.DragOver(ref p, DragDropEffects.Copy);

drgevent.Effect = DragDropEffects.Copy;
}

protected override void OnDragDrop(DragEventArgs drgevent)
{
System.Runtime.InteropServices.ComTypes.IDataObject dragObject =
(System.Runtime.InteropServices.ComTypes.IDataObject)drgevent.Data;

IDropTargetHelper H = (IDropTargetHelper)Helper;

POINT p = new POINT();
p.x = drgevent.X;
p.y = drgevent.Y;

H.Drop(dragObject, ref p, DragDropEffects.Copy);

base.OnDragDrop(drgevent);
}

protected override void OnDragEnter(DragEventArgs drgevent)
{
System.Runtime.InteropServices.ComTypes.IDataObject dragObject =
(System.Runtime.InteropServices.ComTypes.IDataObject)drgevent.Data;

UpdateDragImage(dragObject);

IDropTargetHelper H = (IDropTargetHelper)Helper;

POINT p = new POINT();
p.x = drgevent.X;
p.y = drgevent.Y;

H.DragEnter(Handle, dragObject, ref p, DragDropEffects.Copy);

drgevent.Effect = DragDropEffects.Copy;
}

protected override void OnDragLeave(EventArgs e)
{
IDropTargetHelper H = (IDropTargetHelper)Helper;
H.DragLeave();

base.OnDragLeave(e);
}

protected override void OnGiveFeedback(GiveFeedbackEventArgs gfbevent)
{
// base.OnGiveFeedback(gfbevent);
}

protected override void OnQueryContinueDrag(QueryContinueDragEventArgs qcdevent)
{
if (qcdevent.EscapePressed == true)
{
qcdevent.Action = DragAction.Cancel;
}
// base.OnQueryContinueDrag(qcdevent);
}

[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int x;
public int y;
}

[StructLayout(LayoutKind.Sequential)]
public struct SHDRAGIMAGE
{
public POINT sizeDragImage;
public POINT ptOffset;
public IntPtr hbmpDragImage;
public uint crColorKey;
}

[ComImport, Guid("DE5BF786-477A-11d2-839D-00C04FD918D0"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IDragSourceHelper
{
void InitializeFromBitmap(
ref SHDRAGIMAGE pshdi,
[MarshalAs(UnmanagedType.Interface)]
System.Runtime.InteropServices.ComTypes.IDataObject pDataObject);
void InitializeFromWindow(
IntPtr hwnd,
ref POINT point,
[MarshalAs(UnmanagedType.Interface)]
System.Runtime.InteropServices.ComTypes.IDataObject pDataObject);
}

[ComImport, Guid("4657278B-411B-11d2-839A-00C04FD918D0"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IDropTargetHelper
{
void DragEnter(
IntPtr hwnd,
[MarshalAs(UnmanagedType.Interface)]
System.Runtime.InteropServices.ComTypes.IDataObject pDataObject,
ref POINT point,
DragDropEffects dwEffect);
void DragLeave();
void DragOver(ref POINT point, DragDropEffects dwEffect);
void Drop(
[MarshalAs(UnmanagedType.Interface)]
System.Runtime.InteropServices.ComTypes.IDataObject pDataObject,
ref POINT point,
DragDropEffects dwEffect);
void Show([MarshalAs(UnmanagedType.Bool)] bool fShow);
}

[ComImport, Guid("4657278A-411B-11d2-839A-00C04FD918D0")]
class DragDropHelper
{
}

public class FormatetDataObject
{
public FORMATETC format;
public STGMEDIUM medium;
}

public class DataObjectEx : DataObject, System.Runtime.InteropServices.ComTypes.IDataObject
{
public DataObjectEx(string format, object data)
: base(format, data)
{

}

public void SetData(ref FORMATETC formatIn, ref STGMEDIUM medium, bool release)
{
string s = formatIn.tymed.ToString() + formatIn.cfFormat.ToString();
FormatetDataObject dataObject = new FormatetDataObject();
dataObject.format = formatIn;
dataObject.medium = medium;
base.SetData(s, dataObject);
}

public void GetData(ref FORMATETC format, out STGMEDIUM medium)
{
string key = format.tymed.ToString() + format.dwAspect.ToString() + format.cfFormat.ToString();
FormatetDataObject dataObject;
dataObject = (FormatetDataObject)base.GetData(key);
if (dataObject != null)
{
System.Diagnostics.Debug.Assert(format.cfFormat == dataObject.format.cfFormat &&
format.dwAspect == dataObject.format.dwAspect &&
format.tymed == dataObject.format.tymed);
medium = dataObject.medium;
}
else
//
// return empty medium
//
{
STGMEDIUM m;
m.unionmember = IntPtr.Zero;
m.tymed = TYMED.TYMED_NULL;
m.pUnkForRelease = null;
medium = m;
}
}

public int QueryGetData(ref FORMATETC format)
{
string key = format.tymed.ToString() + format.dwAspect.ToString() + format.cfFormat.ToString();
if (base.GetDataPresent(key))
{
return 0; //S_OK
}
else
{
return 1; // S_FALSE
}
}
}


}
Questionopen an associated filetype Pin
peatle3-Jan-08 1:00
peatle3-Jan-08 1:00 
GeneralRe: open an associated filetype Pin
Phil J Pearson3-Jan-08 1:32
Phil J Pearson3-Jan-08 1:32 
GeneralRe: open an associated filetype Pin
Giorgi Dalakishvili3-Jan-08 1:39
mentorGiorgi Dalakishvili3-Jan-08 1:39 
GeneralWriting Objects into a Databases Pin
00carl003-Jan-08 0:50
00carl003-Jan-08 0:50 
GeneralRe: Writing Objects into a Databases Pin
darkelv3-Jan-08 0:57
darkelv3-Jan-08 0:57 
GeneralRe: Writing Objects into a Databases Pin
Giorgi Dalakishvili3-Jan-08 1:39
mentorGiorgi Dalakishvili3-Jan-08 1:39 
GeneralRe: Writing Objects into a Databases Pin
Russell Jones3-Jan-08 5:51
Russell Jones3-Jan-08 5:51 
GeneralRe: Writing Objects into a Databases Pin
Giorgi Dalakishvili3-Jan-08 7:59
mentorGiorgi Dalakishvili3-Jan-08 7:59 
GeneralRe: Writing Objects into a Databases Pin
Russell Jones3-Jan-08 5:55
Russell Jones3-Jan-08 5:55 
QuestionWhich one is good practice? Pin
Hum Dum3-Jan-08 0:00
Hum Dum3-Jan-08 0:00 
AnswerRe: Which one is good practice? Pin
Colin Angus Mackay3-Jan-08 0:49
Colin Angus Mackay3-Jan-08 0:49 
AnswerRe: Which one is good practice? Pin
Alaric_3-Jan-08 4:16
professionalAlaric_3-Jan-08 4:16 
GeneralIN operator Pin
mpavas2-Jan-08 23:32
mpavas2-Jan-08 23:32 
GeneralRe: IN operator Pin
sujithkumarsl2-Jan-08 23:39
sujithkumarsl2-Jan-08 23:39 
GeneralRe: IN operator Pin
mpavas2-Jan-08 23:41
mpavas2-Jan-08 23:41 
GeneralRe: IN operator Pin
Alaric_3-Jan-08 4:30
professionalAlaric_3-Jan-08 4:30 
GeneralRe: IN operator Pin
Colin Angus Mackay3-Jan-08 5:10
Colin Angus Mackay3-Jan-08 5:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.