则GetSaveFileName()如何更新在&QUOT文件扩展名;文件名:"控制? [英] GetSaveFileName() how to update the file extension in the "File name:" control?

查看:332
本文介绍了则GetSaveFileName()如何更新在&QUOT文件扩展名;文件名:"控制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code(提取物),以显示另存为对话框:

I have the following code (extract) to display Save As dialog:

char FileName[MAX_PATH] = "TestImage.jpg"

...

lpofn.lStructSize = sizeof(OPENFILENAME);
lpofn.hwndOwner = hWnd;
lpofn.hInstance = GetWindowInstance (hWnd);
lpofn.lpstrFilter = "JPG - JPEG File\0*.JPG\0TIF - TIFF File\0*.TIF\0PNG File\0*.PNG\0BMP - Bitmat File\0*.BMP\0";
lpofn.lpstrCustomFilter = NULL;
lpofn.nMaxCustFilter = NULL;
lpofn.nFilterIndex = 0;
lpofn.lpstrFile = FileName;
lpofn.nMaxFile = MAX_PATH;
lpofn.lpstrFileTitle = NULL;
lpofn.nMaxFileTitle = NULL;
lpofn.lpstrInitialDir = NULL;
lpofn.lpstrTitle = NULL;
lpofn.Flags = OFN_HIDEREADONLY | OFN_ENABLEHOOK | OFN_EXPLORER;
lpofn.nFileOffset = 0;
lpofn.nFileExtension = 0;
lpofn.lpstrDefExt = NULL;
lpofn.lCustData = NULL;
lpofn.lpfnHook = &UpdateFilename;
lpofn.lpTemplateName = NULL;
if(!GetSaveFileName(&lpofn)) return;

...

如。
- 用户没有保存为,默认文件名=TestImage.jpg类型的默认文件= JPG
- 用户更改文件类型为PNG,文件名控制保持在TestImage.jpg,而不是更改为TestImage.png

eg. - User does save as, default File name = "TestImage.jpg", default Files of Type = JPG - User changes Files of Type to PNG, File name control remains at "TestImage.jpg" instead of changing to "TestImage.png"

我是不是做错了什么?是否有可能指示则GetSaveFileName()来更改扩展名,或者我必须有一个自定义另存为对话框(任何的例子吗?)

Am I doing something wrong? Is it possible to instruct GetSaveFileName() to change the extension, or do I have to have a custom save as dialog (any examples?)

我使用的Win32 API,VC6。

I'm using Win32 API, VC6.

更新:这里是钩子函数:

UINT CALLBACK UpdateFilename(HWND hdlg, UINT uiMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uiMsg)
    {
         case WM_NOTIFY:
          // Check for CDN_TYPECHANGE etc
              return FALSE;
    }

    return FALSE;
}

注意钩子函数不停止断点。我故意不处理CDN_TYPECHANGE进一步进行,直到为何启用挂接时的对话框改变一下我可以计算出,以及如何解决它。

Note that the hook function does stop on breakpoints. I've purposely not proceeded further with handling CDN_TYPECHANGE until I can figure out why the look of the dialog changes when the hook is enabled and how to fix it.

推荐答案

为了更新对话框,同时它仍在运行,你需要提供一个指向 lpfnHook 回调在 OPENFILENAME 结构,并有回调处理的 CDN_TYPECHANGE 通知。它可以发送对话框中 CDM_GETFILEPATH CDM_GETSPEC 消息获取当前的文件名,按需要调整它,然后发送的 CDM_SETCONTROLTEXT 消息更新编辑字段(文件名编辑字段的ID是 0x442 )用新的价值。

In order to update the dialog while it is still running, you need to provide a pointer to an lpfnHook callback in the OPENFILENAME struct, and have the callback handle the CDN_TYPECHANGE notification. It can send the dialog a CDM_GETFILEPATH or CDM_GETSPEC message to get the current filename, tweak it as needed, and then send a CDM_SETCONTROLTEXT message to update the edit field (the ID of the filename edit field is 0x442) with the new value.

更新:有什么不对你的钩子code。 则GetSaveFileName()是德precated开始在Windows Vista中,取而代之的是(和周围变成一个包装)的的通用项对话框。该GSFN对话框UI不是由XP钩子改变,所以你必须使用Vista +,在这种情况下,使挂钩简单地使包装使用不同的设置在内部调用CID的时候。很多新的CID功能都基于 的IShellItem ,而不是文件名字符串,所以包装删除任何不能被重新psented作为一个老风格的文件名$ p $,并使得该对话框看起来像在XP旧式GSFN对话和更早版本。所以你看到的是的正常的行为则GetSaveFileName()在Vista下+!如果你不喜欢它,那么就不要使用则GetSaveFileName()了。使用新的 IFileSaveDialog 接口,而不是。事实上,它本身为您更改文件扩展名,如果你配置了多个文件类型,指定其中一个作为默认的扩展名,然后设置默认的扩展名相匹配的初始文件名。但是,如果你愿意,你可以或者实现 IFileDialogEvents 在code接口接收 OnTypeChange 通知,然后使用的 IFileDialog :: SetFileName() 方法neded更新显示的文件名。

Update: There is nothing wrong with your hook code. GetSaveFileName() is deprecated starting in Windows Vista, replaced by (and becoming a wrapper around) the Common Item Dialog. The GSFN dialog UI is not altered by a hook in XP, so you must be using Vista+, in which case enabling the hook simply causes the wrapper to use different settings when invoking the CID internally. A lot of the new CID features are based on IShellItem, not filename strings, so the wrapper removes anything that cannot be represented as a old-style filename, and makes the dialog look like the old-style GSFN dialog in XP and earlier. So what you are seeing is normal behavior for GetSaveFileName() under Vista+! If you do not like it, then do not use GetSaveFileName() anymore. Use the new IFileSaveDialog interface instead. In fact, it natively changes the file extension for you if you configure multiple file types, designate one of them as the default extension, and then set an initial filename that matches the default extension. But if you wanted to, you can alternatively implement the IFileDialogEvents interface in your code to receive OnTypeChange notifications and then use the IFileDialog::SetFileName() method to update the displayed filename as neded.

这篇关于则GetSaveFileName()如何更新在&QUOT文件扩展名;文件名:"控制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