怎样用C#调用comdlg32.dll,利用GetOpenFileName实现打开文件对话框,最好有源码。

我知道C#可以引用System.Windows.Forms,不过我想实现用C#单纯调用Windows API实现打开文件对话框功能,希望高手指点,给我源码啊。我的QQ:474186854
[DllImport("dll文件名")]
public static extern 返回值 函数名(参数) 这种方法我知道,不过对于Win API的comdlg32.dll 中的 GetOpenFileName方法,他的参数是 C定义的一个Struct结构,我不知道在 C#中如何实现。希望能有完整源码,一定加分。

// Copyright
// Microsoft Corporation
// All rights reserved

// OpenFileDlg.cs

using System;
using System.Text;
using System.Runtime.InteropServices;

/*
typedef struct tagOFN {
DWORD lStructSize;
HWND hwndOwner;
HINSTANCE hInstance;
LPCTSTR lpstrFilter;
LPTSTR lpstrCustomFilter;
DWORD nMaxCustFilter;
DWORD nFilterIndex;
LPTSTR lpstrFile;
DWORD nMaxFile;
LPTSTR lpstrFileTitle;
DWORD nMaxFileTitle;
LPCTSTR lpstrInitialDir;
LPCTSTR lpstrTitle;
DWORD Flags;
WORD nFileOffset;
WORD nFileExtension;
LPCTSTR lpstrDefExt;
LPARAM lCustData;
LPOFNHOOKPROC lpfnHook;
LPCTSTR lpTemplateName;
#if (_WIN32_WINNT >= 0x0500)
void * pvReserved;
DWORD dwReserved;
DWORD FlagsEx;
#endif // (_WIN32_WINNT >= 0x0500)
} OPENFILENAME, *LPOPENFILENAME;
*/

[ StructLayout( LayoutKind.Sequential, CharSet=CharSet.Auto )]
public class OpenFileName
{
public int structSize = 0;
public IntPtr dlgOwner = IntPtr.Zero;
public IntPtr instance = IntPtr.Zero;

public String filter = null;
public String customFilter = null;
public int maxCustFilter = 0;
public int filterIndex = 0;

public String file = null;
public int maxFile = 0;

public String fileTitle = null;
public int maxFileTitle = 0;

public String initialDir = null;

public String title = null;

public int flags = 0;
public short fileOffset = 0;
public short fileExtension = 0;

public String defExt = null;

public IntPtr custData = IntPtr.Zero;
public IntPtr hook = IntPtr.Zero;

public String templateName = null;

public IntPtr reservedPtr = IntPtr.Zero;
public int reservedInt = 0;
public int flagsEx = 0;
}

public class LibWrap
{
//BOOL GetOpenFileName(LPOPENFILENAME lpofn);

[ DllImport( "Comdlg32.dll", CharSet=CharSet.Auto )]
public static extern bool GetOpenFileName([ In, Out ] OpenFileName ofn );
}

