VB.NET 二〇〇七重启/关机程序

[csharp]
// Microsoft Office Outlook 2007 Add-in Sample Code 
// 
// THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF
ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE 
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
PURPOSE. 
//  
using System; 
using System.Drawing; 
using System.Collections.Generic; 
using System.Runtime.InteropServices; 
 
public static class PictureDispConverter 

    //IPictureDisp guid 
    public static Guid iPictureDispGuid =
typeof(stdole.IPictureDisp).GUID; 
 
    /// Converts an Icon into a IPictureDisp 
    public static stdole.IPictureDisp ToIPictureDisp(Icon icon) 
    { 
        PICTDESC.Icon pictIcon = new PICTDESC.Icon(icon); 
        return PictureDispConverter.OleCreatePictureIndirect(pictIcon,
ref iPictureDispGuid, true); 
    } 
 
    /// Converts an image into a IPictureDisp 
    public static stdole.IPictureDisp ToIPictureDisp(Image image) 
    { 
        Bitmap bitmap = (image is Bitmap) ? (Bitmap)image : new
Bitmap(image); 
        PICTDESC.Bitmap pictBit = new PICTDESC.Bitmap(bitmap); 
        return PictureDispConverter.OleCreatePictureIndirect(pictBit,
ref iPictureDispGuid, true); 
    } 
 
 
    [DllImport(“OleAut32.dll”, EntryPoint = “OleCreatePictureIndirect”,
ExactSpelling = true, PreserveSig = false)] 
    private static extern stdole.IPictureDisp
OleCreatePictureIndirect([MarshalAs(UnmanagedType.AsAny)] object
picdesc, ref Guid iid, bool fOwn); 
     
    private readonly static HandleCollector handleCollector = new
HandleCollector(“Icon handles”, 1000); 
 
    // WINFORMS COMMENT: 
    // PICTDESC is a union in native, so we’ll just 
    // define different ones for the different types 
    // the “unused” fields are there to make it the right 
    // size, since the struct in native is as big as the biggest 
    // union. 
    private static class PICTDESC 
    { 
        //Picture Types 
        public const short PICTYPE_UNINITIALIZED = -1; 
        public const short PICTYPE_NONE = 0; 
        public const short PICTYPE_BITMAP = 1; 
        public const short PICTYPE_METAFILE = 2; 
        public const short PICTYPE_ICON = 3; 
        public const short PICTYPE_ENHMETAFILE = 4; 
 
        [StructLayout(LayoutKind.Sequential)] 
        public class Icon 
        { 
            internal int cbSizeOfStruct =
Marshal.SizeOf(typeof(PICTDESC.Icon)); 
            internal int picType = PICTDESC.PICTYPE_ICON; 
            internal IntPtr hicon = IntPtr.Zero; 
            internal int unused1 = 0; 
            internal int unused2 = 0; 
 
            internal Icon(System.Drawing.Icon icon) 
            { 
                this.hicon = icon.ToBitmap().GetHicon(); 
            } 
        } 
 
        [StructLayout(LayoutKind.Sequential)] 
        public class Bitmap 
        { 
            internal int cbSizeOfStruct =
Marshal.SizeOf(typeof(PICTDESC.Bitmap)); 
            internal int picType = PICTDESC.PICTYPE_BITMAP; 
            internal IntPtr hbitmap = IntPtr.Zero; 
            internal IntPtr hpal = IntPtr.Zero; 
            internal int unused = 0; 
 
            internal Bitmap(System.Drawing.Bitmap bitmap) 
            { 
                this.hbitmap = bitmap.GetHbitmap(); 
            } 
        } 
    } 

Imports System 
Imports System.Drawing 
Imports System.Drawing.Imaging 
Imports System.Windows.Forms 
Imports System.Runtime.InteropServices 
 
 
#Region “Win32 Class” 
Friend Class Win32 
 
#Region “常量” 
    Public Const ULW_COLORKEY As Int32 = &H1 
    Public Const ULW_ALPHA As Int32 = &H2 
    Public Const ULW_OPAQUE As Int32 = &H4 
    Public Const WS_EX_LAYERED As Int32 = &H80000 
    Public Const AC_SRC_OVER As Byte = &H0 
    Public Const AC_SRC_ALPHA As Byte = &H1 
