我以前的练习<br>=========================================================================<br>dlgUnit.pas<br>===========<br>unit dlgUnit;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls,<br> Forms, Dialogs,StdCtrls, ExtCtrls;<br><br>type<br> TForm1 = class(TForm)<br> Button1: TButton;<br> Button2: TButton;<br> Button3: TButton;<br> Button4: TButton;<br> Button5: TButton;<br> Label1: TLabel;<br> CheckBox1: TCheckBox;<br> Button6: TButton;<br> Bevel1: TBevel;<br> Img: TImage;<br> Button7: TButton;<br> Memo1: TMemo;<br> procedure Button2Click(Sender: TObject);<br> procedure Button6Click(Sender: TObject);<br> private<br> procedure WMQueryEndSession(var Msg: TMessage);<br> message WM_QueryEndSession; // 这个消息是为防止<br> // 真的退出而设<br>// function InitIconFileName: string;<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br>// 退出系统的关机函数<br>procedure ExitWindowsDialog(ParentWnd: HWND); stdcall;<br> external 'Shell32.dll' index 60;<br><br>// 设备变化后的关机重启选择函数的标志说明<br>// Logoff: Flags := EWX_LOGOFF;<br>// Shutdown: Flags := EWX_SHUTDOWN;<br>// Reboot: Flags := EWX_REBOOT;<br>// Restart: Flags := EW_RESTARTWINDOWS;<br>function RestartDialog(ParentWnd: HWND; Reason: PAnsiChar;<br> Flags: LongInt): LongInt; stdcall;<br> external 'Shell32.dll' index 59;<br><br>// 选择图标的函数<br>function PickIconDlgA(OwnerWnd: HWND; lpstrFile: PAnsiChar;<br> var nMaxFile: LongInt; var lpdwIconIndex: LongInt):<br> LongBool;stdcall; external 'SHELL32.DLL' index 62;<br><br>// 运行对话框函数<br>procedure RunFileDlgA(OwnerWnd: HWND; Icon: HICON; lpstrDirectory: PAnsiChar;<br> lpstrTitle: PAnsiChar; lpstrDescription: PAnsiChar; Flags: LongInt);<br> stdcall; external 'Shell32.dll' index 61;<br><br>implementation<br> uses ShellAPI;<br> Const App='Windows 未文档化的函数演示';<br> Flags: LongInt = 0;<br> RFF_NOBROWSE = 1;<br>{$R *.DFM}<br><br>procedure TForm1.WMQueryEndSession(var Msg: TMessage);<br>begin // 只演示,不要真的退出 Windows 消息响应<br> if CheckBox1.Checked then Msg.Result := 0<br> else Msg.Result := 1;<br>end;<br><br>{function TForm1.InitIconFileName: string;<br>var<br> MSysDir: array[0..MAX_PATH - 1] of Char;<br> SysDir: string;<br>begin<br> GetSystemDirectory(MSysDir, 255);<br> SysDir := StrPas(MSysDir);<br> if SysDir[Length(SysDir)] <> '/' then<br> begin<br> SysDir := SysDir + '/';<br> end;<br> Result := SysDir + 'SHELL32.DLL';<br>end; }<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>{====实际使用这个函数作为过渡====<br>function RestartExecute: Boolean;<br>begin<br>if RestartDialog(0,nil,Flags) = IDYES then<br> begin<br> Result := True;<br> end<br>else<br> Result := False;<br>end;<br>=================================}<br>begin<br>Flags := Flags or RFF_NOBROWSE;<br> case (Sender as TButton).tag of<br> 0: RestartDialog(self.Handle, '', EWX_SHUTDOWN);<br> 1: RestartDialog(self.Handle, '', EWX_LOGOFF);<br> 2: RestartDialog(self.Handle, '', EWX_REBOOT);<br> 3: RestartDialog(self.Handle, '', EW_RESTARTWINDOWS);<br><br> 4: ExitWindowsDialog(self.Handle);<br> { 下一句文字显示在 Win2000 中有问题,没时间了,不搞了}<br> 5: RunFileDlgA(self.Handle,self.Icon.Handle,'C:/',<br> '运行','',Flags);<br> end;<br>end;<br><br>procedure TForm1.Button6Click(Sender: TObject);<br>var // 本段在调试中老放CPU画面,但实际却可以用。<br> fFileName,Temp: string;<br> S: array [0..MAX_PATH - 1] of WideChar;//StrPas<br>// Tem: array [0..MAX_PATH - 1] of Char;<br> fSize, fIndex: LongInt;<br> I:Integer;<br> Icon: TIcon;<br>begin // 我没有办法解决发出的 PAnsiChar 如何保证<br> // 既能送出PAnsiChar又能接收PWideChar!!<br> SetLength(fFileName, 255); // 给fFileName设定长度<br><br> GetSystemDirectory(PChar(fFileName), 255);// 取得系统目录<br> { 获得实际的fFileName }<br> SetLength(fFileName, StrLen(PChar(fFileName)));<br> { 拼接Shell32.dll }<br> fFileName:=fFileName + '/' + 'SHELL32.DLL';<br> fSize := MAX_PATH;<br> { 我用了以下的办法解决我的困惑 }<br> For I:=Low(S) to Length(fFileName) do<br> S:=WideChar(fFileName);<br><br> if PickIconDlgA(self.Handle,PAnsiChar(@S),fSize, fIndex)<br> and (fIndex <> -1) then<br> begin<br> SetLength(Temp, MAX_PATH-1);<br> Temp := S; // 将WideString转换回来,这里的 S<br> // 已经是 PAnsiChar 了<br><br> Icon := TIcon.Create;<br> Form1.Caption :=App+' '+ 'FileName:'+Temp+' ,'<br> +'Icon:'+ IntToStr(fIndex);<br> Icon.Handle := ExtractIcon(hInstance, PChar(Temp), fIndex);<br> Img.Picture.Icon := Icon;<br> Form1.Icon := Img.Picture.Icon;<br> Icon := nil;<br> Icon.Free;<br> end;<br><br>end;<br><br>end.<br>====================================================================<br>dlgUnit.dfm<br>===================<br>object Form1: TForm1<br> Left = 207<br> Top = 143<br> BorderIcons = [biSystemMenu, biMinimize]<br> BorderStyle = bsSingle<br> Caption = 'Windows 未文档化的函数演示'<br> ClientHeight = 400<br> ClientWidth = 632<br> Color = clBtnFace<br> Font.Charset = GB2312_CHARSET<br> Font.Color = clWindowText<br> Font.Height = -12<br> Font.Name = '宋体'<br> Font.Style = []<br> Icon.Data = {<br> 0000010001002020000000000000A80800001600000028000000200000004000<br> 0000010008000000000080040000000000000000000000000000000000000000<br> 0000000080000080000000808000800000008000800080800000C0C0C000C0DC<br> C000F0CAA6000020400000206000002080000020A0000020C0000020E0000040<br> 0000004020000040400000406000004080000040A0000040C0000040E0000060<br> 0000006020000060400000606000006080000060A0000060C0000060E0000080<br> 0000008020000080400000806000008080000080A0000080C0000080E00000A0<br> 000000A0200000A0400000A0600000A0800000A0A00000A0C00000A0E00000C0<br> 000000C0200000C0400000C0600000C0800000C0A00000C0C00000C0E00000E0<br> 000000E0200000E0400000E0600000E0800000E0A00000E0C00000E0E0004000<br> 0000400020004000400040006000400080004000A0004000C0004000E0004020<br> 0000402020004020400040206000402080004020A0004020C0004020E0004040<br> 0000404020004040400040406000404080004040A0004040C0004040E0004060<br> 0000406020004060400040606000406080004060A0004060C0004060E0004080<br> 0000408020004080400040806000408080004080A0004080C0004080E00040A0<br> 000040A0200040A0400040A0600040A0800040A0A00040A0C00040A0E00040C0<br> 000040C0200040C0400040C0600040C0800040C0A00040C0C00040C0E00040E0<br> 000040E0200040E0400040E0600040E0800040E0A00040E0C00040E0E0008000<br> 0000800020008000400080006000800080008000A0008000C0008000E0008020<br> 0000802020008020400080206000802080008020A0008020C0008020E0008040<br> 0000804020008040400080406000804080008040A0008040C0008040E0008060<br> 0000806020008060400080606000806080008060A0008060C0008060E0008080<br> 0000808020008080400080806000808080008080A0008080C0008080E00080A0<br> 000080A0200080A0400080A0600080A0800080A0A00080A0C00080A0E00080C0<br> 000080C0200080C0400080C0600080C0800080C0A00080C0C00080C0E00080E0<br> 000080E0200080E0400080E0600080E0800080E0A00080E0C00080E0E000C000<br> 0000C0002000C0004000C0006000C0008000C000A000C000C000C000E000C020<br> 0000C0202000C0204000C0206000C0208000C020A000C020C000C020E000C040<br> 0000C0402000C0404000C0406000C0408000C040A000C040C000C040E000C060<br> 0000C0602000C0604000C0606000C0608000C060A000C060C000C060E000C080<br> 0000C0802000C0804000C0806000C0808000C080A000C080C000C080E000C0A0<br> 0000C0A02000C0A04000C0A06000C0A08000C0A0A000C0A0C000C0A0E000C0C0<br> 0000C0C02000C0C04000C0C06000C0C08000C0C0A000F0FBFF00A4A0A0008080<br> 80000000FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF000000<br> 0000000000000000000000000000000000000000000000000000000000000000<br> 000000000000000000000000000000A4A4A4F700000000000000000000000000<br> 0000000000000000000000000098FC0000000000000000010100000000000000<br> 00000000000000000000FC000000000000FC989898FC0000009BA40000000000<br> 0000000000000000FC00000000FC98FCFCFCFCFCFC0000000A0001A400000000<br> 0000000000000000000000FC98FCFCFC000000FC00000000000A010100000000<br> 000000000000000000FC98FCFCFC0000B7A400000000000000031D0000000000<br> 00000000000000ACAC00FCFC000007B7F76EA4000101011F0100000000000000<br> 000000000000E3D9D9000000B707B7F76EA40001016EA4011F00EC0000000000<br> 00000000F4E1D9DADADA000007B7076EA46E00A46EF76EA400EC000000000000<br> 000000F4E1D9D9D1D19999000007B7F700000000006EA400ECA4000000000000<br> 0000F4E0D1D09090D0905900006E076EA46EA4B7A4006E00EC9B000000000000<br> 0000EAD090D0D0D0D090000000006E07B707B707B70000000000000000000000<br> 0009D090D0D0D0D0D8000000000000B707B7F76EF7006EF76EF7000000000000<br> 00EAD0D0D0D8D8D8D80000000000B7F7B7F7B707B700006EF7B7000000000000<br> 09D8D0D0D8D8D8D80000000000B707B7F7B7F6B707B707006EF7000000000000<br> 09D0D0D8D8D8D8E0000000000007B707B7F6B7F6B707006EF76E000000000000<br> EBD0D8D8D8E0E0E0000000000000076EF7B70700F7B700F7B7076EF700000000<br> EBD8D8D8E0E0A80600000000000000006EF7B700000000B7076EF70000000000<br> EBD8D8E0E06828E00000000000000000006E07B7070A00F700076E0000000000<br> EBD8E0E068292972000000000000000000006E07B707006E0000000B00000000<br> 09D8E0E02932323200000000000000000000006EF7B700F76EF70A0B00000000<br> 09E0E0E0323273737300000000000000000000000000006EF7B7F70000000000<br> 09EAE0693273737CBC000000000000000000000000000A006EF76E0000000000<br> 0009683273737CF409BD000000000000000000000A0A00000000000000000000<br> 00097332737C7CF5080808FF00000000000A0A0A000000000000000000000000<br> 0000097373BBBCBD08F60809BC73730A0A000000000000000000000000000000<br> 00000009B2B3BCF509080809BC733269E8000000000000000000000000000000<br> 0000000008BCB3F4F5F5F5BC7C737272E0E0E0EA080000000000000000000000<br> 00000000000909F4F3BCBC7C73733229E8E9F408000000000000000000000000<br> 000000000000000809F4F4BCF2B2B3F309090000000000000000000000000000<br> 000000000000000000090909090909090000000000000000000000000000FFFF<br> FFFFFFFF87FFFFFE001FFFF00007FFC00003FFC00003FFC00003FF800003FF00<br> 0007FC00000FF800000FF000000FF000000FE0000007E0000007C0000007C000<br> 0003C0000001C0000003C0000003C0000003C0000003C0000003C0000001E000<br> 0001E0000001F0000003F800000FFC0001FFFE0003FFFF800FFFFFE03FFF}<br> OldCreateOrder = False<br> PixelsPerInch = 96<br> TextHeight = 12<br> object Bevel1: TBevel<br> Left = 11<br> Top = 217<br> Width = 36<br> Height = 36<br> Shape = bsFrame<br> end<br> object Label1: TLabel<br> Left = 8<br> Top = 3<br> Width = 189<br> Height = 12<br> Caption = '以下均在 Windows 2000 中编制'<br> Font.Charset = GB2312_CHARSET<br> Font.Color = clBlack<br> Font.Height = -12<br> Font.Name = '宋体'<br> Font.Style = [fsBold]<br> ParentFont = False<br> Transparent = True<br> end<br> object Img: TImage<br> Left = 13<br> Top = 219<br> Width = 32<br> Height = 32<br> Transparent = True<br> end<br> object Button1: TButton<br> Tag = 4<br> Left = 11<br> Top = 54<br> Width = 171<br> Height = 25<br> Caption = '关机重新启动效果'<br> TabOrder = 0<br> OnClick = Button2Click<br> end<br> object Button2: TButton<br> Left = 11<br> Top = 87<br> Width = 171<br> Height = 25<br> Caption = '是否立即关机效果'<br> TabOrder = 1<br> OnClick = Button2Click<br> end<br> object Button3: TButton<br> Tag = 1<br> Left = 11<br> Top = 119<br> Width = 171<br> Height = 25<br> Caption = '设置改变重新启动效果 A'<br> TabOrder = 2<br> OnClick = Button2Click<br> end<br> object Button4: TButton<br> Tag = 2<br> Left = 11<br> Top = 152<br> Width = 171<br> Height = 25<br> Caption = '设置改变重新启动效果 B'<br> TabOrder = 3<br> OnClick = Button2Click<br> end<br> object Button5: TButton<br> Tag = 3<br> Left = 11<br> Top = 184<br> Width = 171<br> Height = 25<br> Caption = '设置改变重新启动效果 C'<br> TabOrder = 4<br> OnClick = Button2Click<br> end<br> object CheckBox1: TCheckBox<br> Left = 7<br> Top = 28<br> Width = 193<br> Height = 17<br> Caption = '只演示,不要真的退出 Windows'<br> Checked = True<br> State = cbChecked<br> TabOrder = 5<br> end<br> object Button6: TButton<br> Left = 66<br> Top = 222<br> Width = 115<br> Height = 25<br> Caption = '图标选择效果'<br> TabOrder = 6<br> OnClick = Button6Click<br> end<br> object Button7: TButton<br> Tag = 5<br> Left = 11<br> Top = 258<br> Width = 171<br> Height = 25<br> Caption = '运行对话框效果'<br> TabOrder = 7<br> OnClick = Button2Click<br> end<br> object Memo1: TMemo<br> Left = 204<br> Top = 1<br> Width = 426<br> Height = 397<br> Color = clInfoBk<br> Font.Charset = ANSI_CHARSET<br> Font.Color = clWindowText<br> Font.Height = -12<br> Font.Name = 'Courier New'<br> Font.Style = []<br> Lines.Strings = (<br> 'unit dlgUnit;'<br> ''<br> 'interface'<br> ''<br> 'uses'<br> ' Windows, Messages, SysUtils, Classes, Graphics, '<br> 'Controls,'<br> ' Forms, Dialogs,StdCtrls, ExtCtrls;'<br> ''<br> 'type'<br> ' TForm1 = class(TForm)'<br> ' Button1: TButton;'<br> ' Button2: TButton;'<br> ' Button3: TButton;'<br> ' Button4: TButton;'<br> ' Button5: TButton;'<br> ' Label1: TLabel;'<br> ' CheckBox1: TCheckBox;'<br> ' Button6: TButton;'<br> ' Bevel1: TBevel;'<br> ' Img: TImage;'<br> ' Button7: TButton;'<br> ' Memo1: TMemo;'<br> ' procedure Button2Click(Sender: TObject);'<br> ' procedure Button6Click(Sender: TObject);'<br> ' private'<br> ' procedure WMQueryEndSession(var Msg: TMessage);'<br> ' message WM_QueryEndSession; // 这个消息是为防止'<br> ' // 真的退出而设'<br> '// function InitIconFileName: string;'<br> ' public'<br> ' { Public declarations }'<br> ' end;'<br> ''<br> 'var'<br> ' Form1: TForm1;'<br> '// 退出系统的关机函数'<br> 'procedure ExitWindowsDialog(ParentWnd: HWND); stdcall;'<br> ' external '#39'Shell32.dll'#39' index 60;'<br> ''<br> '// 设备变化后的关机重启选择函数的标志说明'<br> '// Logoff: Flags := EWX_LOGOFF;'<br> '// Shutdown: Flags := EWX_SHUTDOWN;'<br> '// Reboot: Flags := EWX_REBOOT;'<br> '// Restart: Flags := EW_RESTARTWINDOWS;'<br> 'function RestartDialog(ParentWnd: HWND; Reason: '<br> 'PAnsiChar;'<br> ' Flags: LongInt): LongInt; '<br> 'stdcall;'<br> ' external '#39'Shell32.dll'#39' index '<br> '59;'<br> ''<br> '// 选择图标的函数'<br> 'function PickIconDlgA(OwnerWnd: HWND; lpstrFile: '<br> 'PAnsiChar;'<br> ' var nMaxFile: LongInt; var lpdwIconIndex: '<br> 'LongInt):'<br> ' LongBool;stdcall; external '#39'SHELL32.DLL'#39' index '<br> '62;'<br> ''<br> '// 运行对话框函数'<br> 'procedure RunFileDlgA(OwnerWnd: HWND; Icon: HICON; '<br> 'lpstrDirectory: PAnsiChar;'<br> ' lpstrTitle: PAnsiChar; lpstrDescription: '<br> 'PAnsiChar; '<br> 'Flags: LongInt);'<br> ' stdcall; external '#39'Shell32.dll'#39' index 61;'<br> ''<br> 'implementation'<br> ' uses ShellAPI;'<br> ' Const App='#39'Windows 未文档化的函数演示'#39';'<br> ' Flags: LongInt = 0;'<br> ' RFF_NOBROWSE = 1;'<br> '{$R *.DFM}'<br> ''<br> 'procedure TForm1.WMQueryEndSession(var Msg: TMessage);'<br> 'begin // 只演示,不要真的退出 Windows 消息响应'<br> ' // 这里不能自己表示 ,要以非模态处理!!'<br> ' if CheckBox1.Checked then Msg.Result := 1'<br> ' else Msg.Result := 0;'<br> 'end;'<br> ''<br> '{function TForm1.InitIconFileName: string;'<br> 'var'<br> ' MSysDir: array[0..MAX_PATH - 1] of Char;'<br> ' SysDir: string;'<br> 'begin'<br> ' GetSystemDirectory(MSysDir, 255);'<br> ' SysDir := StrPas(MSysDir);'<br> ' if SysDir[Length(SysDir)] <> '#39'/'#39' then'<br> ' begin'<br> ' SysDir := SysDir + '#39'/'#39';'<br> ' end;'<br> ' Result := SysDir + '#39'SHELL32.DLL'#39';'<br> 'end; }'<br> ''<br> 'procedure TForm1.Button2Click(Sender: TObject);'<br> '{====实际使用这个函数作为过渡===='<br> 'function RestartExecute: Boolean;'<br> 'begin'<br> 'if RestartDialog(0,nil,Flags) = IDYES then'<br> ' begin'<br> ' Result := True;'<br> ' end'<br> 'else'<br> ' Result := False;'<br> 'end;'<br> '=================================}'<br> 'begin'<br> 'Flags := Flags or RFF_NOBROWSE;'<br> ' case (Sender as TButton).tag of'<br> ' 0: RestartDialog(self.Handle, '#39#39', EWX_SHUTDOWN);'<br> ' 1: RestartDialog(self.Handle, '#39#39', EWX_LOGOFF);'<br> ' 2: RestartDialog(self.Handle, '#39#39', EWX_REBOOT);'<br> ' 3: RestartDialog(self.Handle, '#39#39', EW_RESTARTWINDOWS);'<br> ''<br> ' 4: ExitWindowsDialog(self.Handle);'<br> ' { 下一句文字显示在 Win2000 中有问题,没时间了,不搞了}'<br> ' 5: RunFileDlgA(self.Handle,self.Icon.Handle,'#39'C:/'#39','<br> ' '#39'运行'#39','#39#39',Flags);'<br> ' end;'<br> 'end;'<br> ''<br> 'procedure TForm1.Button6Click(Sender: TObject);'<br> 'var // 本段在调试中老放CPU画面,但实际却可以用。'<br> ' fFileName,Temp: string;'<br> ' S: array [0..MAX_PATH - 1] of WideChar;//StrPas'<br> '// Tem: array [0..MAX_PATH - 1] of Char;'<br> ' fSize, fIndex: LongInt;'<br> ' I:Integer;'<br> ' Icon: TIcon;'<br> 'begin // 我没有办法解决发出的 PAnsiChar 如何保证'<br> ' // 既能送出PAnsiChar又能接收PWideChar!!'<br> ' SetLength(fFileName, 255); // 给fFileName设定长度'<br> ''<br> ' GetSystemDirectory(PChar(fFileName), 255);// 取得系统目录'<br> ' { 获得实际的fFileName }'<br> ' SetLength(fFileName, StrLen(PChar(fFileName)));'<br> ' { 拼接Shell32.dll }'<br> ' fFileName:=fFileName + '#39'/'#39' + '#39'SHELL32.DLL'#39';'<br> ' fSize := MAX_PATH;'<br> ' { 我用了以下的办法解决我的困惑 }'<br> ' For I:=Low(S) to Length(fFileName) do'<br> ' S:=WideChar(fFileName);'<br> ''<br> ' if PickIconDlgA(self.Handle,PAnsiChar(@S),fSize, '<br> 'fIndex)'<br> ' and (fIndex <> -1) '<br> 'then'<br> ' begin'<br> ' SetLength(Temp, MAX_PATH-1);'<br> ' Temp := S; // 将WideString转换回来,这里的 S'<br> ' // 已经是 PAnsiChar 了'<br> ''<br> ' Icon := TIcon.Create;'<br> ' Form1.Caption :=App+'#39' '#39'+ '#39'FileName:'#39'+Temp+'#39' ,'#39<br> ' +'#39'Icon:'#39'+ IntToStr(fIndex);'<br> ' Icon.Handle := ExtractIcon(hInstance, PChar(Temp), '<br> 'fIndex);'<br> ' Img.Picture.Icon := Icon;'<br> ' Form1.Icon := Img.Picture.Icon;'<br> ' Icon := nil;'<br> ' Icon.Free;'<br> ' end;'<br> ''<br> 'end;'<br> ''<br> 'end.')<br> ParentFont = False<br> ScrollBars = ssVertical<br> TabOrder = 8<br> end<br>end<br><br>