中易网

如何在exe中提取dll

答案:2  悬赏:40  
解决时间 2021-04-28 02:12
  • 提问者网友:虛偽丶靜
  • 2021-04-27 07:35

请各位看清问题再回答

最佳答案
  • 二级知识专家网友:懂得ㄋ、沉默
  • 2021-04-27 08:41
using System;
using System.Runtime.InteropServices;
using System.Drawing;
namespace WindowsAppTmp
{
public class ExtractIcon
{
[DllImport("Shell32.dll")]
private static extern int SHGetFileInfo
(
string pszPath,
uint dwFileAttributes,
out SHFILEINFO psfi,
uint cbfileInfo,
SHGFI uFlags
);

[StructLayout(LayoutKind.Sequential)]
private struct SHFILEINFO
{
public SHFILEINFO(bool b)
{
hIcon=IntPtr.Zero;iIcon=0;dwAttributes=0;szDisplayName="";szTypeName="";
}
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.LPStr, SizeConst=260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.LPStr, SizeConst=80)]
public string szTypeName;
};

private ExtractIcon()
{
}

private enum SHGFI
{
SmallIcon = 0x00000001,
LargeIcon = 0x00000000,
Icon = 0x00000100,
DisplayName = 0x00000200,
Typename = 0x00000400,
SysIconIndex = 0x00004000,
UseFileAttributes = 0x00000010
}


public static Icon GetIcon(string strPath, bool bSmall)
{
SHFILEINFO info = new SHFILEINFO(true);
int cbFileInfo = Marshal.SizeOf(info);
SHGFI flags;
if (bSmall)
flags = SHGFI.Icon|SHGFI.SmallIcon|SHGFI.UseFileAttributes;
else
flags = SHGFI.Icon|SHGFI.LargeIcon|SHGFI.UseFileAttributes;

SHGetFileInfo(strPath, 256, out info,(uint)cbFileInfo, flags);
return Icon.FromHandle(info.hIcon);
}
}
}



using System;
using System.Windows.Forms;
using System.Drawing;

namespace MyForm {
public class CreatedForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button;
private Label label;
public static void Main( )
{
Application.Run( new CreatedForm( ) );
}
public CreatedForm()
{
InitializeComponents();
}

void InitializeComponents() {
//
// Set up generated class form
//
this.SuspendLayout();
this.Name = "form";
this.Size = new System.Drawing.Size(200, 96);
this.Text = "Test";

//
// Set up member button
//
button = new System.Windows.Forms.Button();
button.Name = "button";
button.Location = new System.Drawing.Point(56, 24);
button.Size = new Size(100,22);
button.Text = "按钮";
button.Anchor = (System.Windows.Forms.AnchorStyles.Top|System.Windows.Forms.AnchorStyles.Left);
button.TabIndex = 0;
this.Controls.Add(button);
button.Click+=new EventHandler(this.button_Click);
this.ResumeLayout(false);
//
label = new Label();
//label.Text = "文件图标";
label.Location=new Point(5,5);
label.Size = new Size(40,40);
this.Controls.Add(label);
}
protected void button_Click(object sender, System.EventArgs e)
{
//Stream myStream;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\" ;
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
openFileDialog1.FilterIndex = 2 ;
openFileDialog1.RestoreDirectory = true ;
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
if((openFileDialog1.OpenFile())!= null)
{
//label.Text=openFileDialog1.FileName;
label.Image=WindowsAppTmp.ExtractIcon.GetIcon(openFileDialog1.FileName, false).ToBitmap();
//Console.WriteLine(openFileDialog1.FileName);
}
}
}
}
}
全部回答
  • 1楼网友:两不相欠
  • 2021-04-27 09:28
using System; using System.Runtime.InteropServices; using System.Drawing; namespace WindowsAppTmp { public class ExtractIcon { [DllImport("Shell32.dll")] private static extern int SHGetFileInfo ( string pszPath, uint dwFileAttributes, out SHFILEINFO psfi, uint cbfileInfo, SHGFI uFlags ); [StructLayout(LayoutKind.Sequential)] private struct SHFILEINFO { public SHFILEINFO(bool b) { hIcon=IntPtr.Zero;iIcon=0;dwAttributes=0;szDisplayName="";szTypeName=""; } public IntPtr hIcon; public int iIcon; public uint dwAttributes; [MarshalAs(UnmanagedType.LPStr, SizeConst=260)] public string szDisplayName; [MarshalAs(UnmanagedType.LPStr, SizeConst=80)] public string szTypeName; }; private ExtractIcon() { } private enum SHGFI { SmallIcon = 0x00000001, LargeIcon = 0x00000000, Icon = 0x00000100, DisplayName = 0x00000200, Typename = 0x00000400, SysIconIndex = 0x00004000, UseFileAttributes = 0x00000010 } public static Icon GetIcon(string strPath, bool bSmall) { SHFILEINFO info = new SHFILEINFO(true); int cbFileInfo = Marshal.SizeOf(info); SHGFI flags; if (bSmall) flags = SHGFI.Icon|SHGFI.SmallIcon|SHGFI.UseFileAttributes; else flags = SHGFI.Icon|SHGFI.LargeIcon|SHGFI.UseFileAttributes; SHGetFileInfo(strPath, 256, out info,(uint)cbFileInfo, flags); return Icon.FromHandle(info.hIcon); } } } using System; using System.Windows.Forms; using System.Drawing; namespace MyForm { public class CreatedForm : System.Windows.Forms.Form { private System.Windows.Forms.Button button; private Label label; public static void Main( ) { Application.Run( new CreatedForm( ) ); } public CreatedForm() { InitializeComponents(); } void InitializeComponents() { // // Set up generated class form // this.SuspendLayout(); this.Name = "form"; this.Size = new System.Drawing.Size(200, 96); this.Text = "Test"; // // Set up member button // button = new System.Windows.Forms.Button(); button.Name = "button"; button.Location = new System.Drawing.Point(56, 24); button.Size = new Size(100,22); button.Text = "按钮"; button.Anchor = (System.Windows.Forms.AnchorStyles.Top|System.Windows.Forms.AnchorStyles.Left); button.TabIndex = 0; this.Controls.Add(button); button.Click+=new EventHandler(this.button_Click); this.ResumeLayout(false); // label = new Label(); //label.Text = "文件图标"; label.Location=new Point(5,5); label.Size = new Size(40,40); this.Controls.Add(label); } protected void button_Click(object sender, System.EventArgs e) { //Stream myStream; OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.InitialDirectory = "c:\\" ; openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ; openFileDialog1.FilterIndex = 2 ; openFileDialog1.RestoreDirectory = true ; if(openFileDialog1.ShowDialog() == DialogResult.OK) { if((openFileDialog1.OpenFile())!= null) { //label.Text=openFileDialog1.FileName; label.Image=WindowsAppTmp.ExtractIcon.GetIcon(openFileDialog1.FileName, false).ToBitmap(); //Console.WriteLine(openFileDialog1.FileName); } }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息