#End Region 
 
#Region “枚举” 
    Public Enum Bool 
        [False] = 0 
        [True] 
    End Enum 
#End Region 
 
#Region “结构” 
    <StructLayout(LayoutKind.Sequential, Pack:=1)> _ 
    Private Structure ARGB 
        Public Blue As Byte 
        Public Green As Byte 
        Public Red As Byte 
        Public Alpha As Byte 
    End Structure 
 
    <StructLayout(LayoutKind.Sequential)> _ 
    Public Structure Size 
        Public cx As Int32 
        Public cy As Int32 
 
        Public Sub New(ByVal cx As Int32, ByVal cy As Int32) 
            Me.cx = cx 
            Me.cy = cy 
        End Sub 
    End Structure 
 
    <StructLayout(LayoutKind.Sequential)> _ 
    Public Structure Point 
        Public x As Int32 
        Public y As Int32 
 
        Public Sub New(ByVal x As Int32, ByVal y As Int32) 
            Me.x = x 
            Me.y = y 
        End Sub 
    End Structure 
 
    <StructLayout(LayoutKind.Sequential, Pack:=1)> _ 
    Public Structure BLENDFUNCTION 
        Public BlendOp As Byte 
        Public BlendFlags As Byte 
        Public SourceConstantAlpha As Byte 
        Public AlphaFormat As Byte 
    End Structure 
#End Region 
 
#Region “API” 
   
‘该函数检索一内定窗口的顾客区域或任何荧屏的来得设备上下文遭受的句柄,未来能够在GDI函数中选取该句柄来在设施上下文意况中绘制。 
    Public Declare Auto Function GetDC Lib “user32.dll” (ByVal hWnd As
IntPtr) As IntPtr 
 
   
‘该函数成立叁个与钦定设备相当的内部存款和储蓄器设备上下文碰到(DC)。通过GetDc()获取的HDC直接与连锁设施沟通,而本函数创制的DC,则是与内部存款和储蓄器中的叁个外表相关联。 
    Public Declare Auto Function CreateCompatibleDC Lib “gdi32.dll”
(ByVal hDC As IntPtr) As IntPtr 
 
   
‘该函数选取一对象到钦赐的设施上下文景况中,该新对象替换先前的同等等级次序的目标。 
    Public Declare Auto Function SelectObject Lib “gdi32.dll” (ByVal hDC
As IntPtr, ByVal hObject As IntPtr) As IntPtr 
 
    ‘该函数更新三个分支的窗口的职位,大小,形状,内容和半光滑度。 
    Public Declare Auto Function UpdateLayeredWindow Lib “user32.dll”
(ByVal hwnd As IntPtr, ByVal hdcDst As IntPtr, ByRef pptDst As Point,
ByRef psize As Size, ByVal hdcSrc As IntPtr, ByRef pprSrc As Point,
ByVal crKey As Int32, ByRef pblend As BLENDFUNCTION, ByVal dwFlags As
Int32) As Bool 
 
   
‘该函数释放设备上下文意况(DC)供别的应用程序使用。函数的成效与器材上下文情状项目有关。它只释放公用的和装备上下文景况,对于类或个人的则不算。 
    Public Declare Auto Function ReleaseDC Lib “user32.dll” (ByVal hWnd
As IntPtr, ByVal hDC As IntPtr) As Integer 
 
   
‘该函数删除一个逻辑笔、画笔、字体、位图、区域或然调色板,释放具备与该指标有关的系统能源,在目的被删除之后,内定的句柄也就失效了。 
    Public Declare Auto Function DeleteObject Lib “gdi32.dll” (ByVal
hObject As IntPtr) As Bool 
 
    ‘该函数删除内定的设备上下文情形(DC)。 
    Public Declare Auto Function DeleteDC Lib “gdi32.dll” (ByVal hdc As
IntPtr) As Bool 
#End Region 
End Class 
 
#End Region 
 
Public Class AlphaForm 
    Inherits Form 
 
    Public Sub SetBitmap(ByVal opacity As Byte) 
        Dim bmp As Bitmap = Me.BackgroundImage 
        If bmp.PixelFormat <> PixelFormat.Format32bppArgb Then 
            Throw New
