やりたいこと
wordで web上にある 写真フレームの素材を使って年賀状をつくりたい。
いままで
paint.netをつかって四苦八苦しながらやってました。
ここをさんこうにしたらできた。。。
http://cp.c-ij.com/ja/otasuke/nenga/howto/word2010.html#no3
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace GetMyComputerIcon
{
public partial class Form1 : Form
{
private int nIndex = 0;
public Form1()
{
InitializeComponent();
}
//===================
// ボタン押下時のイベントハンドラ
//===================
private void button1_Click(object sender, EventArgs e)
{
IntPtr hImgSmall; //the handle to the system image list
IntPtr hImgLarge; //the handle to the system image list
SHFILEINFO shinfo = new SHFILEINFO();
listView1.SmallImageList = imageList1;
listView1.LargeImageList = imageList1;
FolderID startLocation = FolderID.MyComputer;
IntPtr pidlRoot = IntPtr.Zero;
Win32.SHGetSpecialFolderLocation(this.Handle, (int)startLocation, out pidlRoot);
//Use this to get the small Icon
hImgSmall = Win32.SHGetFileInfo(pidlRoot, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON | Win32.SHGFI_PIDL);
//Use this to get the large Icon
//hImgLarge = Win32.SHGetFileInfo(pidlRoot, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON | Win32.SHGFI_PIDL);
//The icon is returned in the hIcon member of the shinfo struct
System.Drawing.Icon myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
imageList1.Images.Add(myIcon);
//Add file name and icon to listview
listView1.Items.Add("my computer", nIndex++);
// pidlを解放する。
if (pidlRoot != IntPtr.Zero)
{
Win32.IMalloc malloc;
int nret = Win32.SHGetMalloc(out malloc);
malloc.Free(pidlRoot);
}
}
}
[StructLayout(LayoutKind.Sequential)]
public struct SHFILEINFO
{
public IntPtr hIcon;
public IntPtr iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
//===================
// win 32 api dll import
//===================
class Win32
{
// SHGetFileInfo用のフラグ。
public const uint SHGFI_ICON = 0x100;
public const uint SHGFI_LARGEICON = 0x0; // 'Large icon
public const uint SHGFI_SMALLICON = 0x1; // 'Small icon
public const uint SHGFI_PIDL = 0x8; // pidl
//[DllImport("shell32.dll")] こちらはパスからアイコン取得するときに必要。今回は不要。
//public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
[DllImport("shell32.dll")] // これが重要。
public static extern IntPtr SHGetFileInfo(IntPtr pIDL, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
[DllImport("Shell32.DLL")] // こちらで特殊フォルダのpidlを取得する。
public static extern int SHGetSpecialFolderLocation(IntPtr hwndOwner, int nFolder, out IntPtr ppidl);
// pidlのfree用
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
Guid("00000002-0000-0000-C000-000000000046")]
public interface IMalloc
{
[PreserveSig]
IntPtr Alloc([In] int cb);
[PreserveSig]
IntPtr Realloc([In] IntPtr pv, [In] int cb);
[PreserveSig]
void Free([In] IntPtr pv);
[PreserveSig]
int GetSize([In] IntPtr pv);
[PreserveSig]
int DidAlloc(IntPtr pv);
[PreserveSig]
void HeapMinimize();
}
[DllImport("Shell32.DLL")]
public static extern int SHGetMalloc(out IMalloc ppMalloc);
}
//===================
// SHGetSpecialFolderLocationに使用するFolderのIDの定義。
// ここをかえるといろいろな特殊フォルダpidlが取得できる。
//===================
public enum FolderID
{
Desktop = 0x0000,
Printers = 0x0004,
MyDocuments = 0x0005,
Favorites = 0x0006,
Recent = 0x0008,
SendTo = 0x0009,
StartMenu = 0x000b,
MyComputer = 0x0011,
NetworkNeighborhood = 0x0012,
Templates = 0x0015,
MyPictures = 0x0027,
NetAndDialUpConnections = 0x0031,
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace GetIconSample
{
public partial class Form1 : Form
{
private int nIndex = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
IntPtr hImgSmall; //the handle to the system image list
IntPtr hImgLarge; //the handle to the system image list
string fName; // 'the file name to get icon from
SHFILEINFO shinfo = new SHFILEINFO();
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\temp\\";
openFileDialog1.Filter = "All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
listView1.SmallImageList = imageList1;
listView1.LargeImageList = imageList1;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
fName = openFileDialog1.FileName;
//Use this to get the small Icon
hImgSmall = Win32.SHGetFileInfo(fName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON);
//Use this to get the large Icon
//hImgLarge = Win32.SHGetFileInfo(fName, 0,
// ref shinfo, (uint)Marshal.SizeOf(shinfo),
// Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON);
//The icon is returned in the hIcon member of the shinfo struct
System.Drawing.Icon myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
imageList1.Images.Add(myIcon);
//Add file name and icon to listview
listView1.Items.Add(fName, nIndex++);
}
}
}
[StructLayout(LayoutKind.Sequential)]
public struct SHFILEINFO
{
public IntPtr hIcon;
public IntPtr iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
class Win32
{
public const uint SHGFI_ICON = 0x100;
public const uint SHGFI_LARGEICON = 0x0; // 'Large icon
public const uint SHGFI_SMALLICON = 0x1; // 'Small icon
[DllImport("shell32.dll")]
public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
}
}
PaperSize pkSize;
for (int i = 0; i < printDoc.PrinterSettings.PaperSizes.Count; i++ )
{
pkSize = printDoc.PrinterSettings.PaperSizes[i];
comboPaperSize.Items.Add(pkSize);
}
■変更後のソース
PaperSize[] pkSizearr = new PaperSize[printDoc.PrinterSettings.PaperSizes.Count]; printDoc.PrinterSettings.PaperSizes.CopyTo(pkSizearr, 0); comboPaperSize.Items.AddRange(pkSizearr);★ついでにコレクションから配列に変換する方法もわかったので、メモ
コレクション.CopyTo(配列, 0)
use strict; use warnings; use Text::CSV_XS; #------------------------- open(FH," < team.csv"); my @list =; close(FH); my $csv = new Text::CSV_XS({binary => 1}); my %hash; foreach my $line( @list ) { $csv->parse($line) || die "parse失敗: " . $csv->error_input() . "\n"; my ( $key, @items ) = $csv->fields(); $hash{$key} = [@items]; } printarrayhash( %hash ); #------------------------- sub printarrayhash { my %hash = @_; foreach my $key( keys( %hash ) ) { print "キー値 : $key\n"; my @val = @{$hash{$key}}; my $length = @val; print "length =$length\n"; print "val = @val\n"; } }
my %hash;
foreach my $line( @list )
{
chomp $line;
my ( $key, @items ) = split( /,/, $line );
$hash{$key} = [@items];
}
use strict; use warnings; #------------------------- open(FH," < team.csv"); my @list =; close(FH); my %hash; foreach my $line( @list ) { chomp $line; my ( $key, @items ) = split( /,/, $line ); $hash{$key} = [@items]; } printhash( %hash ); #------------------------- sub printhash { my %hash = @_; foreach my $key( keys( %hash ) ) { print "キー値 : $key\n"; my @val = @{$hash{$key}}; my $length = @val; print "length =$length\n"; print "val = @val\n"; } }
%Hash = () ;・値を代入
$Hash{key} = value;
■その他#コメントです。・変数
$abc # $をつけると変数となる。
use strict; use warnings;