DoboWiki
Top
> .NETプログラミング研究/46 をテンプレートにして作成
.NETプログラミング研究/46 をテンプレートにして作成
開始行:
#title(.NETプログラミング研究 第46号)
#navi(.NETプログラミング研究)
#contents
*.NETプログラミング研究 第46号 [#bf80fa86]
**.NET質問箱 [#x2280b7c]
「.NET質問箱」では、「どぼん!のプログラミング掲示板」に...
-[[どぼん!のプログラミング掲示板>https://dobon.net/vb/bb...
***ListViewのColumnの順番を取得、設定するには? [#pb5e3966]
#column(注意){{
この記事の最新版は「[[ListViewのColumnの順番を取得、設定...
}}
''【質問】''
ListViewコントロールのAllowColumnReorderプロパティをTrue...
''【回答】''
ListViewコントロールのColumnの順番を取得する方法は、残念...
この問題の解決法として「どぼん!のプログラミング掲示板」...
LVM_GETCOLUMNORDERARRAYメッセージを使用した場合、Columnの...
以下にその具体例を示します。ここでは、WindowsフォームにLi...
#code(vbnet){{
Private Declare Auto Function SendMessage Lib "user32.dll...
ByVal hWnd As IntPtr, ByVal msg As Integer, _
ByVal wParam As Integer, ByVal lParam() As Integer) A...
Private Const LVM_FIRST As Integer = &H1000
Private Const LVM_GETCOLUMNORDERARRAY As Integer = LVM_FI...
'/ <summary>
'/ ListViewの列ヘッダの順番を取得する
'/ </summary>
'/ <param name="lv">対象とするListView</param>
'/ <returns>列ヘッダの順番を示した配列</returns>
Private Shared Function GetColumnHeaderOrder( _
ByVal lv As ListView) As Integer()
Dim count As Integer = lv.Columns.Count
Dim order(count) As Integer
If SendMessage(lv.Handle, LVM_GETCOLUMNORDERARRAY, _
count, order) = 0 Then
Throw New ApplicationException( _
"Columnの順番の取得に失敗しました。")
End If
Return order
End Function
}}
#code(csharp){{
[DllImport("user32.dll", CharSet=CharSet.Auto)]
private static extern int SendMessage(
IntPtr hWnd, int msg, int wParam, int [] lParam);
private const int LVM_FIRST = 0x1000;
private const int LVM_GETCOLUMNORDERARRAY = (LVM_FIRST + ...
/// <summary>
/// ListViewの列ヘッダの順番を取得する
/// </summary>
/// <param name="lv">対象とするListView</param>
/// <returns>列ヘッダの順番を示した配列</returns>
private static int[] GetColumnHeaderOrder(ListView lv)
{
int count = lv.Columns.Count;
int[] order = new int[count];
if (SendMessage(lv.Handle, LVM_GETCOLUMNORDERARRAY, c...
== 0)
throw new ApplicationException(
"Columnの順番の取得に失敗しました。");
return order;
}
//Button1のクリックイベントハンドラ
private void Button1_Click(object sender, System.EventArg...
{
//列ヘッダの順番を取得
int[] orders = GetColumnHeaderOrder(ListView1);
//列ヘッダのTextとその順番を表示する
for (int i = 0; i < ListView1.Columns.Count; i++)
Console.WriteLine(ListView1.Columns[i].Text +
":Position=" + orders[i].ToString());
}
}}
次にListViewコントロールのColumnの順番を設定する方法を紹...
これは、ListViewコントロールにColumnHeaderを追加するとき...
以下にLVM_SETCOLUMNORDERARRAYメッセージを使ってListViewコ...
#code(vbnet){{
Private Declare Auto Function SendMessage Lib "user32.dll...
ByVal hWnd As IntPtr, ByVal msg As Integer, _
ByVal wParam As Integer, ByVal lParam() As Integer) A...
Private Const LVM_FIRST As Integer = &H1000
Private Const LVM_SETCOLUMNORDERARRAY As Integer = LVM_FI...
'/ <summary>
'/ ListViewの列ヘッダの順番を設定する
'/ </summary>
'/ <param name="lv">対象とするListView</param>
'/ <param name="order">列ヘッダの順番を示した配列</param>
Private Shared Sub SetColumnHeaderOrder( _
ByVal lv As ListView, ByVal order() As Integer)
If SendMessage(lv.Handle, LVM_SETCOLUMNORDERARRAY, _
order.Length, order) = 0 Then
Throw New ApplicationException( _
"Columnの順番の設定に失敗しました。")
End If
End Sub
'Button1のクリックイベントハンドラ
Private Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
'列ヘッダの順番を配列に入れる
Dim orders(ListView1.Columns.Count - 1) As Integer
Dim i As Integer
For i = 0 To orders.Length - 1
orders(i) = i
Next i
'列ヘッダの順番を設定する
SetColumnHeaderOrder(ListView1, orders)
End Sub
}}
#code(csharp){{
[DllImport("user32.dll", CharSet=CharSet.Auto)]
private static extern int SendMessage(
IntPtr hWnd, int msg, int wParam, int [] lParam);
private const int LVM_FIRST = 0x1000;
private const int LVM_SETCOLUMNORDERARRAY = (LVM_FIRST + ...
/// <summary>
/// ListViewの列ヘッダの順番を設定する
/// </summary>
/// <param name="lv">対象とするListView</param>
/// <param name="order">列ヘッダの順番を示した配列</param>
private static void SetColumnHeaderOrder(ListView lv, int...
{
if (SendMessage(lv.Handle, LVM_SETCOLUMNORDERARRAY,
order.Length, order) == 0)
throw new ApplicationException(
"Columnの順番の設定に失敗しました。");
}
//Button1のクリックイベントハンドラ
private void Button1_Click(object sender, System.EventArg...
{
//列ヘッダの順番を配列に入れる
int[] orders = new int[ListView1.Columns.Count];
for (int i = 0; i < orders.Length; i++)
orders[i] = i;
//列ヘッダの順番を設定する
SetColumnHeaderOrder(ListView1, orders);
}
}}
○この記事の基になった掲示板のスレッド
-[[ListViewで列順の変更を保持する方法 | 投稿者(敬称略) こ...
***画像をクリックして拡大、縮小表示できるようにするには? ...
#column(注意){{
この記事の最新版は「[[ピクチャボックスの画像をクリックし...
}}
''【質問】''
ピクチャボックスに表示した画像をマウスクリックにより、ク...
''【回答】''
ここでは、ピクチャボックスのPaintイベントでGraphics.DrawI...
それでは実際に作成してみましょう。仕様としては、テキスト...
まずフォーム(Form1)にピクチャボックス(PictureBox1)と...
これで準備は完了です。後はForm1クラスに以下のようなコード...
#code(vbnet){{
'表示する画像
Private img As Bitmap
'倍率
Private ratio As Single = 1.0F
'画像を表示させる範囲
Private imgRect As Rectangle
'Button1のクリックイベントハンドラ
Private Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
'表示する画像を読み込む
img = New Bitmap(TextBox1.Text)
'初期化
imgRect = New Rectangle(0, 0, img.Width, img.Height)
ratio = 1.0F
'画像を表示する
PictureBox1.Invalidate()
End Sub
'PictureBox1のMouseDownイベントハンドラ
Private Sub PictureBox1_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles PictureBox1.MouseDown
Dim pb As PictureBox = CType(sender, PictureBox)
'クリックされた位置を画像上の位置に変換
Dim imgPoint As New Point(CInt((e.X - imgRect.X) / ra...
CInt((e.Y - imgRect.Y) / ratio))
'倍率を変更する
If e.Button = MouseButtons.Left Then
ratio *= 2.0F
Else
If e.Button = MouseButtons.Right Then
ratio *= 0.5F
End If
End If
'画像の表示範囲を計算する
imgRect.Width = CInt(Math.Round((img.Width * ratio)))
imgRect.Height = CInt(Math.Round((img.Height * ratio)))
imgRect.X = CInt(Math.Round((pb.Width / 2 - imgPoint....
imgRect.Y = CInt(Math.Round((pb.Height / 2 - imgPoint...
'画像を表示する
PictureBox1.Invalidate()
End Sub
'PictureBox1のPaintイベントハンドラ
Private Sub PictureBox1_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles PictureBox1.Paint
If Not (img Is Nothing) Then
'画像を指定された範囲に描画する
e.Graphics.DrawImage(img, imgRect)
End If
End Sub
}}
#code(csharp){{
//表示する画像
private Bitmap img;
//倍率
private float ratio = 1F;
//画像を表示させる範囲
private Rectangle imgRect;
//Button1のクリックイベントハンドラ
private void Button1_Click(object sender, System.EventArg...
{
//表示する画像を読み込む
img = new Bitmap(TextBox1.Text);
//初期化
imgRect = new Rectangle(0, 0, img.Width, img.Height);
ratio = 1F;
//画像を表示する
PictureBox1.Invalidate();
}
//PictureBox1のMouseDownイベントハンドラ
private void PictureBox1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
PictureBox pb = (PictureBox) sender;
//クリックされた位置を画像上の位置に変換
Point imgPoint = new Point(
(int) Math.Round((e.X - imgRect.X) / ratio),
(int) Math.Round((e.Y - imgRect.Y) / ratio));
//倍率を変更する
if (e.Button == MouseButtons.Left)
{
ratio *= 2F;
}
else if (e.Button == MouseButtons.Right)
{
ratio *= 0.5F;
}
//画像の表示範囲を計算する
imgRect.Width = (int) Math.Round(img.Width * ratio);
imgRect.Height = (int) Math.Round(img.Height * ratio);
imgRect.X = (int) Math.Round(pb.Width / 2 - imgPoint....
imgRect.Y = (int) Math.Round(pb.Height / 2 - imgPoint...
//画像を表示する
PictureBox1.Invalidate();
}
//PictureBox1のPaintイベントハンドラ
private void PictureBox1_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
if (img != null)
{
//画像を指定された範囲に描画する
e.Graphics.DrawImage(img, imgRect);
}
}
}}
○この記事の基になった掲示板のスレッド
-[[ピクチャーボックス | 投稿者(敬称略) 水野, 琴, 管理人>h...
**.NET Tips [#ob38f0b4]
***PictureBoxに表示されている画像を取得する [#q38a9e32]
#column(注意){{
この記事の最新版は「[[フォーム、コントロールの外観をキャ...
}}
ここではInvokePaintメソッド、及びInvokePaintBackgroundメ...
以下に、この方法によりPictureBox(PictureBox1)に表示されて...
#code(vbnet){{
'PictureBox1の大きさを取得
Dim rect As Rectangle = PictureBox1.ClientRectangle
'PictureBox1に表示されている画像を取得するためのBitmap
Dim bmp As New Bitmap(rect.Width, rect.Height)
'bmpに画像を入れるための準備
Dim g As Graphics = Graphics.FromImage(bmp)
Dim pea As New PaintEventArgs(g, rect)
'PaintBackgroundイベントを発生
Me.InvokePaintBackground(PictureBox1, pea)
'Paintイベントを発生
Me.InvokePaint(PictureBox1, pea)
'画像を保存する
bmp.Save("C:\test.png")
'後始末
g.Dispose()
bmp.Dispose()
}}
#code(csharp){{
//PictureBox1の大きさを取得
Rectangle rect = PictureBox1.ClientRectangle;
//PictureBox1に表示されている画像を取得するためのBitmap
Bitmap bmp = new Bitmap(rect.Width, rect.Height);
//bmpに画像を入れるための準備
Graphics g = Graphics.FromImage(bmp);
PaintEventArgs pea = new PaintEventArgs(g, rect);
//PaintBackgroundイベントを発生
this.InvokePaintBackground(PictureBox1, pea);
//Paintイベントを発生
this.InvokePaint(PictureBox1, pea);
//画像を保存する
bmp.Save(@"C:\test.png");
//後始末
g.Dispose();
bmp.Dispose();
}}
この方法によりPictureBox以外のコントロールの外見も画像と...
***Refresh、Update、Invalidateメソッドの違い [#v539f330]
#column(注意){{
この記事の最新版は「[[Refresh、Update、Invalidateメソッド...
}}
ControlクラスのRefresh、Update、Invalidateメソッドは主に...
ヘルプによると、これらのメソッドは次のように説明されてい...
Refresh : 強制的に、コントロールがクライアント領域を無効...
Update : コントロールによって、クライアント領域内の無効化...
Invalidate : コントロールの特定の領域を無効にし、そのコン...
-[[Refreshメソッド>http://www.microsoft.com/japan/msdn/li...
-[[Updateメソッド>http://www.microsoft.com/japan/msdn/lib...
-[[Invalidateメソッド>http://www.microsoft.com/japan/msdn...
これらのメソッドが実際に何をしているのか調べるには、「Ref...
-[[Reflector for .NET>http://www.aisto.com/roeder/DotNet/]]
まず、Refreshメソッドは、Invalidateメソッド(パラメータはT...
また、Invalidateメソッドは、Win32 APIのRedrawWindow(子コ...
つまり、Invalidateメソッドはアプリケーションキューが空に...
言葉だけでは分かりにくいので、実際のコードでその動作を確...
#code(vbnet){{
'カウンターの値
Private counter As Integer = 0
'Button1のクリックイベントハンドラ
Private Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
For i = 0 To 9
'カウンターを増やす
counter += 1
'フォームを再描画しない
'1秒間停止する
System.Threading.Thread.Sleep(1000)
Next i
End Sub
'Button2のクリックイベントハンドラ
Private Sub Button2_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Dim i As Integer
For i = 0 To 9
'カウンターを増やす
counter += 1
'Invalidateを呼び出す
Me.Invalidate()
'1秒間停止する
System.Threading.Thread.Sleep(1000)
Next i
End Sub
'Button3のクリックイベントハンドラ
Private Sub Button3_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button3.Click
Dim i As Integer
For i = 0 To 9
'カウンターを増やす
counter += 1
'Updateを呼び出す
Me.Update()
'1秒間停止する
System.Threading.Thread.Sleep(1000)
Next i
End Sub
'Button4のクリックイベントハンドラ
Private Sub Button4_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button4.Click
Dim i As Integer
For i = 0 To 9
'カウンターを増やす
counter += 1
'Refreshを呼び出す
Me.Refresh()
'1秒間停止する
System.Threading.Thread.Sleep(1000)
Next i
End Sub
'PictureBox1のPaintイベントハンドラ
Private Sub PictureBox1_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles MyBase.Paint
'カウンターを描画する
e.Graphics.DrawString(counter.ToString(), Me.Font, _
Brushes.Black, 0, 0)
End Sub
}}
#code(csharp){{
//カウンターの値
private int counter = 0;
//Button1のクリックイベントハンドラ
private void Button1_Click(object sender, System.EventArg...
{
for (int i = 0; i < 10; i++)
{
//カウンターを増やす
counter++;
//フォームを再描画しない
//1秒間停止する
System.Threading.Thread.Sleep(1000);
}
}
//Button2のクリックイベントハンドラ
private void Button2_Click(object sender, System.EventArg...
{
for (int i = 0; i < 10; i++)
{
//カウンターを増やす
counter++;
//Invalidateを呼び出す
this.Invalidate();
//1秒間停止する
System.Threading.Thread.Sleep(1000);
}
}
//Button3のクリックイベントハンドラ
private void Button3_Click(object sender, System.EventArg...
{
for (int i = 0; i < 10; i++)
{
//カウンターを増やす
counter++;
//Updateを呼び出す
this.Update();
//1秒間停止する
System.Threading.Thread.Sleep(1000);
}
}
//Button4のクリックイベントハンドラ
private void Button4_Click(object sender, System.EventArg...
{
for (int i = 0; i < 10; i++)
{
//カウンターを増やす
counter++;
//Refreshを呼び出す
this.Refresh();
//1秒間停止する
System.Threading.Thread.Sleep(1000);
}
}
//PictureBox1のPaintイベントハンドラ
private void PictureBox1_Paint(
object sender, System.Windows.Forms.PaintEventArgs e)
{
//カウンターを描画する
e.Graphics.DrawString(
counter.ToString(), this.Font, Brushes.Black, 0, ...
}
}}
まず、カウンターをインクリメントした後に何もしない時(But...
Invalidateメソッドを呼び出した時(Button2をクリックした時...
Updateメソッドを呼び出した時(Button3をクリックした時)も...
Refreshメソッドを呼び出した時(Button4をクリックした時)...
実際のこれらのメソッドの使い分けとしては、コントロールを...
**コメント [#la699a35]
#comment
//これより下は編集しないでください
#pageinfo([[:Category/.NET]],2004-11-23 (火) 06:00:00,DOB...
終了行:
#title(.NETプログラミング研究 第46号)
#navi(.NETプログラミング研究)
#contents
*.NETプログラミング研究 第46号 [#bf80fa86]
**.NET質問箱 [#x2280b7c]
「.NET質問箱」では、「どぼん!のプログラミング掲示板」に...
-[[どぼん!のプログラミング掲示板>https://dobon.net/vb/bb...
***ListViewのColumnの順番を取得、設定するには? [#pb5e3966]
#column(注意){{
この記事の最新版は「[[ListViewのColumnの順番を取得、設定...
}}
''【質問】''
ListViewコントロールのAllowColumnReorderプロパティをTrue...
''【回答】''
ListViewコントロールのColumnの順番を取得する方法は、残念...
この問題の解決法として「どぼん!のプログラミング掲示板」...
LVM_GETCOLUMNORDERARRAYメッセージを使用した場合、Columnの...
以下にその具体例を示します。ここでは、WindowsフォームにLi...
#code(vbnet){{
Private Declare Auto Function SendMessage Lib "user32.dll...
ByVal hWnd As IntPtr, ByVal msg As Integer, _
ByVal wParam As Integer, ByVal lParam() As Integer) A...
Private Const LVM_FIRST As Integer = &H1000
Private Const LVM_GETCOLUMNORDERARRAY As Integer = LVM_FI...
'/ <summary>
'/ ListViewの列ヘッダの順番を取得する
'/ </summary>
'/ <param name="lv">対象とするListView</param>
'/ <returns>列ヘッダの順番を示した配列</returns>
Private Shared Function GetColumnHeaderOrder( _
ByVal lv As ListView) As Integer()
Dim count As Integer = lv.Columns.Count
Dim order(count) As Integer
If SendMessage(lv.Handle, LVM_GETCOLUMNORDERARRAY, _
count, order) = 0 Then
Throw New ApplicationException( _
"Columnの順番の取得に失敗しました。")
End If
Return order
End Function
}}
#code(csharp){{
[DllImport("user32.dll", CharSet=CharSet.Auto)]
private static extern int SendMessage(
IntPtr hWnd, int msg, int wParam, int [] lParam);
private const int LVM_FIRST = 0x1000;
private const int LVM_GETCOLUMNORDERARRAY = (LVM_FIRST + ...
/// <summary>
/// ListViewの列ヘッダの順番を取得する
/// </summary>
/// <param name="lv">対象とするListView</param>
/// <returns>列ヘッダの順番を示した配列</returns>
private static int[] GetColumnHeaderOrder(ListView lv)
{
int count = lv.Columns.Count;
int[] order = new int[count];
if (SendMessage(lv.Handle, LVM_GETCOLUMNORDERARRAY, c...
== 0)
throw new ApplicationException(
"Columnの順番の取得に失敗しました。");
return order;
}
//Button1のクリックイベントハンドラ
private void Button1_Click(object sender, System.EventArg...
{
//列ヘッダの順番を取得
int[] orders = GetColumnHeaderOrder(ListView1);
//列ヘッダのTextとその順番を表示する
for (int i = 0; i < ListView1.Columns.Count; i++)
Console.WriteLine(ListView1.Columns[i].Text +
":Position=" + orders[i].ToString());
}
}}
次にListViewコントロールのColumnの順番を設定する方法を紹...
これは、ListViewコントロールにColumnHeaderを追加するとき...
以下にLVM_SETCOLUMNORDERARRAYメッセージを使ってListViewコ...
#code(vbnet){{
Private Declare Auto Function SendMessage Lib "user32.dll...
ByVal hWnd As IntPtr, ByVal msg As Integer, _
ByVal wParam As Integer, ByVal lParam() As Integer) A...
Private Const LVM_FIRST As Integer = &H1000
Private Const LVM_SETCOLUMNORDERARRAY As Integer = LVM_FI...
'/ <summary>
'/ ListViewの列ヘッダの順番を設定する
'/ </summary>
'/ <param name="lv">対象とするListView</param>
'/ <param name="order">列ヘッダの順番を示した配列</param>
Private Shared Sub SetColumnHeaderOrder( _
ByVal lv As ListView, ByVal order() As Integer)
If SendMessage(lv.Handle, LVM_SETCOLUMNORDERARRAY, _
order.Length, order) = 0 Then
Throw New ApplicationException( _
"Columnの順番の設定に失敗しました。")
End If
End Sub
'Button1のクリックイベントハンドラ
Private Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
'列ヘッダの順番を配列に入れる
Dim orders(ListView1.Columns.Count - 1) As Integer
Dim i As Integer
For i = 0 To orders.Length - 1
orders(i) = i
Next i
'列ヘッダの順番を設定する
SetColumnHeaderOrder(ListView1, orders)
End Sub
}}
#code(csharp){{
[DllImport("user32.dll", CharSet=CharSet.Auto)]
private static extern int SendMessage(
IntPtr hWnd, int msg, int wParam, int [] lParam);
private const int LVM_FIRST = 0x1000;
private const int LVM_SETCOLUMNORDERARRAY = (LVM_FIRST + ...
/// <summary>
/// ListViewの列ヘッダの順番を設定する
/// </summary>
/// <param name="lv">対象とするListView</param>
/// <param name="order">列ヘッダの順番を示した配列</param>
private static void SetColumnHeaderOrder(ListView lv, int...
{
if (SendMessage(lv.Handle, LVM_SETCOLUMNORDERARRAY,
order.Length, order) == 0)
throw new ApplicationException(
"Columnの順番の設定に失敗しました。");
}
//Button1のクリックイベントハンドラ
private void Button1_Click(object sender, System.EventArg...
{
//列ヘッダの順番を配列に入れる
int[] orders = new int[ListView1.Columns.Count];
for (int i = 0; i < orders.Length; i++)
orders[i] = i;
//列ヘッダの順番を設定する
SetColumnHeaderOrder(ListView1, orders);
}
}}
○この記事の基になった掲示板のスレッド
-[[ListViewで列順の変更を保持する方法 | 投稿者(敬称略) こ...
***画像をクリックして拡大、縮小表示できるようにするには? ...
#column(注意){{
この記事の最新版は「[[ピクチャボックスの画像をクリックし...
}}
''【質問】''
ピクチャボックスに表示した画像をマウスクリックにより、ク...
''【回答】''
ここでは、ピクチャボックスのPaintイベントでGraphics.DrawI...
それでは実際に作成してみましょう。仕様としては、テキスト...
まずフォーム(Form1)にピクチャボックス(PictureBox1)と...
これで準備は完了です。後はForm1クラスに以下のようなコード...
#code(vbnet){{
'表示する画像
Private img As Bitmap
'倍率
Private ratio As Single = 1.0F
'画像を表示させる範囲
Private imgRect As Rectangle
'Button1のクリックイベントハンドラ
Private Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
'表示する画像を読み込む
img = New Bitmap(TextBox1.Text)
'初期化
imgRect = New Rectangle(0, 0, img.Width, img.Height)
ratio = 1.0F
'画像を表示する
PictureBox1.Invalidate()
End Sub
'PictureBox1のMouseDownイベントハンドラ
Private Sub PictureBox1_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles PictureBox1.MouseDown
Dim pb As PictureBox = CType(sender, PictureBox)
'クリックされた位置を画像上の位置に変換
Dim imgPoint As New Point(CInt((e.X - imgRect.X) / ra...
CInt((e.Y - imgRect.Y) / ratio))
'倍率を変更する
If e.Button = MouseButtons.Left Then
ratio *= 2.0F
Else
If e.Button = MouseButtons.Right Then
ratio *= 0.5F
End If
End If
'画像の表示範囲を計算する
imgRect.Width = CInt(Math.Round((img.Width * ratio)))
imgRect.Height = CInt(Math.Round((img.Height * ratio)))
imgRect.X = CInt(Math.Round((pb.Width / 2 - imgPoint....
imgRect.Y = CInt(Math.Round((pb.Height / 2 - imgPoint...
'画像を表示する
PictureBox1.Invalidate()
End Sub
'PictureBox1のPaintイベントハンドラ
Private Sub PictureBox1_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles PictureBox1.Paint
If Not (img Is Nothing) Then
'画像を指定された範囲に描画する
e.Graphics.DrawImage(img, imgRect)
End If
End Sub
}}
#code(csharp){{
//表示する画像
private Bitmap img;
//倍率
private float ratio = 1F;
//画像を表示させる範囲
private Rectangle imgRect;
//Button1のクリックイベントハンドラ
private void Button1_Click(object sender, System.EventArg...
{
//表示する画像を読み込む
img = new Bitmap(TextBox1.Text);
//初期化
imgRect = new Rectangle(0, 0, img.Width, img.Height);
ratio = 1F;
//画像を表示する
PictureBox1.Invalidate();
}
//PictureBox1のMouseDownイベントハンドラ
private void PictureBox1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
PictureBox pb = (PictureBox) sender;
//クリックされた位置を画像上の位置に変換
Point imgPoint = new Point(
(int) Math.Round((e.X - imgRect.X) / ratio),
(int) Math.Round((e.Y - imgRect.Y) / ratio));
//倍率を変更する
if (e.Button == MouseButtons.Left)
{
ratio *= 2F;
}
else if (e.Button == MouseButtons.Right)
{
ratio *= 0.5F;
}
//画像の表示範囲を計算する
imgRect.Width = (int) Math.Round(img.Width * ratio);
imgRect.Height = (int) Math.Round(img.Height * ratio);
imgRect.X = (int) Math.Round(pb.Width / 2 - imgPoint....
imgRect.Y = (int) Math.Round(pb.Height / 2 - imgPoint...
//画像を表示する
PictureBox1.Invalidate();
}
//PictureBox1のPaintイベントハンドラ
private void PictureBox1_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
if (img != null)
{
//画像を指定された範囲に描画する
e.Graphics.DrawImage(img, imgRect);
}
}
}}
○この記事の基になった掲示板のスレッド
-[[ピクチャーボックス | 投稿者(敬称略) 水野, 琴, 管理人>h...
**.NET Tips [#ob38f0b4]
***PictureBoxに表示されている画像を取得する [#q38a9e32]
#column(注意){{
この記事の最新版は「[[フォーム、コントロールの外観をキャ...
}}
ここではInvokePaintメソッド、及びInvokePaintBackgroundメ...
以下に、この方法によりPictureBox(PictureBox1)に表示されて...
#code(vbnet){{
'PictureBox1の大きさを取得
Dim rect As Rectangle = PictureBox1.ClientRectangle
'PictureBox1に表示されている画像を取得するためのBitmap
Dim bmp As New Bitmap(rect.Width, rect.Height)
'bmpに画像を入れるための準備
Dim g As Graphics = Graphics.FromImage(bmp)
Dim pea As New PaintEventArgs(g, rect)
'PaintBackgroundイベントを発生
Me.InvokePaintBackground(PictureBox1, pea)
'Paintイベントを発生
Me.InvokePaint(PictureBox1, pea)
'画像を保存する
bmp.Save("C:\test.png")
'後始末
g.Dispose()
bmp.Dispose()
}}
#code(csharp){{
//PictureBox1の大きさを取得
Rectangle rect = PictureBox1.ClientRectangle;
//PictureBox1に表示されている画像を取得するためのBitmap
Bitmap bmp = new Bitmap(rect.Width, rect.Height);
//bmpに画像を入れるための準備
Graphics g = Graphics.FromImage(bmp);
PaintEventArgs pea = new PaintEventArgs(g, rect);
//PaintBackgroundイベントを発生
this.InvokePaintBackground(PictureBox1, pea);
//Paintイベントを発生
this.InvokePaint(PictureBox1, pea);
//画像を保存する
bmp.Save(@"C:\test.png");
//後始末
g.Dispose();
bmp.Dispose();
}}
この方法によりPictureBox以外のコントロールの外見も画像と...
***Refresh、Update、Invalidateメソッドの違い [#v539f330]
#column(注意){{
この記事の最新版は「[[Refresh、Update、Invalidateメソッド...
}}
ControlクラスのRefresh、Update、Invalidateメソッドは主に...
ヘルプによると、これらのメソッドは次のように説明されてい...
Refresh : 強制的に、コントロールがクライアント領域を無効...
Update : コントロールによって、クライアント領域内の無効化...
Invalidate : コントロールの特定の領域を無効にし、そのコン...
-[[Refreshメソッド>http://www.microsoft.com/japan/msdn/li...
-[[Updateメソッド>http://www.microsoft.com/japan/msdn/lib...
-[[Invalidateメソッド>http://www.microsoft.com/japan/msdn...
これらのメソッドが実際に何をしているのか調べるには、「Ref...
-[[Reflector for .NET>http://www.aisto.com/roeder/DotNet/]]
まず、Refreshメソッドは、Invalidateメソッド(パラメータはT...
また、Invalidateメソッドは、Win32 APIのRedrawWindow(子コ...
つまり、Invalidateメソッドはアプリケーションキューが空に...
言葉だけでは分かりにくいので、実際のコードでその動作を確...
#code(vbnet){{
'カウンターの値
Private counter As Integer = 0
'Button1のクリックイベントハンドラ
Private Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
For i = 0 To 9
'カウンターを増やす
counter += 1
'フォームを再描画しない
'1秒間停止する
System.Threading.Thread.Sleep(1000)
Next i
End Sub
'Button2のクリックイベントハンドラ
Private Sub Button2_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Dim i As Integer
For i = 0 To 9
'カウンターを増やす
counter += 1
'Invalidateを呼び出す
Me.Invalidate()
'1秒間停止する
System.Threading.Thread.Sleep(1000)
Next i
End Sub
'Button3のクリックイベントハンドラ
Private Sub Button3_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button3.Click
Dim i As Integer
For i = 0 To 9
'カウンターを増やす
counter += 1
'Updateを呼び出す
Me.Update()
'1秒間停止する
System.Threading.Thread.Sleep(1000)
Next i
End Sub
'Button4のクリックイベントハンドラ
Private Sub Button4_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button4.Click
Dim i As Integer
For i = 0 To 9
'カウンターを増やす
counter += 1
'Refreshを呼び出す
Me.Refresh()
'1秒間停止する
System.Threading.Thread.Sleep(1000)
Next i
End Sub
'PictureBox1のPaintイベントハンドラ
Private Sub PictureBox1_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles MyBase.Paint
'カウンターを描画する
e.Graphics.DrawString(counter.ToString(), Me.Font, _
Brushes.Black, 0, 0)
End Sub
}}
#code(csharp){{
//カウンターの値
private int counter = 0;
//Button1のクリックイベントハンドラ
private void Button1_Click(object sender, System.EventArg...
{
for (int i = 0; i < 10; i++)
{
//カウンターを増やす
counter++;
//フォームを再描画しない
//1秒間停止する
System.Threading.Thread.Sleep(1000);
}
}
//Button2のクリックイベントハンドラ
private void Button2_Click(object sender, System.EventArg...
{
for (int i = 0; i < 10; i++)
{
//カウンターを増やす
counter++;
//Invalidateを呼び出す
this.Invalidate();
//1秒間停止する
System.Threading.Thread.Sleep(1000);
}
}
//Button3のクリックイベントハンドラ
private void Button3_Click(object sender, System.EventArg...
{
for (int i = 0; i < 10; i++)
{
//カウンターを増やす
counter++;
//Updateを呼び出す
this.Update();
//1秒間停止する
System.Threading.Thread.Sleep(1000);
}
}
//Button4のクリックイベントハンドラ
private void Button4_Click(object sender, System.EventArg...
{
for (int i = 0; i < 10; i++)
{
//カウンターを増やす
counter++;
//Refreshを呼び出す
this.Refresh();
//1秒間停止する
System.Threading.Thread.Sleep(1000);
}
}
//PictureBox1のPaintイベントハンドラ
private void PictureBox1_Paint(
object sender, System.Windows.Forms.PaintEventArgs e)
{
//カウンターを描画する
e.Graphics.DrawString(
counter.ToString(), this.Font, Brushes.Black, 0, ...
}
}}
まず、カウンターをインクリメントした後に何もしない時(But...
Invalidateメソッドを呼び出した時(Button2をクリックした時...
Updateメソッドを呼び出した時(Button3をクリックした時)も...
Refreshメソッドを呼び出した時(Button4をクリックした時)...
実際のこれらのメソッドの使い分けとしては、コントロールを...
**コメント [#la699a35]
#comment
//これより下は編集しないでください
#pageinfo([[:Category/.NET]],2004-11-23 (火) 06:00:00,DOB...
ページ名:
▲
▼
[
トップ
] [
新規
|
子ページ作成
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
]