ApplicationException(“窗体背景必得选取带Alpha通道的三十四人图片。”) 
        End If 
 
        ‘根据图片大小设置窗体大小 www.2cto.com  
        Me.Size = bmp.Size 
 
        ‘在内部存款和储蓄器中开创与当前显示器包容的DC 
        Dim hDC1 As IntPtr = Win32.GetDC(IntPtr.Zero) 
        Dim hDC2 As IntPtr = Win32.CreateCompatibleDC(hDC1) 
        Dim hBitmap1 As IntPtr = IntPtr.Zero 
        Dim hBitmap2 As IntPtr = IntPtr.Zero 
 
        Try 
            hBitmap1 = bmp.GetHbitmap(Color.FromArgb(0)) 
            hBitmap2 = Win32.SelectObject(hDC2, hBitmap1) 
 
            Dim blend As New Win32.BLENDFUNCTION() 
            With blend 
                .BlendOp = Win32.AC_SRC_OVER 
                .BlendFlags = 0 
                .AlphaFormat = Win32.AC_SRC_ALPHA 
                .SourceConstantAlpha = opacity 
            End With 
 
            Call Win32.UpdateLayeredWindow(Me.Handle, hDC1, New
Win32.Point(Left, Top), 
                                           New Win32.Size(bmp.Width,
bmp.Height), hDC2, 
                                           New Win32.Point(0, 0), 0,
blend, Win32.ULW_ALPHA) 
 
        Finally 
            Call Win32.ReleaseDC(IntPtr.Zero, hDC1) 
            If hBitmap1 <> IntPtr.Zero Then 
                Call Win32.SelectObject(hDC2, hBitmap2) 
                Call Win32.DeleteObject(hBitmap1) 
            End If 
            Call Win32.DeleteDC(hDC2) 
        End Try 
    End Sub 
 
    Protected Overloads Overrides ReadOnly Property CreateParams() As
CreateParams 
        Get 
            If Not DesignMode Then 
                Dim cp As CreateParams = MyBase.CreateParams 
                cp.ExStyle = cp.ExStyle Or Win32.WS_EX_LAYERED 
                Return cp 
            Else 
                Return MyBase.CreateParams 
            End If 
        End Get 
    End Property 
 
End Class 
(魏滔序原创,转帖请注解出处。)

