Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop
Module Module1
Dim sw As Integer = Screen.PrimaryScreen.WorkingArea.Right
Dim sh As Integer = Screen.PrimaryScreen.WorkingArea.Bottom
Dim ax, ay As Integer, dr As Boolean
Public myform As Form = Nothing
Public apppath As String = IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) & "\"
Public xdb As String = apppath & "db.xlsx"
Public excelapp As Excel.Application
Public excelworkbook As Excel.Workbook
Public excelworksheet As Excel.Worksheet
Public misv As Object = Reflection.Missing.Value
Public Sub xc()
excelworkbook.Close()
excelapp.Quit()
releaseObject(excelapp)
releaseObject(excelworkbook)
releaseObject(excelworksheet)
End Sub
Public Sub CreateExcelFile()
If IO.File.Exists(xdb) Then
MsgBox("الملف موجود مسبقا", MsgBoxStyle.Information, "")
Exit Sub
Else
excelapp = New Excel.Application
excelworkbook = excelapp.Workbooks.Add(misv)
excelworksheet = CType(excelworkbook.Worksheets(1), Excel.Worksheet)
excelworksheet.Name = "tb1"
excelworkbook.SaveAs(xdb)
xc()
End If
End Sub
Public Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
Public Sub rep(f As Form)
If f.Left < 0 Then f.Left = 0
If f.Top < 0 Then f.Top = 0
If f.Right > sw Then f.Left = (sw - f.Width)
If f.Bottom > sh Then f.Top = (sh - f.Height)
End Sub
Public Sub mdown(sender As Object, e As MouseEventArgs)
If e.Button = MouseButtons.Left Then
dr = True
ax = sender.MousePosition.X - myform.Left
ay = sender.MousePosition.Y - myform.Top
If TypeOf sender Is Button Then
CType(sender, Button).ForeColor = Color.Red
End If
End If
End Sub
Public Sub mmove(sender As Object, e As MouseEventArgs)
If dr Then
myform.Left = sender.MousePosition.X - ax
myform.Top = sender.MousePosition.Y - ay
End If
End Sub
Public Sub mup(sender As Object, e As MouseEventArgs)
dr = False
rep(myform)
If TypeOf sender Is Button Then
CType(sender, Button).ForeColor = Color.Black
End If
End Sub
Public Sub mcontrols(f As Control)
If TypeOf f Is Form Then
AddHandler f.MouseDown, AddressOf mdown
AddHandler f.MouseMove, AddressOf mmove
AddHandler f.MouseUp, AddressOf mup
End If
For Each c As Control In f.Controls
mcontrols(c)
If TypeOf (c) Is Panel Or TypeOf (c) Is Label Or TypeOf (c) Is PictureBox Or TypeOf (c) Is GroupBox Then
AddHandler c.MouseDown, AddressOf mdown
AddHandler c.MouseMove, AddressOf mmove
AddHandler c.MouseUp, AddressOf mup
End If
Next
End Sub
End Module