ttp://blogs.yahoo.co.jp/koga2020/54275972.html
上記サイトに書かれている方法で、メッセージボックスを自動で閉じることができたのですが、OKボタンを押したときのリザルトの取得の仕方がわかりません。
どうすればできるでしょうか?
また、上記URLに書かれている方法よりももっとスマートなやりかたがあれば教えてください。m(__)m
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答3件
0
ベストアンサー
また、上記URLに書かれている方法よりももっとスマートなやりかたがあれば教えてください。m(__)m
FindWindowで検索する方法だと、他に同じキャプションのウィンドウが存在した場合の挙動を制御できないので、あまりお勧めしません。
こんな感じでどうでしょうか。
C#
1public class AutoClosingMessageBox 2{ 3 public static DialogResult Show(string text, string caption, int timeout) 4 { 5 using(var parent = new Control()) 6 using(var timer = new System.Timers.Timer()) 7 { 8 bool autoclose = false; 9 timer.Interval = timeout; 10 timer.Elapsed += (object source, System.Timers.ElapsedEventArgs e) => 11 { 12 parent.Invoke(new Action(parent.Dispose)); 13 autoclose = true; 14 }; 15 timer.Start(); 16 var result = MessageBox.Show(parent, text, caption); 17 return autoclose ? DialogResult.None : result; 18 } 19 } 20}
親ウィンドウを破棄すると子ウィンドウも一緒に破棄されるという仕組みを利用しています。
投稿2016/03/03 20:14
総合スコア5938
0
元ソースに一部加筆です。
lang
1public class AutoClosingMessageBox 2{ 3 int text_max_length = 200; //最大の文字列長の指定 4 System.Threading.Timer _timeoutTimer; 5 string _caption; 6 7 // 【追加】 8 public DialogResult Result { get; } 9 10 AutoClosingMessageBox(string text, string caption, int timeout) 11 { 12 if (text.length > text_max_length) //内容の長さを制限する追加部分 13 { 14 text = text.Substring(0, text_max_length); 15 } 16 _caption = caption; 17 _timeoutTimer = new System.Threading.Timer(OnTimerElapsed, null, timeout, System.Threading.Timeout.Infinite); 18 19 // 【追加】ここで結果を代入 20 this.Result = MessageBox.Show(text, caption); 21 } 22 23 public static void Show(string text, string caption, int timeout) 24 { 25 new AutoClosingMessageBox(text, caption, timeout); 26 } 27 28 void OnTimerElapsed(object state) 29 { 30 IntPtr mbWnd = FindWindow(null, _caption); 31 if (mbWnd != IntPtr.Zero) 32 { 33 SendMessage(mbWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero); 34 } 35 _timeoutTimer.Dispose(); 36 } 37 38 const int WM_CLOSE = 0x0010; 39 [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)] 40 static extern IntPtr FindWindow(string lpClassName, string lpWindowName); 41 [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)] 42 static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam); 43}
次のように使えるはずです。
lang
1var result = AutoClosingMessageBox.Show("text", "caption", 1000).Result;
投稿2016/03/03 15:57
編集2016/03/03 15:58総合スコア1610
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
0
MessageBoxのShow メソッドの戻り値を取ってないからじゃないですか?
実際にどう実装しているかわかりませんが、
- AutoClosingMessageBoxにMessageBoxのShow メソッドの戻り値を保持するプロパティを実装
するくらいじゃないですかね?
あと
- 一定時間経過によって閉じられたのか
- ユーザーのアクションによって閉じられたのか
を区別できるようにしておいた方が良いかもです
投稿2016/03/03 16:05
総合スコア3939
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/03/04 10:24