| VB.NET | |
|
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 |
Imports System.Drawing Imports System.Reflection Imports System.Runtime.InteropServices Public Class Form1 Inherits System.Windows.Forms.Form ' アプリケーションに関連づけられたアイコンを取得するためのAPI関数 <DllImport("shell32.dll", EntryPoint:="ExtractAssociatedIcon")> _ Private Shared Function ExtractAssociatedIcon _ ( _ ByVal hInst As System.IntPtr, _ <MarshalAs(UnmanagedType.LPStr)> ByVal lpIconPath As String, _ ByRef lpiIcon As Integer _ ) _ As System.IntPtr End Function Dim ico As Icon Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim iconPath As String = "E:\Test.exe" Dim hInst As IntPtr = Marshal.GetHINSTANCE([Assembly].GetExecutingAssembly().GetModules()(0)) Dim iIcon As Int32 Dim hIcon As IntPtr ' ファイルに関連付けられたアイコンのハンドルを取得する hIcon = ExtractAssociatedIcon(hInst, iconPath, iIcon) ' アイコンハンドルからIconオブジェクトを作成する ico = Icon.FromHandle(hIcon) End Sub Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint e.Graphics.DrawIcon(ico, 0, 0) End Sub End Class |
| C# | |
|
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 |
using System.Drawing; using System.Reflection; using System.Runtime.InteropServices; public class Form1 : System.Windows.Forms.Form { // アプリケーションに関連づけられたアイコンを取得するためのAPI関数 [DllImport( "shell32.dll", EntryPoint = "ExtractAssociatedIcon" )] private extern static IntPtr ExtractAssociatedIcon ( IntPtr hInst, [MarshalAs(UnmanagedType.LPStr)] string lpIconPath, ref int lpiIcon ); Icon ico; private void Form1_Load( object sender, System.EventArgs e ) { string iconPath = "E:\\Test.exe"; IntPtr hInst = Marshal.GetHINSTANCE( Assembly.GetExecutingAssembly().GetModules()[0] ); Int32 iIcon = 0; IntPtr hIcon; // ファイルに関連付けられたアイコンのハンドルを取得する hIcon = ExtractAssociatedIcon( hInst, iconPath, ref iIcon ); // アイコンハンドルからIconオブジェクトを作成する ico = Icon.FromHandle( hIcon ); } private void Form1_Paint( object sender, System.Windows.Forms.PaintEventArgs e ) { e.Graphics.DrawIcon( ico, 0, 0 ); } } |
