You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. {*
  2. * Vista image improvements from http://www.installationexcellence.com/articles/VistaWithDelphi/Original/Index.html
  3. * and http://www.installationexcellence.com/articles/VistaWithDelphi/Index.html
  4. *}
  5. unit Vista;
  6. //{$IFDEF LAZARUS}
  7. // {$DEFINE MESSAGEDLG}
  8. // {$ENDIF}
  9. interface
  10. uses {$IFDEF LAZARUS}Forms, Graphics, Controls,{$IFDEF MESSAGEDLG} Dialogs,{$ENDIF}{$ENDIF} Windows, SysUtils;
  11. function IsWindowsVista: Boolean;
  12. function TaskDialog(const AHandle: THandle; const ATitle, ADescription, AContent: WideString; const Icon, Buttons: integer; includeDescInXP: boolean = false; stripLineFeed: boolean = true): Integer;
  13. {$IFDEF LAZARUS}
  14. procedure SetVistaFonts(const AForm: TCustomForm);
  15. {$ENDIF}
  16. const
  17. VistaFont = 'Segoe UI';
  18. VistaContentFont = 'Calibri';
  19. XPContentFont = 'Verdana';
  20. XPFont = 'Tahoma';
  21. TD_ICON_BLANK = 0;
  22. TD_ICON_WARNING = 84;
  23. TD_ICON_QUESTION = 99;
  24. TD_ICON_ERROR = 98;
  25. TD_ICON_INFORMATION = 81;
  26. TD_ICON_SHIELD_QUESTION = 104;
  27. TD_ICON_SHIELD_ERROR = 105;
  28. TD_ICON_SHIELD_OK = 106;
  29. TD_ICON_SHIELD_WARNING = 107;
  30. TD_BUTTON_OK = 1;
  31. TD_BUTTON_YES = 2;
  32. TD_BUTTON_NO = 4;
  33. TD_BUTTON_CANCEL = 8;
  34. TD_BUTTON_RETRY = 16;
  35. TD_BUTTON_CLOSE = 32;
  36. TD_RESULT_OK = 1;
  37. TD_RESULT_CANCEL = 2;
  38. TD_RESULT_RETRY = 4;
  39. TD_RESULT_YES = 6;
  40. TD_RESULT_NO = 7;
  41. TD_RESULT_CLOSE = 8;
  42. {$IFNDEF LAZARUS}
  43. mrNone = 0;
  44. mrOK = mrNone + 1;
  45. mrCancel = mrNone + 2;
  46. mrAbort = mrNone + 3;
  47. mrRetry = mrNone + 4;
  48. mrYes = mrNone + 6;
  49. mrNo = mrNone + 7;
  50. {$ENDIF}
  51. implementation
  52. {$IFDEF LAZARUS}
  53. procedure SetVistaFonts(const AForm: TCustomForm);
  54. begin
  55. if IsWindowsVista and not SameText(AForm.Font.Name, VistaFont) and (Screen.Fonts.IndexOf(VistaFont) >= 0) then
  56. begin
  57. AForm.Font.Size := AForm.Font.Size + 1;
  58. AForm.Font.Name := VistaFont;
  59. end;
  60. end;
  61. {$ENDIF}
  62. function IsWindowsVista: Boolean;
  63. var
  64. VerInfo: TOSVersioninfo;
  65. begin
  66. VerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);
  67. GetVersionEx(VerInfo);
  68. Result := VerInfo.dwMajorVersion >= 6;
  69. end;
  70. // http://www.swissdelphicenter.ch/en/showcode.php?id=1692
  71. {:Converts Unicode string to Ansi string using specified code page.
  72. @param ws Unicode string.
  73. @param codePage Code page to be used in conversion.
  74. @returns Converted ansi string.
  75. }
  76. function WideStringToString(const ws: WideString; codePage: Word): AnsiString;
  77. var
  78. l: integer;
  79. begin
  80. if ws = '' then begin
  81. Result := ''
  82. end
  83. else begin
  84. l := WideCharToMultiByte(codePage, WC_COMPOSITECHECK or WC_DISCARDNS or WC_SEPCHARS or WC_DEFAULTCHAR, @ws[1], - 1, nil, 0, nil, nil);
  85. SetLength(Result, l - 1);
  86. if l > 1 then begin
  87. WideCharToMultiByte(codePage, WC_COMPOSITECHECK or WC_DISCARDNS or WC_SEPCHARS or WC_DEFAULTCHAR, @ws[1], - 1, @Result[1], l - 1, nil, nil);
  88. end;
  89. end;
  90. end;
  91. //from http://www.tmssoftware.com/atbdev5.htm
  92. function TaskDialog(const AHandle: THandle; const ATitle, ADescription, AContent: WideString; const Icon, Buttons: integer; includeDescInXP: boolean = false; stripLineFeed: boolean = true): Integer;
  93. var
  94. DLLHandle: THandle;
  95. res: integer;
  96. wS: WideString;
  97. S: String;
  98. {$IFDEF MESSAGEDLG}
  99. Btns: TMsgDlgButtons;
  100. DlgType: TMsgDlgType;
  101. {$ELSE}
  102. Btns: Integer;
  103. myIcon: Integer;
  104. {$ENDIF}
  105. TaskDialogFound: boolean;
  106. TaskDialogProc: function(HWND: THandle; hInstance: THandle; cTitle, cDescription, cContent: pwidechar; Buttons: Integer; Icon: integer; ResButton: pinteger): integer; stdcall;
  107. begin
  108. TaskDialogFound := false;
  109. Result := 0;
  110. if IsWindowsVista then begin
  111. DLLHandle := LoadLibrary('comctl32.dll');
  112. if DLLHandle >= 32 then begin
  113. @TaskDialogProc := GetProcAddress(DLLHandle,'TaskDialog');
  114. if Assigned(TaskDialogProc) then begin
  115. if stripLineFeed then begin
  116. wS := StringReplace(AContent, #10, '', [rfReplaceAll]);
  117. wS := StringReplace(wS, #13, '', [rfReplaceAll]);
  118. end
  119. else begin
  120. wS := AContent;
  121. end;
  122. TaskDialogProc(AHandle, 0, PWideChar(ATitle), PWideChar(ADescription), PWideChar(wS), Buttons, Icon, @res);
  123. TaskDialogFound := true;
  124. Result := mrOK;
  125. case res of
  126. TD_RESULT_CANCEL : Result := mrCancel;
  127. TD_RESULT_RETRY : Result := mrRetry;
  128. TD_RESULT_YES : Result := mrYes;
  129. TD_RESULT_NO : Result := mrNo;
  130. TD_RESULT_CLOSE : Result := mrAbort;
  131. end;
  132. end;
  133. FreeLibrary(DLLHandle);
  134. end;
  135. end;
  136. if not TaskDialogFound then begin
  137. S := '';
  138. if includeDescInXP then S := ADescription + #10#13 + #10#13 + AContent else S := AContent;
  139. {$IFDEF MESSAGEDLG}
  140. Btns := [];
  141. if Buttons and TD_BUTTON_OK = TD_BUTTON_OK then Btns := Btns + [MBOK];
  142. if Buttons and TD_BUTTON_YES = TD_BUTTON_YES then Btns := Btns + [MBYES];
  143. if Buttons and TD_BUTTON_NO = TD_BUTTON_NO then Btns := Btns + [MBNO];
  144. if Buttons and TD_BUTTON_CANCEL = TD_BUTTON_CANCEL then Btns := Btns + [MBCANCEL];
  145. if Buttons and TD_BUTTON_RETRY = TD_BUTTON_RETRY then Btns := Btns + [MBRETRY];
  146. if Buttons and TD_BUTTON_CLOSE = TD_BUTTON_CLOSE then Btns := Btns + [MBABORT];
  147. DlgType := mtCustom;
  148. case Icon of
  149. TD_ICON_WARNING : DlgType := mtWarning;
  150. TD_ICON_QUESTION : DlgType := mtConfirmation;
  151. TD_ICON_ERROR : DlgType := mtError;
  152. TD_ICON_INFORMATION: DlgType := mtInformation;
  153. end;
  154. Result := MessageDlg(S, DlgType, Btns, 0);
  155. {$ELSE}
  156. Btns := 0;
  157. if Buttons and TD_BUTTON_OK = TD_BUTTON_OK then Btns := MB_OK;
  158. if (Buttons and TD_BUTTON_YES = TD_BUTTON_YES) and (Buttons and TD_BUTTON_NO = TD_BUTTON_NO) then Btns := MB_YESNO;
  159. if (Buttons and TD_BUTTON_CANCEL = TD_BUTTON_CANCEL) and (Buttons and TD_BUTTON_YES = TD_BUTTON_YES) and (Buttons and TD_BUTTON_NO = TD_BUTTON_NO) then Btns := MB_YESNOCANCEL;
  160. if (Buttons and TD_BUTTON_CANCEL = TD_BUTTON_CANCEL) and (Buttons and TD_BUTTON_OK = TD_BUTTON_OK) then Btns := MB_OKCANCEL;
  161. if (Buttons and TD_BUTTON_CANCEL = TD_BUTTON_CANCEL) and (Buttons and TD_BUTTON_RETRY = TD_BUTTON_RETRY) then Btns := MB_RETRYCANCEL;
  162. myIcon := 0;
  163. case Icon of
  164. TD_ICON_QUESTION : myIcon := MB_ICONQUESTION;
  165. TD_ICON_ERROR : myIcon := MB_ICONSTOP;
  166. TD_ICON_INFORMATION: myIcon := MB_ICONINFORMATION;
  167. end;
  168. Result := MessageBox(0, pchar(S), pchar(String(ATitle)), Btns + myIcon);
  169. {$ENDIF}
  170. end;
  171. end;
  172. end.