Method, device and related medium for implementing a file and directory mixed selection dialog box
By initializing COM in the Windows system and using the FileDialogEvents object to handle the IFileOpenDialog event, the error issue of the IFileOpenDialog interface when the number of files selected is less than 2 is resolved, thus achieving accuracy and simplicity in mixed selection of files and directories.
Patent Information
- Application Number
- CN202211586558.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-09
- Publication Date
- 2026-11-20
- Estimated Expiration
- 2042-12-09
AI Technical Summary
In the existing technology, the GetSelectedItem function of the IFileOpenDialog interface in Windows system returns an error when the number of files selected is less than 2, resulting in inaccurate results for mixed directory and file selection.
The COM is initialized through the CoInitialize function, an IFileOpenDialog object is created, and the OnSelectionChange interface of the FileDialogEvents object is used to calculate the results of the user's selected file and directory, which are saved to the IShellItemArray array collection. The IShellItem interface is used to obtain the string list, which is stored in the global hash table. After the selection completion event is obtained through the OnFileOk interface, it is converted into a file list and the COM is released.
It avoids errors when there are fewer than two files, ensures the accuracy and simplicity of file and directory selection results, and improves the reliability of the mixed selection dialog box.
Smart Images

Figure CN115729902B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of Windows system technology, and in particular to a method, apparatus, and related media for implementing a dialog box for mixed selection of files and directories. Background Technology
[0002] Currently, the file selection dialog box in Windows cannot select files and directories simultaneously, making it impossible to achieve mixed selection of files and directories. Most existing solutions use API interfaces, such as the GetOpenFileName interface with hook functions or the SHBrowseForFolder interface, to achieve mixed selection of files and directories in the dialog box. However, using these methods results in a less concise dialog box display.
[0003] In addition, there are many existing methods for implementing mixed file and directory selection using the IFileOpenDialog interface component, but they all have a common problem: the IFileOpenDialog interface component in Windows has a bug where the GetSelectedItem function returns an incorrect value when the number of files selected is less than two. For example, if one file and five directories are selected simultaneously in one operation, the GetSelectedItem function will return an error and the value will be 1 because the number of files is less than two. Summary of the Invention
[0004] This invention provides a method, apparatus, and related medium for implementing a dialog box for mixed selection of files and directories, aiming to solve the problem in the prior art where errors occur when the number of files is less than two when mixed selection of directories and files.
[0005] In a first aspect, embodiments of the present invention provide a method for implementing a dialog box for mixed selection of files and directories, including:
[0006] The CoInitialize function initializes COM, and the CoCreateInstance function creates an IFileOpenDialog object.
[0007] Construct a FileDialogEvents object, and set the FileDialogEvents object as the event handler of the IFileOpenDialog object through the Advise function of the IFileOpenDialog object;
[0008] The OnSelectionChange interface in the FileDialogEvents object is used to calculate and obtain the results of the file and directory selected by the user, and the results are saved to the IShellItemArray array collection;
[0009] Iterate through the IShellItemArray array collection, obtain the string list of files and directories selected by the user through the GetDisplayName function of the IShellItem interface of the IShellItemArray array collection, and store it in the hash table of the global file;
[0010] When the OnFileOk interface of the FileDialogEvents object receives the event indicating that the user has completed the selection, it converts the hash table into a file list, clears the variable memory, and calls the CoUninitialize function to release the COM.
[0011] Secondly, embodiments of the present invention provide an apparatus for implementing a dialog box for mixed selection of files and directories, comprising:
[0012] The first creation unit is used to initialize COM using the CoInitialize function and create an IFileOpenDialog object using the CoCreateInstance function.
[0013] The second creation unit is used to construct a FileDialogEvents object and set the FileDialogEvents object as the event handler of the IFileOpenDialog object through the Advise function of the IFileOpenDialog object;
[0014] The first processing unit is used to calculate and obtain the results of the file and directory selected by the user through the OnSelectionChange interface in the FileDialogEvents object, and save the obtained results to the IShellItemArray array collection;
[0015] The second processing unit is used to traverse the IShellItemArray array collection, obtain the string list of files and directories selected by the user through the GetDisplayName function of the IShellItem interface of the IShellItemArray array collection, and store it in the hash table of the global file;
[0016] The third processing unit is used to convert the hash table into a file list, clear the variable memory, and call the CoUninitialize function to release the COM after the OnFileOk interface of the FileDialogEvents object obtains the event indicating that the user has completed the selection.
[0017] Thirdly, embodiments of the present invention provide a computer device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the method of implementing a mixed selection dialog box for files and directories in the first aspect.
[0018] Fourthly, embodiments of the present invention provide a computer-readable storage medium, wherein a computer program is stored on the computer-readable storage medium, and when the computer program is executed by a processor, it implements the method of implementing a mixed selection dialog box for files and directories in the first aspect.
[0019] This invention provides a method for implementing a dialog box for mixed selection of files and directories. The method involves initializing the COM component using the `CoInitialize` function and creating an `IFileOpenDialog` object; constructing a `FileDialogEvents` object and setting it as the event handler for the `IFileOpenDialog` object; calculating and obtaining the results of the user's selected files and directories through the `OnSelectionChange` interface and saving the results to an `IShellItemArray` array; traversing the `IShellItemArray` array and obtaining a list of strings representing the user's selected files and directories using the `GetDisplayName` function of the `IShellItem` interface, and storing this list in a global file hash table; when the `OnFileOk` interface of the `FileDialogEvents` object receives an event indicating that the user's selection is complete, converting the hash table into a file list, clearing memory variables, and calling the `CoUninitialize` function to release the COM component. This invention calculates and obtains the results of the user's selected files and directories through the `OnSelectionChange` interface, thus avoiding errors that occur when the number of files selected is less than two.
[0020] This invention also provides an apparatus, computer device, and storage medium for implementing a dialog box for mixed selection of files and directories, which also have the aforementioned beneficial effects. Attached Figure Description
[0021] To more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings used in the following description of the embodiments will be briefly introduced. Obviously, the drawings described below are some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0022] Figure 1 A flowchart illustrating a method for implementing a mixed selection dialog box for files and directories, provided by an embodiment of the present invention;
[0023] Figure 2 This is a schematic block diagram of an apparatus for implementing a mixed selection dialog box for files and directories, provided as an embodiment of the present invention. Detailed Implementation
[0024] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some, not all, of the embodiments of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0025] It should be understood that, when used in this specification and the appended claims, the terms "comprising" and "including" indicate the presence of the described features, integrals, steps, operations, elements and / or components, but do not exclude the presence or addition of one or more other features, integrals, steps, operations, elements, components and / or collections thereof.
[0026] It should also be understood that the terminology used in this specification is for the purpose of describing particular embodiments only and is not intended to limit the invention. As used in this specification and the appended claims, the singular forms “a,” “an,” and “the” are intended to include the plural forms unless the context clearly indicates otherwise.
[0027] It should also be further understood that the term "and / or" as used in this specification and the appended claims refers to any combination of one or more of the associated listed items and all possible combinations, and includes such combinations.
[0028] Please see below. Figure 1 , Figure 1 The flowchart of a method for implementing a mixed selection dialog box for files and directories provided in an embodiment of the present invention specifically includes steps S101 to S105.
[0029] S101. Initialize COM using the CoInitialize function and create an IFileOpenDialog object using the CoCreateInstance function;
[0030] S102. Construct a FileDialogEvents object, and set the FileDialogEvents object as the event handler of the IFileOpenDialog object through the Advise function of the IFileOpenDialog object;
[0031] S103. Calculate and obtain the results of the user's selected file and directory through the OnSelectionChange interface in the FileDialogEvents object, and save the obtained results to the IShellItemArray array collection;
[0032] S104. Traverse the IShellItemArray array collection, obtain the string list of files and directories selected by the user through the GetDisplayName function of the IShellItem interface of the IShellItemArray array collection, and store it in the hash table of the global file;
[0033] S105. When the OnFileOk interface of the FileDialogEvents object receives the event indicating that the user has completed the selection, the hash table is converted into a file list, the variable memory is cleared, and the CoUninitialize function is called to release the COM.
[0034] In step S101, the CoInitialize function is a Windows API function that guides Windows to create COM in a single-threaded manner. Except for CoGetMalloc (which retrieves a pointer to the default OLE task memory allocator) and memory allocation functions, the application must initialize COM before calling it. The IFileOpenDialog object is created using the CoCreateInstance function. Specifically, the CoCreateInstance function creates a COM object (an uninitialized object) with a specified class identifier. The CoCreateInstance function can be called when only one object needs to be created on the local machine.
[0035] In one embodiment, step S101 includes:
[0036] Store the pointer to the IFileOpenDialog object in a static variable.
[0037] In this embodiment, the pointer to the IFileOpenDialog object is stored in a static variable for later use; the pointer points to the address of the IFileOpenDialog object, and subsequent operations on the IFileOpenDialog object are performed through this pointer.
[0038] In step S102, a FileDialogEvents object is first constructed, which implements the entity object of the IFileDialogEvents object. Then, the Advise function of the IFileOpenDialog object is used to set the FileDialogEvents object as the event handler of the IFileOpenDialog object (an event procedure that determines how the control responds to events). After the event handler of the IFileOpenDialog object is set, all dialog box operation events on the Windows system will be handled by the FileDialogEvents object; for example, events such as the user selecting a file or directory and clicking a screen button can be processed by the FileDialogEvents object. The event handler of the IFileOpenDialog object constructs an event entry point for counting the currently selected file or directory by the user.
[0039] In step S103, the OnSelectionChange interface is used to receive a corresponding change event when the user's selection operation changes. Specifically, in the OnSelectionChange interface, the QueryInterface interface is used to query the service interface, and then the IFolderView2 interface is obtained through the service interface. Finally, the GetSelection function of the IFolderView2 interface is used to obtain the result of the currently selected file and directory, and the obtained result is saved to the IShellItemArray array collection.
[0040] In one embodiment, step S103 includes:
[0041] Create an IServiceProvider object pointer, pass the IID_IServiceProvider parameter according to the IFileDialog interface object passed in by the event, call the QueryInterface function, and obtain and save the IServiceProvider object pointer.
[0042] Furthermore, an IShellBrowser object pointer is created, and the QueryService method is called with the IID_IShellBrowser parameter based on the IServiceProvider object pointer to obtain and save the IShellBrowser object pointer.
[0043] Furthermore, an IShellView object pointer is created, and the QueryActiveShellView method is called based on the IShellBrowser object pointer to obtain and save the IShellView object pointer.
[0044] Furthermore, an IFolderView2 object pointer is created, and the QueryInterface method is called with the IID_IFolderView parameter based on the IShellView object pointer to obtain and save the IFolderView2 object pointer.
[0045] Furthermore, an IShellItemArray object pointer is created, and the GetSelection method of the IFolderView2 object pointer is called to obtain the results of the files and directories selected by the user, and the obtained results are saved into the IShellItemArray array collection.
[0046] In this embodiment, the IServiceProvider object pointer is first created, which is used to store variables of the dialog service object; then, the IID_IServiceProvider parameter is passed to the IFileDialog interface object according to the event (current user operation event) to call the QueryInterface function, and finally the IServiceProvider object pointer is obtained and saved for use in the next step.
[0047] Furthermore, an IShellBrowser object pointer is created, which is used to store variables of the dialog box interface frame object; based on the IServiceProvider object pointer, the QueryService method is called through the IID_IShellBrowser parameter to obtain the IShellBrowser object pointer and save it for use in the next step.
[0048] Furthermore, an IShellView object pointer is created, which is used to store variables of the view object in the dialog box. The QueryActiveShellView method is called based on the IShellBrowser object pointer to obtain and save the IShellView object pointer for use in the next step. It should be noted that the QueryActiveShellView method is used to retrieve the currently active IShellView object.
[0049] Furthermore, an IFolderView2 object pointer is created, which is used to store variables of the dialog file selection box object; based on the IShellView object pointer, the QueryInterface method is called through the IID_IFolderView parameter to obtain the IFolderView2 object pointer and save it for use in the next step.
[0050] Furthermore, an IShellItemArray object pointer is created, which is used to store variables representing the selected set of files and directories. The GetSelection method of the IFolderView2 object pointer is called to obtain the results of the files and directories selected by the user, and the obtained results are saved into the IShellItemArray array collection. It should be noted that the GetSelection method can return a Selection object, representing the range of text selected by the user or the current position of the cursor.
[0051] In step S104, using a hash table to store the corresponding file list is to facilitate the user's subsequent deselection operation of files and directories, so as to quickly compare whether the files and directories have been selected before, and if they have been selected, to perform a deselection operation.
[0052] In step S105, when the OnFileOk interface of the FileDialogEvents object (the OnFileOk interface is the event handling entry point for the user clicking the OK button after selection) receives the event indicating that the user has completed the selection, it converts the hash table into a file list and returns it to the user. Then, it clears the memory of all variables generated in the previous steps and calls the CoUninitialize function to release the COM library. Specifically, the CoUninitialize function closes the COM library of the current thread, unloads all DLLs loaded by the thread, releases any other resources, and closes all RPC (Remote Procedure Call) connections maintained on the thread.
[0053] Combination Figure 2 As shown, Figure 2 This is a schematic block diagram of an apparatus for implementing a mixed file and directory selection dialog box, provided by an embodiment of the present invention. The apparatus 200 for implementing the mixed file and directory selection dialog box includes:
[0054] The first creation unit 201 is used to initialize COM using the CoInitialize function and create an IFileOpenDialog object using the CoCreateInstance function;
[0055] The second creation unit 202 is used to construct a FileDialogEvents object and set the FileDialogEvents object as the event handler of the IFileOpenDialog object through the Advise function of the IFileOpenDialog object.
[0056] The first processing unit 203 is used to calculate and obtain the results of the file and directory selected by the user through the OnSelectionChange interface in the FileDialogEvents object, and save the obtained results to the IShellItemArray array collection;
[0057] The second processing unit 204 is used to traverse the IShellItemArray array collection, obtain the string list of files and directories selected by the user through the GetDisplayName function of the IShellItem interface of the IShellItemArray array collection, and store it in the hash table of the global file;
[0058] The third processing unit 205 is used to convert the hash table into a file list, clear the variable memory, and call the CoUninitialize function to release the COM after the OnFileOk interface of the FileDialogEvents object obtains the event indicating that the user has completed the selection.
[0059] In this embodiment, the first creation unit 201 first initializes the COM using the CoInitialize function and creates an IFileOpenDialog object using the CoCreateInstance function; then, the second creation unit 202 constructs a FileDialogEvents object and sets the FileDialogEvents object as the event handler of the IFileOpenDialog object using the Advise function of the IFileOpenDialog object; then, the first processing unit 203 calculates and obtains the results of the user's selected files and directories through the OnSelectionChange interface of the FileDialogEvents object, and saves the obtained results to the IShellItemArray array collection; then, the second processing unit 204 traverses the IShellItemArray array collection, obtains the string list of the user's selected files and directories through the GetDisplayName function of the IShellItem interface of the IShellItemArray array collection, and saves it in the hash table of the global file; finally, the third processing unit 205 is used to convert the hash table into a file list, clear the variable memory, and call the CoUninitialize function to release the COM when the OnFileOk interface of the FileDialogEvents object obtains the event of user selection completion.
[0060] In one embodiment, the first processing unit 203 includes:
[0061] The first calculation unit is used to create an IServiceProvider object pointer, pass the IID_IServiceProvider parameter according to the IFileDialog interface object passed in by the event, call the QueryInterface function, and obtain and save the IServiceProvider object pointer.
[0062] Furthermore, the first computing unit includes:
[0063] The second calculation unit is used to create an IShellBrowser object pointer, and to call the QueryService method with the IID_IShellBrowser parameter based on the IServiceProvider object pointer to obtain and save the IShellBrowser object pointer.
[0064] Furthermore, the second computing unit includes:
[0065] The third calculation unit is used to create an IShellView object pointer, and call the QueryActiveShellView method based on the IShellBrowser object pointer to obtain and save the IShellView object pointer.
[0066] Furthermore, the third computing unit includes:
[0067] The fourth calculation unit is used to create an IFolderView2 object pointer, and to call the QueryInterface method with the IID_IFolderView parameter based on the IShellView object pointer to obtain and save the IFolderView2 object pointer.
[0068] Furthermore, the fourth computing unit includes:
[0069] The fifth calculation unit is used to create an IShellItemArray object pointer, call the GetSelection method of the IFolderView2 object pointer to obtain the results of the files and directories selected by the user, and save the obtained results into the IShellItemArray array collection.
[0070] In one embodiment, the first creation unit 201 includes:
[0071] Store the pointer to the IFileOpenDialog object in a static variable.
[0072] Since the embodiments of the apparatus and the embodiments of the method correspond to each other, please refer to the description of the embodiments of the method for the embodiments of the apparatus, which will not be repeated here.
[0073] This invention also provides a computer-readable storage medium storing a computer program thereon, which, when executed, can perform the steps provided in the above embodiments. The storage medium may include various media capable of storing program code, such as a USB flash drive, a portable hard drive, a read-only memory (ROM), a random access memory (RAM), a magnetic disk, or an optical disk.
[0074] This invention also provides a computer device, which may include a memory and a processor. The memory stores a computer program, and when the processor calls the computer program in the memory, it can implement the steps provided in the above embodiments. Of course, the computer device may also include various network interfaces, power supplies, and other components.
[0075] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on its differences from other embodiments. Similar or identical parts between embodiments can be referred to interchangeably. For the systems disclosed in the embodiments, since they correspond to the methods disclosed in the embodiments, the descriptions are relatively simple; relevant parts can be referred to in the method section. It should be noted that those skilled in the art can make various improvements and modifications to this application without departing from the principles of this application, and these improvements and modifications also fall within the protection scope of the claims of this application.
[0076] It should also be noted that, in this specification, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.
Claims
1. A method for implementing a dialog box for mixed selection of files and directories, characterized in that, include: The CoInitialize function initializes COM, and the CoCreateInstance function creates an IFileOpenDialog object. Construct a FileDialogEvents object, and set the FileDialogEvents object as the event handler of the IFileOpenDialog object through the Advise function of the IFileOpenDialog object; The OnSelectionChange interface in the FileDialogEvents object is used to calculate and obtain the results of the file and directory selected by the user, and the results are saved to the IShellItemArray array collection; Iterate through the IShellItemArray array collection, obtain the string list of files and directories selected by the user through the GetDisplayName function of the IShellItem interface of the IShellItemArray array collection, and store it in the hash table of the global file; When the OnFileOk interface of the FileDialogEvents object receives the event indicating that the user has completed the selection, it converts the hash table into a file list, clears the variable memory, and calls the CoUninitialize function to release the COM. The step of calculating and obtaining the results of the user's selected file and directory through the OnSelectionChange interface in the FileDialogEvents object, and saving the results to the IShellItemArray array collection, includes: creating an IServiceProvider object pointer, which is used to store variables of the dialog service object; passing the IID_IServiceProvider parameter according to the IFileDialog interface object passed in by the event to call the QueryInterface function; and obtaining and saving the IServiceProvider object pointer for use in the next step. Create an IShellBrowser object pointer, which is used to store variables of the dialog box interface frame object. Based on the IServiceProvider object pointer, call the QueryService method with the IID_IShellBrowser parameter to obtain the IShellBrowser object pointer and save it for use in the next step. Create an IShellView object pointer, which is used to store variables of the view object in the dialog box. Call the QueryActiveShellView method based on the IShellBrowser object pointer to obtain the IShellView object pointer and save it for use in the next step. The QueryActiveShellView method is used to retrieve the currently active IShellView object. Create an IFolderView2 object pointer, which is used to store variables of the dialog file selection box object. Based on the IShellView object pointer, call the QueryInterface method through the IID_IFolderView parameter to obtain the IFolderView2 object pointer and save it for use in the next step. Create an IShellItemArray object pointer, which is used to store variables of the selected file and directory collection. Call the GetSelection method of the IFolderView2 object pointer to get the results of the user's selected files and directories, and save the results to the IShellItemArray array collection.
2. The method for implementing a mixed file and directory selection dialog box according to claim 1, characterized in that, The process of initializing COM using the CoInitialize function and creating an IFileOpenDialog object using the CoCreateInstance function includes: Store the pointer to the IFileOpenDialog object in a static variable.
3. An apparatus for implementing a dialog box for mixed selection of files and directories, characterized in that, include: The first creation unit is used to initialize COM using the CoInitialize function and create an IFileOpenDialog object using the CoCreateInstance function. The second creation unit is used to construct a FileDialogEvents object and set the FileDialogEvents object as the event handler of the IFileOpenDialog object through the Advise function of the IFileOpenDialog object; The first processing unit is used to calculate and obtain the results of the file and directory selected by the user through the OnSelectionChange interface in the FileDialogEvents object, and save the obtained results to the IShellItemArray array collection; The second processing unit is used to traverse the IShellItemArray array collection, obtain the string list of files and directories selected by the user through the GetDisplayName function of the IShellItem interface of the IShellItemArray array collection, and store it in the hash table of the global file; The third processing unit is used to convert the hash table into a file list, clear the variable memory, and call the CoUninitialize function to release the COM after the OnFileOk interface of the FileDialogEvents object obtains the event that the user has completed the selection. The first processing unit is specifically used to create an IServiceProvider object pointer, which is used to store variables of the dialog service object. The IID_IServiceProvider parameter is passed in according to the IFileDialog interface object passed in by the event to call the QueryInterface function, and obtain and save the IServiceProvider object pointer. Create an IShellBrowser object pointer, which is used to store variables of the dialog box interface frame object. Based on the IServiceProvider object pointer, call the QueryService method with the IID_IShellBrowser parameter to obtain and save the IShellBrowser object pointer; Create an IShellView object pointer, which is used to store variables of the view object in the dialog box. Call the QueryActiveShellView method based on the IShellBrowser object pointer to obtain and save the IShellView object pointer. The QueryActiveShellView method is used to retrieve the currently active IShellView object. Create an IFolderView2 object pointer, which is used to store variables of the dialog file selection box object. Based on the IShellView object pointer, call the QueryInterface method through the IID_IFolderView parameter to obtain and save the IFolderView2 object pointer. Create an IShellItemArray object pointer, which is used to store variables of the selected file and directory collection. Call the GetSelection method of the IFolderView2 object pointer to get the results of the user's selected files and directories, and save the results to the IShellItemArray array collection.
4. A computer device, characterized in that, The device includes a memory, a processor, and a computer program stored on the memory and executable on the processor, wherein the processor, when executing the computer program, implements the method for implementing a mixed file and directory selection dialog box as described in claim 1 or 2.
5. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that, when executed by a processor, implements the method for implementing a mixed selection dialog box for files and directories as described in claim 1 or 2.
Citation Information
Patent Citations
Method and system for using character string on Android platform, storage medium and electronic equipment
CN113391792A
Data processing method and device and computer readable storage medium
CN114679469A
