Anyone who has used QQ knows that you only need to drag the file to the message box to transfer the file. How is this function implemented? In fact, it is very simple. You only need to respond to a wm_dropfiles message.
In the dialog box-based program, this message is not displayed by default. Press Ctrl + W to bring up the Class Wizard dialog box. Select the class info tag and select window from the message fileter drop-down list, then, click the message maps tag to display the wm_dropfiles message. Add the response function of the message:
Void ctestdlg: ondropfiles (hdrop hdropinfo)
{
// Todo: add your message handler code here and/or call default
Cdialog: ondropfiles (hdropinfo );
}
In addition, to enable the dialog box to accept drag and drop of files, you also need to set the properties of the dialog box. In the dialog box, right-click Properties> extended styles and click the accept files option.
To obtain the complete file name (including path) of the drag-and-drop file, you only need one function:
Uint dragqueryfile (
Hdrop,
Uint ifile,
Lptstr lpszfile,
Uint CCH
);
Parameter description:
Hdrop: hdrop identifier, which is the hdropinfo parameter in the response function.
Ifile: The index number of the file to be queried, starting from 0. You can drag multiple files at the same time, so you need an index number to differentiate them. If this parameter is set to 0 xffffffff, the function returns the number of files to be dragged.
Lpszfile: The first buffer address used to store the file name
CCH: Buffer Length
Return Value: File Name Length
In addition, you need to release the memory allocated by the system after the query is complete. Use the following function:
Void dragfinish (
Hddrop
);
The following is a complete code example. After you drag a file to the dialog box, a message box will pop up showing the complete file name:
Void ctestdlg: ondropfiles (hdrop hdropinfo)
{
// Todo: add your message handler code here and/or call default
Uint count;
Char filepath [200];
Count = dragqueryfile (hdropinfo, 0 xffffffff, null, 0 );
If (count)
{
For (uint I = 0; I <count; I ++)
{
Int pathlen = dragqueryfile (hdropinfo, I, filepath, sizeof (filepath ));
Afxmessagebox (filepath );
}
}
Dragfinish (hdropinfo );
Cdialog: ondropfiles (hdropinfo );
}
Similarly, if a response is returned only when you drag a file to a specific control, you only need to check the accept files style of the control.
========================================================== ==============================================
// Reclogcheckdlg. h: header file
//
# Pragma once
// Creclogcheckdlg dialog box
Class creclogcheckdlg: Public cdialog
{
// Construct
Public:
Creclogcheckdlg (cwnd * pparent = NULL); // standard Constructor
// Dialog box data
Enum {IDD = idd_reclogcheck_dialog };
Protected:
Virtual void dodataexchange (cdataexchange * PDX); // supported by DDX/DDV
// Implementation
Protected:
Hicon m_hicon;
// Message ing function generated
Virtual bool oninitdialog ();
Afx_msg void onsyscommand (uint NID, lparam );
Afx_msg void onpaint ();
Afx_msg hcursor onquerydragicon ();
Declare_message_map ()
Public:
Afx_msg void ondropfiles (hdrop hdropinfo );
};
========================================================== ==============================================
// Reclogcheckdlg. cpp: implementation file
//
# Include "stdafx. H"
# Include "reclogcheck. H"
# Include "reclogcheckdlg. H"
# Include "./reclogcheckdlg. H"
# Ifdef _ debug
# Define new debug_new
# Endif
// Used for the caboutdlg dialog box of the "about" menu item of the application
Class caboutdlg: Public cdialog
{
Public:
Caboutdlg ();
// Dialog box data
Enum {IDD = idd_aboutbox };
Protected:
Virtual void dodataexchange (cdataexchange * PDX); // supported by DDX/DDV
// Implementation
Protected:
Declare_message_map ()
};
Caboutdlg: caboutdlg (): cdialog (caboutdlg: IDD)
{
}
Void caboutdlg: dodataexchange (cdataexchange * PDX)
{
Cdialog: dodataexchange (PDX );
}
Begin_message_map (caboutdlg, cdialog)
End_message_map ()
// Creclogcheckdlg dialog box
Creclogcheckdlg: creclogcheckdlg (cwnd * pparent/* = NULL */)
: Cdialog (creclogcheckdlg: IDD, pparent)
{
M_hicon = afxgetapp ()-> loadicon (idr_mainframe );
}
Void creclogcheckdlg: dodataexchange (cdataexchange * PDX)
{
Cdialog: dodataexchange (PDX );
}
Begin_message_map (creclogcheckdlg, cdialog)
On_wm_syscommand ()
On_wm_paint ()
On_wm_querydragicon ()
//} Afx_msg_map
On_wm_dropfiles ()
End_message_map ()
// Creclogcheckdlg Message Processing Program
Bool creclogcheckdlg: oninitdialog ()
{
Cdialog: oninitdialog ();
// Add the/"about.../" menu to the system menu.
// Idm_aboutbox must be in the range of system commands.
Assert (idm_aboutbox & 0xfff0) = idm_aboutbox );
Assert (idm_aboutbox <0xf000 );
Cmenu * psysmenu = getsystemmenu (false );
If (psysmenu! = NULL)
{
Cstring straboutmenu;
Straboutmenu. loadstring (ids_aboutbox );
If (! Straboutmenu. isempty ())
{
Psysmenu-> appendmenu (mf_separator );
Psysmenu-> appendmenu (mf_string, idm_aboutbox, straboutmenu );
}
}
// Set the icon in this dialog box. When the application main window is not a dialog box, the framework will automatically
// Perform this operation
Seticon (m_hicon, true); // you can specify a large icon.
Seticon (m_hicon, false); // you can specify a small icon.
// Todo: add additional initialization code here
Return true; // return true unless the control focus is set.
}
Void creclogcheckdlg: onsyscommand (uint NID, lparam)
{
If (NID & 0xfff0) = idm_aboutbox)
{
Caboutdlg dlgabout;
Dlgabout. domodal ();
}
Else
{
Cdialog: onsyscommand (NID, lparam );
}
}
// If you add the minimization button to the dialog box, the following code is required:
// To draw the icon. For MFC applications that use document/view models,
// This will be automatically completed by the framework.
Void creclogcheckdlg: onpaint ()
{
If (isiconic ())
{
Cpaintdc DC (this); // device context used for plotting
Sendmessage (wm_iconerasebkgnd, reinterpret_cast <wparam> (DC. getsafehdc (), 0 );
// Center the icon in the work rectangle
Int cxicon = getsystemmetrics (sm_cxicon );
Int cyicon = getsystemmetrics (sm_cyicon );
Crect rect;
Getclientrect (& rect );
Int x = (rect. Width ()-cxicon + 1)/2;
Int y = (rect. Height ()-cyicon + 1)/2;
// Draw the icon
DC. drawicon (X, Y, m_hicon );
}
Else
{
Cdialog: onpaint ();
}
}
// When you drag the minimize window, the system calls this function to display the cursor.
Hcursor creclogcheckdlg: onquerydragicon ()
{
Return static_cast }
Void creclogcheckdlg: ondropfiles (hdrop hdropinfo)
{
// Todo: add the message processing program code and/or call the default value here
Uint count;
Char filepath [200];
Count = dragqueryfile (hdropinfo, 0 xffffffff, null, 0 );
If (count)
{
For (uint I = 0; I <count; I ++)
{
Int pathlen = dragqueryfile (hdropinfo, I, filepath, sizeof (filepath ));
Afxmessagebox (filepath );
}
}
Dragfinish (hdropinfo );
Cdialog: ondropfiles (hdropinfo );
}
========================================================== ==============================================