FlatSysex.pas 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290
  1. unit FlatSysex;
  2. {$I FlatStyle.inc}
  3. interface
  4. uses
  5. Windows, Messages, Controls, Forms, SysUtils, DB, DBCtrls, DBGrids,
  6. MMSystem, Classes, DBConsts, Grids, FlatExcfm, FlatUtils, Dialogs,
  7. Menus, Graphics, ShellApi, ExtCtrls, FlatWatet;
  8. type
  9. //导出数据选项,
  10. //dmDefault为导出的数据默认为字段类型的数据,
  11. //dmString为导出的所有数据全部转换为字符类型
  12. TEduceType = (dmDefault,dmString);
  13. TEduceMode = (emDefault,emSingle);
  14. TEduceData = class;
  15. TEduceDatas = class;
  16. TEduceLink = class;
  17. { TDefineExcel }
  18. TDefineExcel = Class(TVersionComponent)
  19. Private
  20. fCol : word;
  21. fRow : word;
  22. ExcelStream : TStream;
  23. FEduceType : TEduceType;
  24. FColumns : TEduceDatas;
  25. FUpdateLock : Byte;
  26. FLayoutLock : Byte;
  27. FDataLink : TEduceLink;
  28. FLayoutSet : Boolean;
  29. FEduceTitle : Boolean;
  30. FExcelForm : TExcelForm;
  31. FInterval : integer;
  32. FShowProgress: boolean;
  33. FFileName: String;
  34. FEduceMode: TEduceMode;
  35. FDefaultExt: String;
  36. function GetFieldCount: Integer;
  37. function GetDataSource: TDataSource;
  38. function GetColumnCount: integer;
  39. function GetEduceCount: integer;
  40. procedure SeTEduceType(const Value: TEduceType);
  41. procedure EndProgress;
  42. procedure StartProgress(Max: Integer);
  43. procedure SetColumns(const Value: TEduceDatas);
  44. procedure SetDataSource(const Value: TDataSource);
  45. procedure DefineFieldMap;
  46. function GetFields(FieldIndex: Integer): TField;
  47. procedure SetDefaultExt(Value: String);
  48. protected
  49. // 以下是导出到 MS-Excel 操作过程
  50. procedure WriteData(Field: TField);
  51. procedure WriteTitle;
  52. procedure WriteBlankCell;
  53. procedure WriteFloatCell(const AValue: Double);
  54. procedure WriteIntegerCell(const AValue: Integer);
  55. procedure WriteStringCell(const AValue: string);
  56. procedure WritePrefix;
  57. procedure WriteSuffix;
  58. procedure WriteDataCells;
  59. procedure SaveExcel(Save: TStream);
  60. // 结束 MS-Excel 操作过程
  61. procedure BeginLayout;
  62. procedure EndLayout;
  63. procedure BeginUpdate;
  64. procedure EndUpdate;
  65. procedure LayoutChanged; virtual;
  66. procedure LinkActive(Value: Boolean); virtual;
  67. procedure CancelLayout;
  68. procedure DefineProperties(Filer: TFiler); override;
  69. procedure ReadColumns(Reader: TReader);
  70. procedure WriteColumns(Writer: TWriter);
  71. procedure Loaded; override;
  72. procedure InitColumns;
  73. procedure IncColRow;
  74. function CreateDataLink: TEduceLink; dynamic;
  75. function CreateColumns: TEduceDatas;
  76. function AcquireLayoutLock: Boolean;
  77. property UpdateLock: Byte read FUpdateLock;
  78. property LayoutLock: Byte read FLayoutLock;
  79. property DataLink: TEduceLink read FDataLink;
  80. property LayoutSet: Boolean read FLayoutSet write FLayoutSet;
  81. property EduceType: TEduceType read FEduceType write SeTEduceType default dmDefault;
  82. property EduceDatas: TEduceDatas read FColumns write SetColumns;
  83. property DataSource: TDataSource read GetDataSource write SetDataSource;
  84. property EduceTitle: Boolean read FEduceTitle write FEduceTitle default true;
  85. property ExcelForm: TExcelForm read FExcelForm;
  86. property Interval: integer read FInterval write FInterval default 500;
  87. property ShowProgress: boolean read FShowProgress write FShowProgress default true;
  88. property FileName: String read FFileName write FFileName;
  89. property EduceMode: TEduceMode read FEduceMode write FEduceMode default emSingle;
  90. property DefaultExt: String read FDefaultExt write SetDefaultExt;
  91. public
  92. constructor Create(AOwner: TComponent); override;
  93. destructor Destroy; override;
  94. procedure ExportAll;
  95. procedure ExecuteSave;
  96. procedure InitFields;
  97. procedure RestoreFields;
  98. procedure ClearFields;
  99. property Fields[FieldIndex: Integer]: TField read GetFields;
  100. property FieldCount: Integer read GetFieldCount;
  101. property ColumnCount: integer read GetColumnCount;
  102. property EduceCount: integer read GetEduceCount;
  103. end;
  104. { FlatExcel }
  105. TFlatExcel = Class(TDefineExcel)
  106. published
  107. property EduceType;
  108. property EduceDatas stored False;
  109. property DataSource;
  110. property EduceTitle;
  111. property Interval;
  112. property ShowProgress;
  113. property FileName;
  114. property EduceMode;
  115. property DefaultExt;
  116. end;
  117. { TEduceLink }
  118. TEduceLink = class(TDataLink)
  119. private
  120. FCells: TDefineExcel;
  121. FFieldCount: Integer;
  122. FFieldMap: array of Integer;
  123. FModified: Boolean;
  124. FSparseMap: Boolean;
  125. function GetDefaultFields: Boolean;
  126. function GetFields(I: Integer): TField;
  127. protected
  128. procedure ActiveChanged; override;
  129. procedure LayoutChanged; override;
  130. function GetMappedIndex(ColIndex: Integer): Integer;
  131. function IsAggRow(Value: Integer): Boolean; virtual;
  132. public
  133. constructor Create(ADSExcel: TDefineExcel);
  134. destructor Destroy; override;
  135. procedure ClearMapping;
  136. function AddMapping(const FieldName: string): Boolean;
  137. property DefaultFields: Boolean read GetDefaultFields;
  138. property FieldCount: Integer read FFieldCount;
  139. property Fields[I: Integer]: TField read GetFields;
  140. property SparseMap: Boolean read FSparseMap write FSparseMap;
  141. property Cells: TDefineExcel read FCells;
  142. end;
  143. { TEduceData }
  144. TEduceData = class(TCollectionItem)
  145. private
  146. FFieldName: string;
  147. FVisible: Boolean;
  148. FStored: Boolean;
  149. FCaption: String;
  150. FField: TField;
  151. procedure SetCaption(const Value: String);
  152. procedure SetField(Value: TField);
  153. function GetField: TField;
  154. procedure SetFieldName(const Value: String);
  155. procedure SetVisible(const Value: Boolean);
  156. protected
  157. function GetExcel: TDefineExcel;
  158. function GetDisplayName: string; override;
  159. public
  160. constructor Create(Collection: TCollection); override;
  161. destructor Destroy; override;
  162. procedure Assign(Source: TPersistent); override;
  163. property Cells: TDefineExcel read GetExcel;
  164. property Field: TField read GetField write SetField;
  165. property IsStored: Boolean read FStored write FStored default false;
  166. published
  167. property Caption: string read fCaption write SetCaption;
  168. property FieldName: String read fFieldName write SetFieldName;
  169. property Visible: Boolean read FVisible write SetVisible;
  170. end;
  171. TEduceDataClass = class of TEduceData;
  172. TEduceDatasState = (csDefault, csCustomized);
  173. { TEduceDatas }
  174. TEduceDatas = class(TCollection)
  175. private
  176. FCells: TDefineExcel;
  177. function GetColumn(Index: Integer): TEduceData;
  178. function GetState: TEduceDatasState;
  179. procedure SetColumn(Index: Integer; Value: TEduceData);
  180. procedure SetState(NewState: TEduceDatasState);
  181. protected
  182. function GetOwner: TPersistent; override;
  183. function InternalAdd: TEduceData;
  184. procedure Update(Item: TCollectionItem); override;
  185. public
  186. constructor Create(DSExcel: TDefineExcel; ColumnClass: TEduceDataClass);
  187. procedure LoadFromFile(const Filename: string);
  188. procedure LoadFromStream(S: TStream);
  189. procedure RebuildColumns;
  190. procedure SaveToFile(const Filename: string);
  191. procedure SaveToStream(S: TStream);
  192. function Add: TEduceData;
  193. property State: TEduceDatasState read GetState write SetState;
  194. property Cells: TDefineExcel read FCells;
  195. property Items[Index: Integer]: TEduceData read GetColumn write SetColumn; default;
  196. end;
  197. { TFlatSound }
  198. TSoundEvent = (seBtnClick, seMenu, seMenuClick, seMoveIntoBtn, sePanelExpand);
  199. TFlatSound = class(TVersionComponent)
  200. private
  201. FEvent: TSoundEvent;
  202. public
  203. procedure Play;
  204. procedure PlayThis(ThisEvent: TSoundEvent);
  205. constructor Create(AOwner: TComponent); override;
  206. published
  207. property Event: TSoundEvent read FEvent write FEvent;
  208. end;
  209. { TFlatAnimWnd }
  210. TFlatAnimWnd = class;
  211. TFlatAnimHookWnd = class(TWinControl)
  212. private
  213. FAnimateWindow: TFlatAnimWnd;
  214. procedure WMCreate (var Message: TMessage); message WM_CREATE;
  215. procedure WMDestroy (var Message: TMessage); message WM_DESTROY;
  216. public
  217. constructor Create (AOwner: TComponent); override;
  218. end;
  219. TFlatAnimWnd = class(TVersionComponent)
  220. private
  221. FOwner: TComponent;
  222. FNewProc, FOldProc, FNewAppProc, FOldAppProc: TFarProc;
  223. FOnMinimize: TNotifyEvent;
  224. FOnRestore: TNotifyEvent;
  225. procedure NewWndProc (var Message: TMessage);
  226. procedure NewAppWndProc (var Message: TMessage);
  227. procedure MinimizeWnd;
  228. procedure RestoreWnd;
  229. procedure OwnerWndCreated;
  230. procedure OwnerWndDestroyed;
  231. protected
  232. FHookWnd: TFlatAnimHookWnd;
  233. procedure SetParentComponent(Value: TComponent); override;
  234. public
  235. constructor Create (AOwner: TComponent); override;
  236. destructor Destroy; override;
  237. procedure Minimize;
  238. published
  239. property OnMinimize: TNotifyEvent read FOnMinimize write FOnMinimize;
  240. property OnRestore: TNotifyEvent read FOnRestore write FOnRestore;
  241. end;
  242. { TDefineSingle }
  243. TDefineSingle = class(TVersionComponent)
  244. private
  245. { Private declarations }
  246. FActive: boolean;
  247. FCaption: string;
  248. FTitle: string;
  249. procedure SetActive(Value: boolean);
  250. procedure SetCaption(const Value: string);
  251. procedure SetTitle(const Value: string);
  252. protected
  253. { Protected declarations }
  254. procedure Loaded; override;
  255. procedure Run(State:Boolean; Title:String);
  256. property Active: boolean read FActive write SetActive default True;
  257. property Caption: string read FCaption write SetCaption;
  258. property Title: string read FTitle write SetTitle;
  259. public
  260. { Public declarations }
  261. constructor Create(AOwner: TComponent); override;
  262. end;
  263. TFlatSingle = class(TDefineSingle)
  264. published
  265. { Published declarations }
  266. property Active;
  267. property Caption;
  268. property Title;
  269. end;
  270. { TDefineTimer }
  271. TDefineTimer = class(TVersionComponent)
  272. private
  273. uTimerID: MMRESULT;
  274. FInterval: Cardinal;
  275. FPeriod: Cardinal;
  276. FOnTimer: TNotifyEvent;
  277. FEnabled: Boolean;
  278. procedure SetEnabled(Value: Boolean);
  279. procedure SetInterval(Value: Cardinal);
  280. procedure SetOnTimer(Value: TNotifyEvent);
  281. procedure SetPeriod(Value: Cardinal); //设置分辨率
  282. protected
  283. procedure Timer; dynamic;
  284. procedure UpdateTimer;
  285. property Enabled: Boolean read FEnabled write SetEnabled default True;
  286. property Interval: Cardinal read FInterval write SetInterval default 1000;
  287. property Period: Cardinal read FPeriod write SetPeriod default 10;
  288. property OnTimer: TNotifyEvent read FOnTimer write SetOnTimer;
  289. public
  290. constructor Create(AOwner: TComponent); override;
  291. destructor Destroy; override;
  292. end;
  293. { TFlatTimer }
  294. TFlatTimer = class(TDefineTimer)
  295. published
  296. property Enabled;
  297. property Interval;
  298. property Period;
  299. property OnTimer;
  300. end;
  301. { TDefineTaskbarIcon }
  302. TDefineTaskbarMode = (thDefault,thCustom);
  303. TDefineTaskbarIcon = class(TVersionComponent)
  304. private
  305. FActive: Boolean;
  306. FHint: string;
  307. FIcon: TIcon;
  308. FHandle: HWnd;
  309. FOnClick: TNotifyEvent;
  310. FOnDblClick: TNotifyEvent;
  311. FOnRightClick: TNotifyEvent;
  312. FOnMouseMove: TNotifyEvent;
  313. FWMTaskbarCreated: UINT;
  314. FPopupMenu: TPopupMenu;
  315. FIconMode: TDefineTaskbarMode;
  316. FHintMode: TDefineTaskbarMode;
  317. //FOnMinimize: TNotifyEvent;
  318. //FOnRestore: TNotifyEvent;
  319. procedure SetActive(Value: Boolean);
  320. procedure SetHint(Value: string);
  321. procedure SetIcon(Value: TIcon);
  322. procedure SetPopupMenu(const Value: TPopupMenu);
  323. procedure SetHintMode(const Value: TDefineTaskbarMode);
  324. procedure SetIconMode(const Value: TDefineTaskbarMode);
  325. protected
  326. procedure PrivateWndProc(var Message: TMessage);
  327. procedure WndProc(var Message: TMessage); dynamic;
  328. procedure Loaded; override;
  329. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  330. function AppHook(var Message: TMessage): Boolean;
  331. function AddIcon: Boolean; dynamic;
  332. function DeleteIcon: Boolean; dynamic;
  333. function ModifyIcon(Aspect: Integer): Boolean; dynamic;
  334. function DoIcon(Action: DWORD; Aspect: UINT): Boolean; dynamic;
  335. property Handle: HWnd read FHandle;
  336. property Active: Boolean read FActive write SetActive;
  337. property Hint: string read FHint write SetHint;
  338. property HintMode: TDefineTaskbarMode read FHintMode write SetHintMode default thDefault;
  339. property Icon: TIcon read FIcon write SetIcon;
  340. property IconMode: TDefineTaskbarMode read FIconMode write SetIconMode default thDefault;
  341. property PopupMenu: TPopupMenu read FPopupMenu write SetPopupMenu;
  342. property OnClick: TNotifyEvent read FOnClick write FOnClick;
  343. property OnDblClick: TNotifyEvent read FOnDblClick write FOnDblClick;
  344. property OnRightClick: TNotifyEvent read FOnRightClick write FOnRightClick;
  345. property OnMouseMove: TNotifyEvent read FOnMouseMove write FOnMouseMove;
  346. //property OnAppMinimize: TNotifyEvent read FOnMinimize write FOnMinimize;
  347. //property OnAppRestore: TNotifyEvent read FOnRestore write FOnRestore;
  348. public
  349. constructor Create(AOwner: TComponent); override;
  350. destructor Destroy; override;
  351. end;
  352. TFlatTaskbarIcon = class(TDefineTaskbarIcon)
  353. published
  354. property Active;
  355. property Hint;
  356. property HintMode;
  357. property Icon;
  358. property IconMode;
  359. property PopupMenu;
  360. property OnClick;
  361. property OnDblClick;
  362. property OnRightClick;
  363. property OnMouseMove;
  364. //property OnAppMinimize;
  365. //property OnAppRestore;
  366. end;
  367. { TDefineAnimation }
  368. TDefineAnimation = class(TVersionControl)
  369. private
  370. FTransparent: Boolean;
  371. FAnimation: TBitmap;
  372. FFrames: Integer;
  373. FFrameWidth: Integer;
  374. FFrame: Integer;
  375. FInterval: Integer;
  376. FTransColor: TColor;
  377. FActive: Boolean;
  378. FLoop: Boolean;
  379. FReverse: Boolean;
  380. FTimer: TTimer;
  381. FBorderColor: TColor;
  382. FBorder: Boolean;
  383. FFrameChange: TOnFrameChange;
  384. FAnimationLayout: TAnimationLayout;
  385. procedure SetAnimation(Value: TBitmap);
  386. procedure SetFrames(Value: Integer);
  387. procedure SetFrameWidth(Value: Integer);
  388. procedure SetFrame(Value: Integer);
  389. procedure SetActive(Value: Boolean);
  390. procedure SetTransparent(Value: Boolean);
  391. procedure SetLoop(Value: Boolean);
  392. procedure SetReverse(Value: Boolean);
  393. procedure SetInterval(Value: Integer);
  394. procedure SetBorder(Value: Boolean);
  395. procedure DoTimer(Sender: TObject);
  396. procedure SetColors(Index: Integer; Value: TColor);
  397. procedure CMSysColorChange(var Message: TMessage); message CM_SYSCOLORCHANGE;
  398. procedure CMParentColorChanged(var Message: TWMNoParams); message CM_PARENTCOLORCHANGED;
  399. procedure WMSize(var Message: TWMSize); message WM_SIZE;
  400. procedure SetAnimationLayout(const Value: TAnimationLayout);
  401. protected
  402. procedure Paint; override;
  403. property Animation: TBitmap read FAnimation write SetAnimation;
  404. property Frames: Integer read FFrames write SetFrames;
  405. property FrameWidth: Integer read FFrameWidth write SetFrameWidth;
  406. property Frame: Integer read FFrame write SetFrame default 1;
  407. property Interval: Integer read FInterval write SetInterval;
  408. property ColorTransparent: TColor index 0 read FTransColor write SetColors default clFuchsia;
  409. property ColorBorder: TColor index 1 read FBorderColor write SetColors default DefaultBorderColor;
  410. property Active: Boolean read FActive write SetActive;
  411. property Loop: Boolean read FLoop write SetLoop;
  412. property Reverse: Boolean read FReverse write SetReverse;
  413. property Border: Boolean read FBorder write SetBorder default false;
  414. property AnimationLayout: TAnimationLayout read FAnimationLayout write SetAnimationLayout;
  415. property OnFrameChange: TOnFrameChange read FFrameChange write FFrameChange;
  416. property Transparent: Boolean read FTransparent write SetTransparent default false;
  417. public
  418. constructor Create(AOwner: TComponent); override;
  419. destructor Destroy; override;
  420. end;
  421. { TFlatAnimation }
  422. TFlatAnimation = class(TDefineAnimation)
  423. published
  424. property Color;
  425. property Animation;
  426. property Frames;
  427. property FrameWidth;
  428. property Frame;
  429. property Interval;
  430. property ColorTransparent;
  431. property ColorBorder;
  432. property Active;
  433. property Loop;
  434. property Reverse;
  435. property Border;
  436. property AnimationLayout;
  437. property OnFrameChange;
  438. property Transparent;
  439. property Align;
  440. property Enabled;
  441. property ParentColor;
  442. property ParentShowHint;
  443. property ShowHint;
  444. property Visible;
  445. property OnDragDrop;
  446. property OnDragOver;
  447. property OnEndDrag;
  448. property OnMouseDown;
  449. property OnMouseMove;
  450. property OnMouseUp;
  451. property OnStartDrag;
  452. end;
  453. { TDefineHint }
  454. TDefineHint = class(TVersionComponent)
  455. private
  456. FHintFont: TFont;
  457. FBackgroundColor: TColor;
  458. FBorderColor: TColor;
  459. FArrowBackgroundColor: TColor;
  460. FArrowColor: TColor;
  461. FHintWidth: Integer;
  462. FOnShowHint: TShowHintEvent;
  463. procedure SetColors (Index: Integer; Value: TColor);
  464. procedure SetHintFont (Value: TFont);
  465. procedure GetHintInfo (var HintStr: string; var CanShow: Boolean; var HintInfo: THintInfo);
  466. public
  467. constructor Create (AOwner: TComponent); override;
  468. destructor Destroy; override;
  469. protected
  470. property ColorBackground: TColor index 0 read FBackgroundColor write SetColors default clWhite;
  471. property ColorBorder: TColor index 1 read FBorderColor write SetColors default clBlack;
  472. property ColorArrowBackground: TColor index 2 read FArrowBackgroundColor write SetColors default $0053D2FF;
  473. property ColorArrow: TColor index 3 read FArrowColor write SetColors default clBlack;
  474. property MaxWidth: Integer read FHintWidth write FHintWidth default 200;
  475. property Font: TFont read FHintFont write SetHintFont;
  476. property OnShowHint: TShowHintEvent read FOnShowHint write FOnShowHint;
  477. end;
  478. { TDefineHintWindow }
  479. TDefineHintWindow = class(THintWindow)
  480. private
  481. FArrowPos: TArrowPos;
  482. FArrowPoint: TPoint;
  483. FHint: TDefineHint;
  484. function FindFlatHint: TDefineHint;
  485. protected
  486. procedure Paint; override;
  487. procedure CreateParams(var Params: TCreateParams); override;
  488. public
  489. procedure ActivateHint(HintRect: TRect; const AHint: string); Override;
  490. end;
  491. TFlatHint = class(TDefineHint)
  492. published
  493. property ColorBackground;
  494. property ColorBorder;
  495. property ColorArrowBackground;
  496. property ColorArrow;
  497. property MaxWidth;
  498. property Font;
  499. property OnShowHint;
  500. end;
  501. { TBaseWater }
  502. TBaseWater = class(TVersionComponent)
  503. private
  504. FInterval: Cardinal;
  505. FHandle: HWND;
  506. FOnTimer: TNotifyEvent;
  507. FEnabled: Boolean;
  508. procedure SetEnabled(Value: Boolean);
  509. procedure SetInterval(Value: Cardinal);
  510. procedure SetOnTimer(Value: TNotifyEvent);
  511. protected
  512. procedure Timer; dynamic;
  513. procedure UpdateTimer;
  514. procedure WndProc(var Msg: TMessage);
  515. property Enabled: Boolean read FEnabled write SetEnabled default True;
  516. property Interval: Cardinal read FInterval write SetInterval default 50;
  517. property OnTimer: TNotifyEvent read FOnTimer write SetOnTimer;
  518. public
  519. constructor Create(AOwner: TComponent); override;
  520. destructor Destroy; override;
  521. end;
  522. TDefineImage = class(TVersionGraphic)
  523. private
  524. FBitmap: TBitmap;
  525. FOnProgress: TProgressEvent;
  526. FStretch: Boolean;
  527. FCenter: Boolean;
  528. FIncrementalDisplay: Boolean;
  529. FTransparent: Boolean;
  530. FDrawing: Boolean;
  531. FProportional: Boolean;
  532. FAutoShowCursor: Boolean;
  533. FAutoImage: Boolean;
  534. FLeaveImage: TBitmap;
  535. FEnterImage: TBitmap;
  536. FAutoCursor: TCursor;
  537. FMouseState: Boolean;
  538. procedure PictureChanged(Sender: TObject);
  539. procedure SetCenter(Value: Boolean);
  540. procedure SetPicture(Value: TBitmap);
  541. procedure SetStretch(Value: Boolean);
  542. procedure SetTransparent(Value: Boolean);
  543. procedure SetProportional(Value: Boolean);
  544. procedure SetEnterImage(const Value: TBitmap);
  545. procedure SetLeaveImage(const Value: TBitmap);
  546. protected
  547. procedure MouseEnter(Var Msg:TMessage);message CM_MouseEnter;
  548. procedure MouseLeave(Var Msg:TMessage);message CM_MouseLeave;
  549. function CanAutoSize(var NewWidth, NewHeight: Integer): Boolean; override;
  550. function DestRect: TRect;
  551. function DoPaletteChange: Boolean;
  552. function GetPalette: HPALETTE; override;
  553. function GetCanvas: TCanvas;
  554. procedure Paint; override;
  555. procedure Progress(Sender: TObject; Stage: TProgressStage;
  556. PercentDone: Byte; RedrawNow: Boolean; const R: TRect; const Msg: string); dynamic;
  557. property Center: Boolean read FCenter write SetCenter default False;
  558. property IncrementalDisplay: Boolean read FIncrementalDisplay write FIncrementalDisplay default False;
  559. property Proportional: Boolean read FProportional write SetProportional default false;
  560. property Stretch: Boolean read FStretch write SetStretch default False;
  561. property Transparent: Boolean read FTransparent write SetTransparent default False;
  562. property OnProgress: TProgressEvent read FOnProgress write FOnProgress;
  563. property BMPEnter:TBitmap read FEnterImage write SetEnterImage;
  564. property BMPLeave:TBitmap read FLeaveImage write SetLeaveImage;
  565. property AutoImage:Boolean read FAutoImage write FAutoImage default false;
  566. property AutoCursor:TCursor read FAutoCursor Write FAutoCursor default crHandPoint;
  567. property AutoShowCursor:Boolean read FAutoShowCursor write FAutoShowCursor default false;
  568. public
  569. constructor Create(AOwner: TComponent); override;
  570. destructor Destroy; override;
  571. property Bitmap: TBitmap read FBitmap write SetPicture;
  572. property Canvas: TCanvas read GetCanvas;
  573. property OnMouseMove;
  574. end;
  575. { TDefineWater }
  576. TDefineWater = class(TBaseWater)
  577. private
  578. FState: Integer;
  579. FParam: TOtherParam;
  580. FDamping: TWaterDamping;
  581. FBitmap: TBitmap;
  582. FImage: TDefineImage;
  583. FPlayState: boolean;
  584. FItems: TStringList;
  585. procedure SetDamping(Value: TWaterDamping);
  586. procedure SetItems(const Value: TStringList);
  587. protected
  588. FWater: TDefineWatet;
  589. FMoveHeight: Integer;
  590. procedure OnMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  591. procedure Notification(AComponent: TComponent;Operation: TOperation); override;
  592. procedure Play(sender: TObject);
  593. procedure InitiateWater;
  594. property Bitmap: TBitmap read FBitmap;
  595. public
  596. constructor Create(AOwner: TComponent); override;
  597. destructor Destroy; override;
  598. published
  599. property Damping: TWaterDamping read FDamping write SetDamping;
  600. property CtrlImage: TDefineImage read FImage write FImage;
  601. property Items: TStringList read FItems write SetItems;
  602. property Enabled;
  603. property Interval;
  604. end;
  605. { TFlatImage }
  606. TFlatImage = class(TDefineImage)
  607. published
  608. property AutoImage;
  609. property AutoCursor;
  610. property AutoShowCursor;
  611. property Align;
  612. property Anchors;
  613. property AutoSize;
  614. property Center;
  615. property Constraints;
  616. property DragCursor;
  617. property DragKind;
  618. property DragMode;
  619. property Enabled;
  620. property IncrementalDisplay;
  621. property ParentShowHint;
  622. property Bitmap;
  623. property BMPEnter;
  624. property BMPLeave;
  625. property PopupMenu;
  626. property Proportional;
  627. property ShowHint;
  628. property Stretch;
  629. property Transparent;
  630. property Visible;
  631. property OnClick;
  632. property OnContextPopup;
  633. property OnDblClick;
  634. property OnDragDrop;
  635. property OnDragOver;
  636. property OnEndDock;
  637. property OnEndDrag;
  638. property OnMouseDown;
  639. property OnMouseMove;
  640. property OnMouseUp;
  641. property OnProgress;
  642. property OnStartDock;
  643. property OnStartDrag;
  644. end;
  645. { TFlatWater }
  646. TFlatWater = class(TDefineWater)
  647. published
  648. property Damping;
  649. property CtrlImage;
  650. property Items;
  651. property Enabled;
  652. property Interval;
  653. end;
  654. implementation
  655. {$R FlatSysex.res}
  656. uses FlatExcpt, FlatCnsts;
  657. { Error reporting }
  658. procedure RaiseGridError(const S: string);
  659. begin
  660. raise EInvalidGridOperation.Create(S);
  661. end;
  662. { TEduceData }
  663. constructor TEduceData.Create(Collection: TCollection);
  664. var
  665. Excel: TDefineExcel;
  666. begin
  667. Excel := nil;
  668. if Assigned(Collection) and (Collection is TEduceDatas) then
  669. Excel := TEduceDatas(Collection).Cells;
  670. if Assigned(Excel) then Excel.BeginLayout;
  671. try
  672. inherited Create(Collection);
  673. FVisible := True;
  674. FStored := True;
  675. finally
  676. if Assigned(Excel) then Excel.EndLayout;
  677. end;
  678. end;
  679. destructor TEduceData.Destroy;
  680. begin
  681. inherited Destroy;
  682. end;
  683. procedure TEduceData.Assign(Source: TPersistent);
  684. begin
  685. if Source is TEduceData then
  686. begin
  687. if Assigned(Collection) then Collection.BeginUpdate;
  688. try
  689. FieldName := TEduceData(Source).FieldName;
  690. FCaption := TEduceData(Source).Caption;
  691. FVisible := TEduceData(Source).Visible;
  692. Changed(false);
  693. finally
  694. if Assigned(Collection) then Collection.EndUpdate;
  695. end;
  696. end else inherited Assign(Source);
  697. end;
  698. function TEduceData.GetExcel: TDefineExcel;
  699. begin
  700. if Assigned(Collection) and (Collection is TEduceDatas) then
  701. Result := TEduceDatas(Collection).Cells
  702. else
  703. Result := nil;
  704. end;
  705. function TEduceData.GetDisplayName: string;
  706. begin
  707. Result := FCaption;
  708. if Result = '' then
  709. Result := inherited GetDisplayName;
  710. end;
  711. procedure TEduceData.SetCaption(const Value: String);
  712. begin
  713. if (Value <> FCaption) then
  714. begin
  715. FCaption := Value;
  716. Changed(false);
  717. end;
  718. end;
  719. procedure TEduceData.SetField(Value: TField);
  720. begin
  721. if FField = Value then Exit;
  722. if Assigned(FField) and (GetExcel <> nil) then
  723. FField.RemoveFreeNotification(GetExcel);
  724. if Assigned(Value) and (csDestroying in Value.ComponentState) then
  725. Value := nil;
  726. FField := Value;
  727. if Assigned(Value) then
  728. begin
  729. if GetExcel <> nil then
  730. FField.FreeNotification(GetExcel);
  731. FFieldName := Value.FullName;
  732. if (Length(FCaption)=0) and (Length(FieldName) > 0) then
  733. begin
  734. if Value.DisplayLabel = '' then
  735. FCaption := Value.FullName
  736. else
  737. FCaption := Value.DisplayLabel;
  738. end;
  739. end;
  740. if not IsStored then
  741. begin
  742. if Value = nil then
  743. FFieldName := '';
  744. end;
  745. Changed(False);
  746. end;
  747. function TEduceData.GetField: TField;
  748. var
  749. Cell: TDefineExcel;
  750. begin
  751. Cell := GetExcel;
  752. if (FField = nil) and (Length(FFieldName) > 0) and Assigned(Cell) and
  753. Assigned(Cell.DataLink.DataSet) then
  754. begin
  755. with Cell.Datalink.Dataset do
  756. if Active or (not DefaultFields) then
  757. SetField(FindField(FieldName));
  758. end;
  759. Result := FField;
  760. end;
  761. procedure TEduceData.SetFieldName(const Value: String);
  762. var
  763. AField: TField;
  764. Cells: TDefineExcel;
  765. begin
  766. AField := nil;
  767. Cells := GetExcel;
  768. if Assigned(Cells) and Assigned(Cells.DataLink.DataSet) and
  769. not (csLoading in Cells.ComponentState) and (Length(Value) > 0) then
  770. AField := Cells.DataLink.DataSet.FindField(Value); { no exceptions }
  771. FFieldName := Value;
  772. SetField(AField);
  773. Changed(False);
  774. end;
  775. procedure TEduceData.SetVisible(const Value: Boolean);
  776. begin
  777. if Value <> FVisible then
  778. begin
  779. FVisible := Value;
  780. Changed(false);
  781. end;
  782. end;
  783. { TEduceDatas }
  784. constructor TEduceDatas.Create(DSExcel: TDefineExcel; ColumnClass: TEduceDataClass);
  785. begin
  786. inherited Create(ColumnClass);
  787. FCells := DSExcel;
  788. end;
  789. function TEduceDatas.Add: TEduceData;
  790. begin
  791. Result := TEduceData(inherited Add);
  792. end;
  793. function TEduceDatas.GetColumn(Index: Integer): TEduceData;
  794. begin
  795. Result := TEduceData(inherited Items[Index]);
  796. end;
  797. function TEduceDatas.GetOwner: TPersistent;
  798. begin
  799. Result := FCells;
  800. end;
  801. procedure TEduceDatas.LoadFromFile(const Filename: string);
  802. var
  803. S: TFileStream;
  804. begin
  805. S := TFileStream.Create(Filename, fmOpenRead);
  806. try
  807. LoadFromStream(S);
  808. finally
  809. S.Free;
  810. end;
  811. end;
  812. { TEduceWrapper }
  813. type
  814. TEduceWrapper = class(TComponent)
  815. private
  816. FColumns: TEduceDatas;
  817. published
  818. property Columns: TEduceDatas read FColumns write FColumns;
  819. end;
  820. procedure TEduceDatas.LoadFromStream(S: TStream);
  821. var
  822. Wrapper: TEduceWrapper;
  823. begin
  824. Wrapper := TEduceWrapper.Create(nil);
  825. try
  826. Wrapper.Columns := FCells.CreateColumns;
  827. S.ReadComponent(Wrapper);
  828. Assign(Wrapper.Columns);
  829. finally
  830. Wrapper.Columns.Free;
  831. Wrapper.Free;
  832. end;
  833. end;
  834. procedure TEduceDatas.RebuildColumns;
  835. procedure AddFields(Fields: TFields; Depth: Integer);
  836. var
  837. I: Integer;
  838. begin
  839. Inc(Depth);
  840. for I := 0 to Fields.Count-1 do
  841. begin
  842. Add.FieldName := Fields[I].FullName;
  843. if Fields[I].DataType in [ftADT, ftArray] then
  844. AddFields((Fields[I] as TObjectField).Fields, Depth);
  845. end;
  846. end;
  847. begin
  848. if Assigned(FCells) and Assigned(FCells.DataSource) and
  849. Assigned(FCells.Datasource.DataSet) then
  850. begin
  851. FCells.BeginLayout;
  852. try
  853. Clear;
  854. AddFields(FCells.DataSource.DataSet.Fields, 0);
  855. finally
  856. FCells.EndLayout;
  857. end
  858. end
  859. else
  860. Clear;
  861. end;
  862. procedure TEduceDatas.SaveToFile(const Filename: string);
  863. var
  864. S: TStream;
  865. begin
  866. S := TFileStream.Create(Filename, fmCreate);
  867. try
  868. SaveToStream(S);
  869. finally
  870. S.Free;
  871. end;
  872. end;
  873. procedure TEduceDatas.SaveToStream(S: TStream);
  874. var
  875. Wrapper: TEduceWrapper;
  876. begin
  877. Wrapper := TEduceWrapper.Create(nil);
  878. try
  879. Wrapper.Columns := Self;
  880. S.WriteComponent(Wrapper);
  881. finally
  882. Wrapper.Free;
  883. end;
  884. end;
  885. procedure TEduceDatas.SetColumn(Index: Integer; Value: TEduceData);
  886. begin
  887. Items[Index].Assign(Value);
  888. end;
  889. procedure TEduceDatas.SetState(NewState: TEduceDatasState);
  890. begin
  891. if NewState = State then Exit;
  892. if NewState = csDefault then
  893. Clear
  894. else
  895. RebuildColumns;
  896. end;
  897. function TEduceDatas.InternalAdd: TEduceData;
  898. begin
  899. Result := Add;
  900. Result.FStored := False;
  901. end;
  902. function TEduceDatas.GetState: TEduceDatasState;
  903. begin
  904. Result := TEduceDatasState((Count > 0) and Items[0].IsStored);
  905. end;
  906. procedure TEduceDatas.Update(Item: TCollectionItem);
  907. begin
  908. if (FCells = nil) or (csLoading in FCells.ComponentState) then Exit;
  909. if Item = nil then
  910. begin
  911. FCells.LayoutChanged;
  912. end;
  913. end;
  914. { TDefineExcel }
  915. var
  916. ExcelBof : array[0..5] of Word = ($809, 8, 0, $10, 0, 0);
  917. ExcelEof : array[0..1] of Word = ($0A, 00);
  918. ExcelLabel : array[0..5] of Word = ($204, 0, 0, 0, 0, 0);
  919. ExcelNum : array[0..4] of Word = ($203, 14, 0, 0, 0);
  920. ExcelRec : array[0..4] of Word = ($27E, 10, 0, 0, 0);
  921. ExcelBlank : array[0..4] of Word = ($201, 6, 0, 0, $17);
  922. Constructor TDefineExcel.Create(AOwner: TComponent);
  923. begin
  924. inherited Create(AOwner);
  925. FColumns := CreateColumns;
  926. FDatalink := CreateDatalink;
  927. FEduceType := dmDefault;
  928. FEduceTitle := true;
  929. FInterval := 500;
  930. FShowProgress := true;
  931. FFileName := '未命名表格文件';
  932. FEduceMode := emSingle;
  933. FDefaultExt := '.xls';
  934. end;
  935. destructor TDefineExcel.Destroy;
  936. begin
  937. FColumns.Free;
  938. FColumns := nil;
  939. FDataLink.Free;
  940. FDataLink := nil;
  941. inherited Destroy;
  942. end;
  943. function TDefineExcel.CreateColumns: TEduceDatas;
  944. begin
  945. Result := TEduceDatas.Create(Self, TEduceData);
  946. end;
  947. procedure TDefineExcel.IncColRow;
  948. begin
  949. if fCol = EduceCount - 1 then
  950. begin
  951. Inc(fRow);
  952. fCol :=0;
  953. end else
  954. Inc(fCol);
  955. end;
  956. //写空单元
  957. procedure TDefineExcel.WriteBlankCell;
  958. begin
  959. ExcelBlank[2] := fRow;
  960. ExcelBlank[3] := fCol;
  961. ExcelStream.WriteBuffer(ExcelBlank, SizeOf(ExcelBlank));
  962. IncColRow;
  963. end;
  964. //写浮点单元
  965. procedure TDefineExcel.WriteFloatCell(const AValue: Double);
  966. begin
  967. ExcelNum[2] := fRow;
  968. ExcelNum[3] := fCol;
  969. ExcelStream.WriteBuffer(ExcelNum, SizeOf(ExcelNum));
  970. ExcelStream.WriteBuffer(AValue, 8);
  971. IncColRow;
  972. end;
  973. //写整数单元
  974. procedure TDefineExcel.WriteIntegerCell(const AValue: Integer);
  975. var V: Integer;
  976. begin
  977. ExcelRec[2] := fRow;
  978. ExcelRec[3] := fCol;
  979. ExcelStream.WriteBuffer(ExcelRec, SizeOf(ExcelRec));
  980. V := (AValue shl 2) or 2;
  981. ExcelStream.WriteBuffer(V, 4);
  982. IncColRow;
  983. end;
  984. //写字符单元
  985. procedure TDefineExcel.WriteStringCell(const AValue: string);
  986. var
  987. L: Word;
  988. begin
  989. L := Length(AValue);
  990. ExcelLabel[1] := 8 + L;
  991. ExcelLabel[2] := fRow;
  992. ExcelLabel[3] := fCol;
  993. ExcelLabel[5] := L;
  994. ExcelStream.WriteBuffer(ExcelLabel, SizeOf(ExcelLabel));
  995. ExcelStream.WriteBuffer(Pointer(AValue)^, L);
  996. IncColRow;
  997. end;
  998. //写前缀
  999. procedure TDefineExcel.WritePrefix;
  1000. begin
  1001. ExcelStream.WriteBuffer(ExcelBof, SizeOf(ExcelBof));
  1002. end;
  1003. //写后缀
  1004. procedure TDefineExcel.WriteSuffix;
  1005. begin
  1006. ExcelStream.WriteBuffer(ExcelEof, SizeOf(ExcelEof));
  1007. end;
  1008. //写标题
  1009. procedure TDefineExcel.WriteTitle;
  1010. var n: word;
  1011. begin
  1012. if FEduceTitle then
  1013. begin
  1014. for n:= 0 to FColumns.Count - 1 do
  1015. begin
  1016. if FColumns[n].Visible then WriteStringCell(FColumns[n].Caption);
  1017. end;
  1018. end;
  1019. end;
  1020. procedure TDefineExcel.StartProgress(Max:Integer);
  1021. begin
  1022. if (not Assigned(FExcelForm))and(FShowProgress) then
  1023. Application.CreateForm(TExcelForm, FExcelForm);
  1024. if Assigned(FExcelForm) then
  1025. begin
  1026. with FExcelForm do
  1027. begin
  1028. ProGauge.Max :=Max;
  1029. ProGauge.Min :=0;
  1030. ProGauge.Progress:=0;
  1031. Show;
  1032. BringToFront;
  1033. end;
  1034. end;
  1035. end;
  1036. procedure TDefineExcel.EndProgress;
  1037. begin
  1038. if Assigned(FExcelForm) then
  1039. begin
  1040. with FExcelForm do
  1041. begin
  1042. ProGauge.Progress := ProGauge.Progress+1;
  1043. if ProGauge.Progress >= ProGauge.Max then
  1044. begin
  1045. Sleep(FInterval);
  1046. Close;
  1047. end;
  1048. end;
  1049. Application.ProcessMessages;
  1050. end;
  1051. end;
  1052. procedure TDefineExcel.WriteData(Field:TField);
  1053. begin
  1054. if Field.IsNull then
  1055. WriteBlankCell
  1056. else
  1057. case FEduceType of
  1058. dmDefault:
  1059. case Field.DataType of
  1060. ftSmallint,
  1061. ftInteger,
  1062. ftWord,
  1063. ftAutoInc,
  1064. ftBytes: WriteIntegerCell(Field.AsInteger);
  1065. ftFloat,
  1066. ftCurrency,
  1067. ftBCD: WriteFloatCell(Field.AsFloat);
  1068. else
  1069. WriteStringCell(Field.AsString);
  1070. end;
  1071. dmString:WriteStringCell(Field.AsString);
  1072. end;
  1073. end;
  1074. //正式写入Excel表的数据
  1075. procedure TDefineExcel.WriteDataCells;
  1076. var n: word;
  1077. fBookMark : TBookmark;
  1078. begin
  1079. //写入 Excel 文件开始格式
  1080. WritePrefix;
  1081. //写入标题名称
  1082. WriteTitle;
  1083. //开始写入各字段数据
  1084. with FDataLink.DataSet do
  1085. begin
  1086. //禁止在数据感知控件中显示
  1087. DisableControls;
  1088. //初始化处理进度
  1089. StartProgress(RecordCount);
  1090. //记录当记录的位置
  1091. fBookMark := GetBookmark;
  1092. //指向第一条记录
  1093. First;
  1094. while not Eof do begin
  1095. for n := 0 to ColumnCount - 1 do
  1096. begin
  1097. case FEduceMode of
  1098. emSingle:
  1099. begin
  1100. if FColumns[n].Visible then
  1101. WriteData(FColumns[n].Field);
  1102. end;
  1103. emDefault:
  1104. begin
  1105. WriteData(FColumns[n].Field);
  1106. end;
  1107. end;
  1108. end;
  1109. EndProgress;
  1110. Next;
  1111. end;
  1112. //还原处理前的记录位置
  1113. GotoBookmark(fBookMark);
  1114. //充许在数据感知控件中显示
  1115. EnableControls;
  1116. end;
  1117. //写入 Excel 文件结束标识
  1118. WriteSuffix;
  1119. end;
  1120. procedure TDefineExcel.SaveExcel(Save: TStream);
  1121. begin
  1122. fCol := 0;
  1123. fRow := 0;
  1124. ExcelStream := Save;
  1125. WriteDataCells;
  1126. end;
  1127. procedure TDefineExcel.DefineFieldMap;
  1128. var
  1129. I: Integer;
  1130. begin
  1131. if FColumns.State = csCustomized then
  1132. begin
  1133. FDataLink.SparseMap := True;
  1134. for I := 0 to FColumns.Count-1 do
  1135. FDataLink.AddMapping(FColumns[I].FieldName);
  1136. end
  1137. else
  1138. begin
  1139. FDataLink.SparseMap := False;
  1140. with FDataLink.Dataset do
  1141. for I := 0 to FieldList.Count - 1 do
  1142. with FieldList[I] do if Visible then FDataLink.AddMapping(FullName);
  1143. end;
  1144. end;
  1145. procedure TDefineExcel.InitColumns;
  1146. function FieldIsMapped(F: TField): Boolean;
  1147. var
  1148. X: Integer;
  1149. begin
  1150. Result := False;
  1151. if F = nil then Exit;
  1152. for X := 0 to FDataLink.FieldCount-1 do
  1153. if FDataLink.Fields[X] = F then
  1154. begin
  1155. Result := True;
  1156. Exit;
  1157. end;
  1158. end;
  1159. procedure CheckForPassthroughs; // check for Columns.State flip-flop
  1160. var
  1161. SeenPassthrough: Boolean;
  1162. I, J: Integer;
  1163. Column: TEduceData;
  1164. begin
  1165. SeenPassthrough := False;
  1166. for I := 0 to FColumns.Count-1 do
  1167. if not FColumns[I].IsStored then
  1168. SeenPassthrough := True
  1169. else if SeenPassthrough then
  1170. begin
  1171. for J := FColumns.Count-1 downto 0 do
  1172. begin
  1173. Column := FColumns[J];
  1174. if not Column.IsStored then
  1175. Column.Free;
  1176. end;
  1177. Exit;
  1178. end;
  1179. end;
  1180. procedure ResetColumnFieldBindings;
  1181. var
  1182. I, J, K: Integer;
  1183. Fld: TField;
  1184. Column: TEduceData;
  1185. begin
  1186. if FColumns.State = csDefault then
  1187. begin
  1188. if (not FDataLink.Active) and (FDataLink.DefaultFields) then
  1189. FColumns.Clear
  1190. else
  1191. begin
  1192. for J := FColumns.Count-1 downto 0 do
  1193. begin
  1194. with FColumns[J] do
  1195. begin
  1196. if not Assigned(Field) or not FieldIsMapped(Field) then
  1197. Free;
  1198. end;
  1199. end;
  1200. end;
  1201. I := FDataLink.FieldCount;
  1202. //if (I = 0) and (FColumns.Count = 0) then
  1203. // Inc(I);
  1204. for J := 0 to I-1 do
  1205. begin
  1206. Fld := FDataLink.Fields[J];
  1207. if Assigned(Fld) then
  1208. begin
  1209. K := J;
  1210. while (K < FColumns.Count) and (FColumns[K].Field <> Fld) do
  1211. Inc(K);
  1212. if K < FColumns.Count then
  1213. Column := FColumns[K]
  1214. else
  1215. begin
  1216. Column := FColumns.InternalAdd;
  1217. Column.Field := Fld;
  1218. end;
  1219. end
  1220. else
  1221. Column := FColumns.InternalAdd;
  1222. Column.Index := J;
  1223. end;
  1224. end
  1225. else
  1226. begin
  1227. for I := 0 to FColumns.Count-1 do
  1228. FColumns[I].Field := nil;
  1229. end;
  1230. end;
  1231. begin
  1232. if ([csLoading, csDestroying] * ComponentState) <> [] then
  1233. Exit;
  1234. CheckForPassthroughs;
  1235. FDatalink.ClearMapping;
  1236. if FDatalink.Active then
  1237. DefineFieldMap;
  1238. ResetColumnFieldBindings;
  1239. end;
  1240. procedure TDefineExcel.SeTEduceType(const Value: TEduceType);
  1241. begin
  1242. if FEduceType <> Value then
  1243. FEduceType := Value;
  1244. end;
  1245. procedure TDefineExcel.SetColumns(const Value: TEduceDatas);
  1246. begin
  1247. FColumns.Assign(Value);
  1248. end;
  1249. procedure TDefineExcel.DefineProperties(Filer: TFiler);
  1250. var
  1251. StoreIt: Boolean;
  1252. vState: TEduceDatasState;
  1253. begin
  1254. vState := EduceDatas.State;
  1255. if Filer.Ancestor = nil then
  1256. StoreIt := vState = csCustomized
  1257. else
  1258. if vState <> TDefineExcel(Filer.Ancestor).EduceDatas.State then
  1259. StoreIt := True
  1260. else
  1261. StoreIt := (vState = csCustomized) and
  1262. (not CollectionsEqual(EduceDatas, TDefineExcel(Filer.Ancestor).EduceDatas, Self, TDefineExcel(Filer.Ancestor)));
  1263. Filer.DefineProperty('Columns', ReadColumns, WriteColumns, StoreIt);
  1264. inherited DefineProperties(Filer);
  1265. end;
  1266. procedure TDefineExcel.ReadColumns(Reader: TReader);
  1267. begin
  1268. EduceDatas.Clear;
  1269. Reader.ReadValue;
  1270. Reader.ReadCollection(EduceDatas);
  1271. end;
  1272. procedure TDefineExcel.WriteColumns(Writer: TWriter);
  1273. begin
  1274. if EduceDatas.State = csCustomized then
  1275. Writer.WriteCollection(EduceDatas)
  1276. else // ancestor state is customized, ours is not
  1277. Writer.WriteCollection(nil);
  1278. end;
  1279. function TDefineExcel.GetFieldCount: Integer;
  1280. begin
  1281. if Assigned(FDataLink.DataSet) then
  1282. result := FDataLink.FieldCount
  1283. else
  1284. result := 0;
  1285. end;
  1286. procedure TDefineExcel.BeginLayout;
  1287. begin
  1288. BeginUpdate;
  1289. if FLayoutLock = 0 then
  1290. EduceDatas.BeginUpdate;
  1291. Inc(FLayoutLock);
  1292. end;
  1293. procedure TDefineExcel.BeginUpdate;
  1294. begin
  1295. Inc(FUpdateLock);
  1296. end;
  1297. procedure TDefineExcel.EndLayout;
  1298. begin
  1299. if FLayoutLock > 0 then
  1300. begin
  1301. try
  1302. try
  1303. if FLayoutLock = 1 then
  1304. InitColumns;
  1305. finally
  1306. if FLayoutLock = 1 then
  1307. FColumns.EndUpdate;
  1308. end;
  1309. finally
  1310. Dec(FLayoutLock);
  1311. EndUpdate;
  1312. end;
  1313. end;
  1314. end;
  1315. procedure TDefineExcel.EndUpdate;
  1316. begin
  1317. if FUpdateLock > 0 then
  1318. Dec(FUpdateLock);
  1319. end;
  1320. procedure TDefineExcel.LayoutChanged;
  1321. begin
  1322. if AcquireLayoutLock then
  1323. EndLayout;
  1324. end;
  1325. function TDefineExcel.AcquireLayoutLock: Boolean;
  1326. begin
  1327. Result := (FUpdateLock = 0) and (FLayoutLock = 0);
  1328. if Result then BeginLayout;
  1329. end;
  1330. procedure TDefineExcel.Loaded;
  1331. begin
  1332. inherited Loaded;
  1333. LayoutChanged;
  1334. end;
  1335. function TDefineExcel.GetDataSource: TDataSource;
  1336. begin
  1337. Result := FDataLink.DataSource;
  1338. end;
  1339. procedure TDefineExcel.SetDataSource(const Value: TDataSource);
  1340. begin
  1341. if Value = FDatalink.Datasource then Exit;
  1342. if Assigned(Value) then
  1343. if Assigned(Value.DataSet) then
  1344. if Value.DataSet.IsUnidirectional then
  1345. DatabaseError(SDataSetUnidirectional);
  1346. FDataLink.DataSource := Value;
  1347. if Value <> nil then Value.FreeNotification(Self);
  1348. end;
  1349. procedure TDefineExcel.LinkActive(Value: Boolean);
  1350. begin
  1351. try
  1352. LayoutChanged;
  1353. finally
  1354. //
  1355. end;
  1356. end;
  1357. function TDefineExcel.CreateDataLink: TEduceLink;
  1358. begin
  1359. Result := TEduceLink.Create(Self);
  1360. end;
  1361. function TDefineExcel.GetColumnCount: integer;
  1362. begin
  1363. Result := FColumns.Count;
  1364. end;
  1365. function TDefineExcel.GetEduceCount: integer;
  1366. var
  1367. i:integer;
  1368. begin
  1369. result := 0;
  1370. for i:= 0 to FColumns.Count - 1 do
  1371. if FColumns[i].Visible then result := result + 1;
  1372. end;
  1373. procedure TDefineExcel.ExportAll;
  1374. var i:integer;
  1375. begin
  1376. for i:=0 to ColumnCount - 1 do FColumns[i].Visible := True;
  1377. end;
  1378. function TDefineExcel.GetFields(FieldIndex: Integer): TField;
  1379. begin
  1380. Result := FDatalink.Fields[FieldIndex];
  1381. end;
  1382. procedure TDefineExcel.CancelLayout;
  1383. begin
  1384. if FLayoutLock > 0 then
  1385. begin
  1386. if FLayoutLock = 1 then
  1387. EduceDatas.EndUpdate;
  1388. Dec(FLayoutLock);
  1389. EndUpdate;
  1390. end;
  1391. end;
  1392. procedure TDefineExcel.ExecuteSave;
  1393. var
  1394. SaveDlg: TSaveDialog;
  1395. FileStream: TFileStream;
  1396. inx: integer;
  1397. UseState: boolean;
  1398. tFile:String;
  1399. begin
  1400. case FEduceMode of
  1401. emSingle:
  1402. begin
  1403. FieldForm := TFieldForm.Create(self);
  1404. try
  1405. FieldForm.FieldBox.Items.Clear;
  1406. for inx := 0 to FColumns.Count - 1 do
  1407. begin
  1408. FieldForm.FieldBox.Items.Add(FColumns[inx].Caption);
  1409. FieldForm.FieldBox.Checked[inx] := FColumns[inx].Visible;
  1410. end;
  1411. FieldForm.ShowModal;
  1412. if FieldForm.ModalResult = mrOk then
  1413. begin
  1414. for inx := 0 to FieldForm.FieldBox.Items.Count - 1 do
  1415. FColumns[inx].Visible := FieldForm.FieldBox.Checked[inx];
  1416. SaveDlg := TSaveDialog.Create(self);
  1417. try
  1418. SaveDlg.DefaultExt := FDefaultExt;
  1419. SaveDlg.Filter := '微软电子表格(MS-EXCEL文件)|*.XLS';
  1420. SaveDlg.Title := '保存为';
  1421. SaveDlg.FileName := FFileName;
  1422. if SaveDlg.Execute then
  1423. begin
  1424. if Assigned(FDataLink.DataSet) then
  1425. begin
  1426. useState := true;
  1427. if FileExists(SaveDlg.FileName) then
  1428. useState := DeleteFile(SaveDlg.FileName);
  1429. if useState then
  1430. begin
  1431. FileStream := TFileStream.Create(SaveDlg.FileName, fmCreate);
  1432. try
  1433. SaveExcel(FileStream);
  1434. Finally
  1435. FileStream.Free;
  1436. end;
  1437. end
  1438. else ShowMessage('文件正在使用中,不能覆盖文件!');
  1439. end;
  1440. end;
  1441. finally
  1442. SaveDlg.Free;
  1443. end;
  1444. end;
  1445. finally
  1446. FieldForm.Free;
  1447. FieldForm := Nil;
  1448. end;
  1449. end;
  1450. emDefault:
  1451. begin
  1452. if Assigned(FDataLink.DataSet) then
  1453. begin
  1454. useState := true;
  1455. tFile := FFileName;
  1456. if UpperCase(ExtractFileExt(FFileName))<>UpperCase(FDefaultExt) then
  1457. tFile := FFileName + FDefaultExt;
  1458. if FileExists(tFile) then
  1459. useState := DeleteFile(tFile);
  1460. if useState then
  1461. begin
  1462. FileStream := TFileStream.Create(tFile, fmCreate);
  1463. try
  1464. SaveExcel(FileStream);
  1465. Finally
  1466. FileStream.Free;
  1467. end;
  1468. end
  1469. else ShowMessage('文件正在使用中,不能覆盖文件!');
  1470. end;
  1471. end;
  1472. end;
  1473. end;
  1474. procedure TDefineExcel.InitFields;
  1475. var
  1476. inx: integer;
  1477. Col: TEduceData;
  1478. begin
  1479. if Assigned(FDataLink.DataSet) then
  1480. begin
  1481. with FDataLink.DataSet.FieldDefs do
  1482. begin
  1483. if (not FDataLink.Active) and (Count > 0) then
  1484. begin
  1485. FColumns.BeginUpdate;
  1486. FColumns.Clear;
  1487. for inx:=0 to Count - 1 do
  1488. begin
  1489. Col := FColumns.Add;
  1490. Col.FieldName := Items[inx].Name;
  1491. Col.Caption := Items[inx].Name;
  1492. end;
  1493. FColumns.EndUpdate;
  1494. end;
  1495. end;
  1496. end;
  1497. end;
  1498. procedure TDefineExcel.ClearFields;
  1499. begin
  1500. FColumns.BeginUpdate;
  1501. FColumns.Clear;
  1502. FColumns.EndUpdate;
  1503. end;
  1504. procedure TDefineExcel.RestoreFields;
  1505. var
  1506. inx : integer;
  1507. col : TEduceData;
  1508. begin
  1509. FColumns.BeginUpdate;
  1510. for inx:=0 to FColumns.Count - 1 do
  1511. begin
  1512. Col := FColumns[inx];
  1513. Col.Caption := Col.FieldName;
  1514. Col.Visible := True;
  1515. end;
  1516. FColumns.EndUpdate;
  1517. end;
  1518. procedure TDefineExcel.SetDefaultExt(Value: String);
  1519. begin
  1520. if FDefaultExt <> Value then
  1521. begin
  1522. if Value[1] <> '.' then
  1523. Value := '.'+value;
  1524. FDefaultExt := Value;
  1525. end;
  1526. end;
  1527. { TEduceLink }
  1528. const
  1529. MaxMapSize = (MaxInt div 2) div SizeOf(Integer);
  1530. type
  1531. TIntArray = array[0..MaxMapSize] of Integer;
  1532. PIntArray = ^TIntArray;
  1533. constructor TEduceLink.Create(ADSExcel: TDefineExcel);
  1534. begin
  1535. inherited Create;
  1536. FCells := ADSExcel;
  1537. VisualControl := True;
  1538. end;
  1539. destructor TEduceLink.Destroy;
  1540. begin
  1541. ClearMapping;
  1542. inherited Destroy;
  1543. end;
  1544. function TEduceLink.GetDefaultFields: Boolean;
  1545. var
  1546. I: Integer;
  1547. begin
  1548. Result := True;
  1549. if DataSet <> nil then
  1550. Result := DataSet.DefaultFields;
  1551. if Result and SparseMap then
  1552. for I := 0 to FFieldCount-1 do
  1553. if FFieldMap[I] < 0 then
  1554. begin
  1555. Result := False;
  1556. Exit;
  1557. end;
  1558. end;
  1559. function TEduceLink.GetFields(I: Integer): TField;
  1560. begin
  1561. if (0 <= I) and (I < FFieldCount) and (FFieldMap[I] >= 0) then
  1562. Result := DataSet.FieldList[FFieldMap[I]]
  1563. else
  1564. Result := nil;
  1565. end;
  1566. function TEduceLink.AddMapping(const FieldName: string): Boolean;
  1567. var
  1568. Field: TField;
  1569. NewSize: Integer;
  1570. begin
  1571. Result := True;
  1572. if FFieldCount >= MaxMapSize then
  1573. RaiseGridError(STooManyColumns);
  1574. if SparseMap then
  1575. Field := DataSet.FindField(FieldName)
  1576. else
  1577. Field := DataSet.FieldByName(FieldName);
  1578. if FFieldCount = Length(FFieldMap) then
  1579. begin
  1580. NewSize := Length(FFieldMap);
  1581. if NewSize = 0 then
  1582. NewSize := 8
  1583. else
  1584. Inc(NewSize, NewSize);
  1585. if (NewSize < FFieldCount) then
  1586. NewSize := FFieldCount + 1;
  1587. if (NewSize > MaxMapSize) then
  1588. NewSize := MaxMapSize;
  1589. SetLength(FFieldMap, NewSize);
  1590. end;
  1591. if Assigned(Field) then
  1592. begin
  1593. FFieldMap[FFieldCount] := Dataset.FieldList.IndexOfObject(Field);
  1594. Field.FreeNotification(FCells);
  1595. end
  1596. else
  1597. FFieldMap[FFieldCount] := -1;
  1598. Inc(FFieldCount);
  1599. end;
  1600. procedure TEduceLink.ActiveChanged;
  1601. begin
  1602. if Active and Assigned(DataSource) then
  1603. if Assigned(DataSource.DataSet) then
  1604. if DataSource.DataSet.IsUnidirectional then
  1605. DatabaseError(SDataSetUnidirectional);
  1606. FCells.LinkActive(Active);
  1607. FModified := False;
  1608. end;
  1609. procedure TEduceLink.ClearMapping;
  1610. begin
  1611. FFieldMap := nil;
  1612. FFieldCount := 0;
  1613. end;
  1614. procedure TEduceLink.LayoutChanged;
  1615. var
  1616. SaveState: Boolean;
  1617. begin
  1618. SaveState := FCells.LayoutSet;
  1619. FCells.LayoutSet := True;
  1620. try
  1621. FCells.LayoutChanged;
  1622. finally
  1623. FCells.LayoutSet := SaveState;
  1624. end;
  1625. inherited LayoutChanged;
  1626. end;
  1627. function TEduceLink.GetMappedIndex(ColIndex: Integer): Integer;
  1628. begin
  1629. if (0 <= ColIndex) and (ColIndex < FFieldCount) then
  1630. Result := FFieldMap[ColIndex]
  1631. else
  1632. Result := -1;
  1633. end;
  1634. function TEduceLink.IsAggRow(Value: Integer): Boolean;
  1635. begin
  1636. Result := False;
  1637. end;
  1638. { TFlatSound }
  1639. const
  1640. Flags = SND_RESOURCE or SND_SYNC;
  1641. constructor TFlatSound.Create(AOwner: TComponent);
  1642. begin
  1643. inherited Create(AOwner);
  1644. Event := seBtnClick;
  1645. end;
  1646. procedure TFlatSound.Play;
  1647. begin
  1648. case FEvent of
  1649. seBtnClick: PlaySound('ENC_001',0,Flags);
  1650. seMenu: PlaySound('ENC_002',0,Flags);
  1651. seMenuClick: PlaySound('ENC_003',0,Flags);
  1652. seMoveIntoBtn: PlaySound('ENC_004',0,Flags);
  1653. sePanelExpand: PlaySound('ENC_005',0,Flags);
  1654. end;
  1655. end;
  1656. procedure TFlatSound.PlayThis(ThisEvent: TSoundEvent);
  1657. begin
  1658. case ThisEvent of
  1659. seBtnClick: PlaySound('ENC_001',0,Flags);
  1660. seMenu: PlaySound('ENC_002',0,Flags);
  1661. seMenuClick: PlaySound('ENC_003',0,Flags);
  1662. seMoveIntoBtn: PlaySound('ENC_004',0,Flags);
  1663. sePanelExpand: PlaySound('ENC_005',0,Flags);
  1664. end;
  1665. end;
  1666. { TFlatAnimWnd }
  1667. var
  1668. OwnerList: TList;
  1669. constructor TFlatAnimHookWnd.Create(AOwner: TComponent);
  1670. begin
  1671. inherited Create(AOwner);
  1672. FAnimateWindow := TFlatAnimWnd(AOwner);
  1673. end;
  1674. procedure TFlatAnimHookWnd.WMCreate(var Message: TMessage);
  1675. begin
  1676. inherited;
  1677. FAnimateWindow.OwnerWndCreated;
  1678. end;
  1679. procedure TFlatAnimHookWnd.WMDestroy(var Message: TMessage);
  1680. begin
  1681. FAnimateWindow.OwnerWndDestroyed;
  1682. inherited;
  1683. end;
  1684. constructor TFlatAnimWnd.Create(AOwner: TComponent);
  1685. begin
  1686. FOwner := AOwner;
  1687. if OwnerList.IndexOf(FOwner) <> -1 then
  1688. begin
  1689. FOwner := nil;
  1690. raise Exception.Create('Owner must be TFORM');
  1691. end;
  1692. inherited Create(AOwner);
  1693. if not (csDesigning in ComponentState) then
  1694. begin
  1695. FHookWnd := TFlatAnimHookWnd.Create(Self);
  1696. if Application.MainForm = nil then
  1697. begin
  1698. FNewAppProc := MakeObjectInstance(NewAppWndProc);
  1699. FOldAppProc := Pointer(GetWindowLong(Application.Handle, GWL_WNDPROC));
  1700. SetWindowLong(Application.Handle, GWL_WNDPROC, Longint(FNewAppProc));
  1701. end;
  1702. end;
  1703. OwnerList.Add(FOwner);
  1704. end;
  1705. destructor TFlatAnimWnd.Destroy;
  1706. begin
  1707. if not(csDesigning in ComponentState) then
  1708. begin
  1709. if Application.MainForm = nil then
  1710. begin
  1711. SetWindowLong(Application.Handle, GWL_WNDPROC, Longint(FOldAppProc));
  1712. FreeObjectInstance(FNewAppProc);
  1713. end;
  1714. end;
  1715. if OwnerList.IndexOf(FOwner) <> -1 then
  1716. OwnerList.Remove(FOwner);
  1717. inherited Destroy;
  1718. end;
  1719. procedure TFlatAnimWnd.SetParentComponent(Value: TComponent);
  1720. begin
  1721. inherited SetParentComponent(Value);
  1722. if not(csDesigning in ComponentState) then
  1723. if Value is TWinControl then
  1724. FHookWnd.Parent := TWinControl(Value);
  1725. end;
  1726. procedure TFlatAnimWnd.OwnerWndCreated;
  1727. begin
  1728. FNewProc := MakeObjectInstance(NewWndProc);
  1729. FOldProc := Pointer(GetWindowLong((FOwner as TForm).Handle, GWL_WNDPROC));
  1730. SetWindowLong((FOwner as TForm).Handle, GWL_WNDPROC, Longint(FNewProc));
  1731. end;
  1732. procedure TFlatAnimWnd.OwnerWndDestroyed;
  1733. begin
  1734. SetWindowLong((FOwner as TForm).Handle, GWL_WNDPROC, Longint(FOldProc));
  1735. FreeObjectInstance(FNewProc);
  1736. end;
  1737. procedure TFlatAnimWnd.NewAppWndProc(var Message: TMessage);
  1738. begin
  1739. with Message do
  1740. begin
  1741. if Msg = WM_SYSCOMMAND then
  1742. case WParam of
  1743. SC_MINIMIZE:
  1744. MinimizeWnd;
  1745. SC_RESTORE:
  1746. RestoreWnd;
  1747. end;
  1748. Result := CallWindowProc(FOldAppProc, Application.Handle, Msg, wParam, lParam);
  1749. end;
  1750. end;
  1751. procedure TFlatAnimWnd.NewWndProc(var Message: TMessage);
  1752. begin
  1753. with Message do
  1754. begin
  1755. if (Msg = WM_SYSCOMMAND) and (WParam = SC_MINIMIZE) then
  1756. begin
  1757. if Application.MainForm = FOwner then
  1758. MinimizeWnd
  1759. else
  1760. PostMessage(Application.Handle, WM_SYSCOMMAND, SC_MINIMIZE, 0);
  1761. end
  1762. else
  1763. begin
  1764. if (Msg = WM_WINDOWPOSCHANGING) and (PWindowPos(lParam)^.flags = (SWP_NOSIZE or SWP_NOMOVE)) then
  1765. begin
  1766. if IsIconic(Application.Handle) then
  1767. PostMessage(Application.Handle, WM_SYSCOMMAND, SC_RESTORE, 0);
  1768. end
  1769. end;
  1770. Result := CallWindowProc(FOldProc, (FOwner as TForm).Handle, Msg, wParam, lParam);
  1771. end;
  1772. end;
  1773. procedure TFlatAnimWnd.MinimizeWnd;
  1774. var
  1775. Rect: TRect;
  1776. begin
  1777. with Application do
  1778. begin
  1779. if not(IsWindowEnabled(Handle)) then
  1780. EnableWindow(Handle, True);
  1781. GetWindowRect((FOwner as TForm).Handle, Rect);
  1782. SetForegroundWindow(Handle);
  1783. SetWindowPos(Handle, 0, Rect.Left, Rect.Top, Rect.Right - Rect.Left, 0, SWP_NOZORDER);
  1784. DefWindowProc(Handle, WM_SYSCOMMAND, SC_MINIMIZE, 0);
  1785. ShowWindow(Handle, SW_MINIMIZE);
  1786. end;
  1787. if Assigned(FOnMinimize) then
  1788. FOnMinimize(Application);
  1789. end;
  1790. procedure TFlatAnimWnd.RestoreWnd;
  1791. var
  1792. MainFormPlacement: TWindowPlacement;
  1793. AppWndPlacement: TWindowPlacement;
  1794. begin
  1795. with Application do
  1796. begin
  1797. MainFormPlacement.length := SizeOf(TWindowPlacement);
  1798. MainFormPlacement.flags := 0;
  1799. GetWindowPlacement(MainForm.Handle, @MainFormPlacement);
  1800. AppWndPlacement.length := SizeOf(TWindowPlacement);
  1801. AppWndPlacement.flags := 0;
  1802. GetWindowPlacement(Handle, @AppWndPlacement);
  1803. AppWndPlacement.rcNormalPosition := MainFormPlacement.rcNormalPosition;
  1804. AppWndPlacement.rcNormalPosition.Bottom := AppWndPlacement.rcNormalPosition.Top;
  1805. SetWindowPlacement(Handle, @AppWndPlacement);
  1806. SetForegroundWindow(Handle);
  1807. DefWindowProc(Application.Handle, WM_SYSCOMMAND, SC_RESTORE, 0);
  1808. ShowWindow(Handle, SW_RESTORE);
  1809. SetWindowPos(Handle, 0, 0, 0, 0, 0, SWP_NOZORDER);
  1810. if not(MainForm.Visible) then
  1811. begin
  1812. ShowWindow(MainForm.Handle, SW_RESTORE);
  1813. MainForm.Visible := True;
  1814. end;
  1815. end;
  1816. if Assigned(FOnRestore) then
  1817. FOnRestore(Application);
  1818. end;
  1819. procedure TFlatAnimWnd.Minimize;
  1820. begin
  1821. SendMessage((FOwner as TForm).Handle, WM_SYSCOMMAND, SC_MINIMIZE, 0);
  1822. end;
  1823. { TDefineSingle }
  1824. constructor TDefineSingle.Create(AOwner: TComponent);
  1825. begin
  1826. inherited Create(AOwner);
  1827. FActive := True;
  1828. FTitle := 'This program already run!';
  1829. FCaption := '&Exit';
  1830. end;
  1831. procedure TDefineSingle.Loaded;
  1832. begin
  1833. inherited Loaded;
  1834. Run(Active,Title);
  1835. end;
  1836. procedure TDefineSingle.Run(State: Boolean;Title:String);
  1837. begin
  1838. if (State)and(not(csDesigning in ComponentState)) then
  1839. begin
  1840. //with Application do
  1841. //begin
  1842. try
  1843. if OpenMutex(MUTEX_ALL_ACCESS, False, pchar(Application.Title)) = 0 then
  1844. begin
  1845. inherited;
  1846. ReleaseMutex(CreateMutex(nil, False, pchar(Application.Title)));
  1847. end else begin
  1848. Application.ShowMainForm := False;
  1849. ShowDialog(Title, Caption);
  1850. Application.Terminate;
  1851. end;
  1852. finally
  1853. end;
  1854. //end;
  1855. end;
  1856. end;
  1857. procedure TDefineSingle.SetActive(Value: boolean);
  1858. begin
  1859. if FActive <> Value then begin
  1860. FActive := Value;
  1861. Run(FActive,FTitle);
  1862. end;
  1863. end;
  1864. procedure TDefineSingle.SetTitle(const Value: string);
  1865. begin
  1866. if FTitle <> Value then FTitle := Value;
  1867. end;
  1868. procedure TDefineSingle.SetCaption(const Value: string);
  1869. begin
  1870. if FCaption <> Value then FCaption := Value;
  1871. end;
  1872. { TDefineTimer }
  1873. procedure TimerCallback(uTimerID, uMessage: Cardinal; dwUser, dw1, dw2: Cardinal); stdcall;
  1874. var
  1875. FlatTimer: TDefineTimer;
  1876. begin
  1877. FlatTimer := TDefineTimer(dwUser);
  1878. if Assigned(FlatTimer) then FlatTimer.Timer;
  1879. end;
  1880. constructor TDefineTimer.Create(AOwner: TComponent);
  1881. begin
  1882. inherited Create(AOwner);
  1883. FEnabled := True;
  1884. FInterval := 1000;
  1885. FPeriod := 10;
  1886. uTimerID := 0;
  1887. end;
  1888. destructor TDefineTimer.Destroy;
  1889. begin
  1890. FEnabled := False;
  1891. UpdateTimer;
  1892. inherited Destroy;
  1893. end;
  1894. procedure TDefineTimer.SetEnabled(Value: Boolean);
  1895. begin
  1896. if Value <> FEnabled then begin
  1897. FEnabled := Value;
  1898. UpdateTimer;
  1899. end;
  1900. end;
  1901. procedure TDefineTimer.SetInterval(Value: Cardinal);
  1902. begin
  1903. if Value <> FInterval then begin
  1904. FInterval := Value;
  1905. UpdateTimer;
  1906. end;
  1907. end;
  1908. procedure TDefineTimer.SetOnTimer(Value: TNotifyEvent);
  1909. begin
  1910. FOnTimer := Value;
  1911. UpdateTimer;
  1912. end;
  1913. procedure TDefineTimer.SetPeriod(Value: Cardinal);
  1914. var Caps: TTimeCaps;
  1915. begin
  1916. if (Value <> FPeriod) and (timeGetDevCaps(@Caps, Sizeof(TTimeCaps)) <> 0) then
  1917. begin
  1918. if Value < Caps.wPeriodMin then //小于最小分辨率
  1919. Value := 0
  1920. else if Value > Caps.wPeriodMax then //大于最小分辨率
  1921. Value := Caps.wPeriodMax;
  1922. FInterval := Value;
  1923. UpdateTimer;
  1924. end;
  1925. end;
  1926. procedure TDefineTimer.Timer;
  1927. begin
  1928. if Assigned(FOnTimer) then FOnTimer(self);
  1929. end;
  1930. procedure TDefineTimer.UpdateTimer;
  1931. var lpProc: TFNTimeCallBack;
  1932. begin
  1933. if uTimerID <> 0 then timeKillEvent(uTimerID); //销毁
  1934. if (FInterval > 0) and FEnabled and Assigned(FOnTimer) then
  1935. begin
  1936. lpProc := TimerCallback;
  1937. uTimerID := TimeSetEvent(FInterval,FPeriod,lpProc,DWORD(Self),TIME_PERIODIC);
  1938. if uTimerID = 0 then begin
  1939. FEnabled := FALSE;
  1940. raise Exception.Create('Failed to create Timer!');
  1941. end;
  1942. end;
  1943. end;
  1944. { TDefineTaskbarIcon }
  1945. const WM_TASKICON = WM_USER;
  1946. constructor TDefineTaskbarIcon.Create(AOwner: TComponent);
  1947. begin
  1948. inherited Create(AOwner);
  1949. FHandle := AllocateHWnd(PrivateWndProc);
  1950. FWMTaskBarCreated := RegisterWindowMessage('TaskbarCreated');
  1951. Application.HookMainWindow(AppHook);
  1952. FIcon := TIcon.Create;
  1953. FHintMode := thDefault;
  1954. FIconMode := thDefault;
  1955. end;
  1956. destructor TDefineTaskbarIcon.Destroy;
  1957. begin
  1958. if FActive then SetActive(False);
  1959. Application.UnhookMainWindow(AppHook);
  1960. FIcon.Free;
  1961. if FHandle <> 0 then DeallocateHwnd(FHandle);
  1962. inherited Destroy;
  1963. end;
  1964. procedure TDefineTaskbarIcon.PrivateWndProc(var Message: TMessage);
  1965. begin
  1966. WndProc(Message);
  1967. end;
  1968. procedure TDefineTaskbarIcon.Notification(AComponent: TComponent;
  1969. Operation: TOperation);
  1970. begin
  1971. inherited Notification(AComponent, Operation);
  1972. if (Operation = opRemove) and (AComponent = PopupMenu) then
  1973. PopupMenu := nil;
  1974. end;
  1975. procedure TDefineTaskbarIcon.SetActive(Value: Boolean);
  1976. begin
  1977. if Value <> FActive then
  1978. begin
  1979. FActive := Value;
  1980. if Value then
  1981. AddIcon
  1982. else
  1983. DeleteIcon;
  1984. end;
  1985. end;
  1986. procedure TDefineTaskbarIcon.SetHint(Value: string);
  1987. begin
  1988. FHint := Value;
  1989. ModifyIcon(NIF_TIP);
  1990. end;
  1991. procedure TDefineTaskbarIcon.SetIcon(Value: TIcon);
  1992. begin
  1993. FIcon.Assign(Value);
  1994. ModifyIcon(NIF_ICON);
  1995. end;
  1996. function TDefineTaskbarIcon.DoIcon(Action: DWORD; Aspect: UINT): Boolean;
  1997. var
  1998. Data: TNotifyIconData;
  1999. begin
  2000. with Data do
  2001. begin
  2002. cbSize := SizeOf(Data);
  2003. wnd := FHandle;
  2004. uID := 0;
  2005. uFlags := Aspect or NIF_MESSAGE;
  2006. uCallbackMessage := WM_TASKICON;
  2007. if Aspect and NIF_ICON <> 0 then
  2008. case FIconMode of
  2009. thCustom:
  2010. if FIcon.Handle <> 0 then
  2011. hIcon := FIcon.Handle
  2012. else
  2013. hIcon := LoadIcon(0, IDI_WINLOGO);
  2014. thDefault:
  2015. hIcon := Application.Icon.Handle;
  2016. end;
  2017. if Aspect and NIF_TIP <> 0 then
  2018. Case FHintMode of
  2019. thDefault: StrPLCopy(szTip, PChar(Application.Title), SizeOf(szTip));
  2020. thCustom : StrLCopy(szTip, PChar(FHint), SizeOf(szTip));
  2021. end;
  2022. end;
  2023. if not (csDesigning in ComponentState) then begin
  2024. Result := Shell_NotifyIcon(Action, @Data);
  2025. end else
  2026. Result := False;
  2027. end;
  2028. function TDefineTaskbarIcon.AddIcon: Boolean;
  2029. begin
  2030. Result := DoIcon(NIM_ADD, NIF_TIP or NIF_ICON);
  2031. end;
  2032. function TDefineTaskbarIcon.ModifyIcon(Aspect: Integer): Boolean;
  2033. begin
  2034. if FActive then
  2035. Result := DoIcon(NIM_MODIFY, Aspect)
  2036. else
  2037. Result := False;
  2038. end;
  2039. function TDefineTaskbarIcon.DeleteIcon: Boolean;
  2040. begin
  2041. Result := DoIcon(NIM_DELETE, 0);
  2042. end;
  2043. procedure TDefineTaskbarIcon.WndProc(var Message: TMessage);
  2044. var Pt: TPoint;
  2045. begin
  2046. with Message do
  2047. begin
  2048. if Msg = WM_TASKICON then
  2049. case LParam of
  2050. WM_LBUTTONUP:
  2051. if Assigned(FOnClick) then FOnClick(Self);
  2052. WM_LBUTTONDBLCLK:
  2053. if Assigned(FOnDblClick) then FOnDblClick(Self);
  2054. WM_RBUTTONUP:
  2055. if Assigned(FOnRightClick) then
  2056. FOnRightClick(Self)
  2057. else if Assigned(FPopupMenu) then begin
  2058. SetForegroundWindow(FHandle);
  2059. GetCursorPos(Pt);
  2060. FPopupMenu.Popup(Pt.X, Pt.Y);
  2061. PostMessage(FHandle, WM_USER, 0, 0);
  2062. end;
  2063. WM_MOUSEMOVE:
  2064. if Assigned(FOnMouseMove) then FOnMouseMove(Self);
  2065. end
  2066. else
  2067. Result := DefWindowProc(Handle, Msg, WParam, LParam);
  2068. end;
  2069. end;
  2070. function TDefineTaskbarIcon.AppHook(var Message: TMessage): Boolean;
  2071. begin
  2072. Result := Message.Msg = FWMTaskbarCreated;
  2073. if Result then AddIcon;
  2074. end;
  2075. procedure TDefineTaskbarIcon.SetPopupMenu(const Value: TPopupMenu);
  2076. begin
  2077. FPopupMenu := Value;
  2078. if Value <> nil then Value.FreeNotification(Self);
  2079. end;
  2080. procedure TDefineTaskbarIcon.SetHintMode(const Value: TDefineTaskbarMode);
  2081. begin
  2082. if FHintMode <> Value then
  2083. begin
  2084. FHintMode := Value;
  2085. ModifyIcon(NIF_TIP);
  2086. end;
  2087. end;
  2088. procedure TDefineTaskbarIcon.SetIconMode(const Value: TDefineTaskbarMode);
  2089. begin
  2090. if FIconMode <> Value then
  2091. begin
  2092. FIconMode := Value;
  2093. ModifyIcon(NIF_ICON);
  2094. end;
  2095. end;
  2096. procedure TDefineTaskbarIcon.Loaded;
  2097. begin
  2098. inherited Loaded;
  2099. if FActive then AddIcon;
  2100. end;
  2101. { TDefineAnimation }
  2102. constructor TDefineAnimation.Create(AOwner: TComponent);
  2103. begin
  2104. inherited Create(AOwner);
  2105. FAnimation := TBitmap.Create;
  2106. ControlStyle := ControlStyle + [csOpaque];
  2107. SetBounds(0, 0, 60, 60);
  2108. FTransColor := clFuchsia;
  2109. FBorderColor := DefaultBorderColor;
  2110. FBorder := false;
  2111. FTransparent := false;
  2112. FActive := False;
  2113. FLoop := True;
  2114. FInterval := 100; // 1 Second
  2115. FFrameWidth := 30;
  2116. FFrames := 1;
  2117. FFrame := 0;
  2118. end;
  2119. destructor TDefineAnimation.Destroy;
  2120. begin
  2121. FAnimation.Free;
  2122. inherited Destroy;
  2123. end;
  2124. procedure TDefineAnimation.Paint;
  2125. var
  2126. X, Y, Pos, W, H: Integer;
  2127. SrcRect, DestRect: TRect;
  2128. memGlyph: TBitmap;
  2129. begin
  2130. W := FAnimation.Width div FFrames;
  2131. H := FAnimation.Height div FFrames;
  2132. case FAnimationLayout of
  2133. alAcross:
  2134. begin
  2135. X := (Width - W) div 2;
  2136. Y := (Height - FAnimation.Height) div 2;
  2137. Pos := W * FFrame;
  2138. DestRect := Rect(X, Y, X + W, Y + FAnimation.Height);
  2139. SrcRect := Rect(Pos, 0, Pos + W, FAnimation.Height);
  2140. end;
  2141. alDown:
  2142. begin
  2143. X := (Width - FFrameWidth) div 2;
  2144. Y := (Height - H) div 2;
  2145. Pos := H * FFrame;
  2146. DestRect := Rect(X, Y, X + FFrameWidth, Y + H);
  2147. SrcRect := Rect(0, Pos, FFrameWidth, Pos + FFrameWidth);
  2148. end;
  2149. end;
  2150. memGlyph := TBitmap.Create;
  2151. try
  2152. memGlyph.Height := Height;
  2153. memGlyph.Width := Width;
  2154. with memGlyph.Canvas do
  2155. begin
  2156. Brush.Style := bsClear;
  2157. Brush.Color := Color;
  2158. FillRect(ClipRect);
  2159. if FTransparent then begin
  2160. DrawParentImage(self, memGlyph.Canvas);
  2161. Brush.Style := bsClear;
  2162. Brush.Color := FTransColor;
  2163. BrushCopy(DestRect, FAnimation, SrcRect, FTransColor);
  2164. end else begin
  2165. CopyRect(DestRect, FAnimation.Canvas, SrcRect);
  2166. end;
  2167. if (csDesigning in ComponentState) and (not FBorder) then
  2168. begin
  2169. Pen.Style := psDot;
  2170. Pen.Color := clBlack;
  2171. Brush.Style := bsClear;
  2172. Rectangle(ClipRect);
  2173. end else if FBorder then begin
  2174. DrawButtonBorder(memGlyph.Canvas, ClipRect, FBorderColor, 1);
  2175. end;
  2176. end;
  2177. Canvas.CopyRect(ClientRect, memGlyph.Canvas, ClientRect);
  2178. finally
  2179. memGlyph.Free;
  2180. end;
  2181. end;
  2182. procedure TDefineAnimation.SetAnimation(Value: TBitmap);
  2183. begin
  2184. if Value <> FAnimation then
  2185. begin
  2186. FAnimation.Assign(Value);
  2187. if not FAnimation.Empty then
  2188. begin
  2189. if FAnimation.Width > FAnimation.Height then
  2190. FAnimationLayout := alAcross
  2191. else
  2192. FAnimationLayout := alDown;
  2193. case FAnimationLayout of
  2194. alAcross:
  2195. if FAnimation.Width mod FAnimation.Height = 0 then
  2196. FFrames := FAnimation.Width div FAnimation.Height;
  2197. alDown:
  2198. if FAnimation.Height mod FAnimation.Width = 0 then
  2199. FFrames := FAnimation.Height div FAnimation.Width;
  2200. end;
  2201. FFrame := 1;
  2202. case FAnimationLayout of
  2203. alAcross:
  2204. FFrameWidth := FAnimation.Width div FFrames;
  2205. alDown:
  2206. FFrameWidth := FAnimation.Height div FFrames;
  2207. end;
  2208. FTransColor := FAnimation.Canvas.Pixels[0, FAnimation.Height - 1];
  2209. end;
  2210. Invalidate;
  2211. end;
  2212. end;
  2213. procedure TDefineAnimation.SetFrames(Value: Integer);
  2214. begin
  2215. if Value <> FFrames then
  2216. begin
  2217. FFrames := Value;
  2218. Invalidate;
  2219. end;
  2220. end;
  2221. procedure TDefineAnimation.SetFrameWidth(Value: Integer);
  2222. begin
  2223. if Value <> FFrameWidth then
  2224. begin
  2225. FFrameWidth := Value;
  2226. Invalidate;
  2227. end;
  2228. end;
  2229. procedure TDefineAnimation.SetFrame(Value: Integer);
  2230. var
  2231. Temp: Integer;
  2232. begin
  2233. if Value < 0 then
  2234. Temp := FFrames - 1
  2235. else
  2236. Temp := Value mod FFrames;
  2237. if Temp <> FFrame then
  2238. begin
  2239. FFrame := Temp;
  2240. if Assigned(FFrameChange) then
  2241. begin
  2242. FFrameChange(Self,FFrame);
  2243. end;
  2244. Invalidate;
  2245. end;
  2246. end;
  2247. procedure TDefineAnimation.SetActive(Value: Boolean);
  2248. begin
  2249. if Value <> FActive then
  2250. begin
  2251. FActive := Value;
  2252. if not Value then
  2253. begin
  2254. FTimer.Free;
  2255. FTimer := nil;
  2256. end
  2257. else
  2258. if FInterval > 0 then
  2259. begin
  2260. FTimer := TTimer.Create(Self);
  2261. FTimer.Interval := FInterval;
  2262. FTimer.OnTimer := DoTimer;
  2263. end;
  2264. end;
  2265. end;
  2266. procedure TDefineAnimation.SetTransparent(Value: Boolean);
  2267. begin
  2268. if Value <> FTransparent then
  2269. begin
  2270. FTransparent := Value;
  2271. Invalidate;
  2272. end;
  2273. end;
  2274. procedure TDefineAnimation.SetLoop(Value: Boolean);
  2275. begin
  2276. if Value <> FLoop then
  2277. begin
  2278. FLoop := Value;
  2279. Invalidate;
  2280. end;
  2281. end;
  2282. procedure TDefineAnimation.SetReverse(Value: Boolean);
  2283. begin
  2284. if Value <> FReverse then
  2285. begin
  2286. FReverse := Value;
  2287. Invalidate;
  2288. end;
  2289. end;
  2290. procedure TDefineAnimation.SetInterval(Value: Integer);
  2291. begin
  2292. if Value <> FInterval then
  2293. begin
  2294. FInterval := Value;
  2295. if FActive then
  2296. FTimer.Interval := Value;
  2297. Invalidate;
  2298. end;
  2299. end;
  2300. procedure TDefineAnimation.SetBorder(Value: Boolean);
  2301. begin
  2302. if Value <> FBorder then
  2303. begin
  2304. FBorder := Value;
  2305. Invalidate;
  2306. end;
  2307. end;
  2308. procedure TDefineAnimation.SetColors (Index: Integer; Value: TColor);
  2309. begin
  2310. case Index of
  2311. 0: FTransColor := Value;
  2312. 1: FBorderColor := Value;
  2313. end;
  2314. Invalidate;
  2315. end;
  2316. procedure TDefineAnimation.CMSysColorChange (var Message: TMessage);
  2317. begin
  2318. inherited;
  2319. if (ParentColor)and(Parent<>nil) then
  2320. begin
  2321. ParentColor := True;
  2322. Color := TForm(Parent).Color;
  2323. end;
  2324. Invalidate;
  2325. end;
  2326. procedure TDefineAnimation.CMParentColorChanged (var Message: TWMNoParams);
  2327. begin
  2328. inherited;
  2329. if (ParentColor)and(Parent<>nil) then
  2330. begin
  2331. ParentColor := True;
  2332. Color := TForm(Parent).Color;
  2333. end;
  2334. Invalidate;
  2335. end;
  2336. procedure TDefineAnimation.WMSize (var Message: TWMSize);
  2337. begin
  2338. inherited;
  2339. Invalidate;
  2340. end;
  2341. procedure TDefineAnimation.DoTimer(Sender: TObject);
  2342. procedure ChkStop;
  2343. begin
  2344. if not FLoop then
  2345. begin
  2346. FActive := False;
  2347. FTimer.Free;
  2348. FTimer := nil;
  2349. end;
  2350. end;
  2351. begin
  2352. if FReverse then
  2353. begin
  2354. Frame := Frame - 1;
  2355. if FFrame = 0 then ChkStop;
  2356. end
  2357. else
  2358. begin
  2359. Frame := Frame + 1;
  2360. if FFrame = Frames - 1 then ChkStop;
  2361. end;
  2362. end;
  2363. procedure TDefineAnimation.SetAnimationLayout(const Value: TAnimationLayout);
  2364. begin
  2365. FAnimationLayout := Value;
  2366. Invalidate;
  2367. end;
  2368. { TDefineHint }
  2369. constructor TDefineHint.Create (AOwner: TComponent);
  2370. begin
  2371. inherited Create(AOwner);
  2372. FHintFont := TFont.Create;
  2373. if not (csDesigning in ComponentState) then
  2374. begin
  2375. HintWindowClass := TDefineHintWindow;
  2376. with Application do
  2377. begin
  2378. ShowHint := not ShowHint;
  2379. ShowHint := not ShowHint;
  2380. OnShowHint := GetHintInfo;
  2381. HintShortPause := 25;
  2382. HintPause := 500;
  2383. HintHidePause := 5000;
  2384. end;
  2385. end;
  2386. FBackgroundColor := clWhite;
  2387. FBorderColor := clBlack;
  2388. FArrowBackgroundColor := $0053D2FF;
  2389. FArrowColor := clBlack;
  2390. FHintWidth := 200;
  2391. end;
  2392. destructor TDefineHint.Destroy;
  2393. begin
  2394. FHintFont.Free;
  2395. inherited Destroy;
  2396. end;
  2397. procedure TDefineHint.SetColors (Index: Integer; Value: TColor);
  2398. begin
  2399. case Index of
  2400. 0: FBackgroundColor := Value;
  2401. 1: FBorderColor := Value;
  2402. 2: FArrowBackgroundColor := Value;
  2403. 3: FArrowColor := Value;
  2404. end;
  2405. end;
  2406. procedure TDefineHint.SetHintFont (Value: TFont);
  2407. begin
  2408. FHintFont.Assign(Value);
  2409. end;
  2410. procedure TDefineHint.GetHintInfo (var HintStr: string; var CanShow: Boolean; var HintInfo: THintInfo);
  2411. begin
  2412. if Assigned(FOnShowHint) then
  2413. FOnShowHint(HintStr, CanShow, HintInfo);
  2414. end;
  2415. { TDefineHintWindow }
  2416. function TDefineHintWindow.FindFlatHint: TDefineHint;
  2417. var
  2418. curInx: Integer;
  2419. begin
  2420. Result := nil;
  2421. with Application.MainForm do
  2422. for curInx := 0 to ComponentCount - 1 do
  2423. if Components[curInx] is TDefineHint then
  2424. begin
  2425. Result := TDefineHint(Components[curInx]);
  2426. Break;
  2427. end;
  2428. end;
  2429. procedure TDefineHintWindow.CreateParams (var Params: TCreateParams);
  2430. begin
  2431. inherited CreateParams(Params);
  2432. Params.Style := Params.Style - WS_BORDER;
  2433. end;
  2434. procedure TDefineHintWindow.Paint;
  2435. var
  2436. ArrowRect, TextRect: TRect;
  2437. begin
  2438. // Set the Rect's
  2439. case FArrowPos of
  2440. NW, SW:
  2441. begin
  2442. ArrowRect := Rect(ClientRect.Left + 1, ClientRect.Top + 1, ClientRect.Left + 15, ClientRect.Bottom - 1);
  2443. TextRect := Rect(ClientRect.Left + 15, ClientRect.Top + 1, ClientRect.Right - 1, ClientRect.Bottom - 1);
  2444. end;
  2445. NE, SE:
  2446. begin
  2447. ArrowRect := Rect(ClientRect.Right - 15, ClientRect.Top + 1, ClientRect.Right - 1, ClientRect.Bottom - 1);
  2448. TextRect := Rect(ClientRect.Left + 1, ClientRect.Top + 1, ClientRect.Right - 15, ClientRect.Bottom - 1);
  2449. end;
  2450. end;
  2451. // DrawBackground
  2452. canvas.brush.color := FHint.FArrowBackgroundColor;
  2453. canvas.FillRect(ArrowRect);
  2454. canvas.brush.color := FHint.FBackgroundColor;
  2455. canvas.FillRect(TextRect);
  2456. // DrawBorder
  2457. canvas.Brush.Color := FHint.FBorderColor;
  2458. canvas.FrameRect(ClientRect);
  2459. // DrawArrow
  2460. case FArrowPos of
  2461. NW: FArrowPoint := Point(ArrowRect.Left + 2, ArrowRect.Top + 2);
  2462. NE: FArrowPoint := Point(ArrowRect.Right - 3, ArrowRect.Top + 2);
  2463. SW: FArrowPoint := Point(ArrowRect.Left + 2, ArrowRect.Bottom - 3);
  2464. SE: FArrowPoint := Point(ArrowRect.Right - 3, ArrowRect.Bottom - 3);
  2465. end;
  2466. canvas.Pen.Color := FHint.FArrowColor;
  2467. case FArrowPos of
  2468. NW: canvas.Polyline([Point(FArrowPoint.x, FArrowPoint.y), Point(FArrowPoint.x, FArrowPoint.y + 6),
  2469. Point(FArrowPoint.x + 1, FArrowPoint.y + 6), Point(FArrowPoint.x + 1, FArrowPoint.y),
  2470. Point(FArrowPoint.x + 6, FArrowPoint.y), Point(FArrowPoint.x + 6, FArrowPoint.y + 1),
  2471. Point(FArrowPoint.x + 2, FArrowPoint.y + 1), Point(FArrowPoint.x + 2, FArrowPoint.y + 4),
  2472. Point(FArrowPoint.x + 5, FArrowPoint.y + 7), Point(FArrowPoint.x + 6, FArrowPoint.y + 7),
  2473. Point(FArrowPoint.x + 3, FArrowPoint.y + 4), Point(FArrowPoint.x + 3, FArrowPoint.y + 3),
  2474. Point(FArrowPoint.x + 6, FArrowPoint.y + 6), Point(FArrowPoint.x + 7, FArrowPoint.y + 6),
  2475. Point(FArrowPoint.x + 3, FArrowPoint.y + 2), Point(FArrowPoint.x + 4, FArrowPoint.y + 2),
  2476. Point(FArrowPoint.x + 7, FArrowPoint.y + 5), Point(FArrowPoint.x + 7, FArrowPoint.y + 6)]);
  2477. NE: canvas.Polyline([Point(FArrowPoint.x, FArrowPoint.y), Point(FArrowPoint.x, FArrowPoint.y + 6),
  2478. Point(FArrowPoint.x - 1, FArrowPoint.y + 6), Point(FArrowPoint.x - 1, FArrowPoint.y),
  2479. Point(FArrowPoint.x - 6, FArrowPoint.y), Point(FArrowPoint.x - 6, FArrowPoint.y + 1),
  2480. Point(FArrowPoint.x - 2, FArrowPoint.y + 1), Point(FArrowPoint.x - 2, FArrowPoint.y + 4),
  2481. Point(FArrowPoint.x - 5, FArrowPoint.y + 7), Point(FArrowPoint.x - 6, FArrowPoint.y + 7),
  2482. Point(FArrowPoint.x - 3, FArrowPoint.y + 4), Point(FArrowPoint.x - 3, FArrowPoint.y + 3),
  2483. Point(FArrowPoint.x - 6, FArrowPoint.y + 6), Point(FArrowPoint.x - 7, FArrowPoint.y + 6),
  2484. Point(FArrowPoint.x - 3, FArrowPoint.y + 2), Point(FArrowPoint.x - 4, FArrowPoint.y + 2),
  2485. Point(FArrowPoint.x - 7, FArrowPoint.y + 5), Point(FArrowPoint.x - 7, FArrowPoint.y + 6)]);
  2486. SW: canvas.Polyline([Point(FArrowPoint.x, FArrowPoint.y), Point(FArrowPoint.x, FArrowPoint.y - 6),
  2487. Point(FArrowPoint.x + 1, FArrowPoint.y - 6), Point(FArrowPoint.x + 1, FArrowPoint.y),
  2488. Point(FArrowPoint.x + 6, FArrowPoint.y), Point(FArrowPoint.x + 6, FArrowPoint.y - 1),
  2489. Point(FArrowPoint.x + 2, FArrowPoint.y - 1), Point(FArrowPoint.x + 2, FArrowPoint.y - 4),
  2490. Point(FArrowPoint.x + 5, FArrowPoint.y - 7), Point(FArrowPoint.x + 6, FArrowPoint.y - 7),
  2491. Point(FArrowPoint.x + 3, FArrowPoint.y - 4), Point(FArrowPoint.x + 3, FArrowPoint.y - 3),
  2492. Point(FArrowPoint.x + 6, FArrowPoint.y - 6), Point(FArrowPoint.x + 7, FArrowPoint.y - 6),
  2493. Point(FArrowPoint.x + 3, FArrowPoint.y - 2), Point(FArrowPoint.x + 4, FArrowPoint.y - 2),
  2494. Point(FArrowPoint.x + 7, FArrowPoint.y - 5), Point(FArrowPoint.x + 7, FArrowPoint.y - 6)]);
  2495. SE: canvas.Polyline([Point(FArrowPoint.x, FArrowPoint.y), Point(FArrowPoint.x, FArrowPoint.y - 6),
  2496. Point(FArrowPoint.x - 1, FArrowPoint.y - 6), Point(FArrowPoint.x - 1, FArrowPoint.y),
  2497. Point(FArrowPoint.x - 6, FArrowPoint.y), Point(FArrowPoint.x - 6, FArrowPoint.y - 1),
  2498. Point(FArrowPoint.x - 2, FArrowPoint.y - 1), Point(FArrowPoint.x - 2, FArrowPoint.y - 4),
  2499. Point(FArrowPoint.x - 5, FArrowPoint.y - 7), Point(FArrowPoint.x - 6, FArrowPoint.y - 7),
  2500. Point(FArrowPoint.x - 3, FArrowPoint.y - 4), Point(FArrowPoint.x - 3, FArrowPoint.y - 3),
  2501. Point(FArrowPoint.x - 6, FArrowPoint.y - 6), Point(FArrowPoint.x - 7, FArrowPoint.y - 6),
  2502. Point(FArrowPoint.x - 3, FArrowPoint.y - 2), Point(FArrowPoint.x - 4, FArrowPoint.y - 2),
  2503. Point(FArrowPoint.x - 7, FArrowPoint.y - 5), Point(FArrowPoint.x - 7, FArrowPoint.y - 6)]);
  2504. end;
  2505. // DrawHintText
  2506. canvas.brush.Style := bsClear;
  2507. InflateRect(TextRect, -3, -1);
  2508. {$IFDEF DFS_COMPILER_4_UP}
  2509. if BidiMode = bdRightToLeft then
  2510. DrawText(canvas.handle, PChar(Caption), Length(Caption), TextRect, DT_RIGHT or DT_WORDBREAK or DT_NOPREFIX)
  2511. else
  2512. DrawText(canvas.handle, PChar(Caption), Length(Caption), TextRect, DT_WORDBREAK or DT_NOPREFIX);
  2513. {$ELSE}
  2514. DrawText(canvas.handle, PChar(Caption), Length(Caption), TextRect, DT_WORDBREAK or DT_NOPREFIX);
  2515. {$ENDIF}
  2516. end;
  2517. procedure TDefineHintWindow.ActivateHint (HintRect: TRect; const AHint: string);
  2518. var
  2519. curWidth: Byte;
  2520. Pnt: TPoint;
  2521. HintHeight, HintWidth: Integer;
  2522. NordWest, NordEast, SouthWest, SouthEast: TRect;
  2523. begin
  2524. Caption := AHint;
  2525. FHint := FindFlatHint;
  2526. if FHint <> nil then
  2527. Canvas.Font.Assign(FHint.Font);
  2528. // Calculate width and height
  2529. HintRect.Right := HintRect.Left + FHint.MaxWidth - 22;
  2530. {$IFDEF DFS_COMPILER_4_UP}
  2531. if BidiMode = bdRightToLeft then
  2532. DrawText(Canvas.Handle, @AHint[1], Length(AHint), HintRect, DT_RIGHT or DT_CALCRECT or DT_WORDBREAK or DT_NOPREFIX)
  2533. else
  2534. DrawText(Canvas.Handle, @AHint[1], Length(AHint), HintRect, DT_CALCRECT or DT_WORDBREAK or DT_NOPREFIX);
  2535. {$ELSE}
  2536. DrawText(Canvas.Handle, @AHint[1], Length(AHint), HintRect, DT_CALCRECT or DT_WORDBREAK or DT_NOPREFIX);
  2537. {$ENDIF}
  2538. DrawText(Canvas.Handle, @AHint[1], Length(AHint), HintRect, DT_CALCRECT or DT_WORDBREAK or DT_NOPREFIX);
  2539. Inc(HintRect.Right, 22);
  2540. Inc(HintRect.Bottom, 6);
  2541. // Divide the screen in 4 pices
  2542. NordWest := Rect(0, 0, Screen.Width div 2, Screen.Height div 2);
  2543. NordEast := Rect(Screen.Width div 2, 0, Screen.Width, Screen.Height div 2);
  2544. SouthWest := Rect(0, Screen.Height div 2, Screen.Width div 2, Screen.Height);
  2545. SouthEast := Rect(Screen.Width div 2, Screen.Height div 2, Screen.Width, Screen.Height);
  2546. GetCursorPos(Pnt);
  2547. if PtInRect(NordWest, Pnt) then
  2548. FArrowPos := NW
  2549. else
  2550. if PtInRect(NordEast, Pnt) then
  2551. FArrowPos := NE
  2552. else
  2553. if PtInRect(SouthWest, Pnt) then
  2554. FArrowPos := SW
  2555. else
  2556. FArrowPos := SE;
  2557. // Calculate the position of the hint
  2558. if FArrowPos = NW then
  2559. curWidth := 12
  2560. else
  2561. curWidth := 5;
  2562. HintHeight := HintRect.Bottom - HintRect.Top;
  2563. HintWidth := HintRect.Right - HintRect.Left;
  2564. case FArrowPos of
  2565. NW: HintRect := Rect(Pnt.x + curWidth, Pnt.y + curWidth, Pnt.x + HintWidth + curWidth, Pnt.y + HintHeight + curWidth);
  2566. NE: HintRect := Rect(Pnt.x - HintWidth - curWidth, Pnt.y + curWidth, Pnt.x - curWidth, Pnt.y + HintHeight + curWidth);
  2567. SW: HintRect := Rect(Pnt.x + curWidth, Pnt.y - HintHeight - curWidth, Pnt.x + HintWidth + curWidth, Pnt.y - curWidth);
  2568. SE: HintRect := Rect(Pnt.x - HintWidth - curWidth, Pnt.y - HintHeight - curWidth, Pnt.x - curWidth, Pnt.y - curWidth);
  2569. end;
  2570. BoundsRect := HintRect;
  2571. Pnt := ClientToScreen(Point(0, 0));
  2572. SetWindowPos(Handle, HWND_TOPMOST, Pnt.X, Pnt.Y, 0, 0, SWP_SHOWWINDOW or SWP_NOACTIVATE or SWP_NOSIZE);
  2573. end;
  2574. { TBaseWater }
  2575. constructor TBaseWater.Create(AOwner: TComponent);
  2576. begin
  2577. inherited Create(AOwner);
  2578. FEnabled := True;
  2579. FInterval := 50;
  2580. {$IFDEF MSWINDOWS}
  2581. FHandle := Classes.AllocateHWnd(WndProc);
  2582. {$ENDIF}
  2583. {$IFDEF LINUX}
  2584. FHandle := WinUtils.AllocateHWnd(WndProc);
  2585. {$ENDIF}
  2586. end;
  2587. destructor TBaseWater.Destroy;
  2588. begin
  2589. FEnabled := False;
  2590. UpdateTimer;
  2591. {$IFDEF MSWINDOWS}
  2592. Classes.DeallocateHWnd(FHandle);
  2593. {$ENDIF}
  2594. {$IFDEF LINUX}
  2595. WinUtils.DeallocateHWnd(FHandle);
  2596. {$ENDIF}
  2597. inherited Destroy;
  2598. end;
  2599. procedure TBaseWater.WndProc(var Msg: TMessage);
  2600. begin
  2601. with Msg do
  2602. if Msg = WM_TIMER then
  2603. try
  2604. Timer;
  2605. except
  2606. Application.HandleException(Self);
  2607. end
  2608. else
  2609. Result := DefWindowProc(FHandle, Msg, wParam, lParam);
  2610. end;
  2611. procedure TBaseWater.UpdateTimer;
  2612. begin
  2613. KillTimer(FHandle, 1);
  2614. if (FInterval <> 0) and FEnabled and Assigned(FOnTimer) then
  2615. if SetTimer(FHandle, 1, FInterval, nil) = 0 then
  2616. raise EOutOfResources.Create(SNoTimers);
  2617. end;
  2618. procedure TBaseWater.SetEnabled(Value: Boolean);
  2619. begin
  2620. if Value <> FEnabled then
  2621. begin
  2622. FEnabled := Value;
  2623. UpdateTimer;
  2624. end;
  2625. end;
  2626. procedure TBaseWater.SetInterval(Value: Cardinal);
  2627. begin
  2628. if Value <> FInterval then
  2629. begin
  2630. FInterval := Value;
  2631. UpdateTimer;
  2632. end;
  2633. end;
  2634. procedure TBaseWater.SetOnTimer(Value: TNotifyEvent);
  2635. begin
  2636. FOnTimer := Value;
  2637. UpdateTimer;
  2638. end;
  2639. procedure TBaseWater.Timer;
  2640. begin
  2641. if Assigned(FOnTimer) then FOnTimer(Self);
  2642. end;
  2643. { TDefineWater }
  2644. const
  2645. RAND_MAX = $7FFF;
  2646. constructor TDefineWater.Create(AOwner: TComponent);
  2647. begin
  2648. FBitmap := TBitmap.Create;
  2649. FWater := TDefineWatet.Create;
  2650. FItems := TStringList.Create;
  2651. inherited Create(AOwner);
  2652. FDamping := csDefDamping;
  2653. FPlayState := true;
  2654. OnTimer := Play;
  2655. end;
  2656. destructor TDefineWater.Destroy;
  2657. begin
  2658. FItems.Free;
  2659. FWater.Free;
  2660. FBitmap.Free;
  2661. inherited Destroy;
  2662. end;
  2663. procedure TDefineWater.OnMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  2664. begin
  2665. FWater.Blob(x,y,1,5000);
  2666. end;
  2667. procedure TDefineWater.SetDamping(Value: TWaterDamping);
  2668. begin
  2669. if (Value >= Low(TWaterDamping)) and (Value <= High(TWaterDamping)) then
  2670. begin
  2671. FDamping := Value;
  2672. FWater.Damping := Value;
  2673. end;
  2674. end;
  2675. procedure TDefineWater.InitiateWater;
  2676. var inx:Integer;
  2677. TitleValue:String;
  2678. begin
  2679. FBitmap.Assign(FImage.Bitmap);
  2680. FImage.OnMouseMove := OnMouseMove;
  2681. with FImage do
  2682. begin
  2683. Bitmap.FreeImage;
  2684. Bitmap.Width := FBitmap.Width;
  2685. Bitmap.Height := FBitmap.Height;
  2686. end;
  2687. FWater.SetSize(FBitmap);
  2688. FState := FBitmap.Height;
  2689. FPlayState := false;
  2690. FMoveHeight := 10;
  2691. for inx := 0 to FItems.Count - 1 do
  2692. begin
  2693. TitleValue := FItems.Strings[inx];
  2694. GetTitleParam(FParam, TitleValue);
  2695. with FImage.Canvas do
  2696. begin
  2697. Font.Name := FParam.Name;
  2698. Font.Size := FParam.Size;
  2699. Font.Style := FParam.Style;
  2700. Font.Pitch := FParam.Pitch;
  2701. FMoveHeight := FMoveHeight + TextHeight(TitleValue)+FParam.Row;
  2702. end;
  2703. end;
  2704. if FMoveHeight < FImage.Height then
  2705. FMoveHeight := FImage.Height+10;
  2706. end;
  2707. procedure TDefineWater.Play;
  2708. var
  2709. TitleValue:String;
  2710. Inx,Cur: Integer;
  2711. begin
  2712. if (FImage <> nil)and(not(csDesigning in ComponentState)) then
  2713. begin
  2714. if FPlayState then
  2715. InitiateWater;
  2716. if Random(8)= 1 then
  2717. FWater.Blob(-1,-1,Random(1)+1,Random(500)+50);
  2718. FWater.Render(Bitmap,FImage.Bitmap);
  2719. FState:=FState-1;
  2720. if FState<-FMoveHeight then
  2721. FState:=FImage.height+10;
  2722. with FImage.Canvas do
  2723. begin
  2724. Brush.Style:=bsClear;
  2725. Cur := FState;
  2726. for inx:=0 to FItems.Count - 1 do
  2727. begin
  2728. TitleValue := FItems.Strings[inx];
  2729. GetTitleParam(FParam, TitleValue);
  2730. Font.Name := FParam.Name;
  2731. Font.Size := FParam.Size;
  2732. Font.Style := FParam.Style;
  2733. Font.Pitch := FParam.Pitch;
  2734. if FParam.Draw3D then
  2735. begin
  2736. Font.Color := 0;
  2737. case FParam.Align of
  2738. wpLeft :TextOut(21,Cur,TitleValue);
  2739. wpCenter:TextOut((FImage.Width - TextWidth(TitleValue))div 2+1,Cur,TitleValue);
  2740. wpRight :TextOut((FImage.Width - TextWidth(TitleValue))-21,Cur,TitleValue);
  2741. end;
  2742. end;
  2743. Font.Color := FParam.Color;
  2744. case FParam.Align of
  2745. wpLeft :TextOut(20,Cur,TitleValue);
  2746. wpCenter:TextOut((FImage.Width - TextWidth(TitleValue))div 2,Cur,TitleValue);
  2747. wpRight :TextOut((FImage.Width - TextWidth(TitleValue))-20,Cur,TitleValue);
  2748. end;
  2749. Cur := Cur+TextHeight('H')+FParam.Row;
  2750. end;
  2751. if FItems.Count <= 0 then
  2752. begin
  2753. TextOut((FImage.Width - TextWidth(''))div 2,Cur,'');
  2754. end;
  2755. end;
  2756. end;
  2757. end;
  2758. procedure TDefineWater.SetItems(const Value: TStringList);
  2759. begin
  2760. FItems.Assign(Value);
  2761. end;
  2762. procedure TDefineWater.Notification(AComponent: TComponent; Operation: TOperation);
  2763. begin
  2764. inherited;
  2765. if (Operation = opRemove) and (AComponent <> nil) then
  2766. begin
  2767. if AComponent=FImage then CtrlImage:=nil;
  2768. end;
  2769. end;
  2770. { TDefineImage }
  2771. constructor TDefineImage.Create(AOwner: TComponent);
  2772. begin
  2773. inherited Create(AOwner);
  2774. ControlStyle := ControlStyle + [csReplicatable];
  2775. FEnterImage := TBitmap.Create;
  2776. FLeaveImage := TBitmap.Create;
  2777. FMouseState := True;
  2778. FAutoImage := False;
  2779. FAutoCursor := crHandPoint;
  2780. FAutoShowCursor := false;
  2781. FBitmap := TBitmap.Create;
  2782. FBitmap.OnChange := PictureChanged;
  2783. FBitmap.OnProgress := Progress;
  2784. Height := 105;
  2785. Width := 105;
  2786. end;
  2787. destructor TDefineImage.Destroy;
  2788. begin
  2789. FEnterImage.Free;
  2790. FLeaveImage.Free;
  2791. FBitmap.Free;
  2792. inherited Destroy;
  2793. end;
  2794. function TDefineImage.GetPalette: HPALETTE;
  2795. begin
  2796. Result := 0;
  2797. if FBitmap <> nil then
  2798. Result := FBitmap.Palette;
  2799. end;
  2800. function TDefineImage.DestRect: TRect;
  2801. var
  2802. w, h, cw, ch: Integer;
  2803. xyaspect: Double;
  2804. begin
  2805. w := Bitmap.Width;
  2806. h := Bitmap.Height;
  2807. cw := ClientWidth;
  2808. ch := ClientHeight;
  2809. if Stretch or (Proportional and ((w > cw) or (h > ch))) then
  2810. begin
  2811. if Proportional and (w > 0) and (h > 0) then
  2812. begin
  2813. xyaspect := w / h;
  2814. if w > h then
  2815. begin
  2816. w := cw;
  2817. h := Trunc(cw / xyaspect);
  2818. if h > ch then // woops, too big
  2819. begin
  2820. h := ch;
  2821. w := Trunc(ch * xyaspect);
  2822. end;
  2823. end
  2824. else
  2825. begin
  2826. h := ch;
  2827. w := Trunc(ch * xyaspect);
  2828. if w > cw then // woops, too big
  2829. begin
  2830. w := cw;
  2831. h := Trunc(cw / xyaspect);
  2832. end;
  2833. end;
  2834. end
  2835. else
  2836. begin
  2837. w := cw;
  2838. h := ch;
  2839. end;
  2840. end;
  2841. with Result do
  2842. begin
  2843. Left := 0;
  2844. Top := 0;
  2845. Right := w;
  2846. Bottom := h;
  2847. end;
  2848. if Center then
  2849. OffsetRect(Result, (cw - w) div 2, (ch - h) div 2);
  2850. end;
  2851. procedure TDefineImage.Paint;
  2852. var
  2853. Save: Boolean;
  2854. begin
  2855. if csDesigning in ComponentState then
  2856. with inherited Canvas do
  2857. begin
  2858. Pen.Style := psDash;
  2859. Brush.Style := bsClear;
  2860. Rectangle(0, 0, Width, Height);
  2861. end;
  2862. Save := FDrawing;
  2863. FDrawing := True;
  2864. try
  2865. with inherited Canvas do
  2866. begin
  2867. StretchDraw(DestRect, Bitmap);
  2868. end;
  2869. finally
  2870. FDrawing := Save;
  2871. end;
  2872. end;
  2873. function TDefineImage.DoPaletteChange: Boolean;
  2874. var
  2875. ParentForm: TCustomForm;
  2876. Tmp: TGraphic;
  2877. begin
  2878. Result := False;
  2879. Tmp := Bitmap;
  2880. if Visible and (not (csLoading in ComponentState)) and (Tmp <> nil) and
  2881. (Tmp.PaletteModified) then
  2882. begin
  2883. if (Tmp.Palette = 0) then
  2884. Tmp.PaletteModified := False
  2885. else
  2886. begin
  2887. ParentForm := GetParentForm(Self);
  2888. if Assigned(ParentForm) and ParentForm.Active and Parentform.HandleAllocated then
  2889. begin
  2890. if FDrawing then
  2891. ParentForm.Perform(wm_QueryNewPalette, 0, 0)
  2892. else
  2893. PostMessage(ParentForm.Handle, wm_QueryNewPalette, 0, 0);
  2894. Result := True;
  2895. Tmp.PaletteModified := False;
  2896. end;
  2897. end;
  2898. end;
  2899. end;
  2900. procedure TDefineImage.Progress(Sender: TObject; Stage: TProgressStage;
  2901. PercentDone: Byte; RedrawNow: Boolean; const R: TRect; const Msg: string);
  2902. begin
  2903. if FIncrementalDisplay and RedrawNow then
  2904. begin
  2905. if DoPaletteChange then Update
  2906. else Paint;
  2907. end;
  2908. if Assigned(FOnProgress) then FOnProgress(Sender, Stage, PercentDone, RedrawNow, R, Msg);
  2909. end;
  2910. function TDefineImage.GetCanvas: TCanvas;
  2911. var
  2912. fBit: TBitmap;
  2913. begin
  2914. if Bitmap = nil then
  2915. begin
  2916. fBit := TBitmap.Create;
  2917. try
  2918. fBit.Width := Width;
  2919. fBit.Height := Height;
  2920. fBit := Bitmap;
  2921. finally
  2922. fBit.Free;
  2923. end;
  2924. end;
  2925. if Bitmap is TBitmap then
  2926. Result := TBitmap(Bitmap).Canvas
  2927. else
  2928. raise EInvalidOperation.Create(SImageCanvasNeedsBitmap);
  2929. end;
  2930. procedure TDefineImage.SetCenter(Value: Boolean);
  2931. begin
  2932. if FCenter <> Value then
  2933. begin
  2934. FCenter := Value;
  2935. PictureChanged(Self);
  2936. end;
  2937. end;
  2938. procedure TDefineImage.SetPicture(Value: TBitmap);
  2939. begin
  2940. FBitmap.Assign(Value);
  2941. end;
  2942. procedure TDefineImage.SetStretch(Value: Boolean);
  2943. begin
  2944. if Value <> FStretch then
  2945. begin
  2946. FStretch := Value;
  2947. PictureChanged(Self);
  2948. end;
  2949. end;
  2950. procedure TDefineImage.SetTransparent(Value: Boolean);
  2951. begin
  2952. if Value <> FTransparent then
  2953. begin
  2954. FTransparent := Value;
  2955. PictureChanged(Self);
  2956. end;
  2957. end;
  2958. procedure TDefineImage.SetProportional(Value: Boolean);
  2959. begin
  2960. if FProportional <> Value then
  2961. begin
  2962. FProportional := Value;
  2963. PictureChanged(Self);
  2964. end;
  2965. end;
  2966. procedure TDefineImage.PictureChanged(Sender: TObject);
  2967. var
  2968. G: TGraphic;
  2969. D : TRect;
  2970. begin
  2971. if AutoSize and (Bitmap.Width > 0) and (Bitmap.Height > 0) then
  2972. SetBounds(Left, Top, Bitmap.Width, Bitmap.Height);
  2973. G := Bitmap;
  2974. if G <> nil then
  2975. begin
  2976. if not ((G is TMetaFile) or (G is TIcon)) then
  2977. G.Transparent := FTransparent;
  2978. D := DestRect;
  2979. if (not G.Transparent) and (D.Left <= 0) and (D.Top <= 0) and
  2980. (D.Right >= Width) and (D.Bottom >= Height) then
  2981. ControlStyle := ControlStyle + [csOpaque]
  2982. else // picture might not cover entire clientrect
  2983. ControlStyle := ControlStyle - [csOpaque];
  2984. if DoPaletteChange and FDrawing then Update;
  2985. end
  2986. else ControlStyle := ControlStyle - [csOpaque];
  2987. if not FDrawing then Invalidate;
  2988. end;
  2989. function TDefineImage.CanAutoSize(var NewWidth, NewHeight: Integer): Boolean;
  2990. begin
  2991. Result := True;
  2992. if not (csDesigning in ComponentState) or (Bitmap.Width > 0) and
  2993. (Bitmap.Height > 0) then
  2994. begin
  2995. if Align in [alNone, alLeft, alRight] then
  2996. NewWidth := Bitmap.Width;
  2997. if Align in [alNone, alTop, alBottom] then
  2998. NewHeight := Bitmap.Height;
  2999. end;
  3000. end;
  3001. procedure TDefineImage.MouseEnter(var Msg: TMessage);
  3002. begin
  3003. if not(csDesigning in ComponentState) then
  3004. begin
  3005. if FAutoImage and FMouseState Then
  3006. begin
  3007. Bitmap.Assign(FEnterImage);
  3008. FMouseState := False;
  3009. end;
  3010. If FAutoShowCursor Then
  3011. Cursor := FAutoCursor;
  3012. end;
  3013. end;
  3014. procedure TDefineImage.MouseLeave(var Msg: TMessage);
  3015. begin
  3016. if not(csDesigning in ComponentState) then
  3017. begin
  3018. if FAutoImage and not FMouseState Then
  3019. begin
  3020. Bitmap.Assign(FLeaveImage);
  3021. FMouseState := True;
  3022. end;
  3023. end;
  3024. end;
  3025. procedure TDefineImage.SetEnterImage(const Value: TBitmap);
  3026. begin
  3027. FEnterImage.Assign(Value);
  3028. end;
  3029. procedure TDefineImage.SetLeaveImage(const Value: TBitmap);
  3030. begin
  3031. FLeaveImage.Assign(Value);
  3032. end;
  3033. initialization
  3034. OwnerList := TList.Create;
  3035. finalization
  3036. OwnerList.Free;
  3037. end.