当前位置:编程文档 >> DELPHI >> Delphi操作系统编程
首页

Delphi操作系统编程

所属类别:DELPHI
推荐指数:★★★☆
文档人气:1815
本周人气:2
发布日期:2006-3-14

更改Windows95的墙纸。

   在Delphi中你可以很方便地更改墙纸,请参考以下的程序
   procedureChangeIt;
   var
   Reg:TregIniFile;
   begin
   Reg:ΚTRegIniFile.Create(′ControlPanel′);
   Reg.WriteString(′desktop′,′Wallpaper′,′c:ιpwin95ιforest.bmp′);
   Reg.WriteString(′desktop′,′TileWallpaper′,′1′);
   Reg.Free;
   SystemParametersInfo(SPI—SETDESKWALLPAPER,0,nil,SPIF—SENDWININICHANGE);
   end;

屏蔽系统按键
也许您希望您的程序在运行时不想让用户按系统按键 Alt-Tab 或  Ctrl-Alt-Del,那么可以通过以下的程序段屏蔽这些按键。  
procedure TForm1.FormCreate(Sender: TObject);  
begin  
var  
tmp : integer;
  begin
  tmp := 0;
  //屏蔽 Alt-Tab
  SystemParametersInfo( SPI_SETFASTTASKSWITCH, 1, @tmp, 0);
  //屏蔽 Ctrl-Alt-Del
  SystemParametersInfo( SPI_SCREENSAVERRUNNING, 1, @tmp, 0);   end;  end;

更换墙纸
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, PChar('WallPaper.BMP', 0);

设置系统日期和时间:
例如:
var
MyST:TSystemTime;
begin
with MyST do
begin
wYear:=1998;
wMonth:=12;
wDay:=7;
wHour:=8;
wMinute:=9;
wSecond:=10;
end;
SetSystemTime(MyST);
end;
s

如果隐藏和显示Windows的任务条?(Delphi 3 and 2.0)
如果隐藏和显示Windows的任务条?仅仅调用以下的函数就可以.
procedure hideTaskbar; //隐藏
var
wndHandle : THandle;
wndClass : array[0..50] of Char;
begin
StrPCopy(@wndClass[0], 'Shell_TrayWnd');
wndHandle := FindWindow(@wndClass[0], nil);
ShowWindow(wndHandle, SW_HIDE);
 
End;
 
procedure showTaskbar;
var
wndHandle : THandle;
wndClass : array[0..50] of Char;
begin
StrPCopy(@wndClass[0], 'Shell_TrayWnd');
wndHandle := FindWindow(@wndClass[0], nil);
ShowWindow(wndHandle, SW_RESTORE);
end;

控制面板大全
   在程序运行过程中启动控制面板的各个设置功能:
var x:cardinal;
begin
{启动控制面板}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL',9);
{辅助选项 属性-键盘}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL access.cpl,,1',9);
{辅助选项 属性-声音}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL access.cpl,,2',9);
{辅助选项 属性-显示}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL access.cpl,,3',9);
{辅助选项 属性-鼠标}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL access.cpl,,4',9);
{辅助选项 属性-常规}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL access.cpl,,5',9);
{添加/删除程序 属性-安装/卸载}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL Appwiz.cpl,,1',9);
{添加/删除程序 属性-Windows安装程序}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL Appwiz.cpl,,2',9);
{添加/删除程序 属性-启动盘}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL Appwiz.cpl,,3',9);
{显示 属性-背景}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,0',9);
{显示 属性-屏幕保护程序}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,1',9);
{显示 属性-外观}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,2',9);
{显示 属性-设置}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,3',9);
{Internet 属性-常规}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL Inetcpl.cpl,,0',9);
{Internet 属性-安全}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL Inetcpl.cpl,,1',9);
{Internet 属性-内容}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL Inetcpl.cpl,,2',9);
{Internet 属性-连接}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL Inetcpl.cpl,,3',9);
{Internet 属性-程序}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL Inetcpl.cpl,,4',9);
{Internet 属性-高级}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL Inetcpl.cpl,,5',9);
{区域设置 属性-区域设置}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL Intl.cpl,,0',9);
{区域设置 属性-数字}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL Intl.cpl,,1',9);
{区域设置 属性-货币}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL Intl.cpl,,2',9);
{区域设置 属性-时间}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL Intl.cpl,,3',9);
{区域设置 属性-日期}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL Intl.cpl,,4',9);
{游戏控制器-一般}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL Joy.cpl,,0',9);
{游戏控制器-高级}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL Joy.cpl,,1',9);
{鼠标 属性}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL Main.cpl',9);
{多媒体 属性-音频}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL Mmsys.cpl,,0',9);
{多媒体 属性-视频}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL Mmsys.cpl,,1',9);
{多媒体 属性-MIDI}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL Mmsys.cpl,,2',9);
{多媒体 属性-CD音乐}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL Mmsys.cpl,,3',9);
{多媒体 属性-设备}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL Mmsys.cpl,,4',9);
{调制解调器 属性}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL Modem.cpl',9);
{网络}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL Netcpl.cpl',9);
{密码 属性}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL Password.cpl',9);
{扫描仪与数字相机 属性}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL Sticpl.cpl',9);
{系统 属性-常规}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL Sysdm.cpl,,0',9);
{系统 属性-设备管理器}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL Sysdm.cpl,,1',9);
{系统 属性-硬件配置文件}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL Sysdm.cpl,,2',9);
{系统 属性-性能}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL Sysdm.cpl,,3',9);
{日期/时间 属性}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL timedate.cpl',9);
{电源管理 属性}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL Powercfg.cpl',9);
{拨号属性}
x:=winexec('rundll32.exe shell32.dll,Control_RunDLL Telephon.cpl',9);
{----------------------调用错误----------------------}
if x=0 then messagebox(0,'程序超出内存','错误',0);
if x=ERROR_BAD_FORMAT then messagebox(0,'该程序非一个合法的Win32.EXE程序).','错误',0);
if x=ERROR_FILE_NOT_FOUND then messagebox(0,'指定文件没找到','错误',0);
if x=ERROR_PATH_NOT_FOUND then messagebox(0,'指定路径没找到','错误',0);
end;

