VB编程技巧两则(2)
?牐犜赪in95/98/2000中,每台计算机都有一个名字。运行regedit后,我们可以在“HKEY-LOCAL-MACHINE\System\CurrentControlSet\control\ComputerName\ComputerName”中发现“ComputerName”=“计算机名”,在regedit下可以查看和修改这个名字。我们还可在程序中通过Win32API提供的GetComputerName、SetComputerName这两个函数来查看和修改计算机的名字,这对于编写基于网络的应用程序是很重要的。下面以VB为例来探讨如何编写一个可查看和修改计算机名字的程序。
?牐?1.插入一个新模块,在其中添加如下代码:
?牐牐?声明 GetComputerName
?牐燚eclare Function GetComputerName Lib“kernel 32”Alias“GetComputerNameA”?煟˙yval lpBuffer As String,nSize As Long)As Long
?牐牐?声明 SetComputerName
?牐燚eclare Function SetComputerName Lib“kernel 32”Alias“SetComputerNameA”(Byval lp ComputerName As String)As Long
?牐牐Фㄒ逡桓龌袢〖扑慊值暮?
?牐燩ublic Function GetCName (CName) As Boolean
?牐燚im sComputerName As String '计算机的名字
?牐燚im lComputerName As Long
?牐牐Ъ扑慊值某ざ?
?牐燚im lResult As Long
?牐牐?GetComputerName的返回值
?牐燚im RV As Boolean
?牐牐?GetCName返回值,若为TRUE则表示操作成功
?牐爈ComputerNameLen=256
?牐爏ComputerName=Space (lComputerNameLen)??
?牐爈Result=GetComputerName(sComputerName,lCompputerNameLen)??
?牐營f lResult <>0 Then Cname=Left$(sComputerName,lComputerNameLen)??
?牐燫V=True
?牐燛lse RV=False
?牐燛nd If
?牐燝etCName=RV
?牐燛nd Function
?牐牐Фㄒ逡桓鲂薷募扑慊值暮?
?牐燩ublic Function SetCName(CName) As Boolean
?牐燚im lResult As Long
?牐燚im RV As Boolean
?牐爈Result=SetComputerName(CName)
?牐營f lResult <>0 Then
?牐燫V=True'修改成功
?牐燛lse RV=False
?牐燛nd If
?牐燬etCName=RV
?牐燛nd Function
?牐?2.在窗体中添加一命令按钮Command1,双击该按钮并在其中添加如下代码:
?牐燬ub Command1-Click() ?煟?
?牐燚IM CN AS String
?牐爔=GetCName(CN)??
?牐燩rint “This Computer Name is :”,CN
?牐燙N=“MYCOMPUTER”
?牐爔=SetCName (CN) ??
?牐燩rint “Now the Computer name is:”,CN
?牐燛nd Sub
?牐燨K,保存上述设置和代码,然后按F5运行该程序。
#1 二、编程实现Windows 95/98操作系统热启动
?牐犚贸绦蚴迪窒低车闹匦缕舳梢栽谀愕某绦蛑械饔肁PI函数来实现。建一个子函数:(以VB为例)
?牐燚eclare Function SystemParametersInfo Lib “user32” Alias -
?牐牎癝ystemParametersInfo”(ByVal uAction As Long,ByVal uParam As Long,
?牐燘yVal lpvParam As Any, ByVal fuWinIni As Long)As Long
?牐燬ub DisableCtrlAltDelete(bDisabled As Boolean)??
?牐燚im X As Long
?牐燲=SystemParametersInfo(97, bDisabled,CStr(1), 0)??
?牐燛nd Sub
?牐燙all DisableCtrlAltDelete(true)'禁止热启
?牐燙all DisableCtrlAltDelete(false)'允许热启