public class App
{
public static void Main()
{
OpenFileName ofn = new OpenFileName();

ofn.structSize = Marshal.SizeOf( ofn );

ofn.filter = "Log files\0*.log\0Batch files\0*.bat\0";

ofn.file = new String( new char[ 256 ]);
ofn.maxFile = ofn.file.Length;

ofn.fileTitle = new String( new char[ 64 ]);
ofn.maxFileTitle = ofn.fileTitle.Length;

ofn.initialDir = "C:\\";
ofn.title = "Open file called using platform invoke...";
ofn.defExt = "txt";

if( LibWrap.GetOpenFileName( ofn ))
{
Console.WriteLine( "Selected file with full path: {0}", ofn.file );
Console.WriteLine( "Selected file name: {0}", ofn.fileTitle );
Console.WriteLine( "Offset from file name: {0}", ofn.fileOffset );
Console.WriteLine( "Offset from file extension: {0}", ofn.fileExtension );
}
}
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2011-02-01
..用API就不要用c#了c++吧

可以采用P/Invoke方法,这是一种C#与win32 API互操作的方法,用法如下
[DllImport("winmm.dll")]
public static extern int waveOutGetVolume(IntPtr hwo, out uint dwVolume);
DllImport用于指定API所在的dll,之后用extern关键字声明一个在C#中和API等价的签名,然后在类型中就可以使用了,如果不清楚C#中与你想用的API等价的方法签名,可以查下百度。
第2个回答  2011-02-04
这个结构体是这样的:
下面是VB代码,
VB定义Struct的格式是:
Type 结构体名
变量 as 类型
…………
End Type
Integer就是C#里面的int32
结构就是这么个结构。
Public Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type

============================
使用时是这样的:不好意思还是VB代码

Dim OpenFile As OPENFILENAME
Dim lReturn As Long
Dim sFilter As String

On Error GoTo HandleErr

OpenFile.lStructSize = Len(OpenFile)
OpenFile.hwndOwner = Me.Hwnd
sFilter = "Textdateien (*.txt)" & Chr(0) & "*.txt" & Chr(0)
OpenFile.lpstrFilter = sFilter
OpenFile.nFilterIndex
OpenFile.lpstrFile = String(257, 0)
OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
OpenFile.lpstrFileTitle = OpenFile.lpstrFile
OpenFile.nMaxFileTitle = OpenFile.nMaxFile
OpenFile.lpstrInitialDir = "C:\"
OpenFile.lpstrTitle = "'Öffnen'-Dialog der Common Dialog API"
OpenFile.flags = 0
lReturn = GetOpenFileName(OpenFile)
If lReturn = 0 Then
MsgBox "Der Anwender hat die Aktion abgebrochen."
Else
MsgBox "Der Anwender hat den folgenden Pfad und Datei angegeben: " _
& Trim(OpenFile.lpstrFile)
End If
第3个回答  2011-02-02
那就用api啊,c#调用API和C类似,你可以百度下

怎样用C#调用comdlg32.dll,利用GetOpenFileName实现打开文件对话框,最...
[ DllImport( "Comdlg32.dll", CharSet=CharSet.Auto )]public static extern bool GetOpenFileName([ In, Out ] OpenFileName ofn );} public class App { public static void Main(){ OpenFileName ofn = new OpenFileName();ofn.structSize = Marshal.SizeOf( ofn );ofn.filter = "Log...

Comdlg32.dll文件丢失怎么?Comdlg32.dll修复方法
手动下载和安装:在正规网站上下载对应版本的dll文件,32位或64位系统需放入对应的Windows\/SysWOW64或System32文件夹,重启电脑完成注册。但需注意,可能需要针对缺失的多个DLL进行安装。DLL修复工具:对于不太熟悉技术的用户,可以考虑使用DLL修复工具,如金山毒霸或驱动精灵等,它们内置修复功能。以金山毒霸为...

comdlg32. ocx怎么用啊!
注册的话要用system32下的一个文件运行,名字叫regsvr32.exe 方法是右键单击comdlg32.ocx——打开方式——浏览找到regsvr32.exe点确定就可以了。或者点“开始”“运行”(或者按快捷键 徽标+R),输入 regsvr32 comdlg32.ocx 点“确定”。对于 Windows XP 以及更高版本系统,点“开始”“运行”(或者...

comdlg32.dll描述
作为系统DLL文件,comdlg32.dll是Windows应用程序不可或缺的一部分,它直接与应用程序交互,为用户提供直观的用户界面。当你尝试打开一个文件或进行类似操作时,这个文件会起到幕后支持的作用。然而,由于其重要性,有时可能会遇到一些问题,比如"File Not Found"或"Missing File"的错误,这通常意味着com...

请问如何替换comdlg32.dll 因为我要弄东西所以下了一个新的 然后把新...
1.先把system32的只读属性去掉 2.把新的comdlg32.dll重命名一下,就能复制到目标文件夹下。3.把旧的comdlg32.dll也重命名一下,4.最后,把新的再恢复正确名称。成功!如果觉得旧的文件碍事删不掉,就用360文件粉碎机去掉。本人是个半桶水,求人不如求己,自己的机子弄好了,大家可以一试。

comdlg32.dll丢失后 无法启动360或者金山卫士修复系统 怎么办_百度...
comdlg32.dll是Windows应用程序公用对话框模块,用于例如打开文件对话框。comdlg32.dll在遇到木马或病毒袭击时可能产生丢失的问题,导致相关应用程序无法正常加载。这情况个人建议你把用360系统急救箱来处理。具体步骤如下:1、双击360系统急救箱,出现如图的界面,然后单击“开始系统急救”。2、系统引擎初始...

VB中怎样用api打开保存文件对话框
首先需要将下面的API函数声明写入一个模块中。Option Explicit Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long Type OPENFILENAME lStructSize As Long hwndOwner As Long hInstance As Long lpstrFilter As String lpstrCustom...

VB 调用打开文件对话框 不用控件
在窗体中加入一个按钮,一个text控件,输入一下代码:Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long Private Type OPENFILENAME lStructSize As Long hwndOwner As Long hInstance As Long lpstrFilter As String l...

用c++打开一个文件夹
\/sizeof(*szBuffer);ofn.nFilterIndex = 0;ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER ;\/\/标志如果是多选要加上OFN_ALLOWMULTISELECT BOOL bSel = GetOpenFileName(&ofn);这样就可以打开选择文件对话框了。可以选择需要的文件。szBuffer是存放的选择的文件路径。

出现“没有找到comdlg32.dll,因此这个应用程序未能启动。重新安装应用...
你这个修复太麻烦了 找个能上网的PE系统U盘上网下载替换msgina.dll 不过以防万一最好重装系统更好 因为也可能是中毒了 问题原因太多

相似回答