控制壁纸
控制Windows 95的壁纸,以下程序使壁纸变为我们想要的位图,如果THEPCHAR为空,那么就取消壁纸,变为Win默认色彩; 但这种方法只是暂时的,在WINDOWS重新启动后还是原来的位图,这时就需要对WIN.INI文件进行写操作,才能保存住我们改动的图片不被替换。
var THEPCHAR:pchar;
begin
THEPCHAR:='e:\a.bmp';
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, THEPCHAR, SPIF_SENDWININICHANGE)
end;

辨别分辨率
要得到显示器的分辨率,由下列程序得到:
var
x:longint;
a:string;
begin
x := GetSystemMetrics(SM_CXSCREEN);
Str(x,a);
Label1.Caption := '显示器水平分辨率' + a;
x := GetSystemMetrics(SM_CYSCREEN);
Str(x,a);
Label2.Caption := '显示器垂直分辨率' + a;
end;

启动屏幕保护
执行下列语句开始Windows屏幕保护程序
SendMessage(HWND_BROADCAST,WM_SYSCOMMAND,SC_SCREENSAVE,0);

取系统颜色
要取得系统颜色,把参数换一下就可以
COLOR_SCROLLBAR      滚动条
COLOR_BACKGROUND      桌面背景
COLOR_ACTIVECAPTION     活动窗口标题
cOLOR_INACTIVECAPTION   非活动窗口标题
cOLOR_MENU         菜单
COLOR_WINDOW        窗口背景
cOLOR_WINDOWFRAME     窗口框
COLOR_MENUTEXT       窗口文字
COLOR_WINDOWTEXT      3D 阴影 (Win95)
COLOR_CAPTIONTEXT     标题文字
COLOR_ACTIVEBORDER     活动窗口边框
COLOR_INACTIVEBORDER    非活动窗口边框
COLOR_APPWORKSPACE     MDI 窗口背景
COLOR_HIGHLIGHT      选择条背景
COLOR_HIGHLIGHTTEXT    选择条文字
COLOR_BTNFACE       按钮
COLOR_BTNSHADOW      3D 按钮阴影
COLOR_GRAYTEXT       灰度文字
COLOR_BTNTEXT       按钮文字
COLOR_INACTIVECAPTIONTEXT 非活动窗口文字
COLOR_BTNHIGHLIGHT     3D 选择按钮}
var i:integer;
begin
//取得活动窗口标题颜色
i :=GetSysColor(COLOR_ACTIVECAPTION);
end;

