最簡(jiǎn)單的方法
專注于為中小企業(yè)提供網(wǎng)站建設(shè)、做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)新樂(lè)免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了成百上千企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
If UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) 0 Then End
前提是你的程序名別太普通了容易和別的進(jìn)程重名就不行了
方法一:通過(guò)Diagnostics.Process.GetProcessesByName函數(shù)來(lái)檢測(cè)程序是否已經(jīng)啟動(dòng)
Imports System.Windows.Forms
Module Module1
Sub Main()
'檢測(cè)多重啟動(dòng)
If Diagnostics.Process.GetProcessesByName( _
Diagnostics.Process.GetCurrentProcess.ProcessName).Length 1 Then
MessageBox.Show("已經(jīng)一個(gè)實(shí)例的本程序正在運(yùn)行。")
Return
End If
Application.Run(New Form())
End Sub
End Module
不過(guò)這個(gè)方法有個(gè)缺點(diǎn),如果用戶改了下exe的名字就檢測(cè)不到了,所以更好的方法如下
方法2:使用Mutex
Imports System.Windows.Forms
Module Module1
Sub Main()
Dim createdNew As Boolean
' 創(chuàng)建mutex
Dim mutex As System.Threading.Mutex = _
New System.Threading.Mutex(True, "YourAppName", createdNew)
If createdNew = False Then
MessageBox.Show("已經(jīng)一個(gè)實(shí)例的本程序正在運(yùn)行。")
Return
End If
Application.Run(New Form())
' 釋放mutex
mutex.ReleaseMutex()
End Sub
End Module
試試看這樣行不:在應(yīng)用程序設(shè)置中,勾選“生成單個(gè)實(shí)例應(yīng)用程序”,然后在應(yīng)用程序事件中處理這個(gè)事件
Private Sub MyApplication_StartupNextInstance(sender As Object, e As StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
'這里的?e.CommandLine應(yīng)該就是雙擊第二個(gè)文件時(shí)傳進(jìn)來(lái)的命令行
End Sub
在項(xiàng)目上右鍵,然后選擇屬性,打開(kāi)如下圖所示的項(xiàng)目屬性對(duì)話框:
然后選擇左邊的“應(yīng)用程序”標(biāo)簽頁(yè),再勾選上“生成單個(gè)實(shí)例應(yīng)用程序”。
Public Class Form1
Dim Files() As String
Dim ImgNum As Integer = 0
Dim CurIdx As Integer = 0
Sub OpenFiles()
InitialSet()
Try
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
If Not OpenFileDialog1.FileNames Is Nothing Then
Files = OpenFileDialog1.FileNames
Else
Files(0) = OpenFileDialog1.FileName
End If
ImgNum = Files.Length
Dim s As String = String.Format("你共打開(kāi) {0} 張圖片", ImgNum)
Me.Text = s
StateSet()
CurIdx = 1
DisplayImage(CurIdx)
Else
StateSet()
Me.PictureBox1.Image = Nothing
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Sub InitialSet()
Me.OpenFileDialog1.Multiselect = True
Me.OpenFileDialog1.Filter = "(*.jpg)|*jpg"
Me.OpenFileDialog1.FilterIndex = 0
ImgNum = 0
CurIdx = 0
If Not Files Is Nothing Then
Array.Clear(Files, 0, Files.Length - 1)
End If
End Sub
Sub DisplayImage(ByRef Idx As Integer)
If IO.Path.GetExtension(Files(Idx - 1)).ToLower = ".jpg" Then
Me.PictureBox1.Load(Files(Idx - 1))
End If
StateSet()
End Sub
Sub StateSet()
If ImgNum 1 Then
Me.btnNext.Enabled = True
Else
Me.btnNext.Enabled = False
Me.btnPervious.Enabled = False
Exit Sub
End If
If CurIdx 1 And ImgNum 1 Then
Me.btnPervious.Enabled = True
Else
Me.btnPervious.Enabled = False
End If
If CurIdx ImgNum And ImgNum 1 Then
Me.btnNext.Enabled = True
Else
Me.btnNext.Enabled = False
End If
End Sub
Sub NextIamge(ByRef Idx As Integer)
If Idx ImgNum Then
Idx += 1
Else
Exit Sub
End If
DisplayImage(Idx)
End Sub
Sub PerviousIagme(ByRef Idx As Integer)
If Idx 1 Then
Idx -= 1
Else
Exit Sub
End If
DisplayImage(Idx)
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
Try
NextIamge(CurIdx)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
OpenFiles()
End Sub
Private Sub btnPervious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPervious.Click
Try
PerviousIagme(CurIdx)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
vb.net沒(méi)用過(guò),vb6.0的話可以用getobject這種方法,你可以改改試試,看能不能用
Dim xlApp As Object
Set xlApp = GetObject(ExcelFileName).Application '獲取Excel對(duì)象