关于文件的打开问题

peacock 2000-01-20 10:28:00
我编了一个文本编辑程序,但怎样才能像记事本那样当双击*.txt文件时,记事本就打开这个文件了,我可以做到文件的关联,但文件没有打开,只是我的程序运行了,和单独运行我的程序没有区别。
...全文
273 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
WHQ 2000-01-21
  • 打赏
  • 举报
回复
对已注册的文档,双击该文档时,系统自动把文件名传递到相关的程序,这个不用你来关心,你需要做的是让你的程序在接收到这个命令行参数之后,再把这个参数当成一个文件名来处理就行了。MFC封装的App类中已处理命令行参数的函数ParseCommand,大概是这么拼的吧,我想VB中也该差不多。
peacock 2000-01-21
  • 打赏
  • 举报
回复
WHQ,怎样把文件名当作命令行参数传递给程序。请具体点
zdg 2000-01-21
  • 打赏
  • 举报
回复
有点像Perl. 下面是个例子:
"C:\Program Files\Microsoft Visual Studio\Common\IDE\IDE98\devenv.exe" "%1"
%1表示第一个参数...
ltp3 2000-01-21
  • 打赏
  • 举报
回复
在工程属性的“生成”选项卡中“命令”行参数域中键入示例的参数。(不要键入应用程序名本身。)
在程序中用command函数返回命令行参数。
ltp3 2000-01-21
  • 打赏
  • 举报
回复
奇怪,为什么不行?
我用以下步骤做的一个exe就行了。
1.新建一个工程。
2.画上一个Richtextbox
3.在Form_Load中写上Richtextbox1.filename=command
4.生成"工程1.exe"
5.选中一个.txt文件,在“打开方式”中选择"工程1.exe"
6.OK!
peacock 2000-01-21
  • 打赏
  • 举报
回复
怎么不行呀?
Richtextbox1.loadfile Command
Richtextbox1.loadfile Command$
Richtextbox1.filename Command
Richtextbox1.filename Command$
都不行,Command和Command$都返回空!
大家具体点!
ltp3 2000-01-21
  • 打赏
  • 举报
回复
你应该用Richtextbox1.loadfile Command
solomon 2000-01-21
  • 打赏
  • 举报
回复
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


peacock 2000-01-21
  • 打赏
  • 举报
回复
wanghongqi:我很想学习VC,可总入不了门。只能在VC中用TC的方法编程序,你能不能教我入门的方法?
WHQ 2000-01-21
  • 打赏
  • 举报
回复
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;
}
peacock 2000-01-21
  • 打赏
  • 举报
回复
我用Richtextbox1.filename=Command()没成功。
你具体怎么实现的?
Firing_Sky 2000-01-21
  • 打赏
  • 举报
回复
就是用Command来捕获参数啊!你设置文件关联后双击文件,就会用“应用程序 参数”的形式来打开它,你要做的是在Form.Load事件中加入Command(),用返回值来作为你要打开的文件名。
peacock 2000-01-21
  • 打赏
  • 举报
回复
WHQ:VB中是Command,但我用过,没成功,因为我不太会用,也没有这方面的资料。
你能用VC做个例子吗?
WHQ 2000-01-20
  • 打赏
  • 举报
回复
你的程序必须接收并处理命令行参数,因为你双击已关联的文档文件时,系统将启动与该文档相关联的程序,并把文件名当作命令行参数传递给程序。另一种途径是采用DDE,这个我不熟悉

7,762

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