→[[.NETプログラミング]]

#contents

*Webサイト [#sd7a34c8]
-[[Working with .NET windows Forms:http://www.only4gurus.com/v3/preview.asp?ID=9246]]

*Tips [#b7e75c96]
-[[英語OSでフォームやボタン位置がずれる問題>http://bbs.wankuma.com/index.cgi?mode=al2&namber=7700&KLOG=7]]
--自動スケーリング機能が働いているため。フォームのAutoScaleプロパティをFalseにすると直るが、MS的にはTrueを推奨しているようだ
--[[フォームの自動スケーリングについて>http://msdn2.microsoft.com/ja-jp/library/ms229605.aspx]]
--※フォームでMaximumSizeを設定していると、AutoScaleをTrueにしていてもそれ以上大きくならない→その結果ボタンが見えない位置に配置されて触れなくなったりするので注意
-[[Creating a Splash Screen that runs without a message pump>http://www.codeproject.com/cs/miscctrl/CustomSplashScreen.asp]] 2007.3.15

-[[Windowsフォームの入力チェックを効率化する>http://codezine.jp/a/article/aid/1012.aspx]] 2007.2.27
--ErrorProviderの紹介

*フォームの強制前出し [#m090ef3a]
-あんまり無闇にこれやると行儀が悪いしウザいので注意
  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;
  }


*フォームのカスタムペイント [#sab2728e]
-[[Google Talk Window Form>http://www.codeproject.com/cs/miscctrl/GoogleTalkWindowsForm.asp]]

*XPスタイル [#s05828c0]
-[[Windows FormをXPスタイルにする:http://www.atmarkit.co.jp/fdotnet/dotnettips/234winxpstyle/winxpstyle.html]]

*Transparent, Click-Through Forms [#vbdb244d]
-http://www.codeproject.com/useritems/ClickThroughWindows.asp

*フォームの形を自由に変えたい [#gc30624b]
-http://www.codeproject.com/csharp/irregularshapedform.asp
-http://www.codeproject.com/useritems/Desktop_Sheep.asp

*フォームを閉じるときに閉じるのを中止したい [#le99190c]
-Closingイベントのハンドラで e.Cancel プロパティに true をセットする。

*フォームの印刷 [#ibe5a9d5]
-http://www.atmarkit.co.jp/fdotnet/dotnettips/393printdoc/printdoc.html

*デスクトップの中央付近に表示させる方法 [#dbf72068]
 int myWid = this.Width;
 Rectangle rect = new Rectangle();
 int deskWid = Screen.GetWorkingArea(rect).Width;
 SetDesktopLocation( (deskWid - myWid )/2, this.Location.Y );

*特定の色ではなく「システムでXXに使っている色」を指定したいとき [#cbde52cd]
-SystemColors クラスをヘルプで参照されたし。
-似たようなものとして KnownColor 列挙体というのがあるんだけどこれとは微妙に異なる模様。

*コモンダイアログの使い方 [#y1b1066a]
-色設定ダイアログ使用例
 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
 }

-[[Customizing OpenFileDialog in .Net>http://www.codeproject.com/useritems/OpenFileDialogEx.asp]]
-[[印刷のプロパティダイアログをダイレクトに表示したい>http://www.codeproject.com/useritems/PrinterPropertiesWindow.asp]]
--OpenPrinterPropertiesDialog で検索のこと
-http://www.atmarkit.co.jp/fdotnet/csharptips/007dialog/007dialog.html
-OpenFileDialog, SaveFileDialogなどでヘルプを検索のこと


*描画のダブルバッファリング [#l3ec4618]
-[[CodeProjectのサンプル:http://www.codeproject.com/useritems/DoubleBuffering.asp]]
-http://www.microsoft.com/japan/msdn/vbasic/migration/tips/DoubleBuffer/


*Formのデザイナ画面が開かなくなった [#hafeff01]
-なぜかコードとして認識されるときがある模様。対応方法は以下の通り
--ソリューション・エクスプローラでxxx.csのプロパティを開く
--「ビルドアクション」プロパティが「コンパイル」になっているのを一旦「なし」にする
--同プロパティをもう一度「コンパイル」にする
-これで直るはず。これでも直らなかったら .csprojをいじってみるとか(未確認)

*フォーム内のコントロールを列挙して何かをしたいとき [#yc90aa4c]
-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

*ドッキングフォームを使いたい [#kea4b687]
-[[Dobon.NET .NETでDocking Windowを使う:http://dobon.net/vb/dotnet/links/dockingwindow.html]]
-[[Magic Docking:http://www.codeproject.com/cs/miscctrl/magicdocking.asp]]

*Formの位置をタスクバーの場所とサイズを考慮して決めたい [#n422b37f]
-SetDesktopLocation メソッドを使ってデスクトップ座標で指定する
-デスクトップ座標とは、タスクバーを除外した画面の作業領域
-http://jeanne.wankuma.com/tips/form/11-desktoplocation.html

*デフォルトボタンの設定 [#q4703d3b]
-親となるダイアログのAcceptButtonプロパティにそのボタンを設定する。CancelButton(ESCキー)も同様の考え方

*フォームのサイズ変更範囲の設定 [#s00a62b9]
-http://www.atmarkit.co.jp/fdotnet/dotnettips/238winlimitsize/winlimitsize.html

トップ   編集 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS