Clipboard: Datei kopieren, später einfügen



  • Ich werd deppert. Folgendes Problem:

    Mein App (Win32) schreibt eine Datei irgendwohin. Dateipfad und -name sind sozusagen bekannt. Jetzt möcht ich folgendes: Ich möcht diese Datei ins Clipboard einhängen, so dass ich später (wenn das App geschlossen ist) via [Ctrl][V] oder anderen Einfüg-Funktionen des Explorers diese Datei irgendwohin kopieren kann. Das App soll also nichts anderes tun als das Äquivalent [Ctrl][C] bei einer Datei im Explorer (oder rechte Maustaste auf Datei -> Kopieren).

    Folgendes weiß ich schon mal:

    HDROP hDragFile;
    
    if(OpenClipboard(NULL)) {
        SetClipboardData(CF_HDROP, hDragFile);
        CloseClipboard();
    }
    

    CF_HDROP ist das Attribut, dass es sich bei dem geclipten Objekt um ein File handelt. HDROP wäre ein Handle auf eine Struktur in der dann eine Liste der Dateinamen enthalten sein sollten. Und da geht mein Problem los. Folgende Methode würde mir etwas im Clipboard Enthaltenes liefern:

    UINT DragQueryFile(
        HDROP hDrop,
        UINT iFile,
        LPTSTR lpszFile,
        UINT cch
    );
    

    Da bin noch nicht ganz durchgestiegen, aber ist auch zweitens. Ich brauch den umgekehrten Weg, sprich: Wie erzeuge ich mir mein HDROP. Mir klingt das alles viel zu sehr nach Drag-Drop-Kram. Hat jemand Rat?



  • Bitte sehr:

    #include <windows.h>
    #include <shlobj.h>
    
    #ifndef HDROP
    typedef HANDLE HDROP; // vs2005 bug
    #endif
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	HWND hwnd = CreateWindow(WC_DIALOG, _T(""), 0, 0, 0, 0, 0,0, 0, 0, 0);
    	OpenClipboard(hwnd);
    	EmptyClipboard();
    
    	char *filepath = "C:\\Dokumente und Einstellungen\\sapero\\Lokale Einstellungen\\Temp\\RegistryLog.txt";
    
    	int size = sizeof(DROPFILES) + (int)strlen(filepath)+2;
    	HDROP hdrop = (HDROP)GlobalAlloc(GMEM_MOVEABLE, size);
    	DROPFILES *df = (DROPFILES*)GlobalLock(hdrop);
    	ZeroMemory(df, size);
    
    	df->pFiles = sizeof(DROPFILES); // string offset
    	//df->pt.x = df->pt.y = 0;
    	//df->pt.fNC = true/false; // nonclient
    	//df->fWide = FALSE; // nicht unicode
    	strcpy((char*)&df[1], filepath); // string1\0string2\0string3\0\0
    	GlobalUnlock(hdrop);
    	SetClipboardData(CF_HDROP, hdrop);
    
    	DWORD preferred = DROPEFFECT_COPY;
    	HGLOBAL hData = GlobalAlloc(GMEM_MOVEABLE, sizeof(DWORD));
    	DWORD *pData = (DWORD*)GlobalLock(hData);
    	*pData = preferred;
    	GlobalUnlock(hData);
    	SetClipboardData(RegisterClipboardFormat(CFSTR_PREFERREDDROPEFFECT), hData);
    
    	size = (int)strlen(filepath)+1;
    	hData = GlobalAlloc(GMEM_MOVEABLE, size);
    	char *psData = (char*)GlobalLock(hData);
    	strcpy(psData, filepath);
    	GlobalUnlock(hData);
    	SetClipboardData(RegisterClipboardFormat(CFSTR_FILENAMEA), hData);
    
    	CloseClipboard();
    	DestroyWindow(hwnd);
    	return 0;
    }
    

Anmelden zum Antworten