动态修改显示器分辨率
Windows提供给我们两个API函数,可以动态调整显示器的分辨率,他们是EnumDisplaySettings() 和ChangeDisplaySettings(),下面这个例子就是了
function DynamicResolution(X, Y: word): BOOL;
var
lpDevMode: TDeviceMode;
begin
Result := EnumDisplaySettings(nil, 0, lpDevMode);
if Result then
begin
lpDevMode.dmFields := DM_PELSWIDTH Or DM_PELSHEIGHT;
lpDevMode.dmPelsWidth := X;
lpDevMode.dmPelsHeight := Y;
Result := ChangeDisplaySettings(lpDevMode, 0) = DISP_CHANGE_SUCCESSFUL;
end
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if DynamicResolution(640, 480) then
ShowMessage('Now is 640*480');
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if DynamicResolution(800, 600) then
ShowMessage('Now is 800*600');
end;

隐藏桌面上的图标
   使桌面上的图标消失,连桌面上的右键功能也不能使用如果不让初学者乱用“我的电脑”的话,那么把程序放到“启动”里,顺便把“Windows资源管理器”也去掉, 再加上个多用户密码,那么想破坏电脑文件也无从下手了。该程序Delphi 4里通过
var
hDesktop : THandle;
begin
hDesktop := FindWindow('Progman', nil);
ShowWindow(hDesktop, SW_HIDE);
end;

怎样获得程序启动快捷键中记载的程序路径
ShortCut that Started Application?
OK, your problem is not setting current directory but you want to take
"ANY" shortcut and resolve its properties. Am I correct now ? :))
Take a look at unit ShlObj and IShellLink. (API help file)
var SCut: IShellLink;
SCut.Resolve
GetWorkingDirectory - Retrieves the name of the working directory for a
shell link object
GetPath - Retrieves the path and filename of a shell link object.
I have not done this before, just giving you ideas

让彩色光标出现在Delphi程序
  在Delphi中用Loadcursor()得到的光标只有黑白两色,怎样在程序中得到彩色光标呢?笔者尝试制作了以下程序
   方法一 用Loadcursorfromfile()从外部调入图标作为光标
   Loadcursorfromfile()函数可以读*CUR,*ICO,*ANI为后缀的文件作为光标,其中ICO为彩色图标格式(可用Image Editor制作),ANI为动画光标格式。以下为打开一图标作为光标的演示程序段,当光标移动到测试区域内光标会变成选定的图案;
   {设:opendialog1:Topendialog;Bitbtn1:Tbitbtn}
   procedure TForm1.BitBtn1Click(Sender:TObject);
   var tt:pchar;size:integer;s:string;
   begin
   if opendialog1.Execute then
   begin
   size:=length(opendialog1.filename);
   getmem(tt,size);
   s:=opendialog1.filename;
   strpcopy(tt,s);
   screen.cursors[2]:=loadcursorfromfile(tt);
   bf.cursor:=2;
   freemem(tt,size);
   end;
   end;
   方法二 从资源文件加载彩色光标
   用方法一发送程序时必须包含*CUR文件,因而从资源文件中加载彩色光标是更可行的方法。用图标存放彩色光标,使用时把图标存入临时文件,用Loadcursorfromfile()从临时文件读出彩色光标。
   程序段:
   procedure ZloadfromResourse(screenindex:integer;name:Pchar);
   var td:ticon;
   begin
   try
   td:=ticon.Create;
   td.Handle:=LoadIcon(Hinstance,name);
   td.SaveToFile(′temp.cur′);
   screen.Cursors[screenindex]:=loadcursorfromfile(′temp.cur′);
   deletefile(′temp.cur′);
   finally
   td.free;
   end;
   end;
   此程序把名字为name的图标变为序号为screenindex的光标;
   例:
   ZloadfromResourse(2,′myicon′);
   Form1.cursor:=2;

制作一个不在任务栏及任务列表中显示的幽灵程序
许多地方提到了如何将在任务栏隐藏应用程序,使用的方法都是将Application的属性变为WS_EX_TOOLWINDOW。即在WinMain函数中加入如下代码:

