DoboWiki
Top
> .NETプログラミング研究/37 をテンプレートにして作成
.NETプログラミング研究/37 をテンプレートにして作成
開始行:
#title(.NETプログラミング研究 第37号)
#navi(.NETプログラミング研究)
#contents
*.NETプログラミング研究 第37号 [#sfa919ae]
**.NET質問箱 [#y06cac1c]
「.NET質問箱」では、「どぼん!のプログラミング掲示板」に...
-[[どぼん!のプログラミング掲示板>https://dobon.net/vb/bb...
*** 拡張子に関連付けられた実行ファイルのパスを取得するに...
#column(注意){{
この記事の最新版は「[[拡張子に関連付けられた実行ファイル...
}}
''【質問】''
あるファイルの拡張子に関連付けられている実行ファイルのフ...
''【回答】''
まず、Win32 APIのFindExecutable関数を使う方法が考えられま...
-[[プラットフォーム SDK FindExecutable>http://www.microso...
この場合、次のようなコードで実行ファイルのパスを取得でき...
#code(vbnet){{
Class MainClass
<System.Runtime.InteropServices.DllImport("shell32.dl...
Public Shared Function FindExecutable(ByVal lpFile As...
ByVal lpDirectory As String, _
ByVal lpResult As System.Text.StringBuilder) As I...
End Function
'/ <summary>
'/ エントリポイントです。
'/ </summary>
Public Shared Sub Main()
'関連付けられた実行ファイルを取得するファイル名
Dim fileName As String = "C:\test.txt"
'結果を受け取るためのStringBuilderオブジェクト
Dim exePath As New System.Text.StringBuilder(255)
'fileNameに関連付けられた実行ファイルのパスを取得...
If FindExecutable(fileName, Nothing, exePath) > 3...
'成功した時は、exePathの内容を表示する
Console.WriteLine(exePath.ToString())
Else
Console.WriteLine("失敗しました。")
End If
End Sub
End Class
}}
#code(csharp){{
class MainClass
{
[System.Runtime.InteropServices.DllImport("shell32.dll")]
public static extern int FindExecutable(
string lpFile, string lpDirectory,
System.Text.StringBuilder lpResult);
/// <summary>
/// エントリポイントです。
/// </summary>
public static void Main()
{
//関連付けられた実行ファイルを取得するファイル名
string fileName = "C:\\test.txt";
//結果を受け取るためのStringBuilderオブジェクト
System.Text.StringBuilder exePath =
new System.Text.StringBuilder(255);
//fileNameに関連付けられた実行ファイルのパスを取得する
if (FindExecutable(fileName, null, exePath) > 32)
{
//成功した時は、exePathの内容を表示する
Console.WriteLine(exePath.ToString());
}
else
{
Console.WriteLine("失敗しました。");
}
}
}
}}
しかし残念ながらFindExecutableには、関連付けられた実行フ...
C:\Program Files\Microsoft Office\WINWORD.EXE
であった場合、FindExecutableは、
C:\Program
という文字列を返したかのようになってしまいます。
-[[マイクロソフト サポート技術情報 - 140724: PRB: FindExe...
FindExecutableのようなWin32 APIを使用しなくても、拡張子の...
拡張子の関連付けに関する情報は、レジストリのHKEY_CLASS_RO...
レジストリを調べて拡張子に関連付けられた実行ファイルのパ...
#code(vbnet){{
'/ <summary>
'/ ファイルに関連付けられた実行ファイルのパスを取得する
'/ </summary>
'/ <param name="fileName">関連付けを調べるファイル</param>
'/ <param name="extra">アクション(open,print,editなど)</p...
'/ <returns>実行ファイルのパス + コマンドライン引数</retu...
Public Shared Function FindAssociatedExecutableFile( _
ByVal fileName As String, ByVal extra As String) As S...
'拡張子を取得
Dim extName As String = System.IO.Path.GetExtension(f...
'ファイルタイプを取得
Dim regKey As Microsoft.Win32.RegistryKey = _
Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(e...
If regKey Is Nothing Then
Throw New Exception("見つかりませんでした。")
End If
Dim fileType As String = CStr(regKey.GetValue(""))
regKey.Close()
'「アクションを実行するアプリケーション」を取得
Dim regKey2 As Microsoft.Win32.RegistryKey = _
Microsoft.Win32.Registry.ClassesRoot.OpenSubKey( _
String.Format("{0}\shell\{1}\command", fileType, ...
If regKey2 Is Nothing Then
Throw New Exception("見つかりませんでした。")
End If
Dim command As String = CStr(regKey2.GetValue(""))
regKey2.Close()
Return command
End Function
'/ <summary>
'/ ファイルに関連付けられた実行ファイルのパスを取得する
'/ </summary>
'/ <param name="fileName">関連付けを調べるファイル</param>
'/ <returns>実行ファイルのパス + コマンドライン引数</retu...
Public Shared Function FindAssociatedExecutableFile( _
ByVal fileName As String) As String
Return FindAssociatedExecutableFile(fileName, "open")
End Function
}}
#code(csharp){{
/// <summary>
/// ファイルに関連付けられた実行ファイルのパスを取得する
/// </summary>
/// <param name="fileName">関連付けを調べるファイル</param>
/// <param name="extra">アクション(open,print,editなど)</...
/// <returns>実行ファイルのパス + コマンドライン引数</ret...
public static string FindAssociatedExecutableFile(
string fileName, string extra)
{
//拡張子を取得
string extName = System.IO.Path.GetExtension(fileName);
//ファイルタイプを取得
Microsoft.Win32.RegistryKey regKey =
Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(extName);
if (regKey == null)
throw new Exception("見つかりませんでした。");
string fileType = (string) regKey.GetValue("");
regKey.Close();
//「アクションを実行するアプリケーション」を取得
Microsoft.Win32.RegistryKey regKey2 =
Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(
string.Format(@"{0}\shell\{1}\command", fileType, extra...
if (regKey2 == null)
throw new Exception("見つかりませんでした。");
string command = (string) regKey2.GetValue("");
regKey2.Close();
return command;
}
/// <summary>
/// ファイルに関連付けられた実行ファイルのパスを取得する
/// </summary>
/// <param name="fileName">関連付けを調べるファイル</param>
/// <returns>実行ファイルのパス + コマンドライン引数</ret...
public static string FindAssociatedExecutableFile(
string fileName)
{
return FindAssociatedExecutableFile(fileName, "open");
}
}}
拡張子に関連付けられている実行可能ファイルパスを取得する...
-[[AssocQueryString Function>http://msdn.microsoft.com/li...
○この記事の基になった掲示板のスレッド
-[[関連付けられている実行可能ファイルパスの取得: 投稿者(...
***ピクチャボックスに表示されている画像をドラッグ&ドロッ...
#column(注意){{
この記事の最新版は「[[ピクチャボックスに表示されている画...
}}
''【質問】''
ピクチャボックスに表示されている画像をワードパッドやWord...
''【回答】''
ドラッグ&ドロップの操作を開始するには、コントロールのDoDr...
-[[ドラッグ&ドロップを行う>https://dobon.net/vb/dotnet/co...
例えば、ピクチャボックス"PictureBox1"のImageプロパティに...
#code(vbnet){{
Private Sub PictureBox1_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles PictureBox1.MouseDown
'ドラッグを開始する
PictureBox1.DoDragDrop(PictureBox1.Image, DragDropEff...
End Sub
}}
#code(csharp){{
private void PictureBox1_MouseDown(
object sender, System.Windows.Forms.MouseEventArgs e)
{
//ドラッグを開始する
PictureBox1.DoDragDrop(PictureBox1.Image, DragDropEffect...
}
}}
さらに、画像をアプリケーションにドロップしたときに、画像...
-[[Newsgroups:microsoft.public.dotnet.framework.windowsfo...
この方法によると、まずピクチャボックスに表示されている画...
次にこの方法を使った例を示します。前と同様にピクチャボッ...
#code(vbnet){{
Private Sub PictureBox1_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles PictureBox1.MouseDown
'画像ファイル名の入ったstring型配列を作る
Dim fileNames As String() = {"c:\temp.bmp"}
'ファイルドロップ形式でDataObjectオブジェクトを作成する
Dim dataObj As New DataObject(DataFormats.FileDrop, f...
'さらにビットマップ形式でも格納する
dataObj.SetData(DataFormats.Bitmap, Image.FromFile(fi...
'ドラッグを開始する
PictureBox1.DoDragDrop(dataObj, DragDropEffects.All)
End Sub
}}
#code(csharp){{
private void PictureBox1_MouseDown(
object sender, System.Windows.Forms.MouseEventArgs e)
{
//画像ファイル名の入ったstring型配列を作る
string[] fileNames = {"c:\\temp.bmp"};
//ファイルドロップ形式でDataObjectオブジェクトを作成する
DataObject dataObj =
new DataObject(DataFormats.FileDrop, fileNames);
//さらにビットマップ形式でも格納する
dataObj.SetData(
DataFormats.Bitmap, Image.FromFile(fileNames[0]));
//ドラッグを開始する
PictureBox1.DoDragDrop(dataObj,DragDropEffects.All);
}
}}
この方法でドラッグ&ドロップした場合、ドロップ先がエクスプ...
○この記事の基になった掲示板のスレッド
-[[ドラッグ&ドロップについて。: 投稿者(敬称略) どんちゃ...
***DataGridにLinkLabelを表示するには? [#v3fe5aee]
#column(注意){{
この記事の最新版は「[[DataGridにLinkLabelを表示する>https...
}}
''【質問】''
System.Windows.Forms.DataGridコントロールにLinkLabelを表...
''【回答】''
DataGridColumnStyleクラスから派生した新しいクラスを作成す...
一番簡単で分かりやすいのは、DataGridColumnStyleクラスのEd...
-[[microsoft.public.dotnet.framework.windowsforms: RE: Ho...
-[[DataGridでComboBoxを使う>https://dobon.net/vb/dotnet/d...
この方法はEditメソッドでLinkLabelを表示しているため、セル...
この方法を使った簡単なサンプルを下に示します(かなりの手...
-[[DataGridの列の幅を変更する>https://dobon.net/vb/dotnet...
#code(vbnet){{
Imports System
Imports System.Data
Imports System.Drawing
Imports System.Windows.Forms
Namespace Dobon.Samples.Forms
'/ <summary>
'/ DataGridにLinkLabelを表示するDataGridColumnStyle
'/ </summary>
Public Class DataGridLinkLabelColumn
Inherits DataGridTextBoxColumn
'TextBoxの代わりに表示するLinkLabel
Private _linkLabel As LinkLabel
Public ReadOnly Property LinkLabel() As LinkLabel
Get
Return _linkLabel
End Get
End Property
Public Sub New()
_linkLabel = New LinkLabel
AddHandler _linkLabel.Click, AddressOf _linkL...
End Sub
Protected Overloads Overrides Sub Edit( _
ByVal source As System.Windows.Forms.Currency...
ByVal rowNum As Integer, _
ByVal bounds As System.Drawing.Rectangle, _
ByVal [readOnly] As Boolean, _
ByVal instantText As String, _
ByVal cellIsVisible As Boolean)
MyBase.Edit(source, rowNum, bounds, [readOnly...
instantText, cellIsVisible)
'TextBoxの代わりにLinkLabelを表示する
TextBox.Visible = False
_linkLabel.Parent = TextBox.Parent
_linkLabel.Bounds = bounds
_linkLabel.Text = TextBox.Text
_linkLabel.Visible = True
_linkLabel.BringToFront()
_linkLabel.Focus()
End Sub
Private Sub _linkLabel_Click( _
ByVal sender As Object, ByVal e As EventArgs)
'LinkLabelがクリックされた時は表示されているT...
Dim link As LinkLabel = CType(sender, LinkLab...
System.Diagnostics.Process.Start(link.Text)
End Sub
End Class
End Namespace
}}
#code(csharp){{
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
namespace Dobon.Samples.Forms
{
/// <summary>
/// DataGridにLinkLabelを表示するDataGridColumnStyle
/// </summary>
public class DataGridLinkLabelColumn : DataGridTextBoxCo...
{
//TextBoxの代わりに表示するLinkLabel
private LinkLabel _linkLabel;
public LinkLabel LinkLabel
{
get {return _linkLabel;}
}
public DataGridLinkLabelColumn()
{
_linkLabel = new LinkLabel();
_linkLabel.Click += new EventHandler(_linkLabel_Click);
}
protected override void Edit(
CurrencyManager source,
int rowNum,
Rectangle bounds,
bool readOnly,
string instantText,
bool cellIsVisible)
{
base.Edit(source, rowNum, bounds, readOnly,
instantText, cellIsVisible);
//TextBoxの代わりにLinkLabelを表示する
TextBox.Visible = false;
_linkLabel.Parent = TextBox.Parent;
_linkLabel.Bounds = bounds;
_linkLabel.Text = TextBox.Text;
_linkLabel.Visible = true;
_linkLabel.BringToFront();
_linkLabel.Focus();
}
private void _linkLabel_Click(object sender, EventArgs e)
{
//LinkLabelがクリックされた時は表示されているTextを実...
LinkLabel link = (LinkLabel) sender;
System.Diagnostics.Process.Start(link.Text);
}
}
}
}}
アクティブなセルだけでなく、すべての行にLinkLabelを表示す...
必要な数だけLinkLabelコントロールを作成する例が、ニュース...
-[[microsoft.public.dotnet.framework.windowsforms.control...
この方法は、LinkLabelコントロールを行ごとにHashtableに保...
それが嫌であれば、PaintメソッドでLinkLabelのような文字列...
以下にPaintメソッドで文字列を描画するDataGridColumnStyle...
#code(vbnet){{
Namespace Dobon.Samples.Forms
'/ <summary>
'/ DataGridにLinkLabelを表示するDataGridColumnStyle
'/ </summary>
Public Class DataGridLinkLabelColumn2
Inherits DataGridTextBoxColumn
Private _margin As New Point(1, 2)
Private _visitedLinks As System.Collections.Array...
Private _dataGrid As DataGrid
Public Sub New()
_visitedLinks = New System.Collections.ArrayL...
End Sub
Protected Overloads Overrides Sub Edit( _
ByVal [source] As CurrencyManager, _
ByVal rowNum As Integer, _
ByVal bounds As Rectangle, _
ByVal [readOnly] As Boolean, _
ByVal instantText As String, _
ByVal cellIsVisible As Boolean)
End Sub
'Paintメソッドをオーバーライドする
Protected Overloads Overrides Sub Paint( _
ByVal g As Graphics, _
ByVal bounds As Rectangle, _
ByVal [source] As CurrencyManager, _
ByVal rowNum As Integer, _
ByVal backBrush As Brush, _
ByVal foreBrush As Brush, _
ByVal alignToRight As Boolean)
'表示する文字列を取得
Dim [text] As String = _
GetColumnValueAtRow([source], rowNum).ToS...
Dim sf As New StringFormat
'配置を指定する
Select Case Me.Alignment
Case HorizontalAlignment.Left
sf.Alignment = StringAlignment.Near
Case HorizontalAlignment.Center
sf.Alignment = StringAlignment.Center
Case HorizontalAlignment.Right
sf.Alignment = StringAlignment.Far
End Select
'テキストの方向を指定する
If alignToRight Then
sf.FormatFlags = _
sf.FormatFlags Or _
StringFormatFlags.DirectionRightToLeft
End If
'背景を塗りつぶす
g.FillRectangle(backBrush, bounds)
'前景色を決める
Dim textBrush As Brush
If _visitedLinks.Contains([text]) Then
textBrush = Brushes.Purple
Else
textBrush = Brushes.Blue
End If 'フォントにアンダーラインをつける
Dim textFont As New Font( _
DataGridTableStyle.DataGrid.Font.FontFami...
DataGridTableStyle.DataGrid.Font.Size, _
DataGridTableStyle.DataGrid.Font.Style _
Or FontStyle.Underline)
Dim rectf As New RectangleF( _
bounds.X, bounds.Y, bounds.Width, bounds....
rectf.Inflate(-_margin.X, -_margin.Y)
'文字列を描画する
g.DrawString([text], textFont, textBrush, rec...
sf.Dispose()
textFont.Dispose()
End Sub
Protected Overrides Sub SetDataGridInColumn( _
ByVal value As DataGrid)
MyBase.SetDataGridInColumn(value)
AddHandler value.MouseMove, AddressOf DataGri...
AddHandler value.MouseUp, AddressOf DataGrid_...
_dataGrid = value
End Sub
Protected Overloads Overrides Sub Dispose( _
ByVal disposing As Boolean)
MyBase.Dispose(disposing)
RemoveHandler _dataGrid.MouseMove, _
AddressOf DataGrid_MouseMove
RemoveHandler _dataGrid.MouseUp, _
AddressOf DataGrid_MouseUp
End Sub
Private Sub DataGrid_MouseMove( _
ByVal sender As Object, ByVal e As MouseEvent...
'マウスがセル上にあるときは、カーソルを変更する
Dim hti As DataGrid.HitTestInfo = _
DataGridTableStyle.DataGrid.HitTest(e.X, ...
If hti.Type = DataGrid.HitTestType.Cell And _
hti.Column = _
DataGridTableStyle.GridColumnStyles.I...
DataGridTableStyle.DataGrid.Parent.Cursor...
Cursors.Hand
Else
DataGridTableStyle.DataGrid.Parent.Cursor...
Cursors.Default
End If
End Sub
Private Sub DataGrid_MouseUp( _
ByVal sender As Object, ByVal e As MouseEvent...
Dim grid As DataGrid = DataGridTableStyle.Dat...
Dim info As DataGrid.HitTestInfo = grid.HitTe...
'マウスがセル上にあるか調べる
If info.Type = DataGrid.HitTestType.Cell And ...
DataGridTableStyle.GridColumnStyles.Index...
'Process.Startを呼び出す
Dim cm As CurrencyManager = _
CType(grid.BindingContext( _
grid.DataSource, grid.DataMember), Cu...
Dim str As String = _
GetColumnValueAtRow(cm, info.Row).ToS...
System.Diagnostics.Process.Start(str)
'訪れたことを記憶する
_visitedLinks.Add(str)
End If
End Sub
End Class
End Namespace
}}
#code(csharp){{
namespace Dobon.Samples.Forms
{
/// <summary>
/// DataGridにLinkLabelを表示するDataGridColumnStyle
/// </summary>
public class DataGridLinkLabelColumn2 : DataGridTextBoxC...
{
Point _margin = new Point(1, 2);
private System.Collections.ArrayList _visitedLinks;
DataGrid _dataGrid;
public DataGridLinkLabelColumn2()
{
_visitedLinks = new System.Collections.ArrayList();
}
protected override void Edit(
CurrencyManager source,
int rowNum,
Rectangle bounds,
bool readOnly,
string instantText,
bool cellIsVisible)
{
}
//Paintメソッドをオーバーライドする
protected override void Paint(Graphics g,
Rectangle bounds,
CurrencyManager source,
int rowNum,
Brush backBrush,
Brush foreBrush,
bool alignToRight)
{
//表示する文字列を取得
string text =
GetColumnValueAtRow(source, rowNum).ToString();
StringFormat sf = new StringFormat();
//配置を指定する
switch (this.Alignment)
{
case HorizontalAlignment.Left:
sf.Alignment = StringAlignment.Near;
break;
case HorizontalAlignment.Center:
sf.Alignment = StringAlignment.Center;
break;
case HorizontalAlignment.Right:
sf.Alignment = StringAlignment.Far;
break;
}
//テキストの方向を指定する
if (alignToRight)
sf.FormatFlags |= StringFormatFlags.DirectionRightToL...
//背景を塗りつぶす
g.FillRectangle(backBrush, bounds);
//前景色を決める
Brush textBrush;
if (_visitedLinks.Contains(text))
textBrush = Brushes.Purple;
else
textBrush = Brushes.Blue;
//フォントにアンダーラインをつける
Font textFont =
new Font(DataGridTableStyle.DataGrid.Font.FontFamily,
DataGridTableStyle.DataGrid.Font.Size,
DataGridTableStyle.DataGrid.Font.Style
| FontStyle.Underline);
RectangleF rectf = new RectangleF(
bounds.X, bounds.Y, bounds.Width, bounds.Height);
rectf.Inflate(-_margin.X, -_margin.Y);
//文字列を描画する
g.DrawString(text, textFont,
textBrush, rectf, sf);
sf.Dispose();
textFont.Dispose();
}
protected override void SetDataGridInColumn(DataGrid va...
{
base.SetDataGridInColumn(value);
value.MouseMove +=
new MouseEventHandler(DataGrid_MouseMove);
value.MouseUp +=
new MouseEventHandler(DataGrid_MouseUp);
_dataGrid = value;
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
_dataGrid.MouseMove -=
new MouseEventHandler(DataGrid_MouseMove);
_dataGrid.MouseUp -=
new MouseEventHandler(DataGrid_MouseUp);
}
private void DataGrid_MouseMove(
object sender, MouseEventArgs e)
{
//マウスがセル上にあるときは、カーソルを変更する
DataGrid.HitTestInfo hti =
DataGridTableStyle.DataGrid.HitTest(e.X, e.Y);
if (hti.Type == DataGrid.HitTestType.Cell &&
hti.Column ==
DataGridTableStyle.GridColumnStyles.IndexOf(this))
DataGridTableStyle.DataGrid.Parent.Cursor =
Cursors.Hand;
else
DataGridTableStyle.DataGrid.Parent.Cursor =
Cursors.Default;
}
private void DataGrid_MouseUp(object sender, MouseEvent...
{
DataGrid grid = DataGridTableStyle.DataGrid;
DataGrid.HitTestInfo info = grid.HitTest(e.X, e.Y);
//マウスがセル上にあるか調べる
if (info.Type == DataGrid.HitTestType.Cell
&& info.Column ==
DataGridTableStyle.GridColumnStyles.IndexOf(this))
{
//Process.Startを呼び出す
CurrencyManager cm =
(CurrencyManager) grid.BindingContext[
grid.DataSource, grid.DataMember];
string str = GetColumnValueAtRow(cm, info.Row).ToStri...
System.Diagnostics.Process.Start(str);
//訪れたことを記憶する
_visitedLinks.Add(str);
}
}
}
}
}}
これらの方法に不満があるならば、市販品を使うのがよいでし...
-[[Extended DataGrid>http://dotnet.leadit.be/extendeddata...
○この記事の基になった掲示板のスレッド
-[[DataGridのプロパティ変更)お願いします: 投稿者(敬称略)...
**コンピュータ雑学 [#d1ac873e]
ここでは、話すと人に嫌われるなまぬるいコンピュータに関す...
を紹介します。
***Windows NTの「NT」の意味は? [#t99112c5]
Microsoft Windows NTの「NT」の意味に関して、Microsoftは公...
しかし広く信じられている説には、別のものがあります。それ...
さらには、Windows & .NET Magazine Networkの「WinInfo Dail...
-[[Windows & .NET Magazine Network: WinInfo Daily UPDATE,...
-[[Paul Thurrott's SuperSite for Windows: Windows Server ...
一体どれが真実でどれがただの噂話(あるいは冗談)なのか、...
**コメント [#nf1a4bfa]
#comment
//これより下は編集しないでください
#pageinfo([[:Category/.NET]],2004-07-14 (水) 06:00:00,DOB...
終了行:
#title(.NETプログラミング研究 第37号)
#navi(.NETプログラミング研究)
#contents
*.NETプログラミング研究 第37号 [#sfa919ae]
**.NET質問箱 [#y06cac1c]
「.NET質問箱」では、「どぼん!のプログラミング掲示板」に...
-[[どぼん!のプログラミング掲示板>https://dobon.net/vb/bb...
*** 拡張子に関連付けられた実行ファイルのパスを取得するに...
#column(注意){{
この記事の最新版は「[[拡張子に関連付けられた実行ファイル...
}}
''【質問】''
あるファイルの拡張子に関連付けられている実行ファイルのフ...
''【回答】''
まず、Win32 APIのFindExecutable関数を使う方法が考えられま...
-[[プラットフォーム SDK FindExecutable>http://www.microso...
この場合、次のようなコードで実行ファイルのパスを取得でき...
#code(vbnet){{
Class MainClass
<System.Runtime.InteropServices.DllImport("shell32.dl...
Public Shared Function FindExecutable(ByVal lpFile As...
ByVal lpDirectory As String, _
ByVal lpResult As System.Text.StringBuilder) As I...
End Function
'/ <summary>
'/ エントリポイントです。
'/ </summary>
Public Shared Sub Main()
'関連付けられた実行ファイルを取得するファイル名
Dim fileName As String = "C:\test.txt"
'結果を受け取るためのStringBuilderオブジェクト
Dim exePath As New System.Text.StringBuilder(255)
'fileNameに関連付けられた実行ファイルのパスを取得...
If FindExecutable(fileName, Nothing, exePath) > 3...
'成功した時は、exePathの内容を表示する
Console.WriteLine(exePath.ToString())
Else
Console.WriteLine("失敗しました。")
End If
End Sub
End Class
}}
#code(csharp){{
class MainClass
{
[System.Runtime.InteropServices.DllImport("shell32.dll")]
public static extern int FindExecutable(
string lpFile, string lpDirectory,
System.Text.StringBuilder lpResult);
/// <summary>
/// エントリポイントです。
/// </summary>
public static void Main()
{
//関連付けられた実行ファイルを取得するファイル名
string fileName = "C:\\test.txt";
//結果を受け取るためのStringBuilderオブジェクト
System.Text.StringBuilder exePath =
new System.Text.StringBuilder(255);
//fileNameに関連付けられた実行ファイルのパスを取得する
if (FindExecutable(fileName, null, exePath) > 32)
{
//成功した時は、exePathの内容を表示する
Console.WriteLine(exePath.ToString());
}
else
{
Console.WriteLine("失敗しました。");
}
}
}
}}
しかし残念ながらFindExecutableには、関連付けられた実行フ...
C:\Program Files\Microsoft Office\WINWORD.EXE
であった場合、FindExecutableは、
C:\Program
という文字列を返したかのようになってしまいます。
-[[マイクロソフト サポート技術情報 - 140724: PRB: FindExe...
FindExecutableのようなWin32 APIを使用しなくても、拡張子の...
拡張子の関連付けに関する情報は、レジストリのHKEY_CLASS_RO...
レジストリを調べて拡張子に関連付けられた実行ファイルのパ...
#code(vbnet){{
'/ <summary>
'/ ファイルに関連付けられた実行ファイルのパスを取得する
'/ </summary>
'/ <param name="fileName">関連付けを調べるファイル</param>
'/ <param name="extra">アクション(open,print,editなど)</p...
'/ <returns>実行ファイルのパス + コマンドライン引数</retu...
Public Shared Function FindAssociatedExecutableFile( _
ByVal fileName As String, ByVal extra As String) As S...
'拡張子を取得
Dim extName As String = System.IO.Path.GetExtension(f...
'ファイルタイプを取得
Dim regKey As Microsoft.Win32.RegistryKey = _
Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(e...
If regKey Is Nothing Then
Throw New Exception("見つかりませんでした。")
End If
Dim fileType As String = CStr(regKey.GetValue(""))
regKey.Close()
'「アクションを実行するアプリケーション」を取得
Dim regKey2 As Microsoft.Win32.RegistryKey = _
Microsoft.Win32.Registry.ClassesRoot.OpenSubKey( _
String.Format("{0}\shell\{1}\command", fileType, ...
If regKey2 Is Nothing Then
Throw New Exception("見つかりませんでした。")
End If
Dim command As String = CStr(regKey2.GetValue(""))
regKey2.Close()
Return command
End Function
'/ <summary>
'/ ファイルに関連付けられた実行ファイルのパスを取得する
'/ </summary>
'/ <param name="fileName">関連付けを調べるファイル</param>
'/ <returns>実行ファイルのパス + コマンドライン引数</retu...
Public Shared Function FindAssociatedExecutableFile( _
ByVal fileName As String) As String
Return FindAssociatedExecutableFile(fileName, "open")
End Function
}}
#code(csharp){{
/// <summary>
/// ファイルに関連付けられた実行ファイルのパスを取得する
/// </summary>
/// <param name="fileName">関連付けを調べるファイル</param>
/// <param name="extra">アクション(open,print,editなど)</...
/// <returns>実行ファイルのパス + コマンドライン引数</ret...
public static string FindAssociatedExecutableFile(
string fileName, string extra)
{
//拡張子を取得
string extName = System.IO.Path.GetExtension(fileName);
//ファイルタイプを取得
Microsoft.Win32.RegistryKey regKey =
Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(extName);
if (regKey == null)
throw new Exception("見つかりませんでした。");
string fileType = (string) regKey.GetValue("");
regKey.Close();
//「アクションを実行するアプリケーション」を取得
Microsoft.Win32.RegistryKey regKey2 =
Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(
string.Format(@"{0}\shell\{1}\command", fileType, extra...
if (regKey2 == null)
throw new Exception("見つかりませんでした。");
string command = (string) regKey2.GetValue("");
regKey2.Close();
return command;
}
/// <summary>
/// ファイルに関連付けられた実行ファイルのパスを取得する
/// </summary>
/// <param name="fileName">関連付けを調べるファイル</param>
/// <returns>実行ファイルのパス + コマンドライン引数</ret...
public static string FindAssociatedExecutableFile(
string fileName)
{
return FindAssociatedExecutableFile(fileName, "open");
}
}}
拡張子に関連付けられている実行可能ファイルパスを取得する...
-[[AssocQueryString Function>http://msdn.microsoft.com/li...
○この記事の基になった掲示板のスレッド
-[[関連付けられている実行可能ファイルパスの取得: 投稿者(...
***ピクチャボックスに表示されている画像をドラッグ&ドロッ...
#column(注意){{
この記事の最新版は「[[ピクチャボックスに表示されている画...
}}
''【質問】''
ピクチャボックスに表示されている画像をワードパッドやWord...
''【回答】''
ドラッグ&ドロップの操作を開始するには、コントロールのDoDr...
-[[ドラッグ&ドロップを行う>https://dobon.net/vb/dotnet/co...
例えば、ピクチャボックス"PictureBox1"のImageプロパティに...
#code(vbnet){{
Private Sub PictureBox1_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles PictureBox1.MouseDown
'ドラッグを開始する
PictureBox1.DoDragDrop(PictureBox1.Image, DragDropEff...
End Sub
}}
#code(csharp){{
private void PictureBox1_MouseDown(
object sender, System.Windows.Forms.MouseEventArgs e)
{
//ドラッグを開始する
PictureBox1.DoDragDrop(PictureBox1.Image, DragDropEffect...
}
}}
さらに、画像をアプリケーションにドロップしたときに、画像...
-[[Newsgroups:microsoft.public.dotnet.framework.windowsfo...
この方法によると、まずピクチャボックスに表示されている画...
次にこの方法を使った例を示します。前と同様にピクチャボッ...
#code(vbnet){{
Private Sub PictureBox1_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles PictureBox1.MouseDown
'画像ファイル名の入ったstring型配列を作る
Dim fileNames As String() = {"c:\temp.bmp"}
'ファイルドロップ形式でDataObjectオブジェクトを作成する
Dim dataObj As New DataObject(DataFormats.FileDrop, f...
'さらにビットマップ形式でも格納する
dataObj.SetData(DataFormats.Bitmap, Image.FromFile(fi...
'ドラッグを開始する
PictureBox1.DoDragDrop(dataObj, DragDropEffects.All)
End Sub
}}
#code(csharp){{
private void PictureBox1_MouseDown(
object sender, System.Windows.Forms.MouseEventArgs e)
{
//画像ファイル名の入ったstring型配列を作る
string[] fileNames = {"c:\\temp.bmp"};
//ファイルドロップ形式でDataObjectオブジェクトを作成する
DataObject dataObj =
new DataObject(DataFormats.FileDrop, fileNames);
//さらにビットマップ形式でも格納する
dataObj.SetData(
DataFormats.Bitmap, Image.FromFile(fileNames[0]));
//ドラッグを開始する
PictureBox1.DoDragDrop(dataObj,DragDropEffects.All);
}
}}
この方法でドラッグ&ドロップした場合、ドロップ先がエクスプ...
○この記事の基になった掲示板のスレッド
-[[ドラッグ&ドロップについて。: 投稿者(敬称略) どんちゃ...
***DataGridにLinkLabelを表示するには? [#v3fe5aee]
#column(注意){{
この記事の最新版は「[[DataGridにLinkLabelを表示する>https...
}}
''【質問】''
System.Windows.Forms.DataGridコントロールにLinkLabelを表...
''【回答】''
DataGridColumnStyleクラスから派生した新しいクラスを作成す...
一番簡単で分かりやすいのは、DataGridColumnStyleクラスのEd...
-[[microsoft.public.dotnet.framework.windowsforms: RE: Ho...
-[[DataGridでComboBoxを使う>https://dobon.net/vb/dotnet/d...
この方法はEditメソッドでLinkLabelを表示しているため、セル...
この方法を使った簡単なサンプルを下に示します(かなりの手...
-[[DataGridの列の幅を変更する>https://dobon.net/vb/dotnet...
#code(vbnet){{
Imports System
Imports System.Data
Imports System.Drawing
Imports System.Windows.Forms
Namespace Dobon.Samples.Forms
'/ <summary>
'/ DataGridにLinkLabelを表示するDataGridColumnStyle
'/ </summary>
Public Class DataGridLinkLabelColumn
Inherits DataGridTextBoxColumn
'TextBoxの代わりに表示するLinkLabel
Private _linkLabel As LinkLabel
Public ReadOnly Property LinkLabel() As LinkLabel
Get
Return _linkLabel
End Get
End Property
Public Sub New()
_linkLabel = New LinkLabel
AddHandler _linkLabel.Click, AddressOf _linkL...
End Sub
Protected Overloads Overrides Sub Edit( _
ByVal source As System.Windows.Forms.Currency...
ByVal rowNum As Integer, _
ByVal bounds As System.Drawing.Rectangle, _
ByVal [readOnly] As Boolean, _
ByVal instantText As String, _
ByVal cellIsVisible As Boolean)
MyBase.Edit(source, rowNum, bounds, [readOnly...
instantText, cellIsVisible)
'TextBoxの代わりにLinkLabelを表示する
TextBox.Visible = False
_linkLabel.Parent = TextBox.Parent
_linkLabel.Bounds = bounds
_linkLabel.Text = TextBox.Text
_linkLabel.Visible = True
_linkLabel.BringToFront()
_linkLabel.Focus()
End Sub
Private Sub _linkLabel_Click( _
ByVal sender As Object, ByVal e As EventArgs)
'LinkLabelがクリックされた時は表示されているT...
Dim link As LinkLabel = CType(sender, LinkLab...
System.Diagnostics.Process.Start(link.Text)
End Sub
End Class
End Namespace
}}
#code(csharp){{
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
namespace Dobon.Samples.Forms
{
/// <summary>
/// DataGridにLinkLabelを表示するDataGridColumnStyle
/// </summary>
public class DataGridLinkLabelColumn : DataGridTextBoxCo...
{
//TextBoxの代わりに表示するLinkLabel
private LinkLabel _linkLabel;
public LinkLabel LinkLabel
{
get {return _linkLabel;}
}
public DataGridLinkLabelColumn()
{
_linkLabel = new LinkLabel();
_linkLabel.Click += new EventHandler(_linkLabel_Click);
}
protected override void Edit(
CurrencyManager source,
int rowNum,
Rectangle bounds,
bool readOnly,
string instantText,
bool cellIsVisible)
{
base.Edit(source, rowNum, bounds, readOnly,
instantText, cellIsVisible);
//TextBoxの代わりにLinkLabelを表示する
TextBox.Visible = false;
_linkLabel.Parent = TextBox.Parent;
_linkLabel.Bounds = bounds;
_linkLabel.Text = TextBox.Text;
_linkLabel.Visible = true;
_linkLabel.BringToFront();
_linkLabel.Focus();
}
private void _linkLabel_Click(object sender, EventArgs e)
{
//LinkLabelがクリックされた時は表示されているTextを実...
LinkLabel link = (LinkLabel) sender;
System.Diagnostics.Process.Start(link.Text);
}
}
}
}}
アクティブなセルだけでなく、すべての行にLinkLabelを表示す...
必要な数だけLinkLabelコントロールを作成する例が、ニュース...
-[[microsoft.public.dotnet.framework.windowsforms.control...
この方法は、LinkLabelコントロールを行ごとにHashtableに保...
それが嫌であれば、PaintメソッドでLinkLabelのような文字列...
以下にPaintメソッドで文字列を描画するDataGridColumnStyle...
#code(vbnet){{
Namespace Dobon.Samples.Forms
'/ <summary>
'/ DataGridにLinkLabelを表示するDataGridColumnStyle
'/ </summary>
Public Class DataGridLinkLabelColumn2
Inherits DataGridTextBoxColumn
Private _margin As New Point(1, 2)
Private _visitedLinks As System.Collections.Array...
Private _dataGrid As DataGrid
Public Sub New()
_visitedLinks = New System.Collections.ArrayL...
End Sub
Protected Overloads Overrides Sub Edit( _
ByVal [source] As CurrencyManager, _
ByVal rowNum As Integer, _
ByVal bounds As Rectangle, _
ByVal [readOnly] As Boolean, _
ByVal instantText As String, _
ByVal cellIsVisible As Boolean)
End Sub
'Paintメソッドをオーバーライドする
Protected Overloads Overrides Sub Paint( _
ByVal g As Graphics, _
ByVal bounds As Rectangle, _
ByVal [source] As CurrencyManager, _
ByVal rowNum As Integer, _
ByVal backBrush As Brush, _
ByVal foreBrush As Brush, _
ByVal alignToRight As Boolean)
'表示する文字列を取得
Dim [text] As String = _
GetColumnValueAtRow([source], rowNum).ToS...
Dim sf As New StringFormat
'配置を指定する
Select Case Me.Alignment
Case HorizontalAlignment.Left
sf.Alignment = StringAlignment.Near
Case HorizontalAlignment.Center
sf.Alignment = StringAlignment.Center
Case HorizontalAlignment.Right
sf.Alignment = StringAlignment.Far
End Select
'テキストの方向を指定する
If alignToRight Then
sf.FormatFlags = _
sf.FormatFlags Or _
StringFormatFlags.DirectionRightToLeft
End If
'背景を塗りつぶす
g.FillRectangle(backBrush, bounds)
'前景色を決める
Dim textBrush As Brush
If _visitedLinks.Contains([text]) Then
textBrush = Brushes.Purple
Else
textBrush = Brushes.Blue
End If 'フォントにアンダーラインをつける
Dim textFont As New Font( _
DataGridTableStyle.DataGrid.Font.FontFami...
DataGridTableStyle.DataGrid.Font.Size, _
DataGridTableStyle.DataGrid.Font.Style _
Or FontStyle.Underline)
Dim rectf As New RectangleF( _
bounds.X, bounds.Y, bounds.Width, bounds....
rectf.Inflate(-_margin.X, -_margin.Y)
'文字列を描画する
g.DrawString([text], textFont, textBrush, rec...
sf.Dispose()
textFont.Dispose()
End Sub
Protected Overrides Sub SetDataGridInColumn( _
ByVal value As DataGrid)
MyBase.SetDataGridInColumn(value)
AddHandler value.MouseMove, AddressOf DataGri...
AddHandler value.MouseUp, AddressOf DataGrid_...
_dataGrid = value
End Sub
Protected Overloads Overrides Sub Dispose( _
ByVal disposing As Boolean)
MyBase.Dispose(disposing)
RemoveHandler _dataGrid.MouseMove, _
AddressOf DataGrid_MouseMove
RemoveHandler _dataGrid.MouseUp, _
AddressOf DataGrid_MouseUp
End Sub
Private Sub DataGrid_MouseMove( _
ByVal sender As Object, ByVal e As MouseEvent...
'マウスがセル上にあるときは、カーソルを変更する
Dim hti As DataGrid.HitTestInfo = _
DataGridTableStyle.DataGrid.HitTest(e.X, ...
If hti.Type = DataGrid.HitTestType.Cell And _
hti.Column = _
DataGridTableStyle.GridColumnStyles.I...
DataGridTableStyle.DataGrid.Parent.Cursor...
Cursors.Hand
Else
DataGridTableStyle.DataGrid.Parent.Cursor...
Cursors.Default
End If
End Sub
Private Sub DataGrid_MouseUp( _
ByVal sender As Object, ByVal e As MouseEvent...
Dim grid As DataGrid = DataGridTableStyle.Dat...
Dim info As DataGrid.HitTestInfo = grid.HitTe...
'マウスがセル上にあるか調べる
If info.Type = DataGrid.HitTestType.Cell And ...
DataGridTableStyle.GridColumnStyles.Index...
'Process.Startを呼び出す
Dim cm As CurrencyManager = _
CType(grid.BindingContext( _
grid.DataSource, grid.DataMember), Cu...
Dim str As String = _
GetColumnValueAtRow(cm, info.Row).ToS...
System.Diagnostics.Process.Start(str)
'訪れたことを記憶する
_visitedLinks.Add(str)
End If
End Sub
End Class
End Namespace
}}
#code(csharp){{
namespace Dobon.Samples.Forms
{
/// <summary>
/// DataGridにLinkLabelを表示するDataGridColumnStyle
/// </summary>
public class DataGridLinkLabelColumn2 : DataGridTextBoxC...
{
Point _margin = new Point(1, 2);
private System.Collections.ArrayList _visitedLinks;
DataGrid _dataGrid;
public DataGridLinkLabelColumn2()
{
_visitedLinks = new System.Collections.ArrayList();
}
protected override void Edit(
CurrencyManager source,
int rowNum,
Rectangle bounds,
bool readOnly,
string instantText,
bool cellIsVisible)
{
}
//Paintメソッドをオーバーライドする
protected override void Paint(Graphics g,
Rectangle bounds,
CurrencyManager source,
int rowNum,
Brush backBrush,
Brush foreBrush,
bool alignToRight)
{
//表示する文字列を取得
string text =
GetColumnValueAtRow(source, rowNum).ToString();
StringFormat sf = new StringFormat();
//配置を指定する
switch (this.Alignment)
{
case HorizontalAlignment.Left:
sf.Alignment = StringAlignment.Near;
break;
case HorizontalAlignment.Center:
sf.Alignment = StringAlignment.Center;
break;
case HorizontalAlignment.Right:
sf.Alignment = StringAlignment.Far;
break;
}
//テキストの方向を指定する
if (alignToRight)
sf.FormatFlags |= StringFormatFlags.DirectionRightToL...
//背景を塗りつぶす
g.FillRectangle(backBrush, bounds);
//前景色を決める
Brush textBrush;
if (_visitedLinks.Contains(text))
textBrush = Brushes.Purple;
else
textBrush = Brushes.Blue;
//フォントにアンダーラインをつける
Font textFont =
new Font(DataGridTableStyle.DataGrid.Font.FontFamily,
DataGridTableStyle.DataGrid.Font.Size,
DataGridTableStyle.DataGrid.Font.Style
| FontStyle.Underline);
RectangleF rectf = new RectangleF(
bounds.X, bounds.Y, bounds.Width, bounds.Height);
rectf.Inflate(-_margin.X, -_margin.Y);
//文字列を描画する
g.DrawString(text, textFont,
textBrush, rectf, sf);
sf.Dispose();
textFont.Dispose();
}
protected override void SetDataGridInColumn(DataGrid va...
{
base.SetDataGridInColumn(value);
value.MouseMove +=
new MouseEventHandler(DataGrid_MouseMove);
value.MouseUp +=
new MouseEventHandler(DataGrid_MouseUp);
_dataGrid = value;
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
_dataGrid.MouseMove -=
new MouseEventHandler(DataGrid_MouseMove);
_dataGrid.MouseUp -=
new MouseEventHandler(DataGrid_MouseUp);
}
private void DataGrid_MouseMove(
object sender, MouseEventArgs e)
{
//マウスがセル上にあるときは、カーソルを変更する
DataGrid.HitTestInfo hti =
DataGridTableStyle.DataGrid.HitTest(e.X, e.Y);
if (hti.Type == DataGrid.HitTestType.Cell &&
hti.Column ==
DataGridTableStyle.GridColumnStyles.IndexOf(this))
DataGridTableStyle.DataGrid.Parent.Cursor =
Cursors.Hand;
else
DataGridTableStyle.DataGrid.Parent.Cursor =
Cursors.Default;
}
private void DataGrid_MouseUp(object sender, MouseEvent...
{
DataGrid grid = DataGridTableStyle.DataGrid;
DataGrid.HitTestInfo info = grid.HitTest(e.X, e.Y);
//マウスがセル上にあるか調べる
if (info.Type == DataGrid.HitTestType.Cell
&& info.Column ==
DataGridTableStyle.GridColumnStyles.IndexOf(this))
{
//Process.Startを呼び出す
CurrencyManager cm =
(CurrencyManager) grid.BindingContext[
grid.DataSource, grid.DataMember];
string str = GetColumnValueAtRow(cm, info.Row).ToStri...
System.Diagnostics.Process.Start(str);
//訪れたことを記憶する
_visitedLinks.Add(str);
}
}
}
}
}}
これらの方法に不満があるならば、市販品を使うのがよいでし...
-[[Extended DataGrid>http://dotnet.leadit.be/extendeddata...
○この記事の基になった掲示板のスレッド
-[[DataGridのプロパティ変更)お願いします: 投稿者(敬称略)...
**コンピュータ雑学 [#d1ac873e]
ここでは、話すと人に嫌われるなまぬるいコンピュータに関す...
を紹介します。
***Windows NTの「NT」の意味は? [#t99112c5]
Microsoft Windows NTの「NT」の意味に関して、Microsoftは公...
しかし広く信じられている説には、別のものがあります。それ...
さらには、Windows & .NET Magazine Networkの「WinInfo Dail...
-[[Windows & .NET Magazine Network: WinInfo Daily UPDATE,...
-[[Paul Thurrott's SuperSite for Windows: Windows Server ...
一体どれが真実でどれがただの噂話(あるいは冗談)なのか、...
**コメント [#nf1a4bfa]
#comment
//これより下は編集しないでください
#pageinfo([[:Category/.NET]],2004-07-14 (水) 06:00:00,DOB...
ページ名:
▲
▼
[
トップ
] [
新規
|
子ページ作成
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
]