→.NETプログラミング
Webサイト†
Tips†
フォームの強制前出し†
- あんまり無闇にこれやると行儀が悪いしウザいので注意
using System.Runtime.InteropServices; //<-これ必要
[DllImport("USER32.DLL", CharSet=CharSet.Auto)]
private static extern int ShowWindow(
IntPtr hWnd,
int nCmdShow
);
[DllImport("USER32.DLL", CharSet=CharSet.Auto)]
private static extern bool SetForegroundWindow(
IntPtr hWnd
);
private const int SW_NORMAL = 1;
/// ------------------------------------------------------------------------------------
/// <summary>
/// 同名のプロセスが起動中の場合、メイン ウィンドウをアクティブにします。</summary>
/// <returns>
/// 既に起動中であれば true。それ以外は false。</returns>
/// ------------------------------------------------------------------------------------
public static bool ShowPrevProcess()
{
Process hThisProcess = Process.GetCurrentProcess();
Process[] hProcesses = Process.GetProcessesByName(hThisProcess.ProcessName);
int iThisProcessId = hThisProcess.Id;
foreach (Process hProcess in hProcesses)
{
if (hProcess.Id != iThisProcessId)
{
ShowWindow(hProcess.MainWindowHandle, SW_NORMAL);
SetForegroundWindow(hProcess.MainWindowHandle);
return true;
}
}
return false;
}
フォームのカスタムペイント†
XPスタイル†
Transparent, Click-Through Forms†
フォームの形を自由に変えたい†
フォームを閉じるときに閉じるのを中止したい†
- Closingイベントのハンドラで e.Cancel プロパティに true をセットする。
フォームの印刷†
デスクトップの中央付近に表示させる方法†
int myWid = this.Width;
Rectangle rect = new Rectangle();
int deskWid = Screen.GetWorkingArea(rect).Width;
SetDesktopLocation( (deskWid - myWid )/2, this.Location.Y );
特定の色ではなく「システムでXXに使っている色」を指定したいとき†
- SystemColors クラスをヘルプで参照されたし。
- 似たようなものとして KnownColor 列挙体というのがあるんだけどこれとは微妙に異なる模様。
コモンダイアログの使い方†
- 色設定ダイアログ使用例
ColorDialog f = new ColorDialog();
f.AllowFullOpen = true;
f.FullOpen = false;
f.AnyColor = true;
f.ShowHelp = false;
f.Color = ls[cnum].BackColor;
DialogResult rc = f.ShowDialog(this);
if(rc == DialogResult.OK )
{
SetColor(ls[cnum], f.Color );
}
- ファイルオープンダイアログの使用例
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = Directory.GetCurrentDirectory();
ofd.Filter = "テキストファイル (*.txt)|*.txt|"
+ "すべてのファイル (*.*)|*.*";
ofd.FilterIndex = 2;
ofd.Multiselect = true;
DialogResult rc = ofd.ShowDialog(this);
if( rc == DialogResult.OK )
{
//hogehoge
}
描画のダブルバッファリング†
Formのデザイナ画面が開かなくなった†
- なぜかコードとして認識されるときがある模様。対応方法は以下の通り
- ソリューション・エクスプローラでxxx.csのプロパティを開く
- 「ビルドアクション」プロパティが「コンパイル」になっているのを一旦「なし」にする
- 同プロパティをもう一度「コンパイル」にする
- これで直るはず。これでも直らなかったら .csprojをいじってみるとか(未確認)
フォーム内のコントロールを列挙して何かをしたいとき†
- http://jeanne.wankuma.com/tips/form/findcontrol.html
- Controls[<コントロール名>] で1階層下のコントロールを取得できる。下の階層は再帰で探す
Public Shared Function FindControl(ByVal hParent As Control, ByVal stName As String) As Control
' hParent 内のすべてのコントロールを列挙する
For Each hControl As Control In hParent.Controls
' 列挙したコントロールにコントロールが含まれている場合は再帰呼び出しする
If hControl.HasChildren = True Then
Dim hFindControl As Control = FindControl(hControl, stName)
' 再帰呼び出し先でコントロールが見つかった場合はそのまま返す
If Not hFindControl Is Nothing Then
Return hFindControl
End If
End If
' コントロール名が合致した場合はそのコントロールのインスタンスを返す
If hControl.Name = stName Then
Return hControl
End If
Next hControl
End Function
ドッキングフォームを使いたい†
Formの位置をタスクバーの場所とサイズを考慮して決めたい†
デフォルトボタンの設定†
- 親となるダイアログのAcceptButtonプロパティにそのボタンを設定する。CancelButton(ESCキー)も同様の考え方
フォームのサイズ変更範囲の設定†