DoboWiki
Top
> .NETプログラミング研究/47 をテンプレートにして作成
.NETプログラミング研究/47 をテンプレートにして作成
開始行:
#title(.NETプログラミング研究 第47号)
#navi(.NETプログラミング研究)
#contents
*.NETプログラミング研究 第47号 [#we300dd9]
**.NET質問箱 [#jf18bb3f]
「.NET質問箱」では、「どぼん!のプログラミング掲示板」に...
-[[どぼん!のプログラミング掲示板>https://dobon.net/vb/bb...
***通常使うプリンタの取得と設定 [#m12448a4]
#column(注意){{
この記事の最新版は「[[通常使うプリンタを取得、設定する>ht...
}}
''【質問】''
「通常使うプリンタ」に指定されているプリンタ名はどのよう...
''【回答】''
「通常使うプリンタ」に指定されているプリンタ名は、新しくP...
#code(vbnet){{
'PrintDocumentの作成
Dim pd As New System.Drawing.Printing.PrintDocument
'プリンタ名の取得
Dim defaultPrinterName As String = pd.PrinterSettings.Pri...
'結果を表示
Console.WriteLine("通常使用するプリンタは " +
defaultPrinterName + " です。")
}}
#code(csharp){{
//PrintDocumentの作成
System.Drawing.Printing.PrintDocument pd =
new System.Drawing.Printing.PrintDocument();
//プリンタ名の取得
string defaultPrinterName = pd.PrinterSettings.PrinterName;
//結果を表示
Console.WriteLine(
"通常使用するプリンタは " + defaultPrinterName + " で...
}}
また、WMIを使用してプリンタを列挙し、デフォルトのプリンタ...
-[[Win32_Printer>http://msdn.microsoft.com/library/en-us/...
#code(vbnet){{
Dim mos As New System.Management.ManagementObjectSearcher...
"Select * from Win32_Printer")
Dim moc As System.Management.ManagementObjectCollection = _
mos.Get()
'プリンタを列挙する
Dim mo As System.Management.ManagementObject
For Each mo In moc
'デフォルトのプリンタか調べる
'mo("Default")はWindows NT 4.0, 2000で使用できません
Dim attr As Long = Convert.ToInt64(mo("Attributes"))
If (attr And 4) = 4 Then
Console.WriteLine(("通常使用するプリンタは " + _
mo("Name").ToString() + " です。"))
Exit For
End If
Next mo
}}
#code(csharp){{
System.Management.ManagementObjectSearcher mos =
new System.Management.ManagementObjectSearcher(
"Select * from Win32_Printer");
System.Management.ManagementObjectCollection moc =
mos.Get();
//プリンタを列挙する
foreach(System.Management.ManagementObject mo in moc)
{
//デフォルトのプリンタか調べる
//mo["Default"]はWindows NT 4.0, 2000で使用できません
if ((((uint) mo["Attributes"]) & 4) == 4)
{
Console.WriteLine(
"通常使用するプリンタは " + mo["Name"] + " で...
break;
}
}
}}
次に「通常使うプリンタ」を設定する方法を紹介します。
まず、WMIを使用した方法を紹介します。ただしこの方法はWind...
#code(vbnet){{
'/ <summary>
'/ 「通常使用するプリンタ」に設定する
'/ </summary>
'/ <param name="printerName">プリンタ名</param>
Public Sub SetDefaultPrinter(ByVal printerName As String)
Dim mos As New System.Management.ManagementObjectSear...
"Select * from Win32_Printer")
Dim moc As System.Management.ManagementObjectCollecti...
mos.Get()
'プリンタを列挙する
Dim mo As System.Management.ManagementObject
For Each mo In moc
If CStr(mo("Name")) = printerName Then
'名前を見つけたとき、デフォルトプリンタに設定...
Dim mbo As System.Management.ManagementBaseOb...
mo.InvokeMethod("SetDefaultPrinter", Noth...
Dim ret As Long = Convert.ToInt64(mbo("return...
If ret <> 0 Then
Throw New Exception("失敗しました。")
End If
End If
Next mo
End Sub
}}
#code(csharp){{
/// <summary>
/// 「通常使用するプリンタ」に設定する
/// </summary>
/// <param name="printerName">プリンタ名</param>
public void SetDefaultPrinter(string printerName)
{
System.Management.ManagementObjectSearcher mos =
new System.Management.ManagementObjectSearcher(
"Select * from Win32_Printer");
System.Management.ManagementObjectCollection moc =
mos.Get();
//プリンタを列挙する
foreach(System.Management.ManagementObject mo in moc)
{
if (((string) mo["Name"]) == printerName)
{
//名前を見つけたとき、デフォルトプリンタに設...
System.Management.ManagementBaseObject mbo =
mo.InvokeMethod("SetDefaultPrinter", null...
if (((uint) mbo["returnValue"]) != 0)
throw new Exception("失敗しました。");
}
}
}
}}
また、Windows Scripting HostのWshNetworkオブジェクトのSet...
-[[SetDefaultPrinter メソッド>http://www.microsoft.com/ja...
#code(vbnet){{
'/ <summary>
'/ 「通常使用するプリンタ」に設定する
'/ </summary>
'/ <param name="printerName">プリンタ名</param>
Public Sub SetDefaultPrinter(ByVal printerName As String)
'WshNetworkオブジェクトを作成する
Dim t As Type = Type.GetTypeFromProgID("WScript.Netwo...
Dim wshNetwork As Object = Activator.CreateInstance(t)
'SetDefaultPrinterメソッドを呼び出す
t.InvokeMember("SetDefaultPrinter", _
System.Reflection.BindingFlags.InvokeMethod, _
Nothing, wshNetwork, New Object() {printerName})
End Sub
}}
#code(csharp){{
/// <summary>
/// 「通常使用するプリンタ」に設定する
/// </summary>
/// <param name="printerName">プリンタ名</param>
public void SetDefaultPrinter(string printerName)
{
//WshNetworkオブジェクトを作成する
Type t = Type.GetTypeFromProgID("WScript.Network");
object wshNetwork = Activator.CreateInstance(t);
//SetDefaultPrinterメソッドを呼び出す
t.InvokeMember("SetDefaultPrinter",
System.Reflection.BindingFlags.InvokeMethod,
null, wshNetwork, new object[] {printerName});
}
}}
上記で紹介した方法以外に通常使うプリンタを取得するためにG...
-[[[HOWTO] Windows の通常使うプリンタを取得および設定する...
GetPrinter関数に関しては、次の紹介する「プリンタのポート...
○この記事の基になった掲示板のスレッド
-[[通常使うプリンタについて質問 | 投稿者(敬称略) PASA, 管...
-[[通常使うプリンタの取得 | 投稿者(敬称略) tab, 管理人>ht...
***プリンタのポート、状態の取得 [#a17f7ee6]
#column(注意){{
この記事の最新版は「[[プリンタのポート、状態を取得する>ht...
}}
''【質問】''
プリンタのポートや現在の状態に関する情報を取得したいので...
''【回答】''
「通常使うプリンタの取得と設定」と同じように、WMIのWin32_...
また、P/InvokeでGetPrinter関数を呼び出すことにより、プリ...
下にプリンタの情報をPRINTER_INFO_2構造体で取得するための...
-[[PRINTER_INFO_2>http://msdn.microsoft.com/library/en-us...
なおこのコードを書くために次の記事を参考にしました。
-[[Newsgroups:microsoft.public.dotnet.framework.interop |...
-[[The Code Project - Changing printer settings using C#>...
#code(vbnet){{
<DllImport("winspool.drv", EntryPoint:="OpenPrinterW", _
CharSet:=CharSet.Auto, SetLastError:=True, ExactSpelling:...
CallingConvention:=CallingConvention.StdCall)> _
Private Shared Function OpenPrinter( _
ByVal pPrinterName As String, ByRef hPrinter As IntPt...
ByVal pDefault As IntPtr) As Boolean
End Function
<DllImport("winspool.drv", CharSet:=CharSet.Auto, _
SetLastError:=True, ExactSpelling:=True, _
CallingConvention:=CallingConvention.StdCall)> _
Private Shared Function ClosePrinter( _
ByVal hPrinter As IntPtr) As Boolean
End Function
<DllImport("winspool.drv", EntryPoint:="GetPrinterW", _
CharSet:=CharSet.Auto, SetLastError:=True, ExactSpelling:...
CallingConvention:=CallingConvention.StdCall)> _
Private Shared Function GetPrinter( _
ByVal hPrinter As IntPtr, ByVal dwLevel As Integer, _
ByVal pPrinter As IntPtr, ByVal cbBuf As Integer, _
ByRef pcbNeeded As Integer) As Boolean
End Function
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Aut...
Public Structure PRINTER_INFO_2
Public pServerName As String
Public pPrinterName As String
Public pShareName As String
Public pPortName As String
Public pDriverName As String
Public pComment As String
Public pLocation As String
Public pDevMode As IntPtr
Public pSepFile As String
Public pPrintProcessor As String
Public pDatatype As String
Public pParameters As String
Public pSecurityDescriptor As IntPtr
Public Attributes As System.UInt32
Public Priority As System.UInt32
Public DefaultPriority As System.UInt32
Public StartTime As System.UInt32
Public UntilTime As System.UInt32
Public Status As System.UInt32
Public cJobs As System.UInt32
Public AveragePPM As System.UInt32
End Structure
'/ <summary>
'/ プリンタの情報をPRINTER_INFO_2で取得する
'/ </summary>
'/ <param name="printerName">プリンタ名</param>
'/ <returns>プリンタの情報</returns>
Public Shared Function GetPrinterInfo( _
ByVal printerName As String) As PRINTER_INFO_2
'プリンタのハンドルを取得する
Dim hPrinter As IntPtr
If Not OpenPrinter(printerName, hPrinter, IntPtr.Zero...
Throw New Win32Exception(Marshal.GetLastWin32Erro...
End If
Dim pPrinterInfo As IntPtr = IntPtr.Zero
Try
'必要なバイト数を取得する
Dim needed As Integer
GetPrinter(hPrinter, 2, IntPtr.Zero, 0, needed)
If needed <= 0 Then
Throw New Exception("失敗しました。")
End If
'メモリを割り当てる
pPrinterInfo = Marshal.AllocHGlobal(needed)
'プリンタ情報を取得する
Dim temp As Integer
If Not GetPrinter(hPrinter, 2, pPrinterInfo, need...
Throw New Win32Exception(Marshal.GetLastWin32...
End If
'PRINTER_INFO_2型にマーシャリングする
Dim printerInfo As PRINTER_INFO_2 = _
CType(Marshal.PtrToStructure( _
pPrinterInfo, GetType(PRINTER_INFO_2)), PRINT...
'結果を返す
Return printerInfo
Finally
'後始末をする
ClosePrinter(hPrinter)
Marshal.FreeHGlobal(pPrinterInfo)
End Try
End Function
}}
#code(csharp){{
[DllImport("winspool.drv", EntryPoint="OpenPrinterW",
CharSet=CharSet.Auto, SetLastError=true, ExactSpellin...
CallingConvention=CallingConvention.StdCall)]
private static extern bool OpenPrinter(string pPrinterName,
out IntPtr hPrinter, IntPtr pDefault);
[DllImport("winspool.drv", CharSet=CharSet.Auto, SetLastE...
ExactSpelling=true, CallingConvention=CallingConventi...
private static extern bool ClosePrinter(IntPtr hPrinter);
[DllImport("winspool.drv", EntryPoint="GetPrinterW",
CharSet=CharSet.Auto, SetLastError=true, ExactSpellin...
CallingConvention=CallingConvention.StdCall)]
private static extern bool GetPrinter(IntPtr hPrinter, in...
IntPtr pPrinter, int cbBuf, out int pcbNeeded);
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public struct PRINTER_INFO_2
{
public string pServerName;
public string pPrinterName;
public string pShareName;
public string pPortName;
public string pDriverName;
public string pComment;
public string pLocation;
public IntPtr pDevMode;
public string pSepFile;
public string pPrintProcessor;
public string pDatatype;
public string pParameters;
public IntPtr pSecurityDescriptor;
public uint Attributes;
public uint Priority;
public uint DefaultPriority;
public uint StartTime;
public uint UntilTime;
public uint Status;
public uint cJobs;
public uint AveragePPM;
}
/// <summary>
/// プリンタの情報をPRINTER_INFO_2で取得する
/// </summary>
/// <param name="printerName">プリンタ名</param>
/// <returns>プリンタの情報</returns>
public static PRINTER_INFO_2 GetPrinterInfo(string printe...
{
//プリンタのハンドルを取得する
IntPtr hPrinter;
if (!OpenPrinter(printerName, out hPrinter, IntPtr.Ze...
{
throw new Win32Exception(Marshal.GetLastWin32Erro...
}
IntPtr pPrinterInfo = IntPtr.Zero;
try
{
//必要なバイト数を取得する
int needed;
GetPrinter(hPrinter, 2, IntPtr.Zero, 0, out neede...
if (needed <= 0)
throw new Exception("失敗しました。");
//メモリを割り当てる
pPrinterInfo = Marshal.AllocHGlobal(needed);
//プリンタ情報を取得する
int temp;
if (!GetPrinter(hPrinter, 2, pPrinterInfo, needed...
{
throw new Win32Exception(Marshal.GetLastWin32...
}
//PRINTER_INFO_2型にマーシャリングする
PRINTER_INFO_2 printerInfo =
(PRINTER_INFO_2) Marshal.PtrToStructure(pPrin...
typeof(PRINTER_INFO_2));
//結果を返す
return printerInfo;
}
finally
{
//後始末をする
ClosePrinter(hPrinter);
Marshal.FreeHGlobal(pPrinterInfo);
}
}
}}
GetPrinterInfoメソッドを使ってプリンタのポートと状態を取...
#code(vbnet){{
'プリンタ名
Dim printerName As String = "PRINTER NAME"
'プリンタ情報を取得する
Dim pinfo As PRINTER_INFO_2 = GetPrinterInfo(printerName)
'ポートを表示する
Console.WriteLine(("Port:" + pinfo.pPortName))
'状態を表示する
Console.WriteLine(("Status:" + pinfo.Status.ToString()))
}}
#code(csharp){{
//プリンタ名
string printerName = "PRINTER NAME";
//プリンタ情報を取得する
PRINTER_INFO_2 pinfo = GetPrinterInfo(printerName);
//ポートを表示する
Console.WriteLine("Port:" + pinfo.pPortName);
//状態を表示する
Console.WriteLine("Status:" + pinfo.Status.ToString());
}}
○この記事の基になった掲示板のスレッド
-[[プリンターポートの取得方法について | 投稿者(敬称略) パ...
***エクスプローラへファイルをドラッグ&ドロップする [#m993...
#column(注意){{
この記事の最新版は「[[エクスプローラへファイルをDrag&Drop...
}}
''【質問】''
ListBoxコントロールのリストにファイル名を追加し、リストか...
''【回答】''
エクスプローラへのドラッグ&ドロップも通常のドラッグ&ドロ...
-[[DOBON.NET .NET Tips - ドラッグ&ドロップを行う>https://...
ただしDoDragDropメソッドを呼び出す時、データ形式にDataFor...
以下にリストボックスからエクスプローラへのファイルのドラ...
#code(vbnet){{
'フォームのLoadイベントハンドラ
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As EventArgs) Handles MyBase.Load
'MouseDownイベントハンドラを追加
AddHandler ListBox1.MouseDown, AddressOf ListBox1_Mou...
'ListBox1に"C:\"にあるファイルを表示する
ListBox1.Items.AddRange( _
System.IO.Directory.GetFiles("C:\", "*.*"))
End Sub
'ListBox1のMouseDownイベントハンドラ
Private Sub ListBox1_MouseDown(ByVal sender As Object, _
ByVal e As MouseEventArgs)
Dim lbx As ListBox = CType(sender, ListBox)
'ドラッグするファイル名を取得する
Dim itemIndex As Integer = lbx.IndexFromPoint(e.X, e.Y)
If itemIndex < 0 Then
Return
End If
Dim files As String() = {CStr(lbx.Items(itemIndex))}
'DataObjectを作成する
Dim dataObj As New DataObject(DataFormats.FileDrop, f...
'ドラッグを開始する
Dim dde As DragDropEffects = _
lbx.DoDragDrop(dataObj, DragDropEffects.Copy)
End Sub
}}
#code(csharp){{
//フォームのLoadイベントハンドラ
private void Form1_Load(object sender, System.EventArgs e)
{
//MouseDownイベントハンドラを追加
ListBox1.MouseDown += new MouseEventHandler(ListBox1_...
//ListBox1に"C:\"にあるファイルを表示する
ListBox1.Items.AddRange(
System.IO.Directory.GetFiles("C:\\", "*.*"));
}
//ListBox1のMouseDownイベントハンドラ
private void ListBox1_MouseDown(object sender, MouseEvent...
{
ListBox lbx = (ListBox) sender;
//ドラッグするファイル名を取得する
int itemIndex = lbx.IndexFromPoint(e.X, e.Y);
if (itemIndex < 0) return;
string[] files = {(string) lbx.Items[itemIndex]};
//DataObjectを作成する
DataObject dataObj = new DataObject(DataFormats.FileD...
//ドラッグを開始する
DragDropEffects dde =
lbx.DoDragDrop(dataObj, DragDropEffects.Copy);
}
}}
なお、ファイルを移動するためにDoDragDropメソッドでDragDro...
○この記事の基になった掲示板のスレッド
-[[VB.Netで作成したアプリからエクスプローラへのドラッグ&...
-[[listBoxでエクスプローラーのドラッグ&ドロップの実現方法...
-[[エクスプローラへのドラッグ&ドロップで移動された場合の...
***ツールバーに画像が表示されない [#uea10adc]
#column(注意){{
この記事の最新版は「[[ToolBarに画像が表示されない問題の解...
}}
''【質問】''
Visual Studio .NETのフォームデザイナでImageListを使ってツ...
''【回答】''
Application.EnableVisualStylesメソッドを使用している時は...
-[[DOBON.NET .NET Tips - コントロールの外観をXPスタイルに...
さらに、EnableVisualStylesメソッドを使用していなくてもツ...
-[[Newsgroups:microsoft.public.dotnet.languages.csharp | ...
この記事によるとこの問題の解決法は、コードエディタで該当...
ツールバーに画像が表示されなくて困っている方は一度おため...
○この記事の基になった掲示板のスレッド
-[[ツールバーのアイコンが表示されない | 投稿者(敬称略) Mo...
**コメント [#b1ee81cd]
#comment
//これより下は編集しないでください
#pageinfo([[:Category/.NET]],2004-12-09 (木) 06:00:00,DOB...
終了行:
#title(.NETプログラミング研究 第47号)
#navi(.NETプログラミング研究)
#contents
*.NETプログラミング研究 第47号 [#we300dd9]
**.NET質問箱 [#jf18bb3f]
「.NET質問箱」では、「どぼん!のプログラミング掲示板」に...
-[[どぼん!のプログラミング掲示板>https://dobon.net/vb/bb...
***通常使うプリンタの取得と設定 [#m12448a4]
#column(注意){{
この記事の最新版は「[[通常使うプリンタを取得、設定する>ht...
}}
''【質問】''
「通常使うプリンタ」に指定されているプリンタ名はどのよう...
''【回答】''
「通常使うプリンタ」に指定されているプリンタ名は、新しくP...
#code(vbnet){{
'PrintDocumentの作成
Dim pd As New System.Drawing.Printing.PrintDocument
'プリンタ名の取得
Dim defaultPrinterName As String = pd.PrinterSettings.Pri...
'結果を表示
Console.WriteLine("通常使用するプリンタは " +
defaultPrinterName + " です。")
}}
#code(csharp){{
//PrintDocumentの作成
System.Drawing.Printing.PrintDocument pd =
new System.Drawing.Printing.PrintDocument();
//プリンタ名の取得
string defaultPrinterName = pd.PrinterSettings.PrinterName;
//結果を表示
Console.WriteLine(
"通常使用するプリンタは " + defaultPrinterName + " で...
}}
また、WMIを使用してプリンタを列挙し、デフォルトのプリンタ...
-[[Win32_Printer>http://msdn.microsoft.com/library/en-us/...
#code(vbnet){{
Dim mos As New System.Management.ManagementObjectSearcher...
"Select * from Win32_Printer")
Dim moc As System.Management.ManagementObjectCollection = _
mos.Get()
'プリンタを列挙する
Dim mo As System.Management.ManagementObject
For Each mo In moc
'デフォルトのプリンタか調べる
'mo("Default")はWindows NT 4.0, 2000で使用できません
Dim attr As Long = Convert.ToInt64(mo("Attributes"))
If (attr And 4) = 4 Then
Console.WriteLine(("通常使用するプリンタは " + _
mo("Name").ToString() + " です。"))
Exit For
End If
Next mo
}}
#code(csharp){{
System.Management.ManagementObjectSearcher mos =
new System.Management.ManagementObjectSearcher(
"Select * from Win32_Printer");
System.Management.ManagementObjectCollection moc =
mos.Get();
//プリンタを列挙する
foreach(System.Management.ManagementObject mo in moc)
{
//デフォルトのプリンタか調べる
//mo["Default"]はWindows NT 4.0, 2000で使用できません
if ((((uint) mo["Attributes"]) & 4) == 4)
{
Console.WriteLine(
"通常使用するプリンタは " + mo["Name"] + " で...
break;
}
}
}}
次に「通常使うプリンタ」を設定する方法を紹介します。
まず、WMIを使用した方法を紹介します。ただしこの方法はWind...
#code(vbnet){{
'/ <summary>
'/ 「通常使用するプリンタ」に設定する
'/ </summary>
'/ <param name="printerName">プリンタ名</param>
Public Sub SetDefaultPrinter(ByVal printerName As String)
Dim mos As New System.Management.ManagementObjectSear...
"Select * from Win32_Printer")
Dim moc As System.Management.ManagementObjectCollecti...
mos.Get()
'プリンタを列挙する
Dim mo As System.Management.ManagementObject
For Each mo In moc
If CStr(mo("Name")) = printerName Then
'名前を見つけたとき、デフォルトプリンタに設定...
Dim mbo As System.Management.ManagementBaseOb...
mo.InvokeMethod("SetDefaultPrinter", Noth...
Dim ret As Long = Convert.ToInt64(mbo("return...
If ret <> 0 Then
Throw New Exception("失敗しました。")
End If
End If
Next mo
End Sub
}}
#code(csharp){{
/// <summary>
/// 「通常使用するプリンタ」に設定する
/// </summary>
/// <param name="printerName">プリンタ名</param>
public void SetDefaultPrinter(string printerName)
{
System.Management.ManagementObjectSearcher mos =
new System.Management.ManagementObjectSearcher(
"Select * from Win32_Printer");
System.Management.ManagementObjectCollection moc =
mos.Get();
//プリンタを列挙する
foreach(System.Management.ManagementObject mo in moc)
{
if (((string) mo["Name"]) == printerName)
{
//名前を見つけたとき、デフォルトプリンタに設...
System.Management.ManagementBaseObject mbo =
mo.InvokeMethod("SetDefaultPrinter", null...
if (((uint) mbo["returnValue"]) != 0)
throw new Exception("失敗しました。");
}
}
}
}}
また、Windows Scripting HostのWshNetworkオブジェクトのSet...
-[[SetDefaultPrinter メソッド>http://www.microsoft.com/ja...
#code(vbnet){{
'/ <summary>
'/ 「通常使用するプリンタ」に設定する
'/ </summary>
'/ <param name="printerName">プリンタ名</param>
Public Sub SetDefaultPrinter(ByVal printerName As String)
'WshNetworkオブジェクトを作成する
Dim t As Type = Type.GetTypeFromProgID("WScript.Netwo...
Dim wshNetwork As Object = Activator.CreateInstance(t)
'SetDefaultPrinterメソッドを呼び出す
t.InvokeMember("SetDefaultPrinter", _
System.Reflection.BindingFlags.InvokeMethod, _
Nothing, wshNetwork, New Object() {printerName})
End Sub
}}
#code(csharp){{
/// <summary>
/// 「通常使用するプリンタ」に設定する
/// </summary>
/// <param name="printerName">プリンタ名</param>
public void SetDefaultPrinter(string printerName)
{
//WshNetworkオブジェクトを作成する
Type t = Type.GetTypeFromProgID("WScript.Network");
object wshNetwork = Activator.CreateInstance(t);
//SetDefaultPrinterメソッドを呼び出す
t.InvokeMember("SetDefaultPrinter",
System.Reflection.BindingFlags.InvokeMethod,
null, wshNetwork, new object[] {printerName});
}
}}
上記で紹介した方法以外に通常使うプリンタを取得するためにG...
-[[[HOWTO] Windows の通常使うプリンタを取得および設定する...
GetPrinter関数に関しては、次の紹介する「プリンタのポート...
○この記事の基になった掲示板のスレッド
-[[通常使うプリンタについて質問 | 投稿者(敬称略) PASA, 管...
-[[通常使うプリンタの取得 | 投稿者(敬称略) tab, 管理人>ht...
***プリンタのポート、状態の取得 [#a17f7ee6]
#column(注意){{
この記事の最新版は「[[プリンタのポート、状態を取得する>ht...
}}
''【質問】''
プリンタのポートや現在の状態に関する情報を取得したいので...
''【回答】''
「通常使うプリンタの取得と設定」と同じように、WMIのWin32_...
また、P/InvokeでGetPrinter関数を呼び出すことにより、プリ...
下にプリンタの情報をPRINTER_INFO_2構造体で取得するための...
-[[PRINTER_INFO_2>http://msdn.microsoft.com/library/en-us...
なおこのコードを書くために次の記事を参考にしました。
-[[Newsgroups:microsoft.public.dotnet.framework.interop |...
-[[The Code Project - Changing printer settings using C#>...
#code(vbnet){{
<DllImport("winspool.drv", EntryPoint:="OpenPrinterW", _
CharSet:=CharSet.Auto, SetLastError:=True, ExactSpelling:...
CallingConvention:=CallingConvention.StdCall)> _
Private Shared Function OpenPrinter( _
ByVal pPrinterName As String, ByRef hPrinter As IntPt...
ByVal pDefault As IntPtr) As Boolean
End Function
<DllImport("winspool.drv", CharSet:=CharSet.Auto, _
SetLastError:=True, ExactSpelling:=True, _
CallingConvention:=CallingConvention.StdCall)> _
Private Shared Function ClosePrinter( _
ByVal hPrinter As IntPtr) As Boolean
End Function
<DllImport("winspool.drv", EntryPoint:="GetPrinterW", _
CharSet:=CharSet.Auto, SetLastError:=True, ExactSpelling:...
CallingConvention:=CallingConvention.StdCall)> _
Private Shared Function GetPrinter( _
ByVal hPrinter As IntPtr, ByVal dwLevel As Integer, _
ByVal pPrinter As IntPtr, ByVal cbBuf As Integer, _
ByRef pcbNeeded As Integer) As Boolean
End Function
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Aut...
Public Structure PRINTER_INFO_2
Public pServerName As String
Public pPrinterName As String
Public pShareName As String
Public pPortName As String
Public pDriverName As String
Public pComment As String
Public pLocation As String
Public pDevMode As IntPtr
Public pSepFile As String
Public pPrintProcessor As String
Public pDatatype As String
Public pParameters As String
Public pSecurityDescriptor As IntPtr
Public Attributes As System.UInt32
Public Priority As System.UInt32
Public DefaultPriority As System.UInt32
Public StartTime As System.UInt32
Public UntilTime As System.UInt32
Public Status As System.UInt32
Public cJobs As System.UInt32
Public AveragePPM As System.UInt32
End Structure
'/ <summary>
'/ プリンタの情報をPRINTER_INFO_2で取得する
'/ </summary>
'/ <param name="printerName">プリンタ名</param>
'/ <returns>プリンタの情報</returns>
Public Shared Function GetPrinterInfo( _
ByVal printerName As String) As PRINTER_INFO_2
'プリンタのハンドルを取得する
Dim hPrinter As IntPtr
If Not OpenPrinter(printerName, hPrinter, IntPtr.Zero...
Throw New Win32Exception(Marshal.GetLastWin32Erro...
End If
Dim pPrinterInfo As IntPtr = IntPtr.Zero
Try
'必要なバイト数を取得する
Dim needed As Integer
GetPrinter(hPrinter, 2, IntPtr.Zero, 0, needed)
If needed <= 0 Then
Throw New Exception("失敗しました。")
End If
'メモリを割り当てる
pPrinterInfo = Marshal.AllocHGlobal(needed)
'プリンタ情報を取得する
Dim temp As Integer
If Not GetPrinter(hPrinter, 2, pPrinterInfo, need...
Throw New Win32Exception(Marshal.GetLastWin32...
End If
'PRINTER_INFO_2型にマーシャリングする
Dim printerInfo As PRINTER_INFO_2 = _
CType(Marshal.PtrToStructure( _
pPrinterInfo, GetType(PRINTER_INFO_2)), PRINT...
'結果を返す
Return printerInfo
Finally
'後始末をする
ClosePrinter(hPrinter)
Marshal.FreeHGlobal(pPrinterInfo)
End Try
End Function
}}
#code(csharp){{
[DllImport("winspool.drv", EntryPoint="OpenPrinterW",
CharSet=CharSet.Auto, SetLastError=true, ExactSpellin...
CallingConvention=CallingConvention.StdCall)]
private static extern bool OpenPrinter(string pPrinterName,
out IntPtr hPrinter, IntPtr pDefault);
[DllImport("winspool.drv", CharSet=CharSet.Auto, SetLastE...
ExactSpelling=true, CallingConvention=CallingConventi...
private static extern bool ClosePrinter(IntPtr hPrinter);
[DllImport("winspool.drv", EntryPoint="GetPrinterW",
CharSet=CharSet.Auto, SetLastError=true, ExactSpellin...
CallingConvention=CallingConvention.StdCall)]
private static extern bool GetPrinter(IntPtr hPrinter, in...
IntPtr pPrinter, int cbBuf, out int pcbNeeded);
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public struct PRINTER_INFO_2
{
public string pServerName;
public string pPrinterName;
public string pShareName;
public string pPortName;
public string pDriverName;
public string pComment;
public string pLocation;
public IntPtr pDevMode;
public string pSepFile;
public string pPrintProcessor;
public string pDatatype;
public string pParameters;
public IntPtr pSecurityDescriptor;
public uint Attributes;
public uint Priority;
public uint DefaultPriority;
public uint StartTime;
public uint UntilTime;
public uint Status;
public uint cJobs;
public uint AveragePPM;
}
/// <summary>
/// プリンタの情報をPRINTER_INFO_2で取得する
/// </summary>
/// <param name="printerName">プリンタ名</param>
/// <returns>プリンタの情報</returns>
public static PRINTER_INFO_2 GetPrinterInfo(string printe...
{
//プリンタのハンドルを取得する
IntPtr hPrinter;
if (!OpenPrinter(printerName, out hPrinter, IntPtr.Ze...
{
throw new Win32Exception(Marshal.GetLastWin32Erro...
}
IntPtr pPrinterInfo = IntPtr.Zero;
try
{
//必要なバイト数を取得する
int needed;
GetPrinter(hPrinter, 2, IntPtr.Zero, 0, out neede...
if (needed <= 0)
throw new Exception("失敗しました。");
//メモリを割り当てる
pPrinterInfo = Marshal.AllocHGlobal(needed);
//プリンタ情報を取得する
int temp;
if (!GetPrinter(hPrinter, 2, pPrinterInfo, needed...
{
throw new Win32Exception(Marshal.GetLastWin32...
}
//PRINTER_INFO_2型にマーシャリングする
PRINTER_INFO_2 printerInfo =
(PRINTER_INFO_2) Marshal.PtrToStructure(pPrin...
typeof(PRINTER_INFO_2));
//結果を返す
return printerInfo;
}
finally
{
//後始末をする
ClosePrinter(hPrinter);
Marshal.FreeHGlobal(pPrinterInfo);
}
}
}}
GetPrinterInfoメソッドを使ってプリンタのポートと状態を取...
#code(vbnet){{
'プリンタ名
Dim printerName As String = "PRINTER NAME"
'プリンタ情報を取得する
Dim pinfo As PRINTER_INFO_2 = GetPrinterInfo(printerName)
'ポートを表示する
Console.WriteLine(("Port:" + pinfo.pPortName))
'状態を表示する
Console.WriteLine(("Status:" + pinfo.Status.ToString()))
}}
#code(csharp){{
//プリンタ名
string printerName = "PRINTER NAME";
//プリンタ情報を取得する
PRINTER_INFO_2 pinfo = GetPrinterInfo(printerName);
//ポートを表示する
Console.WriteLine("Port:" + pinfo.pPortName);
//状態を表示する
Console.WriteLine("Status:" + pinfo.Status.ToString());
}}
○この記事の基になった掲示板のスレッド
-[[プリンターポートの取得方法について | 投稿者(敬称略) パ...
***エクスプローラへファイルをドラッグ&ドロップする [#m993...
#column(注意){{
この記事の最新版は「[[エクスプローラへファイルをDrag&Drop...
}}
''【質問】''
ListBoxコントロールのリストにファイル名を追加し、リストか...
''【回答】''
エクスプローラへのドラッグ&ドロップも通常のドラッグ&ドロ...
-[[DOBON.NET .NET Tips - ドラッグ&ドロップを行う>https://...
ただしDoDragDropメソッドを呼び出す時、データ形式にDataFor...
以下にリストボックスからエクスプローラへのファイルのドラ...
#code(vbnet){{
'フォームのLoadイベントハンドラ
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As EventArgs) Handles MyBase.Load
'MouseDownイベントハンドラを追加
AddHandler ListBox1.MouseDown, AddressOf ListBox1_Mou...
'ListBox1に"C:\"にあるファイルを表示する
ListBox1.Items.AddRange( _
System.IO.Directory.GetFiles("C:\", "*.*"))
End Sub
'ListBox1のMouseDownイベントハンドラ
Private Sub ListBox1_MouseDown(ByVal sender As Object, _
ByVal e As MouseEventArgs)
Dim lbx As ListBox = CType(sender, ListBox)
'ドラッグするファイル名を取得する
Dim itemIndex As Integer = lbx.IndexFromPoint(e.X, e.Y)
If itemIndex < 0 Then
Return
End If
Dim files As String() = {CStr(lbx.Items(itemIndex))}
'DataObjectを作成する
Dim dataObj As New DataObject(DataFormats.FileDrop, f...
'ドラッグを開始する
Dim dde As DragDropEffects = _
lbx.DoDragDrop(dataObj, DragDropEffects.Copy)
End Sub
}}
#code(csharp){{
//フォームのLoadイベントハンドラ
private void Form1_Load(object sender, System.EventArgs e)
{
//MouseDownイベントハンドラを追加
ListBox1.MouseDown += new MouseEventHandler(ListBox1_...
//ListBox1に"C:\"にあるファイルを表示する
ListBox1.Items.AddRange(
System.IO.Directory.GetFiles("C:\\", "*.*"));
}
//ListBox1のMouseDownイベントハンドラ
private void ListBox1_MouseDown(object sender, MouseEvent...
{
ListBox lbx = (ListBox) sender;
//ドラッグするファイル名を取得する
int itemIndex = lbx.IndexFromPoint(e.X, e.Y);
if (itemIndex < 0) return;
string[] files = {(string) lbx.Items[itemIndex]};
//DataObjectを作成する
DataObject dataObj = new DataObject(DataFormats.FileD...
//ドラッグを開始する
DragDropEffects dde =
lbx.DoDragDrop(dataObj, DragDropEffects.Copy);
}
}}
なお、ファイルを移動するためにDoDragDropメソッドでDragDro...
○この記事の基になった掲示板のスレッド
-[[VB.Netで作成したアプリからエクスプローラへのドラッグ&...
-[[listBoxでエクスプローラーのドラッグ&ドロップの実現方法...
-[[エクスプローラへのドラッグ&ドロップで移動された場合の...
***ツールバーに画像が表示されない [#uea10adc]
#column(注意){{
この記事の最新版は「[[ToolBarに画像が表示されない問題の解...
}}
''【質問】''
Visual Studio .NETのフォームデザイナでImageListを使ってツ...
''【回答】''
Application.EnableVisualStylesメソッドを使用している時は...
-[[DOBON.NET .NET Tips - コントロールの外観をXPスタイルに...
さらに、EnableVisualStylesメソッドを使用していなくてもツ...
-[[Newsgroups:microsoft.public.dotnet.languages.csharp | ...
この記事によるとこの問題の解決法は、コードエディタで該当...
ツールバーに画像が表示されなくて困っている方は一度おため...
○この記事の基になった掲示板のスレッド
-[[ツールバーのアイコンが表示されない | 投稿者(敬称略) Mo...
**コメント [#b1ee81cd]
#comment
//これより下は編集しないでください
#pageinfo([[:Category/.NET]],2004-12-09 (木) 06:00:00,DOB...
ページ名:
▲
▼
[
トップ
] [
新規
|
子ページ作成
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
]