[WM][VB][自動關閉訊息對話方塊]

  • 5058
  • 0

[WM][VB][自動關閉訊息對話方塊]

如果你想要把對話方塊自動關閉要怎麼做呢,我們可以利用timer來控制達到自動關閉效果

image

Step1:開啟vs2008 新增一個vb 智慧型裝置專案,在表單上產一個按鍵 test

image

Step2:撰寫Form1程式碼 

Imports System.Runtime.InteropServices
Public Class Form1
    <DllImport("coredll.dll", EntryPoint:="FindWindow", CharSet:=CharSet.Auto)> _
      Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    End Function

    <DllImport("coredll.dll", CharSet:=CharSet.Auto)> _
    Public Shared Function PostMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
    End Function

    Public Const WM_CLOSE As Integer = &H10
    Private Sub StartTimer()
        Dim timer As New Timer
        '3秒啓動  
        timer.Interval = 3000
        AddHandler timer.Tick, AddressOf Timer_Tick
        timer.Enabled = True
    End Sub

    Private Sub CloseMessageBox()
        '依MessageBox的標題,找出MessageBox的視窗    
        Dim value As IntPtr = FindWindow(Nothing, "TEST")
        If value <> IntPtr.Zero Then
            '找到則關閉MessageBox視窗    
            PostMessage(value, WM_CLOSE, IntPtr.Zero, IntPtr.Zero)
        End If
    End Sub

    Private Sub Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        CType(sender, Timer).Enabled = False
        CloseMessageBox()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        StartTimer()
        MessageBox.Show("Auto Close MessageBox", "TEST")
    End Sub

End Class

 

Step3:將滑鼠移到功能表按下偵錯\開始偵錯部署應用程式

image

Step4:按下test 鍵,這時候看一下是否有出現訊息對話方塊,等待三秒就消失不見

 

 

 image   image

 

Step5: 源碼下載