shellexecute调用默认程序(20)

  • 主题发起人 主题发起人 terry_zhou82
  • 开始时间 开始时间
T

terry_zhou82

Unregistered / Unconfirmed
GUEST, unregistred user!
在delphi中如何做到,点击一个文件名后,调用该文件的默认方式打开,如果运行缺省打开方式的程式无,则调用“打开方式”板选择程序打开。 我的源代码如下procedure TForm1.Button1Click(Sender: TObject);begin try ShellExecute(Handle,'open',PChar(edit1.Text),nil,nil, SW_SHOWNORMAL); except MessageBox(0,'文件打开出错,请检查文件是否已关联有效程序!','警告',MB_OK or MB_ICONWARNING); end;end;procedure TForm1.SpeedButton1Click(Sender: TObject);beginif OpenDialog1.Execute thenedit1.Text :=OpenDialog1.FileName;end;end.
 
如果找不到对应的打开程序,该函数不会发生异常的,看看这个函数的返回值帮助吧(<=32表示失败)。现在回答你问题的人越来越少了。Return ValuesIf the function succeeds, the return value is the instance handle of the application that was run, or the handle of a dynamic data exchange (DDE) server application.If the function fails, the return value is an error value that is less than or equal to 32. The following table lists these error values:Value Meaning0 The operating system is out of memory or resources.ERROR_FILE_NOT_FOUND The specified file was not found.ERROR_PATH_NOT_FOUND The specified path was not found.ERROR_BAD_FORMAT The .EXE file is invalid (non-Win32 .EXE or error in .EXE image).SE_ERR_ACCESSDENIED The operating system denied access to the specified file. SE_ERR_ASSOCINCOMPLETE The filename association is incomplete or invalid.SE_ERR_DDEBUSY The DDE transaction could not be completed because other DDE transactions were being processed.SE_ERR_DDEFAIL The DDE transaction failed.SE_ERR_DDETIMEOUT The DDE transaction could not be completed because the request timed out.SE_ERR_DLLNOTFOUND The specified dynamic-link library was not found. SE_ERR_FNF The specified file was not found. SE_ERR_NOASSOC There is no application associated with the given filename extension.SE_ERR_OOM There was not enough memory to complete the operation.SE_ERR_PNF The specified path was not found.SE_ERR_SHARE A sharing violation occurred. 切不可说英文看不明白。
 
是因为我分数给的太少,还是什么,搞不明白啊,还是不知道改怎么改啊
 
分数少,问题总是不结贴,有些问题太简单或者你把简单问题想的太复制了。帮助说,该函数如果执行失败(如文件不存在、文件没关联打开的程序等),则函数返回一个<=32的值。再返回失败(返回值<=32)的情况下,你再调用“打开方式”板选择程序打开。这样说,你还能明白啊?函数不熟悉,先看看Delphi的帮助,盲目地问别人,对于提高自己的能力帮助不大。
 
shellexecute 应当是打开 与win32 exe 有关联文件的,其他的文件打不开(访问失败)如果shellexecute 返回失败则调用 rundll32.exe shell32.dll,OpenAs_RunDLL {drive:/path/filename}(打开方式窗口)procedure TForm1.btn1Click(Sender: TObject);var od:topendialog;filename:string; fileExt:string;h:thandle;begin od:=topendialog.Create(nil); od.Filter :='所有文件(*.*)|*.*'; if od.Execute then begin filename:=od.FileName ; edt1.Text :=filename; fileext:=extractfileExt(filename); { if strupper(pchar(fileext))<>'.EXE' then begin end; } h:= ShellExecute(Handle,'open',PChar(edt1.Text),nil,nil, SW_SHOWNORMAL); if h=SE_ERR_ACCESSDENIED then begin winexec(pchar( 'rundll32.exe shell32.dll,OpenAs_RunDLL '+filename),1); end; end;end;测试通过
 
FindExecutable找到默认打开程序,如果没有找到,再调用打开方式
 
接受答案了.
 
后退
顶部