Delphi 7 和 Vista/Windows 7 通用对话框 - 事件不起作用
我正在尝试修改 Delphi 7 Dialogs.pas 以访问较新的 Windows 7 打开/保存对话框(请参阅使用 Delphi 创建 Windows Vista Ready 应用程序)。我可以使用建议的修改来显示对话框;但是,OnFolderChange 和 OnCanClose 等事件不再起作用。
这似乎与将 Flags:= OFN_ENABLEHOOK 更改为 Flags:=0 有关。当 Flags 设置为 0 时,TOpenDialog.Wndproc 将被绕过,并且不会捕获相应的 CDN_xxxxxxx 消息。
任何人都可以建议对 D7 Dialogs.pas 进行进一步的代码修改,以显示较新的通用对话框并保留原始控件的事件功能吗?
谢谢...
I'm trying to modify the Delphi 7 Dialogs.pas to access the newer Windows 7 Open/Save dialog boxes (see Creating Windows Vista Ready Applications with Delphi). I can display the dialogs using the suggested modifications; however, events such as OnFolderChange and OnCanClose no longer function.
This appears to be related to changing the Flags:= OFN_ENABLEHOOK to Flags:=0. When Flags is set to 0 the TOpenDialog.Wndproc is bypassed and the appropriate CDN_xxxxxxx messages are not trapped.
Can anyone suggest further code modifications to the D7 Dialogs.pas that will both display the newer common dialogs and maintain the event features of the original controls?
Thanks...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该使用 IFileDialog 接口 并调用其
Advise()
方法以及 IFileDialogEvents 接口。 Delphi 7 Windows 标头单元不会包含必要的声明,因此必须从 SDK 标头文件复制(并翻译)它们(或者可能已经有另一个可用的标头翻译?),但除了额外的工作之外,不应该从 Delphi 7(甚至更早的 Delphi 版本)调用它不会有任何问题。编辑:
好的,既然您没有对答案做出任何反应,我将添加更多信息。有关如何使用接口的 AC 示例可以在此处< /a>.只要您有必要的导入单元,就可以轻松地将其转换为 Delphi 代码。
我在 Delphi 4 中拼凑了一个小示例。为了简单起见,我创建了一个
TOpenDialog
后代(您可能会修改原始类)并直接在其上实现IFileDialogEvents
:如果您在 Windows 7 上运行它,它将显示新对话框并仅接受带有
txt
扩展名的文件。这是硬编码的,需要通过对话框的OnClose事件来实现。还有很多工作要做,但提供的代码应该足以作为起点。You should use the IFileDialog Interface and call its
Advise()
method with an implementation of the IFileDialogEvents Interface. The Delphi 7 Windows header units won't contain the necessary declarations, so they must be copied (and translated) from the SDK header files (or maybe there's already another header translation available?), but apart from that additional effort there shouldn't be any problem to call this from Delphi 7 (or even earlier Delphi versions).Edit:
OK, since you didn't react in any way to the answers I'll add some more information. A C sample on how to use the interfaces can be had here. It's easy to translate it to Delphi code, provided you have the necessary import units.
I threw together a small sample in Delphi 4. For simplicity I created a
TOpenDialog
descendant (you would probably modify the original class) and implemented theIFileDialogEvents
directly on it:If you run this on Windows 7 it will show the new dialog and accept only files with the
txt
extension. This is hard-coded and needs to be implemented by going through theOnClose
event of the dialog. There's lots more to be done, but the provided code should suffice as a starting point.这是 Delphi 7 Vista/Win7 对话框组件(以及调用它的单元)的框架。我尝试复制 TOpenDialog 的事件(例如 OnCanClose)。类型定义不包含在组件中,但可以在网上一些较新的 ShlObj 和 ActiveX 单元中找到。
我在尝试将旧式 Filter 字符串转换为 FileTypes 数组时遇到问题(见下文)。因此,现在您可以如图所示设置 FileTypes 数组。欢迎有关过滤器转换问题或其他改进的任何帮助。
这是代码:
Here's the framework for a Delphi 7 Vista/Win7 dialog component (and a unit that calls it). I've tried to duplicate the TOpenDialog's events (e.g., OnCanClose). The type definitions are not included in the component, but can be found in some newer ShlObj and ActiveX units out there on the net.
I had a problem trying to convert an old style Filter string to a FileTypes array (see below). So for now, you can set the FileTypes array as shown. Any help on filter conversion issue or other improvements are welcome.
Here's the code:
JeffR - 您的过滤代码的问题与转换为 WideString 的 PWideChar 的转换有关。
转换后的宽字符串没有分配给任何东西,因此会在堆栈或堆上,在堆栈或堆上保存指向临时值的指针本质上是危险的!
正如 loursonwinny 所建议的,您可以使用 StringToOleStr,但仅此一点就会导致内存泄漏,因为包含创建的 OleStr 的内存永远不会被释放。
我对这部分代码的修改版本是:
非常感谢您的代码示例,因为它节省了我很多工作!
JeffR - The problem with your filtering code was related to the casting to a PWideChar of a conversion to WideString.
The Converted widestring was not assigned to anything, so would have been on the stack or heap, saving a pointer to a temporary value on the stack or heap is inherently dangerous!
As suggested by loursonwinny, you could use StringToOleStr, but this alone will cause a memory leak, as the memory containing the created OleStr would never be released.
My reworked version of this section of the code is:
Many thanks for you code sample as it saved me a lot of work!!
我环顾四周,为 FPC/Lazarus 制作了这个快速补丁,但当然您也可以使用它作为 D7 升级的基础:(
已删除,使用当前的 FPC 源,因为错误修复已应用于此功能)
注意:未经测试,并且可能包含 D7 中没有的符号。
I was looking around a bit, and made this quick patch for FPC/Lazarus, but of course you can use this as basis for D7 upgrading too:
(Deleted, use current FPC sources, since bugfixes were applied to this functionality)
Note: untested, and might contain symbols not in D7.