单击MFC编辑框中 [英] Clicking in an MFC edit box

查看:163
本文介绍了单击MFC编辑框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MFC对话框中创建了一个只读编辑框。我试图让它,以便用户单击编辑框中,它是只读的,它打开一个文件对话框,然后使用UpdateData将此值放入文本框。我捕获 ON_EN_SETFOCUS 消息,但按下文件对话框上的确定重新生成它,所以我陷入无限循环。

 更新数据(TRUE); 
CFileDialog fileDialog(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_FILEMUSTEXIST,_T(Text Files(*。txt)| * .txt ||));
if(fileDialog.DoModal()== IDOK)
{
configFile = fileDialog.GetPathName(); //自我注释,这包括文件名,getPathName包括文件名和路径。

}
else
{
return;
}

UpdateData(FALSE);

如果你有任何想法,我将非常感激。 / p>

解决方案

好吧,先生Lister先生,我想补充一个答案。



首先,我将前言,我可能只是添加一个按钮名称...启动文件对话框的编辑框的右侧,打开文件对话框,因为这是最简单的解决方案,大多数Windows用户将



然而,另一个选择是扩展MFC控件。当决定扩展一个控件,你想选择一个大多具有所需的行为,并有一个虚拟析构函数,这本身就是一个子类。因为你想要类似按钮的行为 CButton 可能是一个不错的选择。



你的类接口可能看起来像这样:

  class CPathButton:public CButton 
{
public:
enum {ID / * IDC_BUTTON1 * /};

const CString GetPath()const;
const CString GetFileName()const;
const CString GetDirectory()const;
const CString GetExtension()const;
//用于设置文件过滤器等的其他有用方法

protected:
//添加ON_CONTROL(BN_CLICKED,ID和& OnClick)或ON_BN_CLICKED(ID,& OnClick)
DECLARE_MESSAGE_MAP()

// CFileDialog fdlg.DoModal(),m_path = fdlg.GetPathName(),SetWindowText(fdlg.GetFileTitle())等
afx_msg void OnClick );

//其他消息处理程序等

private:
CString m_path; //保存对话框关闭后的完整路径
};

您可以根据需要添加或多或少的自定义,具体取决于是否将动态创建控件,通过资源文件,或任何。基本思想是在按钮上显示当前选择的文件名,同时存储其他用途的完整路径作为成员,因此用户不需要看到具有嵌套目录的长路径的混乱。

$默认情况下,你可以覆盖 OnPaint 并处理 WM_PAINT code>消息,并对长文件标题使用自定义字体,大小或添加省略号。您还可以使用文本度量和 GetTextExtent 来处理调整按钮的大小以适应文件标题,以确保名称适合或仅显示 CToolTipCtrl ,当他们将鼠标悬停在按钮上,以便他们可以看到全名。 VS2008 +中的MFC功能包中的 CMFCButton 具有内置的工具提示功能,因此如果您继承而不是 CButton 显示一个工具提示会很简单,如调用 SetTooltip(m_path)



如果你想真的想可以使用某些 uxtheme API 或新的视窗动画API


I've created a read-only edit box in an MFC dialog box. I'm trying to have it so a user clicks in the edit box, which is read-only, it opens a file dialog, and then puts this value into the text box using UpdateData. I'm catching the ON_EN_SETFOCUS message but pressing OK on the file dialog respawns it, so I get caught in an infinite loop.

UpdateData(TRUE);
CFileDialog fileDialog(TRUE,NULL, NULL,OFN_HIDEREADONLY|OFN_FILEMUSTEXIST, _T("Text Files(*.txt)|*.txt||"));
if( fileDialog.DoModal() == IDOK )
{
    configFile=fileDialog.GetPathName(); //Note to self, this includes filename, getPathName includes filename and path.

}
else
{
    return;
}

UpdateData(FALSE);

If you've got any ideas on how this should be done, I would be very grateful.

解决方案

Alright Mr. Lister I guess I'll add an answer.

First off I would preface this with I would probably simply add a button name "..." to launch the file dialog to the right of the edit box for opening the file dialog as that's the simplest solution and what most windows users will expect.

Another option however is to extend an MFC control. When deciding to extend a control you want to pick one that mostly has the desired behavior and that has a virtual destructor which lends itself to being a subclass. Since you want button like behavior CButton may be a good choice.

Your class interface might look something like this:

class CPathButton : public CButton
{
public:
    enum { ID /*= IDC_BUTTON1*/ };

    const CString GetPath() const;
    const CString GetFileName() const;
    const CString GetDirectory() const;
    const CString GetExtension() const;
    // other useful methods for setting file filters etc

protected:
    // add ON_CONTROL(BN_CLICKED, ID, &OnClick) or ON_BN_CLICKED(ID, &OnClick)
    DECLARE_MESSAGE_MAP()

    // CFileDialog fdlg.DoModal(), m_path = fdlg.GetPathName(), SetWindowText(fdlg.GetFileTitle()), etc
    afx_msg void OnClick();

    // additional message handlers etc

private:
    CString m_path; // save full path for after dialog is closed
};

You can add as much or as little customization as you want depending on if the control will be created dynamically, via the resource file, or whatever. The basic idea being that you display the currently selected file name on the button while storing the full path for other uses as a member so the user doesn't need to see the clutter of a long path with nested directories.

If you don't like the way it looks by default you can override OnPaint and handle WM_PAINT messages and use a custom font, size, or add ellipsis for a long file title. You could also handle re-sizing the button to fit the file title by using text metrics and GetTextExtent to ensure the name fits or simply display a CToolTipCtrl when they hover the mouse over the button so they can see the full name. The CMFCButton from the MFC feature pack in VS2008+ has tool tip functionality built in so if you inherit from that instead of CButton displaying a tool tip would be as simple as calling SetTooltip(m_path)

If you want to get really fancy you could use some of the uxtheme API or new windows animation API.

这篇关于单击MFC编辑框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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