2010-12-11 67 views
0
Public Class DFP 


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 
    Dim ofd As New OpenFileDialog 
    ofd.Filter = "Exe files|*.exe|Jpg files|*.jpg|Bat files|*.bat|mp3 files|*.mp3" 
    ofd.ShowDialog() 
    TextBox1.Text = ofd.FileName 
End Sub 

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IconInjector.Click 
    Dim sfd As New SaveFileDialog 
    sfd.Filter = "Exe files|*.exe|Jpg files|*.jpg|Bat files|*.bat|mp3 files|*.mp3" 
    sfd.ShowDialog() 
    Dim filesize As Double = Val(NumericUpDown1.Value) 
    IO.File.Copy(TextBox1.Text, sfd.FileName) 
    If RadioButton1.Checked Then 
     filesize = filesize * 1024 
    ElseIf RadioButton2.Checked Then 
     filesize = filesize * 1048576 
    ElseIf RadioButton3.Checked Then 
     filesize = filesize * 1073741824 
    ElseIf RadioButton4.Checked Then 
     filesize = filesize * 1099511627776 
    End If 
    Dim filetopump = IO.File.OpenWrite(sfd.FileName) 
    Dim size = filetopump.Seek(0, IO.SeekOrigin.[End]) 
    While size < filesize 
     filetopump.WriteByte(0) 
     size += 1 
    End While 
    filetopump.Close() 
    If cb2.Checked Then IconInjector.InjectIcon(sfd.FileName, iconPath) 
    MsgBox("Successfully Pumped!") 
End Sub 

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click 
    Dim ofd As New OpenFileDialog 
    ofd.Filter = "Ico files|*.ico" 
    ofd.ShowDialog() 
    TextBox1.Text = ofd.FileName 
End Sub 


Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cb2.CheckedChanged 

    If cb2.Checked Then 
     Button3.Enabled = True 
    Else 
     Button3.Enabled = False 
    End If 
End Sub 
End Class 
在粗線我得到這個錯誤

VB.net [錯誤我不知道該怎麼辦:(幫助請:)]

「InjectIcon」不是「系統中的一員.Windows.Forms.Button」。

它與基本沒有關係,因爲我使用它:PP.s。

按鈕2名是IconInjector

的錯誤是在這一行

如果cb2.Checked然後IconInjector.InjectIcon(sfd.FileName,iconPath)

+0

'InjectIcon'定義在哪裏? – 2010-12-11 19:18:13

+0

那麼,**是**按鈕上沒有叫做'InjectIcon'的方法。你想做什麼? – ChrisF 2010-12-11 19:18:34

+0

@Daniel - OP指出它是'Button'的名稱。 – ChrisF 2010-12-11 19:18:59

回答

0

您正在嘗試調用不存在的方法Button.InjectIcon

+0

那麼我該怎麼辦? [in begginer語言] – 2010-12-11 19:25:45

+0

@John:這取決於你希望你的代碼做什麼*。你爲什麼試圖首先調用這種方法? – Heinzi 2010-12-12 21:34:57

1

在谷歌的幫助下,我發現這個similar code。我想你想更改可執行文件的圖標/資源。對於初學者來說,這不是正確的主題,你不會學到任何東西,並且依賴於給你源代碼的人。

您的代碼缺少IconInjector類及其函數InjectIcon。不知道這是否有效,但看看你自己(控制名稱變得更有意義):

Imports System.Runtime.InteropServices 
Imports System.Security 

Public Class IconInjectorForm 

    Dim ofdExe As New OpenFileDialog 

    Private Sub BtnSelectExe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSelectExe.Click 
     ofdExe.DefaultExt = "" 
     ofdExe.Filter = "Portable Executable Files (*.exe)|*.exe" 
     ofdExe.FilterIndex = 1 
     ofdExe.FileName = "" 
     If ofdExe.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then 
      TxtExe.Text = ofdExe.FileName 
     End If 
    End Sub 

    Dim ofdIco As New OpenFileDialog 

    Private Sub BtnSelectIcon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSelectIcon.Click 
     ofdIco.DefaultExt = "" 
     ofdIco.Filter = "icon files (*.ico)|*.ico" 
     ofdIco.FilterIndex = 1 
     ofdIco.FileName = "" 
     If ofdIco.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then 
      TxtIcon.Text = ofdIco.FileName 
     End If 
    End Sub 

    Private Sub BtnInjectIcon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnInjectIcon.Click 
     IconInjector.InjectIcon(TxtExe.Text, TxtIcon.Text) 
     MsgBox("Icon Changed Success", MsgBoxStyle.Information, "Success!") 
    End Sub 
End Class 


