双击文件启动关联程序的问题
是这样的,我的程序是限制了在同一时间只能运行一个实例,并且在FORM Create事件中写了
if(FileExists(ParamStr(1)))
{
TabControl1-> Tabs-> Add(ParamStr(1));
RichEdit1-> Lines-> LoadFromFile(ParamStr(1));
StatusBar1-> Panels-> Items[1]-> Text=ParamStr(1);
}
现在我想问下,如何在程序已经启动的情况下,双击任何一个已经关联了这个程序的文件,将文件内容显示在新增标签的RichEdit里,
[解决办法]
下面函数
可以实现文件与程序关联:
//---------------------------------------
bool __stdcall RegFileType(AnsiString Ext, // 扩展名,如: ".dyc "
AnsiString ExtTypeName, // 文件类型,如: "dyc file "
AnsiString AppFileName, // 应用程序名,如 "c:\\dyc.exe "
AnsiString FileDescription, // 文件类型描述,如: "dyc documents "
int IconIndex) // 图标索引,如0
{
bool RegOK;
RegOK = WriteStringToReg(HKEY_CLASSES_ROOT, "\\ "+Ext, " ", ExtTypeName);
if(AppFileName.Pos( " ")> 0) {
AppFileName = "\ " "+AppFileName+ "\ " ";
}
if(RegOK) {
RegOK = WriteStringToReg(HKEY_CLASSES_ROOT, "\\ "+ExtTypeName, " ", FileDescription);
}
if(RegOK) {
RegOK = WriteStringToReg(HKEY_CLASSES_ROOT, "\\ "+ExtTypeName+ "\\DefaultIcon ", " ", AppFileName+ ", "+IntToStr(IconIndex));
}
if(RegOK) {
RegOK = WriteStringToReg(HKEY_CLASSES_ROOT, "\\ "+ExtTypeName+ "\\Shell\\打开\\command ", " ", AppFileName+ " \ "%1\ " ");
}
return RegOK;
}
bool __stdcall WriteStringToReg(HKEY Root, AnsiString Key, AnsiString KeyName, AnsiString KeyValue) {
bool OK = false;
TRegistry *Registry = new TRegistry();
Registry-> RootKey = Root;
if(Registry-> OpenKey(Key, true)) {
try {
Registry-> WriteString(KeyName, KeyValue);
OK = true;
}
catch(...) {
}
}
delete Registry;
return OK;
}