C#中调用WINDOWS API(RegisterDragDrop)失败,不知是何原因?
[DllImport("ole32.dll")]
static extern int RegisterDragDrop(IntPtr hwnd, IDropTarget pDropTarget); public Form1()
{
InitializeComponent();
hwnd = FindWindowEx(GetDesktopWindow(), IntPtr.Zero, "Shell_TrayWnd", "");
hwnd = FindWindowEx(hwnd, IntPtr.Zero, "TrayNotifyWnd", "");
hwnd = FindWindowEx(hwnd, IntPtr.Zero, "SysPager", "");
//hwnd = FindWindowEx(hwnd, 0, "ToolbarWindow32", "");
dt = new DropTarget();
int result = RegisterDragDrop(hwnd, dt);
if (result != 0)
{
String info = String.Format("RegisterDragDrop failed {0:X}", hwnd);
MessageBox.Show(info);
}
}谁有调用成功的代码,100分全归他了
[DllImport("ole32.dll")]
static extern int RegisterDragDrop(IntPtr hwnd, IDropTarget pDropTarget); public Form1()
{
InitializeComponent();
hwnd = FindWindowEx(GetDesktopWindow(), IntPtr.Zero, "Shell_TrayWnd", "");
hwnd = FindWindowEx(hwnd, IntPtr.Zero, "TrayNotifyWnd", "");
hwnd = FindWindowEx(hwnd, IntPtr.Zero, "SysPager", "");
//hwnd = FindWindowEx(hwnd, 0, "ToolbarWindow32", "");
dt = new DropTarget();
int result = RegisterDragDrop(hwnd, dt);
if (result != 0)
{
String info = String.Format("RegisterDragDrop failed {0:X}", hwnd);
MessageBox.Show(info);
}
}谁有调用成功的代码,100分全归他了
#pragma hdrstop#include "Dropmain.h"//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#define WM_MYMESSAGE (0x0400+100)
#define WM_CLOSEMESSAGE (0x0400+101)
#define WM_COPYDATA2 (0x004A)
TfrmDropmain *frmDropmain;class CDropTarget : public IDropTarget
{
public:
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void**ppvObject)
{
if (riid == IID_IUnknown || riid == IID_IDropTarget)
{
*ppvObject = this;
return S_OK;
} else {
*ppvObject = 0;
return E_NOINTERFACE;
}
} ULONG STDMETHODCALLTYPE AddRef( void)
{
return S_OK;
} ULONG STDMETHODCALLTYPE Release( void)
{
return S_OK;
} HRESULT STDMETHODCALLTYPE DragEnter(IDataObject *pDataObj, DWORD grfKeyState, POINTL pt, DWORD *pdwEffect)
{
//Form1->Memo1->Lines->Add("DragEnter");
return S_OK;
} HRESULT STDMETHODCALLTYPE DragOver(DWORD grfKeyState, POINTL pt, DWORD *pdwEffect)
{
//Form1->Memo1->Lines->Add("DragOver");
return S_OK;
} HRESULT STDMETHODCALLTYPE DragLeave( void)
{
//Form1->Memo1->Lines->Add("DragLeave");
return S_OK;
} HRESULT STDMETHODCALLTYPE Drop(IDataObject *pDataObj, DWORD grfKeyState, POINTL pt, DWORD *pdwEffect)
{
IEnumFORMATETC* pEnum = 0;
HRESULT hr = pDataObj->EnumFormatEtc(DATADIR_GET, &pEnum);
if (hr != S_OK)
return hr; FORMATETC formatEtc={CF_TEXT,0,DVASPECT_CONTENT,-1,DWORD(-1)};
if (pDataObj->QueryGetData(&formatEtc)== S_OK)
{ STGMEDIUM Stg; BOOL bValue=(pDataObj->GetData(&formatEtc,&Stg)==S_OK);
if(bValue)
{
AnsiString asUrl = (char*)GlobalLock(Stg.hGlobal); HWND hw = FindWindow(NULL,"Gadget Server");
COPYDATASTRUCT cdsUrl;
cdsUrl.lpData = asUrl.c_str();
cdsUrl.cbData = asUrl.Length() + 1;
//ShowMessage(AnsiString(asUrl.c_str()));
SendMessage(hw,WM_COPYDATA2,WPARAM(0),LPARAM(&cdsUrl));
frmDropmain->Memo1->Lines->Add(asUrl);
GlobalUnlock(Stg.hGlobal);
GlobalFree(Stg.hGlobal);
}
} return S_OK;
}
};CDropTarget aDropTarget;//---------------------------------------------------------------------------
__fastcall TfrmDropmain::TfrmDropmain(TComponent* Owner)
: TForm(Owner)
{
HWND hw = FindWindowEx(GetDesktopWindow(), NULL, "Shell_TrayWnd", 0);
hw = FindWindowEx(hw, NULL, "TrayNotifyWnd", 0);
hw = FindWindowEx(hw, NULL, "SysPager", 0);
//hw = FindWindowEx(hw, NULL, "ToolbarWindow32", 0); HRESULT hr = RegisterDragDrop(hw, &aDropTarget);
if (hr != S_OK)
{
AnsiString Info;
Info.printf("RegisterDragDrop failed %x", hr);
ShowMessage(Info);
}
}
//---------------------------------------------------------------------------
__fastcall TfrmDropmain::~TfrmDropmain()
{ HWND hw = FindWindowEx(GetDesktopWindow(), NULL, "Shell_TrayWnd", 0);
hw = FindWindowEx(hw, NULL, "TrayNotifyWnd", 0);
hw = FindWindowEx(hw, NULL, "SysPager", 0);
//hw = FindWindowEx(hw, NULL, "ToolbarWindow32", 0); RevokeDragDrop(hw);
}
void __fastcall TfrmDropmain::TrayIcon1Click(TObject *Sender)
{
/*
Application->ShowMainForm = true;
ShowWindow(Application->Handle, SW_SHOW);
this->Show(); Application->ShowMainForm = false;
ShowWindow(Application->Handle, SW_HIDE);
Application->MainForm->Visible = false;
Application->Minimize();
HWND hw = FindWindow("TForm1","Form1");
ShowWindow(hw,SW_SHOW); */
//HWND hw = FindWindowEx(GetDesktopWindow(), NULL, "Shell_TrayWnd", 0);
//FrmMain
//SendMessage
}
void __fastcall TfrmDropmain::WndProc(TMessage &Msg)
{ if(Msg.Msg == 0x0400+101)
{ Close();
}
TForm::WndProc(Msg);
}
//---------------------------------------------------------------------------
C++Builder的代码
我先看看效果是什么