图片 1Imports System.Runtime.InteropServices
图片 2Imports Microsoft.VisualBasic
图片 3图片 4Public Class Form1Class Form1 
图片 5<DllImport(“kernel32.dll”, ExactSpelling:=True)> _’调用系统参数
图片 6图片 7Friend Shared Function GetCurrentProcess()Function GetCurrentProcess() As IntPtr
图片 8End Function
图片 9
图片 10<DllImport(“advapi32.dll”, ExactSpelling:=True, SetLastError:=True)> _
图片 11图片 12Friend Shared Function OpenProcessToken()Function OpenProcessToken(ByVal h As IntPtr, ByVal acc As Integer, ByRef phtok As IntPtr) As Boolean
图片 13End Function
图片 14
图片 15<DllImport(“advapi32.dll”, SetLastError:=True)> _
图片 16图片 17Friend Shared Function LookupPrivilegeValue()Function LookupPrivilegeValue(ByVal host As String, ByVal name As String, ByRef pluid As Long) As Boolean
图片 18End Function
图片 19
图片 20<DllImport(“advapi32.dll”, ExactSpelling:=True, SetLastError:=True)> _
图片 21图片 22Friend Shared Function AdjustTokenPrivileges()Function AdjustTokenPrivileges(ByVal htok As IntPtr, ByVal disall As Boolean, ByRef newst As TokPriv1Luid, ByVal len As Integer, ByVal prev As IntPtr, ByVal relen As IntPtr) As Boolean
图片 23End Function
图片 24
图片 25<DllImport(“user32.dll”, ExactSpelling:=True, SetLastError:=True)> _
图片 26图片 27Friend Shared Function ExitWindowsEx()Function ExitWindowsEx(ByVal flg As Integer, ByVal rea As Integer) As Boolean
图片 28End Function
图片 29
图片 30Friend Const SE_PRIVILEGE_ENABLED As Integer = &H2
图片 31Friend Const TOKEN_QUERY As Integer = &H8
图片 32Friend Const TOKEN_ADJUST_PRIVILEGES As Integer = &H20
图片 33Friend Const SE_SHUTDOWN_NAME As String = “SeShutdownPrivilege”
图片 34Friend Const EWX_LOGOFF As Integer = &H0 ‘注销Computer
图片 35Friend Const EWX_SHUTDOWN As Integer = &H1’闭馆Computer
图片 36Friend Const EWX_REBOOT As Integer = &H2’再次启航计算机
图片 37Friend Const EWX_FORCE As Integer = &H4’关闭全部进度,注销计算机
图片 38Friend Const EWX_POWEROFF As Integer = &H8
图片 39Friend Const EWX_FORCEIFHUNG As Integer = &H10
图片 40
图片 41<StructLayout(LayoutKind.Sequential, Pack:=1)> _
图片 42  ‘援引参数
图片 43图片 44  Friend Structure TokPriv1LuidStructure TokPriv1Luid
图片 45  Public Count As Integer
图片 46  Public Luid As Long
图片 47  Public Attr As Integer
图片 48End Structure
图片 49
图片 50图片 51Private Shared Sub DoExitWin()Sub DoExitWin(ByVal flg As Integer)
图片 52  Dim xc As Boolean ‘判定语句
图片 53  Dim tp As TokPriv1Luid
图片 54  Dim hproc As IntPtr = GetCurrentProcess()
图片 55  ’调用进度值
图片 56  Dim htok As IntPtr = IntPtr.Zero
图片 57  xc = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, htok)
图片 58  tp.Count = 1
图片 59  tp.Luid = 0
图片 60  tp.Attr = SE_PRIVILEGE_ENABLED
图片 61  xc = LookupPrivilegeValue(Nothing, SE_SHUTDOWN_NAME, tp.Luid)
图片 62  xc = AdjustTokenPrivileges(htok, False, tp, 0, IntPtr.Zero, IntPtr.Zero)
图片 63  xc = ExitWindowsEx(flg, 0)
图片 64End Sub
图片 65
图片 66图片 67Public Shared Sub Reboot()Sub Reboot()
图片 68  DoExitWin((EWX_FORCE Or EWX_REBOOT)) ‘重新开动计算机
图片 69End Sub
图片 70
图片 71图片 72Public Shared Sub PowerOff()Sub PowerOff()
图片 73  DoExitWin((EWX_FORCE Or EWX_POWEROFF)) ‘关闭计算机
图片 74End Sub
图片 75
图片 76图片 77Public Shared Sub LogoOff()Sub LogoOff()
图片 78  DoExitWin((EWX_FORCE Or EWX_LOGOFF)) ‘注销计算机
图片 79End Sub

    Dim hGraphics As IntPtr = Field.GetValue(G)
    Field = GetType(Font).GetField(“nativeFont”,
BindingFlags.Instance Or
BindingFlags.NonPublic)

[csharp]
<pre name=”code” class=”vb”>’ Microsoft Office Outlook 2007 Add-in
Sample Code 
‘ 
‘ THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF
ANY 
‘ KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 
‘ IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
PURPOSE. 
‘  
Imports System 
Imports System.Drawing 
Imports System.Collections.Generic 
Imports System.Runtime.InteropServices 
 
Public Module PictureDispConverter 
 
    ‘IPictureDisp guid 
    Public iPictureDispGuid As Guid =
GetType(stdole.IPictureDisp).GUID 
 
    ‘Converts an Icon into a IPictureDisp 
    Public Function ToIPictureDisp(ByVal ico As Icon) As
stdole.IPictureDisp 
 
        Dim pictIcon As New PICTDESC.Icon(ico) 
        Return PictureDispConverter.OleCreatePictureIndirect(pictIcon,
iPictureDispGuid, True) 
    End Function 
 
    ‘Converts an image into a IPictureDisp 
    Public Function ToIPictureDisp(ByVal picture As Image) As
stdole.IPictureDisp 
 
        Dim bm As Bitmap 
        If TypeOf picture Is Bitmap Then 
            bm = picture 
        Else 
            bm = New Bitmap(picture) 
        End If 
        Dim pictBit As New PICTDESC.Bitmap(bm) 
        Return PictureDispConverter.OleCreatePictureIndirect(pictBit,
iPictureDispGuid, True) 
    End Function 
 
 
    <DllImport(“OleAut32.dll”,
EntryPoint:=”OleCreatePictureIndirect”, ExactSpelling:=True,
PreserveSig:=False)> _ 
    Private Function