Public Class IconInjector 

    ' Basically, you can change icons with the UpdateResource api call. 
    ' When you make the call you say "I'm updating an icon", and you send the icon data. 
    ' The main problem is that ICO files store the icons in one set of structures, and exe/dll files store them in 
    ' another set of structures. So you have to translate between the two -- you can't just load the ICO file as 
    ' bytes and send them with the UpdateResource api call. 

    <SuppressUnmanagedCodeSecurity()> _ 
    Private Class NativeMethods 
     <DllImport("kernel32")> _ 
     Public Shared Function BeginUpdateResource(_ 
      ByVal fileName As String, _ 
      <MarshalAs(UnmanagedType.Bool)> ByVal deleteExistingResources As Boolean) As IntPtr 
     End Function 

     <DllImport("kernel32")> _ 
     Public Shared Function UpdateResource(_ 
      ByVal hUpdate As IntPtr, _ 
      ByVal type As IntPtr, _ 
      ByVal name As IntPtr, _ 
      ByVal language As Short, _ 
      <MarshalAs(UnmanagedType.LPArray, SizeParamIndex:=5)> _ 
      ByVal data() As Byte, _ 
      ByVal dataSize As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean 
     End Function 

     <DllImport("kernel32")> _ 
     Public Shared Function EndUpdateResource(_ 
      ByVal hUpdate As IntPtr, _ 
      <MarshalAs(UnmanagedType.Bool)> ByVal discard As Boolean) As <MarshalAs(UnmanagedType.Bool)> Boolean 
     End Function 
    End Class 

    ' The first structure in an ICO file lets us know how many images are in the file. 
    <StructLayout(LayoutKind.Sequential)> _ 
    Private Structure ICONDIR 
     Public Reserved As UShort ' Reserved, must be 0 
     Public Type As UShort  ' Resource type, 1 for icons. 
     Public Count As UShort  ' How many images. 
     ' The native structure has an array of ICONDIRENTRYs as a final field. 
    End Structure 

    ' Each ICONDIRENTRY describes one icon stored in the ico file. The offset says where the icon image data 
    ' starts in the file. The other fields give the information required to turn that image data into a valid 
    ' bitmap. 
    <StructLayout(LayoutKind.Sequential)> _ 
    Private Structure ICONDIRENTRY 
     Public Width As Byte   ' Width, in pixels, of the image 
     Public Height As Byte   ' Height, in pixels, of the image 
     Public ColorCount As Byte  ' Number of colors in image (0 if >=8bpp) 
     Public Reserved As Byte   ' Reserved (must be 0) 
     Public Planes As UShort   ' Color Planes 
     Public BitCount As UShort  ' Bits per pixel 
     Public BytesInRes As Integer ' Length in bytes of the pixel data 
     Public ImageOffset As Integer ' Offset in the file where the pixel data starts. 
    End Structure 

    ' Each image is stored in the file as an ICONIMAGE structure: 
    'typdef struct 
    '{ 
    ' BITMAPINFOHEADER icHeader;  // DIB header 
    ' RGBQUAD   icColors[1]; // Color table 
    ' BYTE   icXOR[1];  // DIB bits for XOR mask 
    ' BYTE   icAND[1];  // DIB bits for AND mask 
    '} ICONIMAGE, *LPICONIMAGE; 


    <StructLayout(LayoutKind.Sequential)> _ 
    Private Structure BITMAPINFOHEADER 
     Public Size As UInteger 
     Public Width As Integer 
     Public Height As Integer 
     Public Planes As UShort 
     Public BitCount As UShort 
     Public Compression As UInteger 
     Public SizeImage As UInteger 
     Public XPelsPerMeter As Integer 
     Public YPelsPerMeter As Integer 
     Public ClrUsed As UInteger 
     Public ClrImportant As UInteger 
    End Structure 

    ' The icon in an exe/dll file is stored in a very similar structure: 
    <StructLayout(LayoutKind.Sequential, Pack:=2)> _ 
    Private Structure GRPICONDIRENTRY 
     Public Width As Byte 
     Public Height As Byte 
     Public ColorCount As Byte 
     Public Reserved As Byte 
     Public Planes As UShort 
     Public BitCount As UShort 
     Public BytesInRes As Integer 
     Public ID As UShort 
    End Structure 

    Public Shared Sub InjectIcon(ByVal exeFileName As String, ByVal iconFileName As String) 
     InjectIcon(exeFileName, iconFileName, 1, 1) 
    End Sub 

    Public Shared Sub InjectIcon(ByVal exeFileName As String, ByVal iconFileName As String, ByVal iconGroupID As UInteger, ByVal iconBaseID As UInteger) 
     Const RT_ICON = 3UI 
     Const RT_GROUP_ICON = 14UI 
     Dim iconFile As IconFile = iconFile.FromFile(iconFileName) 
     Dim hUpdate = NativeMethods.BeginUpdateResource(exeFileName, False) 
     Dim data = iconFile.CreateIconGroupData(iconBaseID) 
     NativeMethods.UpdateResource(hUpdate, New IntPtr(RT_GROUP_ICON), New IntPtr(iconGroupID), 0, data, data.Length) 
     For i = 0 To iconFile.ImageCount - 1 
      Dim image = iconFile.ImageData(i) 
      NativeMethods.UpdateResource(hUpdate, New IntPtr(RT_ICON), New IntPtr(iconBaseID + i), 0, image, image.Length) 
     Next 
     NativeMethods.EndUpdateResource(hUpdate, False) 
    End Sub 

    Private Class IconFile 

     Private iconDir As New ICONDIR 
     Private iconEntry() As ICONDIRENTRY 
     Private iconImage()() As Byte 

     Public ReadOnly Property ImageCount() As Integer 
      Get 
       Return iconDir.Count 
      End Get 
     End Property 

     Public ReadOnly Property ImageData(ByVal index As Integer) As Byte() 
      Get 
       Return iconImage(index) 
      End Get 
     End Property 

     Private Sub New() 
     End Sub 

     Public Shared Function FromFile(ByVal filename As String) As IconFile 
      Dim instance As New IconFile 
      ' Read all the bytes from the file. 
      Dim fileBytes() As Byte = IO.File.ReadAllBytes(filename) 
      ' First struct is an ICONDIR 
      ' Pin the bytes from the file in memory so that we can read them. 
      ' If we didn't pin them then they could move around (e.g. when the 
      ' garbage collector compacts the heap) 
      Dim pinnedBytes = GCHandle.Alloc(fileBytes, GCHandleType.Pinned) 
      ' Read the ICONDIR 
      instance.iconDir = DirectCast(Marshal.PtrToStructure(pinnedBytes.AddrOfPinnedObject, GetType(ICONDIR)), ICONDIR) 
      ' which tells us how many images are in the ico file. For each image, there's a ICONDIRENTRY, and associated pixel data. 
      instance.iconEntry = New ICONDIRENTRY(instance.iconDir.Count - 1) {} 
      instance.iconImage = New Byte(instance.iconDir.Count - 1)() {} 
      ' The first ICONDIRENTRY will be immediately after the ICONDIR, so the offset to it is the size of ICONDIR 
      Dim offset = Marshal.SizeOf(instance.iconDir) 
      ' After reading an ICONDIRENTRY we step forward by the size of an ICONDIRENTRY   
      Dim iconDirEntryType = GetType(ICONDIRENTRY) 
      Dim size = Marshal.SizeOf(iconDirEntryType) 
      For i = 0 To instance.iconDir.Count - 1 
       ' Grab the structure. 
       Dim entry = DirectCast(Marshal.PtrToStructure(New IntPtr(pinnedBytes.AddrOfPinnedObject.ToInt64 + offset), iconDirEntryType), ICONDIRENTRY) 
       instance.iconEntry(i) = entry 
       ' Grab the associated pixel data. 
       instance.iconImage(i) = New Byte(entry.BytesInRes - 1) {} 
       Buffer.BlockCopy(fileBytes, entry.ImageOffset, instance.iconImage(i), 0, entry.BytesInRes) 
       offset += size 
      Next 
      pinnedBytes.Free() 
      Return instance 
     End Function 

     Public Function CreateIconGroupData(ByVal iconBaseID As UInteger) As Byte() 
      ' This will store the memory version of the icon. 
      Dim sizeOfIconGroupData As Integer = Marshal.SizeOf(GetType(ICONDIR)) + Marshal.SizeOf(GetType(GRPICONDIRENTRY)) * ImageCount 
      Dim data(sizeOfIconGroupData - 1) As Byte 
      Dim pinnedData = GCHandle.Alloc(data, GCHandleType.Pinned) 
      Marshal.StructureToPtr(iconDir, pinnedData.AddrOfPinnedObject, False) 
      Dim offset = Marshal.SizeOf(iconDir) 
      For i = 0 To ImageCount - 1 
       Dim grpEntry As New GRPICONDIRENTRY 
       Dim bitmapheader As New BITMAPINFOHEADER 
       Dim pinnedBitmapInfoHeader = GCHandle.Alloc(bitmapheader, GCHandleType.Pinned) 
       Marshal.Copy(ImageData(i), 0, pinnedBitmapInfoHeader.AddrOfPinnedObject, Marshal.SizeOf(GetType(BITMAPINFOHEADER))) 
       pinnedBitmapInfoHeader.Free() 
       grpEntry.Width = iconEntry(i).Width 
       grpEntry.Height = iconEntry(i).Height 
       grpEntry.ColorCount = iconEntry(i).ColorCount 
       grpEntry.Reserved = iconEntry(i).Reserved 
       grpEntry.Planes = bitmapheader.Planes 
       grpEntry.BitCount = bitmapheader.BitCount 
       grpEntry.BytesInRes = iconEntry(i).BytesInRes 
       grpEntry.ID = CType(iconBaseID + i, UShort) 
       Marshal.StructureToPtr(grpEntry, New IntPtr(pinnedData.AddrOfPinnedObject.ToInt64 + offset), False) 
       offset += Marshal.SizeOf(GetType(GRPICONDIRENTRY)) 
      Next 
      pinnedData.Free() 
      Return data 
     End Function 

    End Class 

End Class 
相關問題