خاموش، لوگ آف یا استند بای کردن ویندوز
یک کلاس Power Off ویندوز با چندین قابلیت جالب. علاوه بر کارکردهای معمولی، میشه از اون به عنوان سر به سر گذاشتن کاربران هم استفاده کرد :46: ( البته توصیه نمیکنم )
کد:
Public Class Win_ShutDown
Public Enum shutdownTypes
Logoff = 0
Shutdown = 1
Reboot = 2
Force = 4
PowerOff = 8
End Enum
Private Structure LUID
Dim UsedPart As Integer
Dim IgnoredForNowHigh32BitPart As Integer
End Structure
Private Structure TOKEN_PRIVILEGES
Dim PrivilegeCount As Integer
Dim TheLuid As luID
Dim Attributes As Integer
End Structure
Private Declare Function ExitWindowsEx Lib "user32" (ByVal shutdownType As Integer, ByVal dwReserved As _
Integer) As Integer
Private Declare Auto Function InitiateSystemShutdownEx Lib "Advapi32.dll" (ByVal lpMachineName As _
String, ByVal lpMessage As String, ByVal dwTimeout As Integer, ByVal bForceAppsClosed As Boolean, _
ByVal bRebootAfterShutdown As Boolean, ByVal dwReason As shutdownReason) As Boolean
Public Enum shutdownReason
SHTDN_REASON_MAJOR_APPLICATION = &H40000 'Application issue.
SHTDN_REASON_MAJOR_HARDWARE = &H10000 'Hardware issue.
SHTDN_REASON_MAJOR_LEGACY_API = &H70000 'The InitiateSystemShutdown function was used instead of InitiateSystemShutdownEx.
SHTDN_REASON_MAJOR_OPERATINGSYSTEM = &H20000 'Operating system issue.
SHTDN_REASON_MAJOR_OTHER = &H0 ' Other issue.
SHTDN_REASON_MAJOR_POWER = &H60000 'Power failure.
SHTDN_REASON_MAJOR_SOFTWARE = &H30000 'Software issue.
SHTDN_REASON_MAJOR_SYSTEM = &H50000 'System failure
End Enum
Public Enum shutdownMethodToExecute
ExitWindowsExAPI = 1
ProcessShellMethod = 2
End Enum
Private Declare Auto Function AbortSystemShutdown Lib "Advapi32.dll" (ByVal lpMachineName As String) _
As Boolean
Private Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As IntPtr, ByVal _
DesiredAccess As Integer, ByRef TokenHandle As Integer) As Integer
Private Declare Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA" (ByVal _
lpSystemName As String, ByVal lpName As String, ByRef lpLuid As LUID) As Integer
Private Declare Function AdjustTokenPrivileges Lib "advapi32" (ByVal TokenHandle As Integer, ByVal _
DisableAllPrivileges As Boolean, ByRef NewState As TOKEN_PRIVILEGES, ByVal BufferLength As _
Integer, ByRef PreviousState As TOKEN_PRIVILEGES, ByRef ReturnLength As Integer) As Integer
Private Declare Function SHShutDownDialog Lib "shell32" Alias "#60" (ByVal notSureYet As Integer) As Integer
Private Sub AdjustToken()
Const TOKEN_ADJUST_PRIVILEGES As Int32 = &H20
Const TOKEN_QUERY As Int32 = &H8
Const SE_PRIVILEGE_ENABLED As Int32 = &H2
Dim hdlProcessHandle As IntPtr
Dim hdlTokenHandle As Int32
Dim tmpLuid As LUID
Dim tkp As TOKEN_PRIVILEGES
Dim tkpNewButIgnored As TOKEN_PRIVILEGES
Dim lBufferNeeded As Int32
hdlProcessHandle = Process.GetCurrentProcess.Handle
OpenProcessToken(hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hdlTokenHandle)
LookupPrivilegeValue("", "SeShutdownPrivilege", tmpLuid)
tkp.PrivilegeCount = 1 'One privilege to set
tkp.TheLuid = tmpLuid
tkp.Attributes = SE_PRIVILEGE_ENABLED
AdjustTokenPrivileges(hdlTokenHandle, False, tkp, Len(tkpNewButIgnored), tkpNewButIgnored, _
lBufferNeeded)
End Sub
Public Function shutdownWindowsOS(ByVal shutdownType As shutdownTypes, Optional ByVal executeShutdownMethod As _
shutdownMethodToExecute = shutdownMethodToExecute.ExitWindowsExAPI) As Integer
Select Case executeShutdownMethod
Case shutdownMethodToExecute.ExitWindowsExAPI
AdjustToken()
Return ExitWindowsEx(shutdownType, Nothing)
Case Else
If shutdownType = shutdownTypes.Shutdown Then
Process.Start("shutdown -s -f -t 0")
'Shell("shutdown -s -f -t 0", , False, 0)
End If
If shutdownType = shutdownTypes.Reboot Then
'Shell("shutdown -r -f -t 0", , False, 0)
Process.Start("shutdown -r -f -t 0")
End If
If shutdownType = shutdownTypes.Logoff Then
'Shell("shutdown -l -f -t 0", , False, 0)
Process.Start("shutdown -l -f -t 0")
End If
End Select
End Function
Public Function initiateSystemShutdownDialog(Optional ByVal computerName As String = "", Optional ByVal _
shutdownMessage As String = "Computer needs to Shutdown", Optional ByVal shutdownTimeoutBySeconds _
As Integer = 10, Optional ByVal forceShutdown As Boolean = False, Optional ByVal _
rebootAfterShutdown As Boolean = True, Optional ByVal shutdownReason As shutdownReason = _
shutdownReason.SHTDN_REASON_MAJOR_APPLICATION) As Boolean
AdjustToken()
Return InitiateSystemShutdownEx(computerName, shutdownMessage, shutdownTimeoutBySeconds, _
forceShutdown, rebootAfterShutdown, shutdownReason)
End Function
Public Function cancelInitiatedShutdownDialog(Optional ByVal computerName As String = "") As Boolean
Return AbortSystemShutdown(computerName)
End Function
Public Function openTurnOffComputerDialog() As Integer
Return SHShutDownDialog(0)
End Function
End Class
پخش فرمتهای مختلف موزیک توسط وی بی
بوسیله این Dll به راحتی تمامی فرمتهای موزیک در وی بی، قابل پخش خواهد بود.
[html]http://rapidshare.com/files/48354393/Music_Library.dll.html[/html]
عیر فعال کردن دکمه Close فرم
برای این کار کافیست کد زیر رو در هر جایی از کد ادیتور که میخواهید بنویسید ( البته خارج از روال ها ) :
کد:
Private _closeClick AsBoolean
PublicConst SC_CLOSE AsInteger = 61536
PublicConst WM_SYSCOMMAND AsInteger = 274
ProtectedOverloadsOverridesSub WndProc(ByRef m As Message)
If m.Msg = WM_SYSCOMMAND AndAlso m.WParam.ToInt32 = SC_CLOSE Then
ExitSub
Else
Me._closeClick = True
EndIf
MyBase.WndProc(m)
EndSub
پیدا کردن آخرین کانکشنی که با آن به اینترنت وصل شده بودید
کد:
Dim
output AsString = ""
Dim msg AsString
Dim I AsInteger
Dim myEventLog AsNew EventLog
myEventLog.Log = "system"
I = myEventLog.Entries.Count - 1
While I >= 0
If myEventLog.Entries.Item(I).Source = "RemoteAccess"Then
msg = myEventLog.Entries.Item(I).Message
If InStr(msg, "successfully established") Then
output =
"You last connected to the network on: "
output &= myEventLog.Entries.Item(I).TimeWritten
msg = Mid(msg, InStr(msg,
"connection to") + 14)
msg = Microsoft.VisualBasic.Left(msg, InStr(msg, "using") - 2)
output &= vbNewLine & "The name of the connection used was: " & msg
ExitWhile
EndIf
EndIf
I -= 1
EndWhile
MsgBox(output)
EndSub
حرکت دادن کنترلها در RunTime
برای امکان حرکت دادن یا Moving کنترلها ، در هنگام اجرای برنامه کافیست روال زیر رو به پروژه اضافه کنید:
کد:
Private Sub MouseDragging(ByVal e As MouseEventArgs, ByVal Control As Control)
Static OldPosition AsNew Point(-1, -1)
IfNot (e.Button = Nothing) Then
If e.Button = Windows.Forms.MouseButtons.Left Then
If (OldPosition.X = -1) And (OldPosition.Y = -1) Then OldPosition = New Point(e.X, e.Y)
If e.Y <> OldPosition.Y Then
Control.Top += e.Y - OldPosition.Y
'move Up/Down
EndIf
If e.X <> OldPosition.X Then
Control.Left += e.X - OldPosition.X
'move Left/Right
End If
End If
Else
'button is nothing, maybe it was lifted.
OldPosition = New Point(-1, -1)
EndIf
EndSub
روش استفاده:
یک کنترل از نوع Button روی فرم فرار دهید و در رویداد MouseMove کد زیر را وارد نمایید:
MouseDragging(e, Button1)X
تبدیل تاریخ از/به شمسی و دیگر تقویم ها
سلام
این را برای یک سایت یکی از دوستانم نوشته بودم که عیناً در اینجا هم کپی کردم.
تبدیل به میلادی
کد:
Dim x As New System.Globalization.PersianCalendar
Dim dt As Date = x.ToDateTime(1386, 1, 1, 0, 0, 0, 0, 0)
تبدیل از میلادی
کد:
Dim x As New System.Globalization.PersianCalendar()
Dim dt As Date
dt = #1/1/2007#
'Or یا
dt = New Date(2007, 1, 1)
'Or یا
dt = CDate("1/1/2007")
Dim y As Integer = x.GetYear(dt)
Dim m As Integer = x.GetMonth(dt)
Dim d As Integer = x.GetDayOfMonth(dt)
برای تبدیل از/به میلادی بر اساس تقویم های دیگرف کافیست به جای کلاسس PersianCalendar کلاسس تقویم مورد نظر را نمونه سازی کرد.
دات نت 2 با تقویم میلادی در کل شامل 11 تقویم است!
مثلاً PersianCalendar تقویم ایرانی یا همان هجری شمسی است
یا HijriCalendar تقویم هجری قمری است
JapaneseCalendar ژاپنی
KoreanCalendar کره ای
TaiwanCalendar تایوانی
و...
برای تبدیل تاریخ های تقویم های دیگر به هم (که یک سرش میلادی نباشد) باید اول تاریخ مورد نظر را به میلادی تبدیل کرد و سپس میلادی را به تقویم دوم تبدیل کرد.
برای تبدیل به نام روزها و ماه هم بهتر است از آرایه String استفاده کنید نه دستورات IF و یا Select
یک آرایه 12 عنصری که مثلاً نام تمام ماه ها را دارد و فقط کافیست ایندکس را بدهید.....
عیر فعال کردن دکمه Close فرم
نقل قول:
برای این کار کافیست کد زیر رو در هر جایی از کد ادیتور که میخواهید بنویسید ( البته خارج از روال ها ) :
کد:
Private _closeClick AsBoolean
PublicConst SC_CLOSE AsInteger = 61536
PublicConst WM_SYSCOMMAND AsInteger = 274
ProtectedOverloadsOverridesSub WndProc(ByRef m As Message)
If m.Msg = WM_SYSCOMMAND AndAlso m.WParam.ToInt32 = SC_CLOSE Then
ExitSub
Else
Me._closeClick = True
EndIf
MyBase.WndProc(m)
EndSub
این کارو میشه به همین راحتی انجام داد ! ! !
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
e.Cancel = -1
End Sub
اين كد فرم را شبيه يك دايره ميكند
اين كد فرم را شبيه يك دايره ميكند
کد:
Protected Overrides Sub OnPaint( _
ByVal e As System.Windows.Forms.PaintEventArgs)
Dim shape As New System.Drawing.Drawing2D.GraphicsPath
shape.AddEllipse(0, 0, Me.Width, Me.Height)
Me.Region = New System.Drawing.Region(shape)
End Sub
تبديل مبناي 2 به 10 در Vb.net
تبديل مبناي 2 به 10 در VB.NET
کد:
Dim a, x, i, sum As Integer
i = 1
sum = 0
a = TextBox1.Text()
While (a <> 0)
x = a Mod 10
a = a / 10
sum += x * i
i *= 2
End While
MessageBox.Show(sum)
حركت كردن ميان كنترلهاي يك فرم در Vb.net
چگونه مي توان ميان كنترلهاي يك فرم بخصوص TextBox هاي آن حركت كرد؟ (البته با زدن كليد Enter)
دو راه حل وجود دارد:
1- در رويداد KeyDown هر كنترل دستورات زير را بنويسيد:
کد:
If e.KeyCode = Keys.Enter Then
نام كنترل بعدي.Focus()
End If
اما اين كار يك عيب بزرگ دارد وآنكه براي هركنترل بايد اين كار را انجام دهيد كه براي فرمهايي كه كنترل زياد دارند وقت گير و خسته كننده است.
2- ابتدا خاصيت KeyPreview فرم را به True تغيير دهيد سپس در رويداد KeyDown فرم دستورات زير را بنويسيد:
کد:
If e.KeyCode = Keys.Enter Then
Dim Con As Control
Con = Me.GetNextControl(Me.ActiveControl, True)
Con.Focus()
End If
توضيح :
دستور Me.ActiveControl نام كنترلي كه Focus را در اختيار دارد برمي گرداند ودستورMe.GetNextControl نام كنترل بعدي را كه Focus را در اختيار مي گيرد برمي گرداند ودر Con قرار مي دهد و دستورCon.Focus باعث مي شود تا Focus در اختيار كنترل قرار گيرد.
نكته : فراموش نكنيد براي صحيح كاركردن دستورات بالا بايد خاصيت TabIndex هركنترل به درستي تنظيم شده باشد.
برنامه ضرب اعداد بزرگ در Vb.net
کد:
Private Function Multiply(ByVal TX1 As String, ByVal TX2 As String) As String
Dim a(5), b(5), c(5) As Byte
Dim S1, S2 As String
If TX1 = "0" Or TX2 = "0" Then
Return "0"
Else
S1 = TX1
S2 = TX2
ReDim a(S1.Length - 1)
ReDim b(S2.Length - 1)
If S1.Length >= S2.Length Then
ReDim c(S1.Length * 2 - 1)
Else
ReDim c(S2.Length * 2 - 1)
End If
Dim i1 As Short = 0
For i As Short = S1.Length - 1 To 0 Step -1
a(i1) = S1.Chars(i).ToString
i1 += 1
Next
i1 = 0
For i As Short = S2.Length - 1 To 0 Step -1
b(i1) = S2.Chars(i).ToString
i1 += 1
Next
Dim k, t, t1, x As Short
For i As Short = 0 To S1.Length - 1
k = i
t = 0
t1 = 0
For j As Short = 0 To S2.Length - 1
x = a(i) * b(j) + t
c(k) += x Mod 10 + t1
If t1 <> 0 Then
t1 = 0
End If
If c(k) >= 10 Then
t1 = c(k) \ 10
c(k) = c(k) Mod 10
End If
t = x \ 10
k += 1
Next
c(k) = t + t1
Next
Dim S3 As String
Dim flag As Boolean = False
For i As Short = UBound(c) To 0 Step -1
If c(i) <> 0 Then
S3 &= c(i)
flag = True
Else
If flag = True Then
S3 &= c(i)
End If
End If
Next
Return S3
End If
End Function
یک کلاس برای ارتباط با WebCam
کد:
Public Class iCam
#Region "Api/constants"
Private Const WS_CHILD As Integer = &H40000000
Private Const WS_VISIBLE As Integer = &H10000000
Private Const SWP_NOMOVE As Short = &H2S
Private Const SWP_NOZORDER As Short = &H4S
Private Const WM_USER As Short = &H400S
Private Const WM_CAP_DRIVER_CONNECT As Integer = WM_USER + 10
Private Const WM_CAP_DRIVER_DISCONNECT As Integer = WM_USER + 11
Private Const WM_CAP_SET_VIDEOFORMAT As Integer = WM_USER + 45
Private Const WM_CAP_SET_PREVIEW As Integer = WM_USER + 50
Private Const WM_CAP_SET_PREVIEWRATE As Integer = WM_USER + 52
Private Const WM_CAP_GET_FRAME As Long = 1084
Private Const WM_CAP_COPY As Long = 1054
Private Const WM_CAP_START As Long = WM_USER
Private Const WM_CAP_STOP As Long = (WM_CAP_START + 68)
Private Const WM_CAP_SEQUENCE As Long = (WM_CAP_START + 62)
Private Const WM_CAP_SET_SEQUENCE_SETUP As Long = (WM_CAP_START + 64)
Private Const WM_CAP_FILE_SET_CAPTURE_FILEA As Long = (WM_CAP_START + 20)
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Short, ByVal lParam As String) As Integer
Private Declare Function capCreateCaptureWindowA Lib "avicap32.dll" (ByVal lpszWindowName As String, ByVal dwStyle As Integer, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Short, ByVal hWndParent As Integer, ByVal nID As Integer) As Integer
Private Declare Function capGetDriverDescriptionA Lib "avicap32.dll" (ByVal wDriver As Short, ByVal lpszName As String, ByVal cbName As Integer, ByVal lpszVer As String, ByVal cbVer As Integer) As Boolean
Private Declare Function BitBlt Lib "GDI32.DLL" (ByVal hdcDest As IntPtr, ByVal nXDest As Integer, ByVal nYDest As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hdcSrc As IntPtr, ByVal nXSrc As Integer, ByVal nYSrc As Integer, ByVal dwRop As Int32) As Boolean
#End Region
Private iDevice As String
Private hHwnd As Integer
Private lwndC As Integer
Public iRunning As Boolean
Private CamFrameRate As Integer = 15
Private OutputHeight As Integer = 240
Private OutputWidth As Integer = 360
Public Sub resetCam()
'resets the camera after setting change
If iRunning Then
closeCam()
Application.DoEvents()
If setCam() = False Then
MessageBox.Show("Errror Setting/Re-Setting Camera")
End If
End If
End Sub
Public Sub initCam(ByVal parentH As Integer)
'Gets the handle and initiates camera setup
If Me.iRunning = True Then
MessageBox.Show("Camera Is Already Running")
Exit Sub
Else
hHwnd = capCreateCaptureWindowA(iDevice, WS_VISIBLE Or WS_CHILD, 0, 0, OutputWidth, CShort(OutputHeight), parentH, 0)
If setCam() = False Then
MessageBox.Show("Error setting Up Camera")
End If
End If
End Sub
Public Sub setFrameRate(ByVal iRate As Long)
'sets the frame rate of the camera
CamFrameRate = CInt(1000 / iRate)
resetCam()
End Sub
Private Function setCam() As Boolean
'Sets all the camera up
If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, CShort(iDevice), CType(0, String)) = 1 Then
SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, CShort(CamFrameRate), CType(0, String))
SendMessage(hHwnd, WM_CAP_SET_PREVIEW, 1, CType(0, String))
Me.iRunning = True
Return True
Else
Me.iRunning = False
Return False
End If
End Function
Public Function closeCam() As Boolean
'Closes the camera
If Me.iRunning Then
closeCam = CBool(SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, 0, CType(0, String)))
Me.iRunning = False
End If
End Function
Public Function copyFrame(ByVal src As PictureBox, ByVal rect As RectangleF) As Bitmap
If iRunning Then
Dim srcPic As Graphics = src.CreateGraphics
Dim srcBmp As New Bitmap(src.Width, src.Height, srcPic)
Dim srcMem As Graphics = Graphics.FromImage(srcBmp)
Dim HDC1 As IntPtr = srcPic.GetHdc
Dim HDC2 As IntPtr = srcMem.GetHdc
BitBlt(HDC2, 0, 0, CInt(rect.Width), _
CInt(rect.Height), HDC1, CInt(rect.X), CInt(rect.Y), 13369376)
copyFrame = CType(srcBmp.Clone(), Bitmap)
'Clean Up
srcPic.ReleaseHdc(HDC1)
srcMem.ReleaseHdc(HDC2)
srcPic.Dispose()
srcMem.Dispose()
Else
MessageBox.Show("Camera Is Not Running!")
End If
End Function
Public Function FPS() As Integer
Return CInt(1000 / (CamFrameRate))
End Function
End Class
منبع:
کد:
http://barnamenevis.org/forum/showpost.php?p=355455&postcount=55
تشخیص لینک کلیک شده در صفحات وب
روي فرم يک کنترل وب بروزر قرار دهيد و کد هاي زير را وارد کنيد:
کد:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
WebBrowser1.Navigate("http://www.google.com")
End Sub
Private Sub webControl_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
Dim link As HtmlElement
Dim links As HtmlElementCollection = WebBrowser1.Document.Links
For Each link In links
link.AttachEventHandler("onclick", AddressOf LinkClicked)
Next
End Sub
Private Sub LinkClicked(ByVal sender As Object, ByVal e As EventArgs)
Dim link As HtmlElement = WebBrowser1.Document.ActiveElement
Dim url As String = link.GetAttribute("href")
MsgBox("Link Clicked: " & link.InnerText & vbCrLf & _
"Destination: " & url)
End Sub