DWORD dwExStyle = GetWindowLong(Application->Handle, GWL_EXSTYLE);
dwExStyle |= WS_EX_TOOLWINDOW;
SetWindowLong(Application->Handle, GWL_EXSTYLE, dwExStyle);
Application->Initialize();
Application->CreateForm(__classid(TForm1), &Form1);
Application->ShowMainForm = false;
ShowWindow(Application->Handle, SW_HIDE);
Application->Run();
这样做之后,应用程序就不会在任务栏显示了,但在WIN9X的任务列表中仍能看到。要想在任务列表也将自己的程序隐藏就要借用kernel32.DLL中的RegisterServiceProcess这个函数了。由于要从DLL加载函数,我们先在头文件中加入:
typedef DWORD (__stdcall *pRegFunction)(DWORD, DWORD);
在主Form的类定义中加入两个私有属性:
HINSTANCE hKernelLib;
pRegFunction RegisterServiceProcess;
用于记录从DLL加入的函数信息。
然后在主Form的构造代码中加入如下代码:
hKernelLib = LoadLibrary("kernel32.dll");
if(hKernelLib)
{
RegisterServiceProcess =
(pRegFunction)GetProcAddress(hKernelLib,
"RegisterServiceProcess");
if(RegisterServiceProcess)
RegisterServiceProcess(GetCurrentProcessId(),RSP_SIMPLE_SERVICE);
}
这段代码主要是把当前进程变为一个系统服务,从而在任务列表中把程序隐藏起来,相应的在构析代码中加入
if(hKernelLib)
{
if(RegisterServiceProcess)
RegisterServiceProcess(GetCurrentProcessId(),
RSP_UNREGISTER_SERVICE);
FreeLibrary(hKernelLib);
}
这样处理之后,程序就不在任务列表中出现了,但仍可用EnumWindows函数查出来。

如何决定Windows的版本
   Windows具有多个版本,一个应用程序或者具有运行在多个Windows版本下的灵活性,或者通过条件编译指令,编译成只能在一个操作平台下工作。
   下面介绍一种方法,可以使应用程序能动态地决定Windows操作系统的版本。应用程序通过调用Windows API函数GetVersionEx可以获得Windows的版本信息,该函数使用一个TOSVersionInfo类的变参,所有Windows版本信息就包含在其中,其结构如下:
   typedef struct _OSVERSIONINFO{
           DWORD dwOSVersionInfoSize; //结构的大小
           DWORD dwMajorVersion; //主版本
           DWORD dwMinorVersion; //副版本
           DWORD dwBuildNumber; //建立版本
           DWORD dwPlatformId; //操作平台标识
           TCHAR szCSDVersion[ 128 ]; //版本标识字符串 } OSVERSIONINFO;
   下面是使用该函数的一个例子:
   procedure TForm1.Button1Click(Sender: TObject);
   var VersionInfo: TOSVersionInfo;
   begin
    VersionInfo.dwOSVersionInfoSize := Sizeof(TOSVersionInfo);
    GetVersionEx(VersionInfo);
    case VersionInfo.dwPlatformID of
     VER_PLATFORM_WIN32S: Do_SomeThing;
     VER_PLATFORM_WIN32_WINDOWS: Do_SomeOtherThing;
     VER_PLATFORM_WIN32_NT: Do_SomeThingElse;
    end;
   end;

内存知多少?
   下面介绍一种方法可以决定系统内存的多少、使用状态等信息。更重要的是,应用程序可以利用这项技术来决定客户机的可用内存的大小,利用这些信息,应用程序可以动态地优化程序的性能。例如,如果有足够的内存可以利用双缓存优化位图的操作。
   利用Windows API函数GlobalMemoryStatus可以完成上述功能。GlobalMemoryStatus接收一个类型为TMemoryStatus的变参,通过这个参数就可以获得Windows当前的内存状态。TMemoryStatus的结构如下:
     typedef struct _MEMORYSTATUS { // mst
            DWORD dwLength; // sizeof(MEMORYSTATUS),该记录结构的大小
            DWORD dwMemoryLoad; // 使用内存所占百分比
            DWORD dwTotalPhys; // 物理内存的字节数
            DWORD dwAvailPhys; // 自由物理可用内存字节数
            DWORD dwTotalPageFile; // 页文件字节数 
            DWORD dwAvailPageFile; // 页文件的自由字节数
            DWORD dwTotalVirtual; // 地址空间的用户字节数
            DWORD dwAvailVirtual; // 自由用户字节数 } MEMORYSTATUS, *LPMEMORYSTATUS;
   下面是使用GlobalMemoryStatus函数的一个例子:
    procedure TForm1.Button1Click(Sender: TObject);
    var MemoryStatus: TMemoryStatus;
    begin
     MemoryStatus.dwLength := sizeof(MemoryStatus);
     GlobalMemoryStatus(MemoryStatus);
     Label1.Caption := 'Total Physical Memory: ' + IntToStr(MemoryStatus.dwTotalPhys);
    end;

获得消逝时间
   在测试硬件或软件的效率时或跟踪用户的响应速度时,需要测定消逝的时间。多数程序员使用一个TDateTime变量和Now函数来实现测定消逝时间的目的。 但是,一种更简单的方法是使用Windows API函数GetTickCount。GetTickCount函数返回从启动Windows后消逝的毫秒数。如果函数成功地返回,返回值就是从启动Windows后消逝的毫秒数。

文档说明:

     

相关文档