你的程序必须接收并处理命令行参数,因为你双击已关联的文档文件时,系统将启动与该文档相关联的程序,并把文件名当作命令行参数传递给程序。另一种途径是采用DDE,这个我不熟悉

解决方案 »

  1.   

    WHQ,怎样把文件名当作命令行参数传递给程序。请具体点
      

  2.   

    在工程属性的“生成”选项卡中“命令”行参数域中键入示例的参数。(不要键入应用程序名本身。)
    在程序中用command函数返回命令行参数。
      

  3.   

    有点像Perl. 下面是个例子:
    "C:\Program Files\Microsoft Visual Studio\Common\IDE\IDE98\devenv.exe" "%1"
    %1表示第一个参数...
      

  4.   

    对已注册的文档,双击该文档时,系统自动把文件名传递到相关的程序,这个不用你来关心,你需要做的是让你的程序在接收到这个命令行参数之后,再把这个参数当成一个文件名来处理就行了。MFC封装的App类中已处理命令行参数的函数ParseCommand,大概是这么拼的吧,我想VB中也该差不多。
      

  5.   

    WHQ:VB中是Command,但我用过,没成功,因为我不太会用,也没有这方面的资料。
    你能用VC做个例子吗?
      

  6.   

    就是用Command来捕获参数啊!你设置文件关联后双击文件,就会用“应用程序 参数”的形式来打开它,你要做的是在Form.Load事件中加入Command(),用返回值来作为你要打开的文件名。
      

  7.   

    我用Richtextbox1.filename=Command()没成功。
    你具体怎么实现的?
      

  8.   

    MFC中对命令行处理如下:首先是在窗口创建以后但还未显示时解析命令行参数,把结果放入到一个结构中(CCommandLineInfo),然后调用处理外壳命令来打开通过命令行传入的文件。你的程序也应用一个打开文件的函数,在你收到命令行参数这后,调用这个函数就行了。void CWinApp::ParseCommandLine(CCommandLineInfo& rCmdInfo)
    {
    for (int i = 1; i < __argc; i++)
    {
    LPCTSTR pszParam = __targv[i];
    BOOL bFlag = FALSE;
    BOOL bLast = ((i + 1) == __argc);
    if (pszParam[0] == '-'  and  and  pszParam[0] == '/')
    {
    // remove flag specifier
    bFlag = TRUE;
    ++pszParam;
    }
    rCmdInfo.ParseParam(pszParam, bFlag, bLast);
    }
    }
    BOOL CWinApp::ProcessShellCommand(CCommandLineInfo& rCmdInfo)
    {
    BOOL bResult = TRUE;
    switch (rCmdInfo.m_nShellCommand)
    {
    case CCommandLineInfo::FileNew:
    if (!AfxGetApp()->OnCmdMsg(ID_FILE_NEW, 0, NULL, NULL))
    OnFileNew();
    if (m_pMainWnd == NULL)
    bResult = FALSE;
    break; // If we've been asked to open a file, call OpenDocumentFile() case CCommandLineInfo::FileOpen:
    if (!OpenDocumentFile(rCmdInfo.m_strFileName))
    bResult = FALSE;
    break; // If the user wanted to print, hide our main window and
    // fire a message to ourselves to start the printing case CCommandLineInfo::FilePrintTo:
    case CCommandLineInfo::FilePrint:
    m_nCmdShow = SW_HIDE;
    ASSERT(m_pCmdInfo == NULL);
    OpenDocumentFile(rCmdInfo.m_strFileName);
    m_pCmdInfo = &rCmdInfo;
    m_pMainWnd->SendMessage(WM_COMMAND, ID_FILE_PRINT_DIRECT);
    m_pCmdInfo = NULL;
    bResult = FALSE;
    break; // If we're doing DDE, hide ourselves case CCommandLineInfo::FileDDE:
    m_pCmdInfo = (CCommandLineInfo*)m_nCmdShow;
    m_nCmdShow = SW_HIDE;
    break; // If we've been asked to unregister, unregister and then terminate
    case CCommandLineInfo::AppUnregister:
    {
    UnregisterShellFileTypes();
    BOOL bUnregistered = Unregister(); // if you specify /EMBEDDED, we won't make an success/failure box
    // this use of /EMBEDDED is not related to OLE if (!rCmdInfo.m_bRunEmbedded)
    {
    if (bUnregistered)
    AfxMessageBox(AFX_IDP_UNREG_DONE);
    else
    AfxMessageBox(AFX_IDP_UNREG_FAILURE);
    }
    bResult = FALSE;    // that's all we do // If nobody is using it already, we can use it.
    // We'll flag that we're unregistering and not save our state
    // on the way out. This new object gets deleted by the
    // app object destructor. if (m_pCmdInfo == NULL)
    {
    m_pCmdInfo = new CCommandLineInfo;
    m_pCmdInfo->m_nShellCommand = CCommandLineInfo::AppUnregister;
    }
    }
    break;
    }
    return bResult;
    }
      

  9.   

    wanghongqi:我很想学习VC,可总入不了门。只能在VC中用TC的方法编程序,你能不能教我入门的方法?
      

  10.   

      if you use Visual Basic:
         Richtextbox1.filename = Command$
      is that work?
      
      if you use MFC
         CFile pf(__argv[1]); // open the file  if you use SDK  in WinMain
        OpenFile(lpCmdLine, ....) //lpCmdLine is the 3rd parameter
      
      

  11.   

    你应该用Richtextbox1.loadfile Command
      

  12.   

    怎么不行呀?
    Richtextbox1.loadfile Command
    Richtextbox1.loadfile Command$
    Richtextbox1.filename Command
    Richtextbox1.filename Command$
    都不行,Command和Command$都返回空!
    大家具体点!
      

  13.   

    奇怪,为什么不行?
    我用以下步骤做的一个exe就行了。
    1.新建一个工程。
    2.画上一个Richtextbox
    3.在Form_Load中写上Richtextbox1.filename=command
    4.生成"工程1.exe"
    5.选中一个.txt文件,在“打开方式”中选择"工程1.exe"
    6.OK!