MaxScript使用.Net的范例
在图像文件上绘制文字
Fn DrawStringToBitmapFile imageFile theString posX posY drawFont:"黑体" drawColor:Red fontsize:12 =
(
local DotNetImageSource,DotNetImage,DotNetGraphics,DrawFont,DrawColor,DrawBrush
DotNetImageSource = (DotNetClass "System.Drawing.Image").FromFile imageFile
DotNetImage = DotNetObject "System.Drawing.Bitmap" DotNetImageSource.Width DotNetImageSource.Height
DotNetGraphics = (DotNetClass "System.Drawing.Graphics").FromImage DotNetImage
DrawFont = DotNetObject "System.Drawing.Font" drawFont fontsize
DrawColor = (DotNetClass "System.Drawing.Color").fromArgb drawColor.r drawColor.g drawColor.b
DrawBrush = DotNetObject "System.Drawing.SolidBrush" DrawColor
DotNetGraphics.DrawImage DotNetImageSource (DotNetObject "System.Drawing.Point" 0 0)
DotNetImageSource.Dispose()
DotNetGraphics.DrawString theString DrawFont DrawBrush posX posY
DotNetImage.Save imageFile
DotNetImage.Dispose()
)
/*
DrawStringToBitmapFile "D:\\test.png" "TEST%#@嘻嘻哈哈" 150 150
*/
截屏
Fn ScreenCapture PosX PosY ImgW ImgH ImgFileName =
(
try
(
local DotNetBmp,DotNetGraphics,DotNetPoint
local tempBmp,tempGraphic
DotNetBmp = DotNetClass "System.Drawing.Bitmap"
DotNetGraphics = DotNetClass "System.Drawing.Graphics"
DotNetPoint = DotNetClass "System.Drawing.Point"
tempBmp = DotNetObject "System.Drawing.Bitmap" ImgW ImgH
tempGraphic = DotNetGraphics.FromImage tempBmp
tempGraphic.CopyFromScreen (DotNetObject DotNetPoint PosX PosY) \
(DotNetObject DotNetPoint 0 0) tempBmp.Size
tempBmp.Save ImgFileName
tempGraphic.Dispose()
true
)
catch (false)
)
ScreenCapture 0 0 200 200 "c:\\testScreenCapture.png"
创建及写入文本文件
DotNetFile = DotNetClass "System.IO.File"
theFileName = "C:\\test.txt"
global StreamWriter , theString , StreamWriterArray
if ( not (DotNetFile.Exists theFileName)) then
(
tFileStream = DotNetFile.Create theFileName
StreamWriter = DotNetObject "System.IO.StreamWriter" tFileStream
)
else
(
StreamWriter = DotNetFile.AppendText theFileName
)
for i = 1 to 10 do
(
theString = (random 11111111 99999999) as string
StreamWriter.WriteLine theString
)
StreamWriter.Close()
StreamWriter.Dispose ()
多选文件窗口
Fn GetMutiOpenFileName Title:"Select Files" FileFilter:"All Files (*.*)|*.*" =
(
local OpenFileDialog,OpenFileResult
OpenFileDialog = DotNetObject "System.Windows.Forms.OpenFileDialog"
OpenFileDialog.title = Title
OpenFileDialog.Multiselect = true
OpenFileDialog.Filter = FileFilter
OpenFileDialog.FilterIndex = 1
OpenFileResult = OpenFileDialog.ShowDialog()
if OpenFileResult.Equals OpenFileResult.OK then
(
OpenFileDialog.FileNames
)
else Undefined
)
GetMutiOpenFileName()
获取硬件信息
Fn GetHardwareInfo HwClass HwProp =
(
local HwInfo,HwInfoEnumerator,OutArray
OutArray = #()
try
(
HwInfo = (DotNetObject "System.Management.ManagementClass" HwClass).GetInstances()
)
catch
(
DotNet.LoadAssembly "System.Management.dll"
HwInfo = (DotNetObject "System.Management.ManagementClass" HwClass).GetInstances()
)
HwInfoEnumerator = HwInfo.GetEnumerator()
while (HwInfoEnumerator.MoveNext()) do
(
local TempString = HwInfoEnumerator.Current.Item[HwProp] as string
if TempString != "" do append OutArray TempString
)
OutArray
)
Fn GetHardwareClassInfo HwClass =
(
local HwInfo,HwInfoEnumerator,OutArray
OutArray = #()
try
(
HwInfo = (DotNetObject "System.Management.ManagementClass" HwClass).GetInstances()
)
catch
(
DotNet.LoadAssembly "System.Management.dll"
HwInfo = (DotNetObject "System.Management.ManagementClass" HwClass).GetInstances()
)
HwInfoEnumerator = HwInfo.GetEnumerator()
while (HwInfoEnumerator.MoveNext()) do
(
PropEnumerator = HwInfoEnumerator.Current.Properties.GetEnumerator()
while (PropEnumerator.MoveNext()) do
(
local TempString = PropEnumerator.Current.Name as string
if TempString != "" do append OutArray TempString
)
)
OutArray
)
GetHardwareClassInfo "Win32_Processor" --获取相应类别的属性列表,此处获取的是Cpu
GetHardwareInfo "Win32_Processor" "ProcessorID" --获取CpuID
GetHardwareInfo "Win32_PhysicalMedia" "SerialNumber" --获取硬盘序列号
GetHardwareInfo "Win32_DiskDrive" "Model" --获取硬盘名称
/* 以下为可查询的设备类别列表
Win32_OperatingSystemQFE
Win32_OSRecoveryConfiguration
Win32_PageFile
Win32_PageFileElementSetting
Win32_PageFileSetting
Win32_PageFileUsage
Win32_ParallelPort
Win32_Patch
Win32_PatchFile
Win32_PatchPackage
Win32_PCMCIAController
Win32_Perf
Win32_PerfRawData
Win32_PerfRawData_ASP_ActiveServerPages
Win32_PerfRawData_ASPNET_114322_ASPNETAppsv114322
Win32_PerfRawData_ASPNET_114322_ASPNETv114322
Win32_PerfRawData_ASPNET_ASPNET
Win32_PerfRawData_ASPNET_ASPNETApplications
Win32_PerfRawData_IAS_IASAccountingClients
Win32_PerfRawData_IAS_IASAccountingServer
Win32_PerfRawData_IAS_IASAuthenticationClients
Win32_PerfRawData_IAS_IASAuthenticationServer
Win32_PerfRawData_InetInfo_InternetInformationServicesGlobal
Win32_PerfRawData_MSDTC_DistributedTransactionCoordinator
Win32_PerfRawData_MSFTPSVC_FTPService
Win32_PerfRawData_MSSQLSERVER_SQLServerAccessMethods
Win32_PerfRawData_MSSQLSERVER_SQLServerBackupDevice
Win32_PerfRawData_MSSQLSERVER_SQLServerBufferManager
Win32_PerfRawData_MSSQLSERVER_SQLServerBufferPartition
Win32_PerfRawData_MSSQLSERVER_SQLServerCacheManager
Win32_PerfRawData_MSSQLSERVER_SQLServerDatabases
Win32_PerfRawData_MSSQLSERVER_SQLServerGeneralStatistics
Win32_PerfRawData_MSSQLSERVER_SQLServerLatches
Win32_PerfRawData_MSSQLSERVER_SQLServerLocks
Win32_PerfRawData_MSSQLSERVER_SQLServerMemoryManager
Win32_PerfRawData_MSSQLSERVER_SQLServerReplicationAgents
Win32_PerfRawData_MSSQLSERVER_SQLServerReplicationDist
Win32_PerfRawData_MSSQLSERVER_SQLServerReplicationLogreader
Win32_PerfRawData_MSSQLSERVER_SQLServerReplicationMerge
Win32_PerfRawData_MSSQLSERVER_SQLServerReplicationSnapshot
Win32_PerfRawData_MSSQLSERVER_SQLServerSQLStatistics
Win32_PerfRawData_MSSQLSERVER_SQLServerUserSettable
Win32_PerfRawData_NETFramework_NETCLRExceptions
Win32_PerfRawData_NETFramework_NETCLRInterop
Win32_PerfRawData_NETFramework_NETCLRJit
Win32_PerfRawData_NETFramework_NETCLRLoading
Win32_PerfRawData_NETFramework_NETCLRLocksAndThreads
Win32_PerfRawData_NETFramework_NETCLRMemory
Win32_PerfRawData_NETFramework_NETCLRRemoting
Win32_PerfRawData_NETFramework_NETCLRSecurity
Win32_PerfRawData_Outlook_Outlook
Win32_PerfRawData_PerfDisk_PhysicalDisk
Win32_PerfRawData_PerfNet_Browser
Win32_PerfRawData_PerfNet_Redirector
Win32_PerfRawData_PerfNet_Server
Win32_PerfRawData_PerfNet_ServerWorkQueues
Win32_PerfRawData_PerfOS_Cache
Win32_PerfRawData_PerfOS_Memory
Win32_PerfRawData_PerfOS_Objects
Win32_PerfRawData_PerfOS_PagingFile
Win32_PerfRawData_PerfOS_Processor
Win32_PerfRawData_PerfOS_System
Win32_PerfRawData_PerfProc_FullImage_Costly
Win32_PerfRawData_PerfProc_Image_Costly
Win32_PerfRawData_PerfProc_JobObject
Win32_PerfRawData_PerfProc_JobObjectDetails
Win32_PerfRawData_PerfProc_Process
Win32_PerfRawData_PerfProc_ProcessAddressSpace_Costly
Win32_PerfRawData_PerfProc_Thread
Win32_PerfRawData_PerfProc_ThreadDetails_Costly
Win32_PerfRawData_RemoteAccess_RASPort
Win32_PerfRawData_RemoteAccess_RASTotal
Win32_PerfRawData_RSVP_ACSPerRSVPService
Win32_PerfRawData_Spooler_PrintQueue
Win32_PerfRawData_TapiSrv_Telephony
Win32_PerfRawData_Tcpip_ICMP
Win32_PerfRawData_Tcpip_IP
Win32_PerfRawData_Tcpip_NBTConnection
Win32_PerfRawData_Tcpip_NetworkInterface
Win32_PerfRawData_Tcpip_TCP
Win32_PerfRawData_Tcpip_UDP
Win32_PerfRawData_W3SVC_WebService
Win32_PhysicalMemory
Win32_PhysicalMemoryArray
Win32_PhysicalMemoryLocation
Win32_PNPAllocatedResource
Win32_PnPDevice
Win32_PnPEntity
Win32_PointingDevice
Win32_PortableBattery
Win32_PortConnector
Win32_PortResource
Win32_POTSModem
Win32_POTSModemToSerialPort
Win32_PowerManagementEvent
Win32_Printer
Win32_PrinterConfiguration
Win32_PrinterController
Win32_PrinterDriverDll
Win32_PrinterSetting
Win32_PrinterShare
Win32_PrintJob
Win32_PrivilegesStatus
Win32_Process
Win32_Processor
Win32_ProcessStartup
Win32_Product
Win32_ProductCheck
Win32_ProductResource
Win32_ProductSoftwareFeatures
Win32_ProgIDSpecification
Win32_ProgramGroup
Win32_ProgramGroupContents
Win32_ProgramGroupOrItem
Win32_Property
Win32_ProtocolBinding
Win32_PublishComponentAction
Win32_QuickFixEngineering
Win32_Refrigeration
Win32_Registry
Win32_RegistryAction
Win32_RemoveFileAction
Win32_RemoveIniAction
Win32_ReserveCost
Win32_ScheduledJob
Win32_SCSIController
Win32_SCSIControllerDevice
Win32_SecurityDescriptor
Win32_SecuritySetting
Win32_SecuritySettingAccess
Win32_SecuritySettingAuditing
Win32_SecuritySettingGroup
Win32_SecuritySettingOfLogicalFile
Win32_SecuritySettingOfLogicalShare
Win32_SecuritySettingOfObject
Win32_SecuritySettingOwner
Win32_SelfRegModuleAction
Win32_SerialPort
Win32_SerialPortConfiguration
Win32_SerialPortSetting
Win32_Service
Win32_ServiceControl
Win32_ServiceSpecification
Win32_ServiceSpecificationService
Win32_SettingCheck
Win32_Share
Win32_ShareToDirectory
Win32_ShortcutAction
Win32_ShortcutFile
Win32_ShortcutSAP
Win32_SID
Win32_SMBIOSMemory
Win32_SoftwareElement
Win32_SoftwareElementAction
Win32_SoftwareElementCheck
Win32_SoftwareElementCondition
Win32_SoftwareElementResource
Win32_SoftwareFeature
Win32_SoftwareFeatureAction
Win32_SoftwareFeatureCheck
Win32_SoftwareFeatureParent
Win32_SoftwareFeatureSoftwareElements
Win32_SoundDevice
Win32_StartupCommand
Win32_SubDirectory
Win32_SystemAccount
Win32_SystemBIOS
Win32_SystemBootConfiguration
Win32_SystemDesktop
Win32_SystemDevices
Win32_SystemDriver
Win32_SystemDriverPNPEntity
Win32_SystemEnclosure
Win32_SystemLoadOrderGroups
Win32_SystemLogicalMemoryConfiguration
Win32_SystemMemoryResource
Win32_SystemNetworkConnections
Win32_SystemOperatingSystem
Win32_SystemPartitions
Win32_SystemProcesses
Win32_SystemProgramGroups
Win32_SystemResources
Win32_SystemServices
Win32_SystemSetting
Win32_SystemSlot
Win32_SystemSystemDriver
Win32_SystemTimeZone
Win32_SystemUsers
Win32_TapeDrive
Win32_TemperatureProbe
Win32_Thread
Win32_TimeZone
Win32_Trustee
Win32_TypeLibraryAction
Win32_UninterruptiblePowerSupply
Win32_USBController
Win32_USBControllerDevice
Win32_UserAccount
Win32_UserDesktop
Win32_VideoConfiguration
Win32_VideoController
Win32_VideoSettings
Win32_VoltageProbe
Win32_WMIElementSetting
Win32_WMISetting
*/
执行CMD的时候隐藏窗口
Fn HiddenCMD CommandString =
(
local DotNetProcess ,CmdOutput
DotNetProcess = DotNetObject "System.Diagnostics.Process"
DotNetProcess.StartInfo.WindowStyle = (DotNetClass "System.Diagnostics.ProcessWindowStyle").Hidden
DotNetProcess.StartInfo.CreateNoWindow = true
DotNetProcess.StartInfo.UseShellExecute = false
DotNetProcess.StartInfo.RedirectStandardInput = true;
DotNetProcess.StartInfo.RedirectStandardOutput = true;
DotNetProcess.StartInfo.FileName = "cmd.exe"
DotNetProcess.Start()
DotNetProcess.StandardInput.WriteLine(CommandString )
DotNetProcess.StandardInput.WriteLine("exit")
DotNetProcess.WaitForExit(500);
CmdOutput = DotNetProcess.StandardOutput.ReadToEnd()
CmdOutput
)
终止进程
Fn FnCloseProcessByName ProcessName = for tProcess in ((DotNetClass "System.Diagnostics.Process").GetProcessesByName ProcessName) do tProcess.Kill()
FnCloseProcessByName “notepad”
先打开Windows自带的记事本,然后在Max9中运行这段脚本,将会关闭记事本
(DotNetClass "System.Environment").SetEnvironmentVariable "Path" "d:\\"