OleCreatePictureIndirect(<MarshalAs(UnmanagedType.AsAny)> ByVal
picdesc As Object, ByRef iid As Guid, ByVal fOwn As Boolean) As
stdole.IPictureDisp 
    End Function 
 
    Private ReadOnly hCollector As New HandleCollector(“Icon handles”,
1000) 
 
 
    ‘PICTDESC is a union in native, so we’ll just 
    ‘define different ones for the different types 
    ‘the “unused” fields are there to make it the right 
    ‘size, since the struct in native is as big as the biggest 
    ‘union. 
    Private Class PICTDESC 
 
        ‘Picture Types 
        Public Const PICTYPE_UNINITIALIZED As Short = -1 
        Public Const PICTYPE_NONE As Short = 0 
        Public Const PICTYPE_BITMAP As Short = 1 
        Public Const PICTYPE_METAFILE As Short = 2 
        Public Const PICTYPE_ICON As Short = 3 
        Public Const PICTYPE_ENHMETAFILE As Short = 4 
 
        <StructLayout(LayoutKind.Sequential)> _ 
        Public Class Icon 
 
            Friend cbSizeOfStruct As Integer =
Marshal.SizeOf(GetType(PICTDESC.Icon)) 
            Friend picType As Integer = PICTDESC.PICTYPE_ICON 
            Friend hicon As IntPtr = IntPtr.Zero 
            Friend unused1 As Integer = 0 
            Friend unused2 As Integer = 0 
 
            Friend Sub New(ByVal icon As System.Drawing.Icon) 
                Me.hicon = icon.ToBitmap().GetHicon() 
            End Sub 
 
        End Class 
 
        <StructLayout(LayoutKind.Sequential)> _ 
        Public Class Bitmap 
 
            Friend cbSizeOfStruct As Integer =
Marshal.SizeOf(GetType(PICTDESC.Bitmap)) 
            Friend picType As Integer = PICTDESC.PICTYPE_BITMAP 
            Friend hbitmap As IntPtr = IntPtr.Zero 
            Friend hpal As IntPtr = IntPtr.Zero 
            Friend unused As Integer = 0 
 
            Friend Sub New(ByVal bitmap As System.Drawing.Bitmap) 
                Me.hbitmap = bitmap.GetHbitmap() 
            End Sub 
        End Class 
 
    End Class 
 
End Module 

动用办法很简短:

                       ByVal brush As IntPtr) As Integer

C#

5、运行后就可以看出成效。

    For i = 0 To 17

VB.NET

3、设置该窗体的背景图片位32为图像,bmp和png均可;

    Dim hBrush As IntPtr = Field.GetValue(B)

摘自 TX_OfficeDev的专栏

2、新建的窗体承继AlphaForm;

    Friend X As Single

[csharp] // Microsoft Office Outlook 2007
Add-in Sample Code // // THIS CODE AND INFORMATION ARE PROVIDED AS IS
WITHOUT WARRANTY OF ANY // KIND, EITHER EXPRESSED OR IMPLIED,
INC…

1、新建窗体;

  End Sub

摘自 Modest的专栏

  Private Sub DrawLineText(ByVal G As Graphics, ByVal T() As String, ByVal F As Font, ByVal B As Brush)

System Imports System.Drawing Imports
System.Drawing.Imaging Imports System.Windows.Forms Imports
System.Runtime.InteropServices #Region Win32 Class Friend Class Win32
#Reg…

  <DllImport(“gdiplus.dll”,
CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
_

4、调用SetBitmap,传入光滑度;

      Me.Y = y

    Next

      Me.X = rect.X

    End Property

    For i = 0 To j – 1

    ReDim tS(j – 1)

  Public Sub Draw4(ByVal Text As String)

    Friend Sub New(ByVal rect As RectangleF)

      tS(i) = Text.Substring(i * 52, 52)

  经测量试验,方法四和章程三的频率各有优劣。不过,方法四更类似大家直观的主张,并且对Font的供给亦不是极高。

  End Function

发表评论

电子邮件地址不会被公开。 必填项已用*标注