C#擴(kuò)展WebBrowser控件,使其支持拖放文件
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
public partial class UserControl1 : WebBrowser { private const int WmDropfiles = 0x233; [DllImport( "shell32.dll" )] private static extern uint DragQueryFile(IntPtr hDrop, uint iFile, StringBuilder lpszFile, uint cch); [DllImport( "shell32.dll" )] private static extern void DragAcceptFiles(IntPtr hWnd, bool fAccept); protected override void OnHandleCreated(EventArgs e) { base .OnHandleCreated(e); //指示接受文件拖放操作 if (!DesignMode) { DragAcceptFiles(Handle, true ); } } protected override void WndProc( ref Message m) { if (m.Msg == WmDropfiles) { #region 解析消息 //獲取拖放的文件數(shù)量 var count = DragQueryFile(m.WParam, 0xffffffff, null , 0); //解析所有播放的文件路徑 var filePaths = new string [count]; for ( uint i = 0; i < count; i++) { var sb = new StringBuilder(); DragQueryFile(m.WParam, i, sb, 1024); filePaths[i] = sb.ToString(); } #endregion //是否繼續(xù)觸發(fā)之后的消息處理 var isCancel = false ; #region 觸發(fā)自定義文件拖放事件 if (DragFile != null ) { var args = new DragFileEventArgs(filePaths); DragFile( this , args); isCancel = args.IsCancel; } #endregion if (isCancel) return ; } base .WndProc( ref m); } public event EventHandler<DragFileEventArgs> DragFile; public class DragFileEventArgs : EventArgs { private readonly string [] _filePaths; public bool IsCancel { get ; set ; } public DragFileEventArgs( string [] filePaths) { _filePaths = filePaths; } public string [] FilePaths { get { return _filePaths; } } } } 該文章在 2021/2/8 17:07:23 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |