C#

[C#/PDA]MessageBox Auto Closing

선영아 사랑해 2016. 1. 18. 13:50

.net compact framework을 이용한 PDA 프로그램 개발에서 메시지 박스를 특정한 시간이 지나면 자동으로 종료되는 샘플 코드입니다.


public class AutoClosingMessageBox
{
    System.Threading.Timer _timeoutTimer;
    string _caption;


    AutoClosingMessageBox(string text, string caption, int timeout)
    {
        _caption = caption;

        _timeoutTimer = new System.Threading.Timer(OnTimerElapsed, null, timeout, System.Threading.Timeout.Infinite);

        
        System.Windows.Forms.MessageBox.Show(text, caption);
    }
    public static void Show(string text, string caption, int timeout)
    {
        new AutoClosingMessageBox(text, caption, timeout);
    }

    void onTimerElapsed(object state)
    {
        IntPtr mbWnd = FindWindow(null, _caption);

        if (mbWnd != IntPtr.Zero)
            SendMessage(mbWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);

        _timeoutTimer.Dispose();
    }

    const int WM_CLOSE = 0x0010;

    [System.Runtime.InteropServices.DllImport("coredll.dll", SetLastError = true)]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    [System.Runtime.InteropServices.DllImport("coredll.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
}


private void button_Click(object sender, EventArgs e)

{

       AutoClosingMessageBox.Show("정상처리되었습니다.", "성공", 1000);//1초뒤에 자동 종료

}