日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java messagebox 关闭_wince/WinForm下实现一个自动关闭的MessageBox

發布時間:2025/4/5 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java messagebox 关闭_wince/WinForm下实现一个自动关闭的MessageBox 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

WinForm 下我們可以調用MessageBox.Show 來顯示一個消息對話框,提示用戶確認等操作。在有些應用中我們需要通過程序來自動關閉這個消息對話框而不是由用戶點擊確認按鈕來關閉。然而.Net framework 沒有為我們提供自動關閉MessageBox 的方法,要實現這個功能,我們需要使用Window API 來完成。

首先我們需要找到這個消息對話框的窗口句柄,一個比較簡單的方法就是用 FindWindow API 來查找對應的窗體句柄。

第一種方法:

[DllImport("user32.dll",?SetLastError=true)]

staticexternIntPtr?FindWindow(stringlpClassName,stringlpWindowName);

有了這兩個API函數,我們就可以來關閉消息對話框了。思路是在調用MessageBox.Show 前啟動一個后臺工作線程,這個工作線程等待一定時間后開始查找消息對話框的窗口句柄,找到后調用EndDialog API 函數關閉這個消息對話框。不過這個方法有個問題,就是如果同時又多個同名的消息對話框(可能不一定是這個應用的),這樣做可能會關錯窗口,如何解決這個問題,我還沒有想出比較好的方法,如果大家有更好的方法解決這個問題,不妨一起討論討論。

我根據這個思路編寫了延時關閉消息對話框的函數

public void ShowMessageBoxTimeout(string text, string caption,

MessageBoxButtons buttons, int timeout)

{

ThreadPool.QueueUserWorkItem(new WaitCallback(CloseMessageBox),

new CloseState(caption, timeout));

MessageBox.Show(text, caption,buttons);

}

這個函數中timeout 參數單位是毫秒,其他參數和MessageBox.Show的參數含義是一樣的,這里不再詳細說明。

這個函數中首先利用線程池調用一個工作線程 CloseMessageBox ,并將對話框的標題和延時時間通過CloseState這個類傳遞給CloseMessageBox函數。

CloseState 的定義如下:

private class CloseState

{

private int _Timeout;

///

/// In millisecond

///

public int Timeout

{

get

{

return _Timeout;

}

}

private string _Caption;

///

/// Caption of dialog

///

public string Caption

{

get

{

return _Caption;

}

}

public CloseState(string caption, int timeout)

{

_Timeout = timeout;

_Caption = caption;

}

}

最后就是CloseMessageBox函數了,直接看代碼吧

privatevoidCloseMessageBox(objectstate)

{

????????????CloseState?closeState=stateasCloseState;

????????????Thread.Sleep(closeState.Timeout);

????????????IntPtr?dlg=FindWindow(null,?closeState.Caption);

if(dlg!=IntPtr.Zero)

{

????????????????IntPtr?result;

????????????????EndDialog(dlg,outresult);

????????????}????????}

第二種方法:

//注意提示出現的是、否按鈕 不能自動關閉

//例如這個提示就不能自動關閉:

//if (MessageBox.Show("是否接聽?", "來電:" + num, MessageBoxButtons.YesNo,

//MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button2) == DialogResult.Yes)

// {}

//以下是源碼

[DllImport("coredll.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]

private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("coredll.dll", CharSet = CharSet.Auto)]

public static extern int PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

public const int WM_CLOSE = 0x10;

private string _caption;//標題名字//在彈出提示窗體,需為這個變量賦值例如:if (MessageBox.Show("是否接聽?", "來電:" + num, MessageBoxButtons.YesNo,

MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button2) == DialogResult.Yes)

{}賦值就是:_caption="來電:" + num;//明白

[System.Runtime.InteropServices.DllImport("coredll")]

public static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);//參數:窗體句柄

public const int SW_MINIMIZE = 6;

public static string CodePath = "";

//這個方法直接調用就行,直接關閉當前show出的界面,根據那個標題名字

private void KillMessageBox()

{

try

{

//查找MessageBox的彈出窗口,注意對應標題

IntPtr ptr = FindWindow(null, this._caption);

if (ptr != IntPtr.Zero)

{

//查找到窗口則關閉

PostMessage(ptr, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);

}

}

catch (Exception ex)

{

MessageBox.Show("關閉show"+ex.ToString());

}

}

總結

以上是生活随笔為你收集整理的java messagebox 关闭_wince/WinForm下实现一个自动关闭的MessageBox的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。