NativeMethods.cs 121 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474
  1. /*************************************************************************************
  2. Extended WPF Toolkit
  3. Copyright (C) 2007-2013 Xceed Software Inc.
  4. This program is provided to you under the terms of the Microsoft Public
  5. License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license
  6. For more features, controls, and fast professional support,
  7. pick up the Plus Edition at http://xceed.com/wpf_toolkit
  8. Stay informed: follow @datagrid on Twitter or Like http://facebook.com/datagrids
  9. ***********************************************************************************/
  10. /**************************************************************************\
  11. Copyright Microsoft Corporation. All Rights Reserved.
  12. \**************************************************************************/
  13. namespace Standard
  14. {
  15. using System;
  16. using System.ComponentModel;
  17. using System.Diagnostics.CodeAnalysis;
  18. using System.IO;
  19. using System.Runtime.ConstrainedExecution;
  20. using System.Runtime.InteropServices;
  21. using System.Runtime.InteropServices.ComTypes;
  22. using System.Security.Permissions;
  23. using System.Text;
  24. using Microsoft.Win32.SafeHandles;
  25. // Some COM interfaces and Win32 structures are already declared in the framework.
  26. // Interesting ones to remember in System.Runtime.InteropServices.ComTypes are:
  27. using FILETIME = System.Runtime.InteropServices.ComTypes.FILETIME;
  28. using IPersistFile = System.Runtime.InteropServices.ComTypes.IPersistFile;
  29. using IStream = System.Runtime.InteropServices.ComTypes.IStream;
  30. #region Native Values
  31. internal static class Win32Value
  32. {
  33. public const uint MAX_PATH = 260;
  34. public const uint INFOTIPSIZE = 1024;
  35. public const uint TRUE = 1;
  36. public const uint FALSE = 0;
  37. public const uint sizeof_WCHAR = 2;
  38. public const uint sizeof_CHAR = 1;
  39. public const uint sizeof_BOOL = 4;
  40. }
  41. /// <summary>
  42. /// HIGHCONTRAST flags
  43. /// </summary>
  44. [Flags]
  45. internal enum HCF
  46. {
  47. HIGHCONTRASTON = 0x00000001,
  48. AVAILABLE = 0x00000002,
  49. HOTKEYACTIVE = 0x00000004,
  50. CONFIRMHOTKEY = 0x00000008,
  51. HOTKEYSOUND = 0x00000010,
  52. INDICATOR = 0x00000020,
  53. HOTKEYAVAILABLE = 0x00000040,
  54. }
  55. /// <summary>
  56. /// BITMAPINFOHEADER Compression type. BI_*.
  57. /// </summary>
  58. internal enum BI
  59. {
  60. RGB = 0,
  61. }
  62. /// <summary>
  63. /// CombingRgn flags. RGN_*
  64. /// </summary>
  65. internal enum RGN
  66. {
  67. /// <summary>
  68. /// Creates the intersection of the two combined regions.
  69. /// </summary>
  70. AND = 1,
  71. /// <summary>
  72. /// Creates the union of two combined regions.
  73. /// </summary>
  74. OR = 2,
  75. /// <summary>
  76. /// Creates the union of two combined regions except for any overlapping areas.
  77. /// </summary>
  78. XOR = 3,
  79. /// <summary>
  80. /// Combines the parts of hrgnSrc1 that are not part of hrgnSrc2.
  81. /// </summary>
  82. DIFF = 4,
  83. /// <summary>
  84. /// Creates a copy of the region identified by hrgnSrc1.
  85. /// </summary>
  86. COPY = 5,
  87. }
  88. internal enum CombineRgnResult
  89. {
  90. ERROR = 0,
  91. NULLREGION = 1,
  92. SIMPLEREGION = 2,
  93. COMPLEXREGION = 3,
  94. }
  95. /// <summary>
  96. /// For IWebBrowser2. OLECMDEXECOPT_*
  97. /// </summary>
  98. internal enum OLECMDEXECOPT
  99. {
  100. DODEFAULT = 0,
  101. PROMPTUSER = 1,
  102. DONTPROMPTUSER = 2,
  103. SHOWHELP = 3
  104. }
  105. /// <summary>
  106. /// For IWebBrowser2. OLECMDF_*
  107. /// </summary>
  108. internal enum OLECMDF
  109. {
  110. SUPPORTED = 1,
  111. ENABLED = 2,
  112. LATCHED = 4,
  113. NINCHED = 8,
  114. INVISIBLE = 16,
  115. DEFHIDEONCTXTMENU = 32
  116. }
  117. /// <summary>
  118. /// For IWebBrowser2. OLECMDID_*
  119. /// </summary>
  120. internal enum OLECMDID
  121. {
  122. OPEN = 1,
  123. NEW = 2,
  124. SAVE = 3,
  125. SAVEAS = 4,
  126. SAVECOPYAS = 5,
  127. PRINT = 6,
  128. PRINTPREVIEW = 7,
  129. PAGESETUP = 8,
  130. SPELL = 9,
  131. PROPERTIES = 10,
  132. CUT = 11,
  133. COPY = 12,
  134. PASTE = 13,
  135. PASTESPECIAL = 14,
  136. UNDO = 15,
  137. REDO = 16,
  138. SELECTALL = 17,
  139. CLEARSELECTION = 18,
  140. ZOOM = 19,
  141. GETZOOMRANGE = 20,
  142. UPDATECOMMANDS = 21,
  143. REFRESH = 22,
  144. STOP = 23,
  145. HIDETOOLBARS = 24,
  146. SETPROGRESSMAX = 25,
  147. SETPROGRESSPOS = 26,
  148. SETPROGRESSTEXT = 27,
  149. SETTITLE = 28,
  150. SETDOWNLOADSTATE = 29,
  151. STOPDOWNLOAD = 30,
  152. ONTOOLBARACTIVATED = 31,
  153. FIND = 32,
  154. DELETE = 33,
  155. HTTPEQUIV = 34,
  156. HTTPEQUIV_DONE = 35,
  157. ENABLE_INTERACTION = 36,
  158. ONUNLOAD = 37,
  159. PROPERTYBAG2 = 38,
  160. PREREFRESH = 39,
  161. SHOWSCRIPTERROR = 40,
  162. SHOWMESSAGE = 41,
  163. SHOWFIND = 42,
  164. SHOWPAGESETUP = 43,
  165. SHOWPRINT = 44,
  166. CLOSE = 45,
  167. ALLOWUILESSSAVEAS = 46,
  168. DONTDOWNLOADCSS = 47,
  169. UPDATEPAGESTATUS = 48,
  170. PRINT2 = 49,
  171. PRINTPREVIEW2 = 50,
  172. SETPRINTTEMPLATE = 51,
  173. GETPRINTTEMPLATE = 52,
  174. PAGEACTIONBLOCKED = 55,
  175. PAGEACTIONUIQUERY = 56,
  176. FOCUSVIEWCONTROLS = 57,
  177. FOCUSVIEWCONTROLSQUERY = 58,
  178. SHOWPAGEACTIONMENU = 59
  179. }
  180. /// <summary>
  181. /// For IWebBrowser2. READYSTATE_*
  182. /// </summary>
  183. enum READYSTATE
  184. {
  185. UNINITIALIZED = 0,
  186. LOADING = 1,
  187. LOADED = 2,
  188. INTERACTIVE = 3,
  189. COMPLETE = 4
  190. }
  191. /// <summary>
  192. /// DATAOBJ_GET_ITEM_FLAGS. DOGIF_*.
  193. /// </summary>
  194. internal enum DOGIF
  195. {
  196. DEFAULT = 0x0000,
  197. TRAVERSE_LINK = 0x0001, // if the item is a link get the target
  198. NO_HDROP = 0x0002, // don't fallback and use CF_HDROP clipboard format
  199. NO_URL = 0x0004, // don't fallback and use URL clipboard format
  200. ONLY_IF_ONE = 0x0008, // only return the item if there is one item in the array
  201. }
  202. internal enum DWM_SIT
  203. {
  204. None,
  205. DISPLAYFRAME = 1,
  206. }
  207. [Flags]
  208. internal enum ErrorModes
  209. {
  210. /// <summary>Use the system default, which is to display all error dialog boxes.</summary>
  211. Default = 0x0,
  212. /// <summary>
  213. /// The system does not display the critical-error-handler message box.
  214. /// Instead, the system sends the error to the calling process.
  215. /// </summary>
  216. FailCriticalErrors = 0x1,
  217. /// <summary>
  218. /// 64-bit Windows: The system automatically fixes memory alignment faults and makes them
  219. /// invisible to the application. It does this for the calling process and any descendant processes.
  220. /// After this value is set for a process, subsequent attempts to clear the value are ignored.
  221. /// </summary>
  222. NoGpFaultErrorBox = 0x2,
  223. /// <summary>
  224. /// The system does not display the general-protection-fault message box.
  225. /// This flag should only be set by debugging applications that handle general
  226. /// protection (GP) faults themselves with an exception handler.
  227. /// </summary>
  228. NoAlignmentFaultExcept = 0x4,
  229. /// <summary>
  230. /// The system does not display a message box when it fails to find a file.
  231. /// Instead, the error is returned to the calling process.
  232. /// </summary>
  233. NoOpenFileErrorBox = 0x8000
  234. }
  235. /// <summary>
  236. /// Non-client hit test values, HT*
  237. /// </summary>
  238. internal enum HT
  239. {
  240. ERROR = -2,
  241. TRANSPARENT = -1,
  242. NOWHERE = 0,
  243. CLIENT = 1,
  244. CAPTION = 2,
  245. SYSMENU = 3,
  246. GROWBOX = 4,
  247. SIZE = GROWBOX,
  248. MENU = 5,
  249. HSCROLL = 6,
  250. VSCROLL = 7,
  251. MINBUTTON = 8,
  252. MAXBUTTON = 9,
  253. LEFT = 10,
  254. RIGHT = 11,
  255. TOP = 12,
  256. TOPLEFT = 13,
  257. TOPRIGHT = 14,
  258. BOTTOM = 15,
  259. BOTTOMLEFT = 16,
  260. BOTTOMRIGHT = 17,
  261. BORDER = 18,
  262. REDUCE = MINBUTTON,
  263. ZOOM = MAXBUTTON,
  264. SIZEFIRST = LEFT,
  265. SIZELAST = BOTTOMRIGHT,
  266. OBJECT = 19,
  267. CLOSE = 20,
  268. HELP = 21
  269. }
  270. /// <summary>
  271. /// GetClassLongPtr values, GCLP_*
  272. /// </summary>
  273. internal enum GCLP
  274. {
  275. HBRBACKGROUND = -10,
  276. }
  277. /// <summary>
  278. /// GetWindowLongPtr values, GWL_*
  279. /// </summary>
  280. internal enum GWL
  281. {
  282. WNDPROC = (-4),
  283. HINSTANCE = (-6),
  284. HWNDPARENT = (-8),
  285. STYLE = (-16),
  286. EXSTYLE = (-20),
  287. USERDATA = (-21),
  288. ID = (-12)
  289. }
  290. /// <summary>
  291. /// SystemMetrics. SM_*
  292. /// </summary>
  293. internal enum SM
  294. {
  295. CXSCREEN = 0,
  296. CYSCREEN = 1,
  297. CXVSCROLL = 2,
  298. CYHSCROLL = 3,
  299. CYCAPTION = 4,
  300. CXBORDER = 5,
  301. CYBORDER = 6,
  302. CXFIXEDFRAME = 7,
  303. CYFIXEDFRAME = 8,
  304. CYVTHUMB = 9,
  305. CXHTHUMB = 10,
  306. CXICON = 11,
  307. CYICON = 12,
  308. CXCURSOR = 13,
  309. CYCURSOR = 14,
  310. CYMENU = 15,
  311. CXFULLSCREEN = 16,
  312. CYFULLSCREEN = 17,
  313. CYKANJIWINDOW = 18,
  314. MOUSEPRESENT = 19,
  315. CYVSCROLL = 20,
  316. CXHSCROLL = 21,
  317. DEBUG = 22,
  318. SWAPBUTTON = 23,
  319. CXMIN = 28,
  320. CYMIN = 29,
  321. CXSIZE = 30,
  322. CYSIZE = 31,
  323. CXFRAME = 32,
  324. CXSIZEFRAME = CXFRAME,
  325. CYFRAME = 33,
  326. CYSIZEFRAME = CYFRAME,
  327. CXMINTRACK = 34,
  328. CYMINTRACK = 35,
  329. CXDOUBLECLK = 36,
  330. CYDOUBLECLK = 37,
  331. CXICONSPACING = 38,
  332. CYICONSPACING = 39,
  333. MENUDROPALIGNMENT = 40,
  334. PENWINDOWS = 41,
  335. DBCSENABLED = 42,
  336. CMOUSEBUTTONS = 43,
  337. SECURE = 44,
  338. CXEDGE = 45,
  339. CYEDGE = 46,
  340. CXMINSPACING = 47,
  341. CYMINSPACING = 48,
  342. CXSMICON = 49,
  343. CYSMICON = 50,
  344. CYSMCAPTION = 51,
  345. CXSMSIZE = 52,
  346. CYSMSIZE = 53,
  347. CXMENUSIZE = 54,
  348. CYMENUSIZE = 55,
  349. ARRANGE = 56,
  350. CXMINIMIZED = 57,
  351. CYMINIMIZED = 58,
  352. CXMAXTRACK = 59,
  353. CYMAXTRACK = 60,
  354. CXMAXIMIZED = 61,
  355. CYMAXIMIZED = 62,
  356. NETWORK = 63,
  357. CLEANBOOT = 67,
  358. CXDRAG = 68,
  359. CYDRAG = 69,
  360. SHOWSOUNDS = 70,
  361. CXMENUCHECK = 71,
  362. CYMENUCHECK = 72,
  363. SLOWMACHINE = 73,
  364. MIDEASTENABLED = 74,
  365. MOUSEWHEELPRESENT = 75,
  366. XVIRTUALSCREEN = 76,
  367. YVIRTUALSCREEN = 77,
  368. CXVIRTUALSCREEN = 78,
  369. CYVIRTUALSCREEN = 79,
  370. CMONITORS = 80,
  371. SAMEDISPLAYFORMAT = 81,
  372. IMMENABLED = 82,
  373. CXFOCUSBORDER = 83,
  374. CYFOCUSBORDER = 84,
  375. TABLETPC = 86,
  376. MEDIACENTER = 87,
  377. REMOTESESSION = 0x1000,
  378. REMOTECONTROL = 0x2001,
  379. }
  380. /// <summary>
  381. /// SystemParameterInfo values, SPI_*
  382. /// </summary>
  383. internal enum SPI
  384. {
  385. GETBEEP = 0x0001,
  386. SETBEEP = 0x0002,
  387. GETMOUSE = 0x0003,
  388. SETMOUSE = 0x0004,
  389. GETBORDER = 0x0005,
  390. SETBORDER = 0x0006,
  391. GETKEYBOARDSPEED = 0x000A,
  392. SETKEYBOARDSPEED = 0x000B,
  393. LANGDRIVER = 0x000C,
  394. ICONHORIZONTALSPACING = 0x000D,
  395. GETSCREENSAVETIMEOUT = 0x000E,
  396. SETSCREENSAVETIMEOUT = 0x000F,
  397. GETSCREENSAVEACTIVE = 0x0010,
  398. SETSCREENSAVEACTIVE = 0x0011,
  399. GETGRIDGRANULARITY = 0x0012,
  400. SETGRIDGRANULARITY = 0x0013,
  401. SETDESKWALLPAPER = 0x0014,
  402. SETDESKPATTERN = 0x0015,
  403. GETKEYBOARDDELAY = 0x0016,
  404. SETKEYBOARDDELAY = 0x0017,
  405. ICONVERTICALSPACING = 0x0018,
  406. GETICONTITLEWRAP = 0x0019,
  407. SETICONTITLEWRAP = 0x001A,
  408. GETMENUDROPALIGNMENT = 0x001B,
  409. SETMENUDROPALIGNMENT = 0x001C,
  410. SETDOUBLECLKWIDTH = 0x001D,
  411. SETDOUBLECLKHEIGHT = 0x001E,
  412. GETICONTITLELOGFONT = 0x001F,
  413. SETDOUBLECLICKTIME = 0x0020,
  414. SETMOUSEBUTTONSWAP = 0x0021,
  415. SETICONTITLELOGFONT = 0x0022,
  416. GETFASTTASKSWITCH = 0x0023,
  417. SETFASTTASKSWITCH = 0x0024,
  418. SETDRAGFULLWINDOWS = 0x0025,
  419. GETDRAGFULLWINDOWS = 0x0026,
  420. GETNONCLIENTMETRICS = 0x0029,
  421. SETNONCLIENTMETRICS = 0x002A,
  422. GETMINIMIZEDMETRICS = 0x002B,
  423. SETMINIMIZEDMETRICS = 0x002C,
  424. GETICONMETRICS = 0x002D,
  425. SETICONMETRICS = 0x002E,
  426. SETWORKAREA = 0x002F,
  427. GETWORKAREA = 0x0030,
  428. SETPENWINDOWS = 0x0031,
  429. GETHIGHCONTRAST = 0x0042,
  430. SETHIGHCONTRAST = 0x0043,
  431. GETKEYBOARDPREF = 0x0044,
  432. SETKEYBOARDPREF = 0x0045,
  433. GETSCREENREADER = 0x0046,
  434. SETSCREENREADER = 0x0047,
  435. GETANIMATION = 0x0048,
  436. SETANIMATION = 0x0049,
  437. GETFONTSMOOTHING = 0x004A,
  438. SETFONTSMOOTHING = 0x004B,
  439. SETDRAGWIDTH = 0x004C,
  440. SETDRAGHEIGHT = 0x004D,
  441. SETHANDHELD = 0x004E,
  442. GETLOWPOWERTIMEOUT = 0x004F,
  443. GETPOWEROFFTIMEOUT = 0x0050,
  444. SETLOWPOWERTIMEOUT = 0x0051,
  445. SETPOWEROFFTIMEOUT = 0x0052,
  446. GETLOWPOWERACTIVE = 0x0053,
  447. GETPOWEROFFACTIVE = 0x0054,
  448. SETLOWPOWERACTIVE = 0x0055,
  449. SETPOWEROFFACTIVE = 0x0056,
  450. SETCURSORS = 0x0057,
  451. SETICONS = 0x0058,
  452. GETDEFAULTINPUTLANG = 0x0059,
  453. SETDEFAULTINPUTLANG = 0x005A,
  454. SETLANGTOGGLE = 0x005B,
  455. GETWINDOWSEXTENSION = 0x005C,
  456. SETMOUSETRAILS = 0x005D,
  457. GETMOUSETRAILS = 0x005E,
  458. SETSCREENSAVERRUNNING = 0x0061,
  459. SCREENSAVERRUNNING = SETSCREENSAVERRUNNING,
  460. GETFILTERKEYS = 0x0032,
  461. SETFILTERKEYS = 0x0033,
  462. GETTOGGLEKEYS = 0x0034,
  463. SETTOGGLEKEYS = 0x0035,
  464. GETMOUSEKEYS = 0x0036,
  465. SETMOUSEKEYS = 0x0037,
  466. GETSHOWSOUNDS = 0x0038,
  467. SETSHOWSOUNDS = 0x0039,
  468. GETSTICKYKEYS = 0x003A,
  469. SETSTICKYKEYS = 0x003B,
  470. GETACCESSTIMEOUT = 0x003C,
  471. SETACCESSTIMEOUT = 0x003D,
  472. GETSERIALKEYS = 0x003E,
  473. SETSERIALKEYS = 0x003F,
  474. GETSOUNDSENTRY = 0x0040,
  475. SETSOUNDSENTRY = 0x0041,
  476. GETSNAPTODEFBUTTON = 0x005F,
  477. SETSNAPTODEFBUTTON = 0x0060,
  478. GETMOUSEHOVERWIDTH = 0x0062,
  479. SETMOUSEHOVERWIDTH = 0x0063,
  480. GETMOUSEHOVERHEIGHT = 0x0064,
  481. SETMOUSEHOVERHEIGHT = 0x0065,
  482. GETMOUSEHOVERTIME = 0x0066,
  483. SETMOUSEHOVERTIME = 0x0067,
  484. GETWHEELSCROLLLINES = 0x0068,
  485. SETWHEELSCROLLLINES = 0x0069,
  486. GETMENUSHOWDELAY = 0x006A,
  487. SETMENUSHOWDELAY = 0x006B,
  488. GETWHEELSCROLLCHARS = 0x006C,
  489. SETWHEELSCROLLCHARS = 0x006D,
  490. GETSHOWIMEUI = 0x006E,
  491. SETSHOWIMEUI = 0x006F,
  492. GETMOUSESPEED = 0x0070,
  493. SETMOUSESPEED = 0x0071,
  494. GETSCREENSAVERRUNNING = 0x0072,
  495. GETDESKWALLPAPER = 0x0073,
  496. GETAUDIODESCRIPTION = 0x0074,
  497. SETAUDIODESCRIPTION = 0x0075,
  498. GETSCREENSAVESECURE = 0x0076,
  499. SETSCREENSAVESECURE = 0x0077,
  500. GETHUNGAPPTIMEOUT = 0x0078,
  501. SETHUNGAPPTIMEOUT = 0x0079,
  502. GETWAITTOKILLTIMEOUT = 0x007A,
  503. SETWAITTOKILLTIMEOUT = 0x007B,
  504. GETWAITTOKILLSERVICETIMEOUT = 0x007C,
  505. SETWAITTOKILLSERVICETIMEOUT = 0x007D,
  506. GETMOUSEDOCKTHRESHOLD = 0x007E,
  507. SETMOUSEDOCKTHRESHOLD = 0x007F,
  508. GETPENDOCKTHRESHOLD = 0x0080,
  509. SETPENDOCKTHRESHOLD = 0x0081,
  510. GETWINARRANGING = 0x0082,
  511. SETWINARRANGING = 0x0083,
  512. GETMOUSEDRAGOUTTHRESHOLD = 0x0084,
  513. SETMOUSEDRAGOUTTHRESHOLD = 0x0085,
  514. GETPENDRAGOUTTHRESHOLD = 0x0086,
  515. SETPENDRAGOUTTHRESHOLD = 0x0087,
  516. GETMOUSESIDEMOVETHRESHOLD = 0x0088,
  517. SETMOUSESIDEMOVETHRESHOLD = 0x0089,
  518. GETPENSIDEMOVETHRESHOLD = 0x008A,
  519. SETPENSIDEMOVETHRESHOLD = 0x008B,
  520. GETDRAGFROMMAXIMIZE = 0x008C,
  521. SETDRAGFROMMAXIMIZE = 0x008D,
  522. GETSNAPSIZING = 0x008E,
  523. SETSNAPSIZING = 0x008F,
  524. GETDOCKMOVING = 0x0090,
  525. SETDOCKMOVING = 0x0091,
  526. GETACTIVEWINDOWTRACKING = 0x1000,
  527. SETACTIVEWINDOWTRACKING = 0x1001,
  528. GETMENUANIMATION = 0x1002,
  529. SETMENUANIMATION = 0x1003,
  530. GETCOMBOBOXANIMATION = 0x1004,
  531. SETCOMBOBOXANIMATION = 0x1005,
  532. GETLISTBOXSMOOTHSCROLLING = 0x1006,
  533. SETLISTBOXSMOOTHSCROLLING = 0x1007,
  534. GETGRADIENTCAPTIONS = 0x1008,
  535. SETGRADIENTCAPTIONS = 0x1009,
  536. GETKEYBOARDCUES = 0x100A,
  537. SETKEYBOARDCUES = 0x100B,
  538. GETMENUUNDERLINES = GETKEYBOARDCUES,
  539. SETMENUUNDERLINES = SETKEYBOARDCUES,
  540. GETACTIVEWNDTRKZORDER = 0x100C,
  541. SETACTIVEWNDTRKZORDER = 0x100D,
  542. GETHOTTRACKING = 0x100E,
  543. SETHOTTRACKING = 0x100F,
  544. GETMENUFADE = 0x1012,
  545. SETMENUFADE = 0x1013,
  546. GETSELECTIONFADE = 0x1014,
  547. SETSELECTIONFADE = 0x1015,
  548. GETTOOLTIPANIMATION = 0x1016,
  549. SETTOOLTIPANIMATION = 0x1017,
  550. GETTOOLTIPFADE = 0x1018,
  551. SETTOOLTIPFADE = 0x1019,
  552. GETCURSORSHADOW = 0x101A,
  553. SETCURSORSHADOW = 0x101B,
  554. GETMOUSESONAR = 0x101C,
  555. SETMOUSESONAR = 0x101D,
  556. GETMOUSECLICKLOCK = 0x101E,
  557. SETMOUSECLICKLOCK = 0x101F,
  558. GETMOUSEVANISH = 0x1020,
  559. SETMOUSEVANISH = 0x1021,
  560. GETFLATMENU = 0x1022,
  561. SETFLATMENU = 0x1023,
  562. GETDROPSHADOW = 0x1024,
  563. SETDROPSHADOW = 0x1025,
  564. GETBLOCKSENDINPUTRESETS = 0x1026,
  565. SETBLOCKSENDINPUTRESETS = 0x1027,
  566. GETUIEFFECTS = 0x103E,
  567. SETUIEFFECTS = 0x103F,
  568. GETDISABLEOVERLAPPEDCONTENT = 0x1040,
  569. SETDISABLEOVERLAPPEDCONTENT = 0x1041,
  570. GETCLIENTAREAANIMATION = 0x1042,
  571. SETCLIENTAREAANIMATION = 0x1043,
  572. GETCLEARTYPE = 0x1048,
  573. SETCLEARTYPE = 0x1049,
  574. GETSPEECHRECOGNITION = 0x104A,
  575. SETSPEECHRECOGNITION = 0x104B,
  576. GETFOREGROUNDLOCKTIMEOUT = 0x2000,
  577. SETFOREGROUNDLOCKTIMEOUT = 0x2001,
  578. GETACTIVEWNDTRKTIMEOUT = 0x2002,
  579. SETACTIVEWNDTRKTIMEOUT = 0x2003,
  580. GETFOREGROUNDFLASHCOUNT = 0x2004,
  581. SETFOREGROUNDFLASHCOUNT = 0x2005,
  582. GETCARETWIDTH = 0x2006,
  583. SETCARETWIDTH = 0x2007,
  584. GETMOUSECLICKLOCKTIME = 0x2008,
  585. SETMOUSECLICKLOCKTIME = 0x2009,
  586. GETFONTSMOOTHINGTYPE = 0x200A,
  587. SETFONTSMOOTHINGTYPE = 0x200B,
  588. GETFONTSMOOTHINGCONTRAST = 0x200C,
  589. SETFONTSMOOTHINGCONTRAST = 0x200D,
  590. GETFOCUSBORDERWIDTH = 0x200E,
  591. SETFOCUSBORDERWIDTH = 0x200F,
  592. GETFOCUSBORDERHEIGHT = 0x2010,
  593. SETFOCUSBORDERHEIGHT = 0x2011,
  594. GETFONTSMOOTHINGORIENTATION = 0x2012,
  595. SETFONTSMOOTHINGORIENTATION = 0x2013,
  596. GETMINIMUMHITRADIUS = 0x2014,
  597. SETMINIMUMHITRADIUS = 0x2015,
  598. GETMESSAGEDURATION = 0x2016,
  599. SETMESSAGEDURATION = 0x2017,
  600. }
  601. /// <summary>
  602. /// SystemParameterInfo flag values, SPIF_*
  603. /// </summary>
  604. [Flags]
  605. internal enum SPIF
  606. {
  607. None = 0,
  608. UPDATEINIFILE = 0x01,
  609. SENDCHANGE = 0x02,
  610. SENDWININICHANGE = SENDCHANGE,
  611. }
  612. [Flags]
  613. internal enum STATE_SYSTEM
  614. {
  615. UNAVAILABLE = 0x00000001, // Disabled
  616. SELECTED = 0x00000002,
  617. FOCUSED = 0x00000004,
  618. PRESSED = 0x00000008,
  619. CHECKED = 0x00000010,
  620. MIXED = 0x00000020, // 3-state checkbox or toolbar button
  621. INDETERMINATE = MIXED,
  622. READONLY = 0x00000040,
  623. HOTTRACKED = 0x00000080,
  624. DEFAULT = 0x00000100,
  625. EXPANDED = 0x00000200,
  626. COLLAPSED = 0x00000400,
  627. BUSY = 0x00000800,
  628. FLOATING = 0x00001000, // Children "owned" not "contained" by parent
  629. MARQUEED = 0x00002000,
  630. ANIMATED = 0x00004000,
  631. INVISIBLE = 0x00008000,
  632. OFFSCREEN = 0x00010000,
  633. SIZEABLE = 0x00020000,
  634. MOVEABLE = 0x00040000,
  635. SELFVOICING = 0x00080000,
  636. FOCUSABLE = 0x00100000,
  637. SELECTABLE = 0x00200000,
  638. LINKED = 0x00400000,
  639. TRAVERSED = 0x00800000,
  640. MULTISELECTABLE = 0x01000000, // Supports multiple selection
  641. EXTSELECTABLE = 0x02000000, // Supports extended selection
  642. ALERT_LOW = 0x04000000, // This information is of low priority
  643. ALERT_MEDIUM = 0x08000000, // This information is of medium priority
  644. ALERT_HIGH = 0x10000000, // This information is of high priority
  645. PROTECTED = 0x20000000, // access to this is restricted
  646. VALID = 0x3FFFFFFF,
  647. }
  648. internal enum StockObject : int
  649. {
  650. WHITE_BRUSH = 0,
  651. LTGRAY_BRUSH = 1,
  652. GRAY_BRUSH = 2,
  653. DKGRAY_BRUSH = 3,
  654. BLACK_BRUSH = 4,
  655. NULL_BRUSH = 5,
  656. HOLLOW_BRUSH = NULL_BRUSH,
  657. WHITE_PEN = 6,
  658. BLACK_PEN = 7,
  659. NULL_PEN = 8,
  660. SYSTEM_FONT = 13,
  661. DEFAULT_PALETTE = 15,
  662. }
  663. /// <summary>
  664. /// CS_*
  665. /// </summary>
  666. [Flags]
  667. internal enum CS : uint
  668. {
  669. VREDRAW = 0x0001,
  670. HREDRAW = 0x0002,
  671. DBLCLKS = 0x0008,
  672. OWNDC = 0x0020,
  673. CLASSDC = 0x0040,
  674. PARENTDC = 0x0080,
  675. NOCLOSE = 0x0200,
  676. SAVEBITS = 0x0800,
  677. BYTEALIGNCLIENT = 0x1000,
  678. BYTEALIGNWINDOW = 0x2000,
  679. GLOBALCLASS = 0x4000,
  680. IME = 0x00010000,
  681. DROPSHADOW = 0x00020000
  682. }
  683. /// <summary>
  684. /// WindowStyle values, WS_*
  685. /// </summary>
  686. [Flags]
  687. internal enum WS : uint
  688. {
  689. OVERLAPPED = 0x00000000,
  690. POPUP = 0x80000000,
  691. CHILD = 0x40000000,
  692. MINIMIZE = 0x20000000,
  693. VISIBLE = 0x10000000,
  694. DISABLED = 0x08000000,
  695. CLIPSIBLINGS = 0x04000000,
  696. CLIPCHILDREN = 0x02000000,
  697. MAXIMIZE = 0x01000000,
  698. BORDER = 0x00800000,
  699. DLGFRAME = 0x00400000,
  700. VSCROLL = 0x00200000,
  701. HSCROLL = 0x00100000,
  702. SYSMENU = 0x00080000,
  703. THICKFRAME = 0x00040000,
  704. GROUP = 0x00020000,
  705. TABSTOP = 0x00010000,
  706. MINIMIZEBOX = 0x00020000,
  707. MAXIMIZEBOX = 0x00010000,
  708. CAPTION = BORDER | DLGFRAME,
  709. TILED = OVERLAPPED,
  710. ICONIC = MINIMIZE,
  711. SIZEBOX = THICKFRAME,
  712. TILEDWINDOW = OVERLAPPEDWINDOW,
  713. OVERLAPPEDWINDOW = OVERLAPPED | CAPTION | SYSMENU | THICKFRAME | MINIMIZEBOX | MAXIMIZEBOX,
  714. POPUPWINDOW = POPUP | BORDER | SYSMENU,
  715. CHILDWINDOW = CHILD,
  716. }
  717. /// <summary>
  718. /// Window message values, WM_*
  719. /// </summary>
  720. internal enum WM
  721. {
  722. NULL = 0x0000,
  723. CREATE = 0x0001,
  724. DESTROY = 0x0002,
  725. MOVE = 0x0003,
  726. SIZE = 0x0005,
  727. ACTIVATE = 0x0006,
  728. SETFOCUS = 0x0007,
  729. KILLFOCUS = 0x0008,
  730. ENABLE = 0x000A,
  731. SETREDRAW = 0x000B,
  732. SETTEXT = 0x000C,
  733. GETTEXT = 0x000D,
  734. GETTEXTLENGTH = 0x000E,
  735. PAINT = 0x000F,
  736. CLOSE = 0x0010,
  737. QUERYENDSESSION = 0x0011,
  738. QUIT = 0x0012,
  739. QUERYOPEN = 0x0013,
  740. ERASEBKGND = 0x0014,
  741. SYSCOLORCHANGE = 0x0015,
  742. SHOWWINDOW = 0x0018,
  743. CTLCOLOR = 0x0019,
  744. WININICHANGE = 0x001A,
  745. SETTINGCHANGE = 0x001A,
  746. ACTIVATEAPP = 0x001C,
  747. SETCURSOR = 0x0020,
  748. MOUSEACTIVATE = 0x0021,
  749. CHILDACTIVATE = 0x0022,
  750. QUEUESYNC = 0x0023,
  751. GETMINMAXINFO = 0x0024,
  752. WINDOWPOSCHANGING = 0x0046,
  753. WINDOWPOSCHANGED = 0x0047,
  754. CONTEXTMENU = 0x007B,
  755. STYLECHANGING = 0x007C,
  756. STYLECHANGED = 0x007D,
  757. DISPLAYCHANGE = 0x007E,
  758. GETICON = 0x007F,
  759. SETICON = 0x0080,
  760. NCCREATE = 0x0081,
  761. NCDESTROY = 0x0082,
  762. NCCALCSIZE = 0x0083,
  763. NCHITTEST = 0x0084,
  764. NCPAINT = 0x0085,
  765. NCACTIVATE = 0x0086,
  766. GETDLGCODE = 0x0087,
  767. SYNCPAINT = 0x0088,
  768. NCMOUSEMOVE = 0x00A0,
  769. NCLBUTTONDOWN = 0x00A1,
  770. NCLBUTTONUP = 0x00A2,
  771. NCLBUTTONDBLCLK = 0x00A3,
  772. NCRBUTTONDOWN = 0x00A4,
  773. NCRBUTTONUP = 0x00A5,
  774. NCRBUTTONDBLCLK = 0x00A6,
  775. NCMBUTTONDOWN = 0x00A7,
  776. NCMBUTTONUP = 0x00A8,
  777. NCMBUTTONDBLCLK = 0x00A9,
  778. SYSKEYDOWN = 0x0104,
  779. SYSKEYUP = 0x0105,
  780. SYSCHAR = 0x0106,
  781. SYSDEADCHAR = 0x0107,
  782. COMMAND = 0x0111,
  783. SYSCOMMAND = 0x0112,
  784. MOUSEMOVE = 0x0200,
  785. LBUTTONDOWN = 0x0201,
  786. LBUTTONUP = 0x0202,
  787. LBUTTONDBLCLK = 0x0203,
  788. RBUTTONDOWN = 0x0204,
  789. RBUTTONUP = 0x0205,
  790. RBUTTONDBLCLK = 0x0206,
  791. MBUTTONDOWN = 0x0207,
  792. MBUTTONUP = 0x0208,
  793. MBUTTONDBLCLK = 0x0209,
  794. MOUSEWHEEL = 0x020A,
  795. XBUTTONDOWN = 0x020B,
  796. XBUTTONUP = 0x020C,
  797. XBUTTONDBLCLK = 0x020D,
  798. MOUSEHWHEEL = 0x020E,
  799. PARENTNOTIFY = 0x0210,
  800. CAPTURECHANGED = 0x0215,
  801. POWERBROADCAST = 0x0218,
  802. DEVICECHANGE = 0x0219,
  803. ENTERSIZEMOVE = 0x0231,
  804. EXITSIZEMOVE = 0x0232,
  805. IME_SETCONTEXT = 0x0281,
  806. IME_NOTIFY = 0x0282,
  807. IME_CONTROL = 0x0283,
  808. IME_COMPOSITIONFULL = 0x0284,
  809. IME_SELECT = 0x0285,
  810. IME_CHAR = 0x0286,
  811. IME_REQUEST = 0x0288,
  812. IME_KEYDOWN = 0x0290,
  813. IME_KEYUP = 0x0291,
  814. NCMOUSELEAVE = 0x02A2,
  815. TABLET_DEFBASE = 0x02C0,
  816. //WM_TABLET_MAXOFFSET = 0x20,
  817. TABLET_ADDED = TABLET_DEFBASE + 8,
  818. TABLET_DELETED = TABLET_DEFBASE + 9,
  819. TABLET_FLICK = TABLET_DEFBASE + 11,
  820. TABLET_QUERYSYSTEMGESTURESTATUS = TABLET_DEFBASE + 12,
  821. CUT = 0x0300,
  822. COPY = 0x0301,
  823. PASTE = 0x0302,
  824. CLEAR = 0x0303,
  825. UNDO = 0x0304,
  826. RENDERFORMAT = 0x0305,
  827. RENDERALLFORMATS = 0x0306,
  828. DESTROYCLIPBOARD = 0x0307,
  829. DRAWCLIPBOARD = 0x0308,
  830. PAINTCLIPBOARD = 0x0309,
  831. VSCROLLCLIPBOARD = 0x030A,
  832. SIZECLIPBOARD = 0x030B,
  833. ASKCBFORMATNAME = 0x030C,
  834. CHANGECBCHAIN = 0x030D,
  835. HSCROLLCLIPBOARD = 0x030E,
  836. QUERYNEWPALETTE = 0x030F,
  837. PALETTEISCHANGING = 0x0310,
  838. PALETTECHANGED = 0x0311,
  839. HOTKEY = 0x0312,
  840. PRINT = 0x0317,
  841. PRINTCLIENT = 0x0318,
  842. APPCOMMAND = 0x0319,
  843. THEMECHANGED = 0x031A,
  844. DWMCOMPOSITIONCHANGED = 0x031E,
  845. DWMNCRENDERINGCHANGED = 0x031F,
  846. DWMCOLORIZATIONCOLORCHANGED = 0x0320,
  847. DWMWINDOWMAXIMIZEDCHANGE = 0x0321,
  848. GETTITLEBARINFOEX = 0x033F,
  849. #region Windows 7
  850. DWMSENDICONICTHUMBNAIL = 0x0323,
  851. DWMSENDICONICLIVEPREVIEWBITMAP = 0x0326,
  852. #endregion
  853. USER = 0x0400,
  854. // This is the hard-coded message value used by WinForms for Shell_NotifyIcon.
  855. // It's relatively safe to reuse.
  856. TRAYMOUSEMESSAGE = 0x800, //WM_USER + 1024
  857. APP = 0x8000,
  858. }
  859. /// <summary>
  860. /// Window style extended values, WS_EX_*
  861. /// </summary>
  862. [Flags]
  863. internal enum WS_EX : uint
  864. {
  865. None = 0,
  866. DLGMODALFRAME = 0x00000001,
  867. NOPARENTNOTIFY = 0x00000004,
  868. TOPMOST = 0x00000008,
  869. ACCEPTFILES = 0x00000010,
  870. TRANSPARENT = 0x00000020,
  871. MDICHILD = 0x00000040,
  872. TOOLWINDOW = 0x00000080,
  873. WINDOWEDGE = 0x00000100,
  874. CLIENTEDGE = 0x00000200,
  875. CONTEXTHELP = 0x00000400,
  876. RIGHT = 0x00001000,
  877. LEFT = 0x00000000,
  878. RTLREADING = 0x00002000,
  879. LTRREADING = 0x00000000,
  880. LEFTSCROLLBAR = 0x00004000,
  881. RIGHTSCROLLBAR = 0x00000000,
  882. CONTROLPARENT = 0x00010000,
  883. STATICEDGE = 0x00020000,
  884. APPWINDOW = 0x00040000,
  885. LAYERED = 0x00080000,
  886. NOINHERITLAYOUT = 0x00100000, // Disable inheritence of mirroring by children
  887. LAYOUTRTL = 0x00400000, // Right to left mirroring
  888. COMPOSITED = 0x02000000,
  889. NOACTIVATE = 0x08000000,
  890. OVERLAPPEDWINDOW = (WINDOWEDGE | CLIENTEDGE),
  891. PALETTEWINDOW = (WINDOWEDGE | TOOLWINDOW | TOPMOST),
  892. }
  893. /// <summary>
  894. /// GetDeviceCaps nIndex values.
  895. /// </summary>
  896. internal enum DeviceCap
  897. {
  898. /// <summary>Number of bits per pixel
  899. /// </summary>
  900. BITSPIXEL = 12,
  901. /// <summary>
  902. /// Number of planes
  903. /// </summary>
  904. PLANES = 14,
  905. /// <summary>
  906. /// Logical pixels inch in X
  907. /// </summary>
  908. LOGPIXELSX = 88,
  909. /// <summary>
  910. /// Logical pixels inch in Y
  911. /// </summary>
  912. LOGPIXELSY = 90,
  913. }
  914. internal enum FO : int
  915. {
  916. MOVE = 0x0001,
  917. COPY = 0x0002,
  918. DELETE = 0x0003,
  919. RENAME = 0x0004,
  920. }
  921. /// <summary>
  922. /// "FILEOP_FLAGS", FOF_*.
  923. /// </summary>
  924. internal enum FOF : ushort
  925. {
  926. MULTIDESTFILES = 0x0001,
  927. CONFIRMMOUSE = 0x0002,
  928. SILENT = 0x0004,
  929. RENAMEONCOLLISION = 0x0008,
  930. NOCONFIRMATION = 0x0010,
  931. WANTMAPPINGHANDLE = 0x0020,
  932. ALLOWUNDO = 0x0040,
  933. FILESONLY = 0x0080,
  934. SIMPLEPROGRESS = 0x0100,
  935. NOCONFIRMMKDIR = 0x0200,
  936. NOERRORUI = 0x0400,
  937. NOCOPYSECURITYATTRIBS = 0x0800,
  938. NORECURSION = 0x1000,
  939. NO_CONNECTED_ELEMENTS = 0x2000,
  940. WANTNUKEWARNING = 0x4000,
  941. NORECURSEREPARSE = 0x8000,
  942. }
  943. /// <summary>
  944. /// EnableMenuItem uEnable values, MF_*
  945. /// </summary>
  946. [Flags]
  947. internal enum MF : uint
  948. {
  949. /// <summary>
  950. /// Possible return value for EnableMenuItem
  951. /// </summary>
  952. DOES_NOT_EXIST = unchecked((uint)-1),
  953. ENABLED = 0,
  954. BYCOMMAND = 0,
  955. GRAYED = 1,
  956. DISABLED = 2,
  957. }
  958. /// <summary>Specifies the type of visual style attribute to set on a window.</summary>
  959. internal enum WINDOWTHEMEATTRIBUTETYPE : uint
  960. {
  961. /// <summary>Non-client area window attributes will be set.</summary>
  962. WTA_NONCLIENT = 1,
  963. }
  964. /// <summary>
  965. /// DWMFLIP3DWINDOWPOLICY. DWMFLIP3D_*
  966. /// </summary>
  967. internal enum DWMFLIP3D
  968. {
  969. DEFAULT,
  970. EXCLUDEBELOW,
  971. EXCLUDEABOVE,
  972. //LAST
  973. }
  974. /// <summary>
  975. /// DWMNCRENDERINGPOLICY. DWMNCRP_*
  976. /// </summary>
  977. internal enum DWMNCRP
  978. {
  979. USEWINDOWSTYLE,
  980. DISABLED,
  981. ENABLED,
  982. //LAST
  983. }
  984. /// <summary>
  985. /// DWMWINDOWATTRIBUTE. DWMWA_*
  986. /// </summary>
  987. internal enum DWMWA
  988. {
  989. NCRENDERING_ENABLED = 1,
  990. NCRENDERING_POLICY,
  991. TRANSITIONS_FORCEDISABLED,
  992. ALLOW_NCPAINT,
  993. CAPTION_BUTTON_BOUNDS,
  994. NONCLIENT_RTL_LAYOUT,
  995. FORCE_ICONIC_REPRESENTATION,
  996. FLIP3D_POLICY,
  997. EXTENDED_FRAME_BOUNDS,
  998. // New to Windows 7:
  999. HAS_ICONIC_BITMAP,
  1000. DISALLOW_PEEK,
  1001. EXCLUDED_FROM_PEEK,
  1002. // LAST
  1003. }
  1004. /// <summary>
  1005. /// WindowThemeNonClientAttributes
  1006. /// </summary>
  1007. [Flags]
  1008. internal enum WTNCA : uint
  1009. {
  1010. /// <summary>Prevents the window caption from being drawn.</summary>
  1011. NODRAWCAPTION = 0x00000001,
  1012. /// <summary>Prevents the system icon from being drawn.</summary>
  1013. NODRAWICON = 0x00000002,
  1014. /// <summary>Prevents the system icon menu from appearing.</summary>
  1015. NOSYSMENU = 0x00000004,
  1016. /// <summary>Prevents mirroring of the question mark, even in right-to-left (RTL) layout.</summary>
  1017. NOMIRRORHELP = 0x00000008,
  1018. /// <summary> A mask that contains all the valid bits.</summary>
  1019. VALIDBITS = NODRAWCAPTION | NODRAWICON | NOMIRRORHELP | NOSYSMENU,
  1020. }
  1021. /// <summary>
  1022. /// SetWindowPos options
  1023. /// </summary>
  1024. [Flags]
  1025. internal enum SWP
  1026. {
  1027. ASYNCWINDOWPOS = 0x4000,
  1028. DEFERERASE = 0x2000,
  1029. DRAWFRAME = 0x0020,
  1030. FRAMECHANGED = 0x0020,
  1031. HIDEWINDOW = 0x0080,
  1032. NOACTIVATE = 0x0010,
  1033. NOCOPYBITS = 0x0100,
  1034. NOMOVE = 0x0002,
  1035. NOOWNERZORDER = 0x0200,
  1036. NOREDRAW = 0x0008,
  1037. NOREPOSITION = 0x0200,
  1038. NOSENDCHANGING = 0x0400,
  1039. NOSIZE = 0x0001,
  1040. NOZORDER = 0x0004,
  1041. SHOWWINDOW = 0x0040,
  1042. }
  1043. /// <summary>
  1044. /// ShowWindow options
  1045. /// </summary>
  1046. internal enum SW
  1047. {
  1048. HIDE = 0,
  1049. SHOWNORMAL = 1,
  1050. NORMAL = 1,
  1051. SHOWMINIMIZED = 2,
  1052. SHOWMAXIMIZED = 3,
  1053. MAXIMIZE = 3,
  1054. SHOWNOACTIVATE = 4,
  1055. SHOW = 5,
  1056. MINIMIZE = 6,
  1057. SHOWMINNOACTIVE = 7,
  1058. SHOWNA = 8,
  1059. RESTORE = 9,
  1060. SHOWDEFAULT = 10,
  1061. FORCEMINIMIZE = 11,
  1062. }
  1063. internal enum SC
  1064. {
  1065. SIZE = 0xF000,
  1066. MOVE = 0xF010,
  1067. MINIMIZE = 0xF020,
  1068. MAXIMIZE = 0xF030,
  1069. NEXTWINDOW = 0xF040,
  1070. PREVWINDOW = 0xF050,
  1071. CLOSE = 0xF060,
  1072. VSCROLL = 0xF070,
  1073. HSCROLL = 0xF080,
  1074. MOUSEMENU = 0xF090,
  1075. KEYMENU = 0xF100,
  1076. ARRANGE = 0xF110,
  1077. RESTORE = 0xF120,
  1078. TASKLIST = 0xF130,
  1079. SCREENSAVE = 0xF140,
  1080. HOTKEY = 0xF150,
  1081. DEFAULT = 0xF160,
  1082. MONITORPOWER = 0xF170,
  1083. CONTEXTHELP = 0xF180,
  1084. SEPARATOR = 0xF00F,
  1085. /// <summary>
  1086. /// SCF_ISSECURE
  1087. /// </summary>
  1088. F_ISSECURE = 0x00000001,
  1089. ICON = MINIMIZE,
  1090. ZOOM = MAXIMIZE,
  1091. }
  1092. /// <summary>
  1093. /// GDI+ Status codes
  1094. /// </summary>
  1095. internal enum Status
  1096. {
  1097. Ok = 0,
  1098. GenericError = 1,
  1099. InvalidParameter = 2,
  1100. OutOfMemory = 3,
  1101. ObjectBusy = 4,
  1102. InsufficientBuffer = 5,
  1103. NotImplemented = 6,
  1104. Win32Error = 7,
  1105. WrongState = 8,
  1106. Aborted = 9,
  1107. FileNotFound = 10,
  1108. ValueOverflow = 11,
  1109. AccessDenied = 12,
  1110. UnknownImageFormat = 13,
  1111. FontFamilyNotFound = 14,
  1112. FontStyleNotFound = 15,
  1113. NotTrueTypeFont = 16,
  1114. UnsupportedGdiplusVersion = 17,
  1115. GdiplusNotInitialized = 18,
  1116. PropertyNotFound = 19,
  1117. PropertyNotSupported = 20,
  1118. ProfileNotFound = 21,
  1119. }
  1120. internal enum MOUSEEVENTF : int
  1121. {
  1122. //mouse event constants
  1123. LEFTDOWN = 2,
  1124. LEFTUP = 4
  1125. }
  1126. /// <summary>
  1127. /// MSGFLT_*. New in Vista. Realiased in Windows 7.
  1128. /// </summary>
  1129. internal enum MSGFLT
  1130. {
  1131. // Win7 versions of this enum:
  1132. RESET = 0,
  1133. ALLOW = 1,
  1134. DISALLOW = 2,
  1135. // Vista versions of this enum:
  1136. // ADD = 1,
  1137. // REMOVE = 2,
  1138. }
  1139. internal enum MSGFLTINFO
  1140. {
  1141. NONE = 0,
  1142. ALREADYALLOWED_FORWND = 1,
  1143. ALREADYDISALLOWED_FORWND = 2,
  1144. ALLOWED_HIGHER = 3,
  1145. }
  1146. internal enum INPUT_TYPE : uint
  1147. {
  1148. MOUSE = 0,
  1149. }
  1150. /// <summary>
  1151. /// Shell_NotifyIcon messages. NIM_*
  1152. /// </summary>
  1153. internal enum NIM : uint
  1154. {
  1155. ADD = 0,
  1156. MODIFY = 1,
  1157. DELETE = 2,
  1158. SETFOCUS = 3,
  1159. SETVERSION = 4,
  1160. }
  1161. /// <summary>
  1162. /// SHAddToRecentDocuments flags. SHARD_*
  1163. /// </summary>
  1164. internal enum SHARD
  1165. {
  1166. PIDL = 0x00000001,
  1167. PATHA = 0x00000002,
  1168. PATHW = 0x00000003,
  1169. APPIDINFO = 0x00000004, // indicates the data type is a pointer to a SHARDAPPIDINFO structure
  1170. APPIDINFOIDLIST = 0x00000005, // indicates the data type is a pointer to a SHARDAPPIDINFOIDLIST structure
  1171. LINK = 0x00000006, // indicates the data type is a pointer to an IShellLink instance
  1172. APPIDINFOLINK = 0x00000007, // indicates the data type is a pointer to a SHARDAPPIDINFOLINK structure
  1173. }
  1174. [Flags]
  1175. enum SLGP
  1176. {
  1177. SHORTPATH = 0x1,
  1178. UNCPRIORITY = 0x2,
  1179. RAWPATH = 0x4
  1180. }
  1181. /// <summary>
  1182. /// Shell_NotifyIcon flags. NIF_*
  1183. /// </summary>
  1184. [Flags]
  1185. internal enum NIF : uint
  1186. {
  1187. MESSAGE = 0x0001,
  1188. ICON = 0x0002,
  1189. TIP = 0x0004,
  1190. STATE = 0x0008,
  1191. INFO = 0x0010,
  1192. GUID = 0x0020,
  1193. /// <summary>
  1194. /// Vista only.
  1195. /// </summary>
  1196. REALTIME = 0x0040,
  1197. /// <summary>
  1198. /// Vista only.
  1199. /// </summary>
  1200. SHOWTIP = 0x0080,
  1201. XP_MASK = MESSAGE | ICON | STATE | INFO | GUID,
  1202. VISTA_MASK = XP_MASK | REALTIME | SHOWTIP,
  1203. }
  1204. /// <summary>
  1205. /// Shell_NotifyIcon info flags. NIIF_*
  1206. /// </summary>
  1207. internal enum NIIF
  1208. {
  1209. NONE = 0x00000000,
  1210. INFO = 0x00000001,
  1211. WARNING = 0x00000002,
  1212. ERROR = 0x00000003,
  1213. /// <summary>XP SP2 and later.</summary>
  1214. USER = 0x00000004,
  1215. /// <summary>XP and later.</summary>
  1216. NOSOUND = 0x00000010,
  1217. /// <summary>Vista and later.</summary>
  1218. LARGE_ICON = 0x00000020,
  1219. /// <summary>Windows 7 and later</summary>
  1220. NIIF_RESPECT_QUIET_TIME = 0x00000080,
  1221. /// <summary>XP and later. Native version called NIIF_ICON_MASK.</summary>
  1222. XP_ICON_MASK = 0x0000000F,
  1223. }
  1224. /// <summary>
  1225. /// AC_*
  1226. /// </summary>
  1227. internal enum AC : byte
  1228. {
  1229. SRC_OVER = 0,
  1230. SRC_ALPHA = 1,
  1231. }
  1232. internal enum ULW
  1233. {
  1234. ALPHA = 2,
  1235. COLORKEY = 1,
  1236. OPAQUE = 4,
  1237. }
  1238. internal enum WVR
  1239. {
  1240. ALIGNTOP = 0x0010,
  1241. ALIGNLEFT = 0x0020,
  1242. ALIGNBOTTOM = 0x0040,
  1243. ALIGNRIGHT = 0x0080,
  1244. HREDRAW = 0x0100,
  1245. VREDRAW = 0x0200,
  1246. VALIDRECTS = 0x0400,
  1247. REDRAW = HREDRAW | VREDRAW,
  1248. }
  1249. #endregion
  1250. #region SafeHandles
  1251. internal sealed class SafeFindHandle : SafeHandleZeroOrMinusOneIsInvalid
  1252. {
  1253. [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
  1254. private SafeFindHandle() : base(true) { }
  1255. protected override bool ReleaseHandle()
  1256. {
  1257. return NativeMethods.FindClose(handle);
  1258. }
  1259. }
  1260. internal sealed class SafeDC : SafeHandleZeroOrMinusOneIsInvalid
  1261. {
  1262. private static class NativeMethods
  1263. {
  1264. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  1265. [DllImport("user32.dll")]
  1266. public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
  1267. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  1268. [DllImport("user32.dll")]
  1269. public static extern SafeDC GetDC(IntPtr hwnd);
  1270. // Weird legacy function, documentation is unclear about how to use it...
  1271. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  1272. [DllImport("gdi32.dll", CharSet = CharSet.Unicode)]
  1273. public static extern SafeDC CreateDC([MarshalAs(UnmanagedType.LPWStr)] string lpszDriver, [MarshalAs(UnmanagedType.LPWStr)] string lpszDevice, IntPtr lpszOutput, IntPtr lpInitData);
  1274. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  1275. [DllImport("gdi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  1276. public static extern SafeDC CreateCompatibleDC(IntPtr hdc);
  1277. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  1278. [DllImport("gdi32.dll")]
  1279. [return: MarshalAs(UnmanagedType.Bool)]
  1280. public static extern bool DeleteDC(IntPtr hdc);
  1281. }
  1282. private IntPtr? _hwnd;
  1283. private bool _created;
  1284. public IntPtr Hwnd
  1285. {
  1286. set
  1287. {
  1288. Assert.NullableIsNull(_hwnd);
  1289. _hwnd = value;
  1290. }
  1291. }
  1292. private SafeDC() : base(true) { }
  1293. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
  1294. protected override bool ReleaseHandle()
  1295. {
  1296. if (_created)
  1297. {
  1298. return NativeMethods.DeleteDC(handle);
  1299. }
  1300. if (!_hwnd.HasValue || _hwnd.Value == IntPtr.Zero)
  1301. {
  1302. return true;
  1303. }
  1304. return NativeMethods.ReleaseDC(_hwnd.Value, handle) == 1;
  1305. }
  1306. [SuppressMessage("Microsoft.Usage", "CA2201:DoNotRaiseReservedExceptionTypes"), SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  1307. public static SafeDC CreateDC(string deviceName)
  1308. {
  1309. SafeDC dc = null;
  1310. try
  1311. {
  1312. // Should this really be on the driver parameter?
  1313. dc = NativeMethods.CreateDC(deviceName, null, IntPtr.Zero, IntPtr.Zero);
  1314. }
  1315. finally
  1316. {
  1317. if (dc != null)
  1318. {
  1319. dc._created = true;
  1320. }
  1321. }
  1322. if (dc.IsInvalid)
  1323. {
  1324. dc.Dispose();
  1325. throw new SystemException("Unable to create a device context from the specified device information.");
  1326. }
  1327. return dc;
  1328. }
  1329. [SuppressMessage("Microsoft.Usage", "CA2201:DoNotRaiseReservedExceptionTypes"), SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  1330. public static SafeDC CreateCompatibleDC(SafeDC hdc)
  1331. {
  1332. SafeDC dc = null;
  1333. try
  1334. {
  1335. IntPtr hPtr = IntPtr.Zero;
  1336. if (hdc != null)
  1337. {
  1338. hPtr = hdc.handle;
  1339. }
  1340. dc = NativeMethods.CreateCompatibleDC(hPtr);
  1341. if (dc == null)
  1342. {
  1343. HRESULT.ThrowLastError();
  1344. }
  1345. }
  1346. finally
  1347. {
  1348. if (dc != null)
  1349. {
  1350. dc._created = true;
  1351. }
  1352. }
  1353. if (dc.IsInvalid)
  1354. {
  1355. dc.Dispose();
  1356. throw new SystemException("Unable to create a device context from the specified device information.");
  1357. }
  1358. return dc;
  1359. }
  1360. public static SafeDC GetDC(IntPtr hwnd)
  1361. {
  1362. SafeDC dc = null;
  1363. try
  1364. {
  1365. dc = NativeMethods.GetDC(hwnd);
  1366. }
  1367. finally
  1368. {
  1369. if (dc != null)
  1370. {
  1371. dc.Hwnd = hwnd;
  1372. }
  1373. }
  1374. if (dc.IsInvalid)
  1375. {
  1376. // GetDC does not set the last error...
  1377. HRESULT.E_FAIL.ThrowIfFailed();
  1378. }
  1379. return dc;
  1380. }
  1381. public static SafeDC GetDesktop()
  1382. {
  1383. return GetDC(IntPtr.Zero);
  1384. }
  1385. [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
  1386. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  1387. public static SafeDC WrapDC(IntPtr hdc)
  1388. {
  1389. // This won't actually get released by the class, but it allows an IntPtr to be converted for signatures.
  1390. return new SafeDC
  1391. {
  1392. handle = hdc,
  1393. _created = false,
  1394. _hwnd = IntPtr.Zero,
  1395. };
  1396. }
  1397. }
  1398. internal sealed class SafeHBITMAP : SafeHandleZeroOrMinusOneIsInvalid
  1399. {
  1400. private SafeHBITMAP() : base(true) { }
  1401. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
  1402. protected override bool ReleaseHandle()
  1403. {
  1404. return NativeMethods.DeleteObject(handle);
  1405. }
  1406. }
  1407. internal sealed class SafeGdiplusStartupToken : SafeHandleZeroOrMinusOneIsInvalid
  1408. {
  1409. private SafeGdiplusStartupToken() : base(true) { }
  1410. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
  1411. protected override bool ReleaseHandle()
  1412. {
  1413. Status s = NativeMethods.GdiplusShutdown(this.handle);
  1414. return s == Status.Ok;
  1415. }
  1416. [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
  1417. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  1418. [SuppressMessage("Microsoft.Usage", "CA2201:DoNotRaiseReservedExceptionTypes")]
  1419. public static SafeGdiplusStartupToken Startup()
  1420. {
  1421. SafeGdiplusStartupToken safeHandle = new SafeGdiplusStartupToken();
  1422. IntPtr unsafeHandle;
  1423. StartupOutput output;
  1424. Status s = NativeMethods.GdiplusStartup(out unsafeHandle, new StartupInput(), out output);
  1425. if (s == Status.Ok)
  1426. {
  1427. safeHandle.handle = unsafeHandle;
  1428. return safeHandle;
  1429. }
  1430. safeHandle.Dispose();
  1431. throw new Exception("Unable to initialize GDI+");
  1432. }
  1433. }
  1434. internal sealed class SafeConnectionPointCookie : SafeHandleZeroOrMinusOneIsInvalid
  1435. {
  1436. private IConnectionPoint _cp;
  1437. // handle holds the cookie value.
  1438. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  1439. [SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "IConnectionPoint")]
  1440. public SafeConnectionPointCookie(IConnectionPointContainer target, object sink, Guid eventId)
  1441. : base(true)
  1442. {
  1443. Verify.IsNotNull(target, "target");
  1444. Verify.IsNotNull(sink, "sink");
  1445. Verify.IsNotDefault(eventId, "eventId");
  1446. handle = IntPtr.Zero;
  1447. IConnectionPoint cp = null;
  1448. try
  1449. {
  1450. int dwCookie;
  1451. target.FindConnectionPoint(ref eventId, out cp);
  1452. cp.Advise(sink, out dwCookie);
  1453. if (dwCookie == 0)
  1454. {
  1455. throw new InvalidOperationException("IConnectionPoint::Advise returned an invalid cookie.");
  1456. }
  1457. handle = new IntPtr(dwCookie);
  1458. _cp = cp;
  1459. cp = null;
  1460. }
  1461. finally
  1462. {
  1463. Utility.SafeRelease(ref cp);
  1464. }
  1465. }
  1466. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  1467. public void Disconnect()
  1468. {
  1469. ReleaseHandle();
  1470. }
  1471. [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
  1472. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
  1473. protected override bool ReleaseHandle()
  1474. {
  1475. try
  1476. {
  1477. if (!this.IsInvalid)
  1478. {
  1479. int dwCookie = handle.ToInt32();
  1480. handle = IntPtr.Zero;
  1481. Assert.IsNotNull(_cp);
  1482. try
  1483. {
  1484. _cp.Unadvise(dwCookie);
  1485. }
  1486. finally
  1487. {
  1488. Utility.SafeRelease(ref _cp);
  1489. }
  1490. }
  1491. return true;
  1492. }
  1493. catch
  1494. {
  1495. return false;
  1496. }
  1497. }
  1498. }
  1499. #endregion
  1500. #region Native Types
  1501. [StructLayout(LayoutKind.Sequential)]
  1502. internal struct BLENDFUNCTION
  1503. {
  1504. // Must be AC_SRC_OVER
  1505. public AC BlendOp;
  1506. // Must be 0.
  1507. public byte BlendFlags;
  1508. // Alpha transparency between 0 (transparent) - 255 (opaque)
  1509. public byte SourceConstantAlpha;
  1510. // Must be AC_SRC_ALPHA
  1511. public AC AlphaFormat;
  1512. }
  1513. [StructLayout(LayoutKind.Sequential)]
  1514. internal struct HIGHCONTRAST
  1515. {
  1516. public int cbSize;
  1517. public HCF dwFlags;
  1518. //[MarshalAs(UnmanagedType.LPWStr, SizeConst=80)]
  1519. //public String lpszDefaultScheme;
  1520. public IntPtr lpszDefaultScheme;
  1521. }
  1522. [StructLayout(LayoutKind.Sequential)]
  1523. internal struct RGBQUAD
  1524. {
  1525. public byte rgbBlue;
  1526. public byte rgbGreen;
  1527. public byte rgbRed;
  1528. public byte rgbReserved;
  1529. }
  1530. [StructLayout(LayoutKind.Sequential, Pack = 2)]
  1531. internal struct BITMAPINFOHEADER
  1532. {
  1533. public int biSize;
  1534. public int biWidth;
  1535. public int biHeight;
  1536. public short biPlanes;
  1537. public short biBitCount;
  1538. public BI biCompression;
  1539. public int biSizeImage;
  1540. public int biXPelsPerMeter;
  1541. public int biYPelsPerMeter;
  1542. public int biClrUsed;
  1543. public int biClrImportant;
  1544. }
  1545. [StructLayout(LayoutKind.Sequential)]
  1546. internal struct BITMAPINFO
  1547. {
  1548. public BITMAPINFOHEADER bmiHeader;
  1549. public RGBQUAD bmiColors;
  1550. }
  1551. // Win7 only.
  1552. [StructLayout(LayoutKind.Sequential)]
  1553. internal struct CHANGEFILTERSTRUCT
  1554. {
  1555. public uint cbSize;
  1556. public MSGFLTINFO ExtStatus;
  1557. }
  1558. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  1559. internal struct CREATESTRUCT
  1560. {
  1561. public IntPtr lpCreateParams;
  1562. public IntPtr hInstance;
  1563. public IntPtr hMenu;
  1564. public IntPtr hwndParent;
  1565. public int cy;
  1566. public int cx;
  1567. public int y;
  1568. public int x;
  1569. public WS style;
  1570. [MarshalAs(UnmanagedType.LPWStr)]
  1571. public string lpszName;
  1572. [MarshalAs(UnmanagedType.LPWStr)]
  1573. public string lpszClass;
  1574. public WS_EX dwExStyle;
  1575. }
  1576. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1)]
  1577. internal struct SHFILEOPSTRUCT
  1578. {
  1579. public IntPtr hwnd;
  1580. [MarshalAs(UnmanagedType.U4)]
  1581. public FO wFunc;
  1582. // double-null terminated arrays of LPWSTRS
  1583. public string pFrom;
  1584. public string pTo;
  1585. [MarshalAs(UnmanagedType.U2)]
  1586. public FOF fFlags;
  1587. [MarshalAs(UnmanagedType.Bool)]
  1588. public int fAnyOperationsAborted;
  1589. public IntPtr hNameMappings;
  1590. public string lpszProgressTitle;
  1591. }
  1592. [StructLayout(LayoutKind.Sequential)]
  1593. internal struct TITLEBARINFO
  1594. {
  1595. public int cbSize;
  1596. public RECT rcTitleBar;
  1597. public STATE_SYSTEM rgstate_TitleBar;
  1598. public STATE_SYSTEM rgstate_Reserved;
  1599. public STATE_SYSTEM rgstate_MinimizeButton;
  1600. public STATE_SYSTEM rgstate_MaximizeButton;
  1601. public STATE_SYSTEM rgstate_HelpButton;
  1602. public STATE_SYSTEM rgstate_CloseButton;
  1603. }
  1604. // New to Vista.
  1605. [StructLayout(LayoutKind.Sequential)]
  1606. internal struct TITLEBARINFOEX
  1607. {
  1608. public int cbSize;
  1609. public RECT rcTitleBar;
  1610. public STATE_SYSTEM rgstate_TitleBar;
  1611. public STATE_SYSTEM rgstate_Reserved;
  1612. public STATE_SYSTEM rgstate_MinimizeButton;
  1613. public STATE_SYSTEM rgstate_MaximizeButton;
  1614. public STATE_SYSTEM rgstate_HelpButton;
  1615. public STATE_SYSTEM rgstate_CloseButton;
  1616. public RECT rgrect_TitleBar;
  1617. public RECT rgrect_Reserved;
  1618. public RECT rgrect_MinimizeButton;
  1619. public RECT rgrect_MaximizeButton;
  1620. public RECT rgrect_HelpButton;
  1621. public RECT rgrect_CloseButton;
  1622. }
  1623. [SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses")]
  1624. [StructLayout(LayoutKind.Sequential)]
  1625. internal class NOTIFYICONDATA
  1626. {
  1627. public int cbSize;
  1628. public IntPtr hWnd;
  1629. public int uID;
  1630. public NIF uFlags;
  1631. public int uCallbackMessage;
  1632. public IntPtr hIcon;
  1633. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]
  1634. public char[] szTip = new char[128];
  1635. /// <summary>
  1636. /// The state of the icon. There are two flags that can be set independently.
  1637. /// NIS_HIDDEN = 1. The icon is hidden.
  1638. /// NIS_SHAREDICON = 2. The icon is shared.
  1639. /// </summary>
  1640. public uint dwState;
  1641. public uint dwStateMask;
  1642. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
  1643. public char[] szInfo = new char[256];
  1644. // Prior to Vista this was a union of uTimeout and uVersion. As of Vista, uTimeout has been deprecated.
  1645. public uint uVersion; // Used with Shell_NotifyIcon flag NIM_SETVERSION.
  1646. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
  1647. public char[] szInfoTitle = new char[64];
  1648. public uint dwInfoFlags;
  1649. public Guid guidItem;
  1650. // Vista only
  1651. IntPtr hBalloonIcon;
  1652. }
  1653. [SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses")]
  1654. [StructLayout(LayoutKind.Explicit)]
  1655. internal class PROPVARIANT : IDisposable
  1656. {
  1657. private static class NativeMethods
  1658. {
  1659. [DllImport("ole32.dll")]
  1660. internal static extern HRESULT PropVariantClear(PROPVARIANT pvar);
  1661. }
  1662. [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
  1663. [FieldOffset(0)]
  1664. private ushort vt;
  1665. [SuppressMessage("Microsoft.Reliability", "CA2006:UseSafeHandleToEncapsulateNativeResources")]
  1666. [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
  1667. [FieldOffset(8)]
  1668. private IntPtr pointerVal;
  1669. [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
  1670. [FieldOffset(8)]
  1671. private byte byteVal;
  1672. [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
  1673. [FieldOffset(8)]
  1674. private long longVal;
  1675. [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
  1676. [FieldOffset(8)]
  1677. private short boolVal;
  1678. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  1679. public VarEnum VarType
  1680. {
  1681. get { return (VarEnum)vt; }
  1682. }
  1683. // Right now only using this for strings.
  1684. [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
  1685. public string GetValue()
  1686. {
  1687. if (vt == (ushort)VarEnum.VT_LPWSTR)
  1688. {
  1689. return Marshal.PtrToStringUni(pointerVal);
  1690. }
  1691. return null;
  1692. }
  1693. public void SetValue(bool f)
  1694. {
  1695. Clear();
  1696. vt = (ushort)VarEnum.VT_BOOL;
  1697. boolVal = (short)(f ? -1 : 0);
  1698. }
  1699. [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
  1700. public void SetValue(string val)
  1701. {
  1702. Clear();
  1703. vt = (ushort)VarEnum.VT_LPWSTR;
  1704. pointerVal = Marshal.StringToCoTaskMemUni(val);
  1705. }
  1706. public void Clear()
  1707. {
  1708. HRESULT hr = NativeMethods.PropVariantClear(this);
  1709. Assert.IsTrue(hr.Succeeded);
  1710. }
  1711. #region IDisposable Pattern
  1712. public void Dispose()
  1713. {
  1714. Dispose(true);
  1715. GC.SuppressFinalize(this);
  1716. }
  1717. ~PROPVARIANT()
  1718. {
  1719. Dispose(false);
  1720. }
  1721. [SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "disposing")]
  1722. private void Dispose(bool disposing)
  1723. {
  1724. Clear();
  1725. }
  1726. #endregion
  1727. }
  1728. [SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses")]
  1729. [StructLayout(LayoutKind.Sequential, Pack = 4)]
  1730. internal class SHARDAPPIDINFO
  1731. {
  1732. [MarshalAs(UnmanagedType.Interface)]
  1733. object psi; // The namespace location of the the item that should be added to the recent docs folder.
  1734. [MarshalAs(UnmanagedType.LPWStr)]
  1735. string pszAppID; // The id of the application that should be associated with this recent doc.
  1736. }
  1737. [SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses")]
  1738. [StructLayout(LayoutKind.Sequential, Pack = 4)]
  1739. internal class SHARDAPPIDINFOIDLIST
  1740. {
  1741. /// <summary>The idlist for the shell item that should be added to the recent docs folder.</summary>
  1742. IntPtr pidl;
  1743. /// <summary>The id of the application that should be associated with this recent doc.</summary>
  1744. [MarshalAs(UnmanagedType.LPWStr)]
  1745. string pszAppID;
  1746. }
  1747. [SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses")]
  1748. [StructLayout(LayoutKind.Sequential, Pack = 4)]
  1749. internal class SHARDAPPIDINFOLINK
  1750. {
  1751. IntPtr psl; // An IShellLink instance that when launched opens a recently used item in the specified
  1752. // application. This link is not added to the recent docs folder, but will be added to the
  1753. // specified application's destination list.
  1754. [MarshalAs(UnmanagedType.LPWStr)]
  1755. string pszAppID; // The id of the application that should be associated with this recent doc.
  1756. }
  1757. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  1758. internal struct LOGFONT
  1759. {
  1760. public int lfHeight;
  1761. public int lfWidth;
  1762. public int lfEscapement;
  1763. public int lfOrientation;
  1764. public int lfWeight;
  1765. public byte lfItalic;
  1766. public byte lfUnderline;
  1767. public byte lfStrikeOut;
  1768. public byte lfCharSet;
  1769. public byte lfOutPrecision;
  1770. public byte lfClipPrecision;
  1771. public byte lfQuality;
  1772. public byte lfPitchAndFamily;
  1773. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
  1774. public string lfFaceName;
  1775. }
  1776. [StructLayout(LayoutKind.Sequential)]
  1777. internal struct MINMAXINFO
  1778. {
  1779. public POINT ptReserved;
  1780. public POINT ptMaxSize;
  1781. public POINT ptMaxPosition;
  1782. public POINT ptMinTrackSize;
  1783. public POINT ptMaxTrackSize;
  1784. }
  1785. [StructLayout(LayoutKind.Sequential)]
  1786. internal struct NONCLIENTMETRICS
  1787. {
  1788. public int cbSize;
  1789. public int iBorderWidth;
  1790. public int iScrollWidth;
  1791. public int iScrollHeight;
  1792. public int iCaptionWidth;
  1793. public int iCaptionHeight;
  1794. public LOGFONT lfCaptionFont;
  1795. public int iSmCaptionWidth;
  1796. public int iSmCaptionHeight;
  1797. public LOGFONT lfSmCaptionFont;
  1798. public int iMenuWidth;
  1799. public int iMenuHeight;
  1800. public LOGFONT lfMenuFont;
  1801. public LOGFONT lfStatusFont;
  1802. public LOGFONT lfMessageFont;
  1803. // Vista only
  1804. public int iPaddedBorderWidth;
  1805. [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
  1806. public static NONCLIENTMETRICS VistaMetricsStruct
  1807. {
  1808. get
  1809. {
  1810. var ncm = new NONCLIENTMETRICS();
  1811. ncm.cbSize = Marshal.SizeOf(typeof(NONCLIENTMETRICS));
  1812. return ncm;
  1813. }
  1814. }
  1815. [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
  1816. public static NONCLIENTMETRICS XPMetricsStruct
  1817. {
  1818. get
  1819. {
  1820. var ncm = new NONCLIENTMETRICS();
  1821. // Account for the missing iPaddedBorderWidth
  1822. ncm.cbSize = Marshal.SizeOf(typeof(NONCLIENTMETRICS)) - sizeof(int);
  1823. return ncm;
  1824. }
  1825. }
  1826. }
  1827. /// <summary>Defines options that are used to set window visual style attributes.</summary>
  1828. [StructLayout(LayoutKind.Explicit)]
  1829. internal struct WTA_OPTIONS
  1830. {
  1831. // public static readonly uint Size = (uint)Marshal.SizeOf(typeof(WTA_OPTIONS));
  1832. public const uint Size = 8;
  1833. /// <summary>
  1834. /// A combination of flags that modify window visual style attributes.
  1835. /// Can be a combination of the WTNCA constants.
  1836. /// </summary>
  1837. [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields", Justification = "Used by native code.")]
  1838. [FieldOffset(0)]
  1839. public WTNCA dwFlags;
  1840. /// <summary>
  1841. /// A bitmask that describes how the values specified in dwFlags should be applied.
  1842. /// If the bit corresponding to a value in dwFlags is 0, that flag will be removed.
  1843. /// If the bit is 1, the flag will be added.
  1844. /// </summary>
  1845. [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields", Justification = "Used by native code.")]
  1846. [FieldOffset(4)]
  1847. public WTNCA dwMask;
  1848. }
  1849. [StructLayout(LayoutKind.Sequential)]
  1850. internal struct MARGINS
  1851. {
  1852. /// <summary>Width of left border that retains its size.</summary>
  1853. public int cxLeftWidth;
  1854. /// <summary>Width of right border that retains its size.</summary>
  1855. public int cxRightWidth;
  1856. /// <summary>Height of top border that retains its size.</summary>
  1857. public int cyTopHeight;
  1858. /// <summary>Height of bottom border that retains its size.</summary>
  1859. public int cyBottomHeight;
  1860. };
  1861. [StructLayout(LayoutKind.Sequential)]
  1862. internal class MONITORINFO
  1863. {
  1864. public int cbSize = Marshal.SizeOf(typeof(MONITORINFO));
  1865. public RECT rcMonitor;
  1866. public RECT rcWork;
  1867. public int dwFlags;
  1868. }
  1869. [StructLayout(LayoutKind.Sequential)]
  1870. internal struct POINT
  1871. {
  1872. public int x;
  1873. public int y;
  1874. }
  1875. [SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses")]
  1876. [StructLayout(LayoutKind.Sequential)]
  1877. internal class RefPOINT
  1878. {
  1879. public int x;
  1880. public int y;
  1881. }
  1882. [StructLayout(LayoutKind.Sequential)]
  1883. internal struct RECT
  1884. {
  1885. private int _left;
  1886. private int _top;
  1887. private int _right;
  1888. private int _bottom;
  1889. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  1890. public void Offset(int dx, int dy)
  1891. {
  1892. _left += dx;
  1893. _top += dy;
  1894. _right += dx;
  1895. _bottom += dy;
  1896. }
  1897. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  1898. public int Left
  1899. {
  1900. get { return _left; }
  1901. set { _left = value; }
  1902. }
  1903. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  1904. public int Right
  1905. {
  1906. get { return _right; }
  1907. set { _right = value; }
  1908. }
  1909. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  1910. public int Top
  1911. {
  1912. get { return _top; }
  1913. set { _top = value; }
  1914. }
  1915. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  1916. public int Bottom
  1917. {
  1918. get { return _bottom; }
  1919. set { _bottom = value; }
  1920. }
  1921. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  1922. public int Width
  1923. {
  1924. get { return _right - _left; }
  1925. }
  1926. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  1927. public int Height
  1928. {
  1929. get { return _bottom - _top; }
  1930. }
  1931. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  1932. public POINT Position
  1933. {
  1934. get { return new POINT { x = _left, y = _top }; }
  1935. }
  1936. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  1937. public SIZE Size
  1938. {
  1939. get { return new SIZE { cx = Width, cy = Height }; }
  1940. }
  1941. public static RECT Union(RECT rect1, RECT rect2)
  1942. {
  1943. return new RECT
  1944. {
  1945. Left = Math.Min(rect1.Left, rect2.Left),
  1946. Top = Math.Min(rect1.Top, rect2.Top),
  1947. Right = Math.Max(rect1.Right, rect2.Right),
  1948. Bottom = Math.Max(rect1.Bottom, rect2.Bottom),
  1949. };
  1950. }
  1951. public override bool Equals(object obj)
  1952. {
  1953. try
  1954. {
  1955. var rc = (RECT)obj;
  1956. return rc._bottom == _bottom
  1957. && rc._left == _left
  1958. && rc._right == _right
  1959. && rc._top == _top;
  1960. }
  1961. catch (InvalidCastException)
  1962. {
  1963. return false;
  1964. }
  1965. }
  1966. public override int GetHashCode()
  1967. {
  1968. return (_left << 16 | Utility.LOWORD(_right)) ^ (_top << 16 | Utility.LOWORD(_bottom));
  1969. }
  1970. }
  1971. [SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses")]
  1972. [StructLayout(LayoutKind.Sequential)]
  1973. internal class RefRECT
  1974. {
  1975. private int _left;
  1976. private int _top;
  1977. private int _right;
  1978. private int _bottom;
  1979. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  1980. public RefRECT(int left, int top, int right, int bottom)
  1981. {
  1982. _left = left;
  1983. _top = top;
  1984. _right = right;
  1985. _bottom = bottom;
  1986. }
  1987. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  1988. public int Width
  1989. {
  1990. get { return _right - _left; }
  1991. }
  1992. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  1993. public int Height
  1994. {
  1995. get { return _bottom - _top; }
  1996. }
  1997. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  1998. public int Left
  1999. {
  2000. get { return _left; }
  2001. set { _left = value; }
  2002. }
  2003. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2004. public int Right
  2005. {
  2006. get { return _right; }
  2007. set { _right = value; }
  2008. }
  2009. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2010. public int Top
  2011. {
  2012. get { return _top; }
  2013. set { _top = value; }
  2014. }
  2015. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2016. public int Bottom
  2017. {
  2018. get { return _bottom; }
  2019. set { _bottom = value; }
  2020. }
  2021. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2022. public void Offset(int dx, int dy)
  2023. {
  2024. _left += dx;
  2025. _top += dy;
  2026. _right += dx;
  2027. _bottom += dy;
  2028. }
  2029. }
  2030. [StructLayout(LayoutKind.Sequential)]
  2031. internal struct SIZE
  2032. {
  2033. public int cx;
  2034. public int cy;
  2035. }
  2036. [StructLayout(LayoutKind.Sequential)]
  2037. internal struct StartupOutput
  2038. {
  2039. public IntPtr hook;
  2040. public IntPtr unhook;
  2041. }
  2042. [StructLayout(LayoutKind.Sequential)]
  2043. internal class StartupInput
  2044. {
  2045. public int GdiplusVersion = 1;
  2046. public IntPtr DebugEventCallback;
  2047. public bool SuppressBackgroundThread;
  2048. public bool SuppressExternalCodecs;
  2049. }
  2050. [SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses")]
  2051. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  2052. [BestFitMapping(false)]
  2053. internal class WIN32_FIND_DATAW
  2054. {
  2055. public FileAttributes dwFileAttributes;
  2056. public System.Runtime.InteropServices.ComTypes.FILETIME ftCreationTime;
  2057. public System.Runtime.InteropServices.ComTypes.FILETIME ftLastAccessTime;
  2058. public System.Runtime.InteropServices.ComTypes.FILETIME ftLastWriteTime;
  2059. public int nFileSizeHigh;
  2060. public int nFileSizeLow;
  2061. public int dwReserved0;
  2062. public int dwReserved1;
  2063. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
  2064. public string cFileName;
  2065. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
  2066. public string cAlternateFileName;
  2067. }
  2068. [StructLayout(LayoutKind.Sequential)]
  2069. internal class WINDOWPLACEMENT
  2070. {
  2071. public int length = Marshal.SizeOf(typeof(WINDOWPLACEMENT));
  2072. public int flags;
  2073. public SW showCmd;
  2074. public POINT ptMinPosition;
  2075. public POINT ptMaxPosition;
  2076. public RECT rcNormalPosition;
  2077. }
  2078. [StructLayout(LayoutKind.Sequential)]
  2079. internal struct WINDOWPOS
  2080. {
  2081. public IntPtr hwnd;
  2082. public IntPtr hwndInsertAfter;
  2083. public int x;
  2084. public int y;
  2085. public int cx;
  2086. public int cy;
  2087. public int flags;
  2088. }
  2089. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  2090. internal struct WNDCLASSEX
  2091. {
  2092. public int cbSize;
  2093. public CS style;
  2094. public WndProc lpfnWndProc;
  2095. public int cbClsExtra;
  2096. public int cbWndExtra;
  2097. public IntPtr hInstance;
  2098. public IntPtr hIcon;
  2099. public IntPtr hCursor;
  2100. public IntPtr hbrBackground;
  2101. [MarshalAs(UnmanagedType.LPWStr)]
  2102. public string lpszMenuName;
  2103. [MarshalAs(UnmanagedType.LPWStr)]
  2104. public string lpszClassName;
  2105. public IntPtr hIconSm;
  2106. }
  2107. [StructLayout(LayoutKind.Sequential)]
  2108. internal struct MOUSEINPUT
  2109. {
  2110. public int dx;
  2111. public int dy;
  2112. public int mouseData;
  2113. public int dwFlags;
  2114. public int time;
  2115. public IntPtr dwExtraInfo;
  2116. }
  2117. [StructLayout(LayoutKind.Sequential)]
  2118. internal struct INPUT
  2119. {
  2120. public uint type;
  2121. public MOUSEINPUT mi;
  2122. };
  2123. [StructLayout(LayoutKind.Sequential)]
  2124. internal struct UNSIGNED_RATIO
  2125. {
  2126. public uint uiNumerator;
  2127. public uint uiDenominator;
  2128. }
  2129. [StructLayout(LayoutKind.Sequential, Pack=1)]
  2130. internal struct DWM_TIMING_INFO
  2131. {
  2132. public int cbSize;
  2133. public UNSIGNED_RATIO rateRefresh;
  2134. public ulong qpcRefreshPeriod;
  2135. public UNSIGNED_RATIO rateCompose;
  2136. public ulong qpcVBlank;
  2137. public ulong cRefresh;
  2138. public uint cDXRefresh;
  2139. public ulong qpcCompose;
  2140. public ulong cFrame;
  2141. public uint cDXPresent;
  2142. public ulong cRefreshFrame;
  2143. public ulong cFrameSubmitted;
  2144. public uint cDXPresentSubmitted;
  2145. public ulong cFrameConfirmed;
  2146. public uint cDXPresentConfirmed;
  2147. public ulong cRefreshConfirmed;
  2148. public uint cDXRefreshConfirmed;
  2149. public ulong cFramesLate;
  2150. public uint cFramesOutstanding;
  2151. public ulong cFrameDisplayed;
  2152. public ulong qpcFrameDisplayed;
  2153. public ulong cRefreshFrameDisplayed;
  2154. public ulong cFrameComplete;
  2155. public ulong qpcFrameComplete;
  2156. public ulong cFramePending;
  2157. public ulong qpcFramePending;
  2158. public ulong cFramesDisplayed;
  2159. public ulong cFramesComplete;
  2160. public ulong cFramesPending;
  2161. public ulong cFramesAvailable;
  2162. public ulong cFramesDropped;
  2163. public ulong cFramesMissed;
  2164. public ulong cRefreshNextDisplayed;
  2165. public ulong cRefreshNextPresented;
  2166. public ulong cRefreshesDisplayed;
  2167. public ulong cRefreshesPresented;
  2168. public ulong cRefreshStarted;
  2169. public ulong cPixelsReceived;
  2170. public ulong cPixelsDrawn;
  2171. public ulong cBuffersEmpty;
  2172. }
  2173. #endregion
  2174. /// <summary>Delegate declaration that matches native WndProc signatures.</summary>
  2175. internal delegate IntPtr WndProc(IntPtr hwnd, WM uMsg, IntPtr wParam, IntPtr lParam);
  2176. /// <summary>Delegate declaration that matches native WndProc signatures.</summary>
  2177. internal delegate IntPtr WndProcHook(IntPtr hwnd, WM uMsg, IntPtr wParam, IntPtr lParam, ref bool handled);
  2178. /// <summary>Delegate declaration that matches managed WndProc signatures.</summary>
  2179. internal delegate IntPtr MessageHandler(WM uMsg, IntPtr wParam, IntPtr lParam, out bool handled);
  2180. // Some native methods are shimmed through public versions that handle converting failures into thrown exceptions.
  2181. internal static class NativeMethods
  2182. {
  2183. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2184. [DllImport("user32.dll", EntryPoint = "AdjustWindowRectEx", SetLastError = true)]
  2185. [return: MarshalAs(UnmanagedType.Bool)]
  2186. private static extern bool _AdjustWindowRectEx(ref RECT lpRect, WS dwStyle, [MarshalAs(UnmanagedType.Bool)] bool bMenu, WS_EX dwExStyle);
  2187. public static RECT AdjustWindowRectEx(RECT lpRect, WS dwStyle, bool bMenu, WS_EX dwExStyle)
  2188. {
  2189. // Native version modifies the parameter in place.
  2190. if (!_AdjustWindowRectEx(ref lpRect, dwStyle, bMenu, dwExStyle))
  2191. {
  2192. HRESULT.ThrowLastError();
  2193. }
  2194. return lpRect;
  2195. }
  2196. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2197. [DllImport("user32.dll", EntryPoint = "ChangeWindowMessageFilter", SetLastError = true)]
  2198. [return: MarshalAs(UnmanagedType.Bool)]
  2199. private static extern bool _ChangeWindowMessageFilter(WM message, MSGFLT dwFlag);
  2200. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2201. [DllImport("user32.dll", EntryPoint = "ChangeWindowMessageFilterEx", SetLastError = true)]
  2202. [return: MarshalAs(UnmanagedType.Bool)]
  2203. private static extern bool _ChangeWindowMessageFilterEx(IntPtr hwnd, WM message, MSGFLT action, [In, Out, Optional] ref CHANGEFILTERSTRUCT pChangeFilterStruct);
  2204. // Note that processes at or below SECURITY_MANDATORY_LOW_RID are not allowed to change the message filter.
  2205. // If those processes call this function, it will fail and generate the extended error code, ERROR_ACCESS_DENIED.
  2206. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2207. public static HRESULT ChangeWindowMessageFilterEx(IntPtr hwnd, WM message, MSGFLT action, out MSGFLTINFO filterInfo)
  2208. {
  2209. filterInfo = MSGFLTINFO.NONE;
  2210. bool ret;
  2211. // This origins of this API were added for Vista. The Ex version was added for Windows 7.
  2212. // If we're not on either, then this message filter isolation doesn't exist.
  2213. if (!Utility.IsOSVistaOrNewer)
  2214. {
  2215. return HRESULT.S_FALSE;
  2216. }
  2217. // If we're on Vista rather than Win7 then we can't use the Ex version of this function.
  2218. // The Ex version is preferred if possible because this results in process-wide modifications of the filter
  2219. // and is deprecated as of Win7.
  2220. if (!Utility.IsOSWindows7OrNewer)
  2221. {
  2222. // Note that the Win7 MSGFLT_ALLOW/DISALLOW enum values map to the Vista MSGFLT_ADD/REMOVE
  2223. ret = _ChangeWindowMessageFilter(message, action);
  2224. if (!ret)
  2225. {
  2226. return (HRESULT)Win32Error.GetLastError();
  2227. }
  2228. return HRESULT.S_OK;
  2229. }
  2230. var filterstruct = new CHANGEFILTERSTRUCT { cbSize = (uint)Marshal.SizeOf(typeof(CHANGEFILTERSTRUCT)) };
  2231. ret = _ChangeWindowMessageFilterEx(hwnd, message, action, ref filterstruct);
  2232. if (!ret)
  2233. {
  2234. return (HRESULT)Win32Error.GetLastError();
  2235. }
  2236. filterInfo = filterstruct.ExtStatus;
  2237. return HRESULT.S_OK;
  2238. }
  2239. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2240. [DllImport("gdi32.dll")]
  2241. public static extern CombineRgnResult CombineRgn(IntPtr hrgnDest, IntPtr hrgnSrc1, IntPtr hrgnSrc2, RGN fnCombineMode);
  2242. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2243. [DllImport("shell32.dll", EntryPoint = "CommandLineToArgvW", CharSet = CharSet.Unicode)]
  2244. private static extern IntPtr _CommandLineToArgvW([MarshalAs(UnmanagedType.LPWStr)] string cmdLine, out int numArgs);
  2245. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2246. public static string[] CommandLineToArgvW(string cmdLine)
  2247. {
  2248. IntPtr argv = IntPtr.Zero;
  2249. try
  2250. {
  2251. int numArgs = 0;
  2252. argv = _CommandLineToArgvW(cmdLine, out numArgs);
  2253. if (argv == IntPtr.Zero)
  2254. {
  2255. throw new Win32Exception();
  2256. }
  2257. var result = new string[numArgs];
  2258. for (int i = 0; i < numArgs; i++)
  2259. {
  2260. IntPtr currArg = Marshal.ReadIntPtr(argv, i * Marshal.SizeOf(typeof(IntPtr)));
  2261. result[i] = Marshal.PtrToStringUni(currArg);
  2262. }
  2263. return result;
  2264. }
  2265. finally
  2266. {
  2267. IntPtr p = _LocalFree(argv);
  2268. // Otherwise LocalFree failed.
  2269. Assert.AreEqual(IntPtr.Zero, p);
  2270. }
  2271. }
  2272. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2273. [DllImport("gdi32.dll", EntryPoint = "CreateDIBSection", SetLastError = true)]
  2274. private static extern SafeHBITMAP _CreateDIBSection(SafeDC hdc, [In] ref BITMAPINFO bitmapInfo, int iUsage, [Out] out IntPtr ppvBits, IntPtr hSection, int dwOffset);
  2275. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2276. [DllImport("gdi32.dll", EntryPoint = "CreateDIBSection", SetLastError = true)]
  2277. private static extern SafeHBITMAP _CreateDIBSectionIntPtr(IntPtr hdc, [In] ref BITMAPINFO bitmapInfo, int iUsage, [Out] out IntPtr ppvBits, IntPtr hSection, int dwOffset);
  2278. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2279. public static SafeHBITMAP CreateDIBSection(SafeDC hdc, ref BITMAPINFO bitmapInfo, out IntPtr ppvBits, IntPtr hSection, int dwOffset)
  2280. {
  2281. const int DIB_RGB_COLORS = 0;
  2282. SafeHBITMAP hBitmap = null;
  2283. if (hdc == null)
  2284. {
  2285. hBitmap = _CreateDIBSectionIntPtr(IntPtr.Zero, ref bitmapInfo, DIB_RGB_COLORS, out ppvBits, hSection, dwOffset);
  2286. }
  2287. else
  2288. {
  2289. hBitmap = _CreateDIBSection(hdc, ref bitmapInfo, DIB_RGB_COLORS, out ppvBits, hSection, dwOffset);
  2290. }
  2291. if (hBitmap.IsInvalid)
  2292. {
  2293. HRESULT.ThrowLastError();
  2294. }
  2295. return hBitmap;
  2296. }
  2297. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2298. [DllImport("gdi32.dll", EntryPoint = "CreateRoundRectRgn", SetLastError = true)]
  2299. private static extern IntPtr _CreateRoundRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidthEllipse, int nHeightEllipse);
  2300. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2301. public static IntPtr CreateRoundRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidthEllipse, int nHeightEllipse)
  2302. {
  2303. IntPtr ret = _CreateRoundRectRgn(nLeftRect, nTopRect, nRightRect, nBottomRect, nWidthEllipse, nHeightEllipse);
  2304. if (IntPtr.Zero == ret)
  2305. {
  2306. throw new Win32Exception();
  2307. }
  2308. return ret;
  2309. }
  2310. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2311. [DllImport("gdi32.dll", EntryPoint = "CreateRectRgn", SetLastError = true)]
  2312. private static extern IntPtr _CreateRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);
  2313. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2314. public static IntPtr CreateRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect)
  2315. {
  2316. IntPtr ret = _CreateRectRgn(nLeftRect, nTopRect, nRightRect, nBottomRect);
  2317. if (IntPtr.Zero == ret)
  2318. {
  2319. throw new Win32Exception();
  2320. }
  2321. return ret;
  2322. }
  2323. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2324. [DllImport("gdi32.dll", EntryPoint = "CreateRectRgnIndirect", SetLastError = true)]
  2325. private static extern IntPtr _CreateRectRgnIndirect([In] ref RECT lprc);
  2326. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2327. public static IntPtr CreateRectRgnIndirect(RECT lprc)
  2328. {
  2329. IntPtr ret = _CreateRectRgnIndirect(ref lprc);
  2330. if (IntPtr.Zero == ret)
  2331. {
  2332. throw new Win32Exception();
  2333. }
  2334. return ret;
  2335. }
  2336. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2337. [DllImport("gdi32.dll")]
  2338. public static extern IntPtr CreateSolidBrush(int crColor);
  2339. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2340. [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "CreateWindowExW")]
  2341. private static extern IntPtr _CreateWindowEx(
  2342. WS_EX dwExStyle,
  2343. [MarshalAs(UnmanagedType.LPWStr)] string lpClassName,
  2344. [MarshalAs(UnmanagedType.LPWStr)] string lpWindowName,
  2345. WS dwStyle,
  2346. int x,
  2347. int y,
  2348. int nWidth,
  2349. int nHeight,
  2350. IntPtr hWndParent,
  2351. IntPtr hMenu,
  2352. IntPtr hInstance,
  2353. IntPtr lpParam);
  2354. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2355. public static IntPtr CreateWindowEx(
  2356. WS_EX dwExStyle,
  2357. string lpClassName,
  2358. string lpWindowName,
  2359. WS dwStyle,
  2360. int x,
  2361. int y,
  2362. int nWidth,
  2363. int nHeight,
  2364. IntPtr hWndParent,
  2365. IntPtr hMenu,
  2366. IntPtr hInstance,
  2367. IntPtr lpParam)
  2368. {
  2369. IntPtr ret = _CreateWindowEx(dwExStyle, lpClassName, lpWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam);
  2370. if (IntPtr.Zero == ret)
  2371. {
  2372. HRESULT.ThrowLastError();
  2373. }
  2374. return ret;
  2375. }
  2376. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2377. [DllImport("user32.dll", CharSet = CharSet.Unicode, EntryPoint = "DefWindowProcW")]
  2378. public static extern IntPtr DefWindowProc(IntPtr hWnd, WM Msg, IntPtr wParam, IntPtr lParam);
  2379. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2380. [DllImport("gdi32.dll")]
  2381. [return: MarshalAs(UnmanagedType.Bool)]
  2382. public static extern bool DeleteObject(IntPtr hObject);
  2383. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2384. [DllImport("user32.dll")]
  2385. [return: MarshalAs(UnmanagedType.Bool)]
  2386. public static extern bool DestroyIcon(IntPtr handle);
  2387. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2388. [DllImport("user32.dll", SetLastError = true)]
  2389. [return: MarshalAs(UnmanagedType.Bool)]
  2390. public static extern bool DestroyWindow(IntPtr hwnd);
  2391. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2392. [DllImport("user32.dll")]
  2393. [return: MarshalAs(UnmanagedType.Bool)]
  2394. public static extern bool IsWindow(IntPtr hwnd);
  2395. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2396. [DllImport("dwmapi.dll", PreserveSig = false)]
  2397. public static extern void DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS pMarInset);
  2398. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2399. [DllImport("dwmapi.dll", EntryPoint = "DwmIsCompositionEnabled", PreserveSig = false)]
  2400. [return: MarshalAs(UnmanagedType.Bool)]
  2401. private static extern bool _DwmIsCompositionEnabled();
  2402. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2403. [DllImport("dwmapi.dll", EntryPoint = "DwmGetColorizationColor", PreserveSig = true)]
  2404. private static extern HRESULT _DwmGetColorizationColor(out uint pcrColorization, [Out, MarshalAs(UnmanagedType.Bool)] out bool pfOpaqueBlend);
  2405. public static bool DwmGetColorizationColor(out uint pcrColorization, out bool pfOpaqueBlend)
  2406. {
  2407. // Make this call safe to make on downlevel OSes...
  2408. if (Utility.IsOSVistaOrNewer && IsThemeActive())
  2409. {
  2410. HRESULT hr = _DwmGetColorizationColor(out pcrColorization, out pfOpaqueBlend);
  2411. if (hr.Succeeded)
  2412. {
  2413. return true;
  2414. }
  2415. }
  2416. // Default values. If for some reason the native DWM API fails it's never enough of a reason
  2417. // to bring down the app. Empirically it still sometimes returns errors even when the theme service is on.
  2418. // We'll still use the boolean return value to allow the caller to respond if they care.
  2419. pcrColorization = 0xFF000000;
  2420. pfOpaqueBlend = true;
  2421. return false;
  2422. }
  2423. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2424. public static bool DwmIsCompositionEnabled()
  2425. {
  2426. // Make this call safe to make on downlevel OSes...
  2427. if (!Utility.IsOSVistaOrNewer)
  2428. {
  2429. return false;
  2430. }
  2431. return _DwmIsCompositionEnabled();
  2432. }
  2433. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2434. [DllImport("dwmapi.dll")]
  2435. [return: MarshalAs(UnmanagedType.Bool)]
  2436. public static extern bool DwmDefWindowProc(IntPtr hwnd, WM msg, IntPtr wParam, IntPtr lParam, out IntPtr plResult);
  2437. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2438. [DllImport("dwmapi.dll", EntryPoint = "DwmSetWindowAttribute")]
  2439. private static extern void _DwmSetWindowAttribute(IntPtr hwnd, DWMWA dwAttribute, ref int pvAttribute, int cbAttribute);
  2440. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2441. public static void DwmSetWindowAttributeFlip3DPolicy(IntPtr hwnd, DWMFLIP3D flip3dPolicy)
  2442. {
  2443. Assert.IsTrue(Utility.IsOSVistaOrNewer);
  2444. var dwPolicy = (int)flip3dPolicy;
  2445. _DwmSetWindowAttribute(hwnd, DWMWA.FLIP3D_POLICY, ref dwPolicy, sizeof(int));
  2446. }
  2447. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2448. public static void DwmSetWindowAttributeDisallowPeek(IntPtr hwnd, bool disallowPeek)
  2449. {
  2450. Assert.IsTrue(Utility.IsOSWindows7OrNewer);
  2451. int dwDisallow = (int)(disallowPeek ? Win32Value.TRUE : Win32Value.FALSE);
  2452. _DwmSetWindowAttribute(hwnd, DWMWA.DISALLOW_PEEK, ref dwDisallow, sizeof(int));
  2453. }
  2454. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2455. [DllImport("user32.dll", EntryPoint = "EnableMenuItem")]
  2456. private static extern int _EnableMenuItem(IntPtr hMenu, SC uIDEnableItem, MF uEnable);
  2457. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2458. public static MF EnableMenuItem(IntPtr hMenu, SC uIDEnableItem, MF uEnable)
  2459. {
  2460. // Returns the previous state of the menu item, or -1 if the menu item does not exist.
  2461. int iRet = _EnableMenuItem(hMenu, uIDEnableItem, uEnable);
  2462. return (MF)iRet;
  2463. }
  2464. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2465. [DllImport("user32.dll", EntryPoint = "RemoveMenu", SetLastError = true)]
  2466. [return: MarshalAs(UnmanagedType.Bool)]
  2467. private static extern bool _RemoveMenu(IntPtr hMenu, uint uPosition, uint uFlags);
  2468. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2469. public static void RemoveMenu(IntPtr hMenu, SC uPosition, MF uFlags)
  2470. {
  2471. if (!_RemoveMenu(hMenu, (uint)uPosition, (uint)uFlags))
  2472. {
  2473. throw new Win32Exception();
  2474. }
  2475. }
  2476. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2477. [DllImport("user32.dll", EntryPoint = "DrawMenuBar", SetLastError = true)]
  2478. [return: MarshalAs(UnmanagedType.Bool)]
  2479. private static extern bool _DrawMenuBar(IntPtr hWnd);
  2480. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2481. public static void DrawMenuBar(IntPtr hWnd)
  2482. {
  2483. if (!_DrawMenuBar(hWnd))
  2484. {
  2485. throw new Win32Exception();
  2486. }
  2487. }
  2488. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2489. [DllImport("kernel32.dll")]
  2490. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  2491. [return: MarshalAs(UnmanagedType.Bool)]
  2492. public static extern bool FindClose(IntPtr handle);
  2493. // Not shimming this SetLastError=true function because callers want to evaluate the reason for failure.
  2494. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2495. [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  2496. public static extern SafeFindHandle FindFirstFileW(string lpFileName, [In, Out, MarshalAs(UnmanagedType.LPStruct)] WIN32_FIND_DATAW lpFindFileData);
  2497. // Not shimming this SetLastError=true function because callers want to evaluate the reason for failure.
  2498. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2499. [DllImport("kernel32.dll", SetLastError = true)]
  2500. [return: MarshalAs(UnmanagedType.Bool)]
  2501. public static extern bool FindNextFileW(SafeFindHandle hndFindFile, [In, Out, MarshalAs(UnmanagedType.LPStruct)] WIN32_FIND_DATAW lpFindFileData);
  2502. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2503. [DllImport("user32.dll", EntryPoint = "GetClientRect", SetLastError = true)]
  2504. [return: MarshalAs(UnmanagedType.Bool)]
  2505. private static extern bool _GetClientRect(IntPtr hwnd, [Out] out RECT lpRect);
  2506. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2507. public static RECT GetClientRect(IntPtr hwnd)
  2508. {
  2509. RECT rc;
  2510. if (!_GetClientRect(hwnd, out rc))
  2511. {
  2512. HRESULT.ThrowLastError();
  2513. }
  2514. return rc;
  2515. }
  2516. [DllImport("uxtheme.dll", EntryPoint="GetCurrentThemeName", CharSet = CharSet.Unicode)]
  2517. private static extern HRESULT _GetCurrentThemeName(
  2518. StringBuilder pszThemeFileName,
  2519. int dwMaxNameChars,
  2520. StringBuilder pszColorBuff,
  2521. int cchMaxColorChars,
  2522. StringBuilder pszSizeBuff,
  2523. int cchMaxSizeChars);
  2524. public static void GetCurrentThemeName(out string themeFileName, out string color, out string size)
  2525. {
  2526. // Not expecting strings longer than MAX_PATH. We will return the error
  2527. var fileNameBuilder = new StringBuilder((int)Win32Value.MAX_PATH);
  2528. var colorBuilder = new StringBuilder((int)Win32Value.MAX_PATH);
  2529. var sizeBuilder = new StringBuilder((int)Win32Value.MAX_PATH);
  2530. // This will throw if the theme service is not active (e.g. not UxTheme!IsThemeActive).
  2531. _GetCurrentThemeName(fileNameBuilder, fileNameBuilder.Capacity,
  2532. colorBuilder, colorBuilder.Capacity,
  2533. sizeBuilder, sizeBuilder.Capacity)
  2534. .ThrowIfFailed();
  2535. themeFileName = fileNameBuilder.ToString();
  2536. color = colorBuilder.ToString();
  2537. size = sizeBuilder.ToString();
  2538. }
  2539. [DllImport("uxtheme.dll")]
  2540. [return: MarshalAs(UnmanagedType.Bool)]
  2541. public static extern bool IsThemeActive();
  2542. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2543. [Obsolete("Use SafeDC.GetDC instead.", true)]
  2544. public static void GetDC() { }
  2545. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2546. [DllImport("gdi32.dll")]
  2547. public static extern int GetDeviceCaps(SafeDC hdc, DeviceCap nIndex);
  2548. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2549. [DllImport("kernel32.dll", EntryPoint = "GetModuleFileName", CharSet = CharSet.Unicode, SetLastError = true)]
  2550. private static extern int _GetModuleFileName(IntPtr hModule, StringBuilder lpFilename, int nSize);
  2551. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2552. public static string GetModuleFileName(IntPtr hModule)
  2553. {
  2554. var buffer = new StringBuilder((int)Win32Value.MAX_PATH);
  2555. while (true)
  2556. {
  2557. int size = _GetModuleFileName(hModule, buffer, buffer.Capacity);
  2558. if (size == 0)
  2559. {
  2560. HRESULT.ThrowLastError();
  2561. }
  2562. // GetModuleFileName returns nSize when it's truncated but does NOT set the last error.
  2563. // MSDN documentation says this has changed in Windows 2000+.
  2564. if (size == buffer.Capacity)
  2565. {
  2566. // Enlarge the buffer and try again.
  2567. buffer.EnsureCapacity(buffer.Capacity * 2);
  2568. continue;
  2569. }
  2570. return buffer.ToString();
  2571. }
  2572. }
  2573. [DllImport("kernel32.dll", EntryPoint = "GetModuleHandleW", CharSet = CharSet.Unicode, SetLastError = true)]
  2574. private static extern IntPtr _GetModuleHandle([MarshalAs(UnmanagedType.LPWStr)] string lpModuleName);
  2575. public static IntPtr GetModuleHandle(string lpModuleName)
  2576. {
  2577. IntPtr retPtr = _GetModuleHandle(lpModuleName);
  2578. if (retPtr == IntPtr.Zero)
  2579. {
  2580. HRESULT.ThrowLastError();
  2581. }
  2582. return retPtr;
  2583. }
  2584. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2585. [DllImport("user32.dll", EntryPoint = "GetMonitorInfo", SetLastError = true)]
  2586. [return: MarshalAs(UnmanagedType.Bool)]
  2587. private static extern bool _GetMonitorInfo(IntPtr hMonitor, [In, Out] MONITORINFO lpmi);
  2588. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2589. public static MONITORINFO GetMonitorInfo(IntPtr hMonitor)
  2590. {
  2591. var mi = new MONITORINFO();
  2592. if (!_GetMonitorInfo(hMonitor, mi))
  2593. {
  2594. throw new Win32Exception();
  2595. }
  2596. return mi;
  2597. }
  2598. [DllImport("gdi32.dll", EntryPoint = "GetStockObject", SetLastError = true)]
  2599. private static extern IntPtr _GetStockObject(StockObject fnObject);
  2600. public static IntPtr GetStockObject(StockObject fnObject)
  2601. {
  2602. IntPtr retPtr = _GetStockObject(fnObject);
  2603. if (retPtr == null)
  2604. {
  2605. HRESULT.ThrowLastError();
  2606. }
  2607. return retPtr;
  2608. }
  2609. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2610. [DllImport("user32.dll")]
  2611. public static extern IntPtr GetSystemMenu(IntPtr hWnd, [MarshalAs(UnmanagedType.Bool)] bool bRevert);
  2612. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2613. [DllImport("user32.dll")]
  2614. public static extern int GetSystemMetrics(SM nIndex);
  2615. // This is aliased as a macro in 32bit Windows.
  2616. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2617. public static IntPtr GetWindowLongPtr(IntPtr hwnd, GWL nIndex)
  2618. {
  2619. IntPtr ret = IntPtr.Zero;
  2620. if (8 == IntPtr.Size)
  2621. {
  2622. ret = GetWindowLongPtr64(hwnd, nIndex);
  2623. }
  2624. else
  2625. {
  2626. ret = new IntPtr(GetWindowLongPtr32(hwnd, nIndex));
  2627. }
  2628. if (IntPtr.Zero == ret)
  2629. {
  2630. throw new Win32Exception();
  2631. }
  2632. return ret;
  2633. }
  2634. /// <summary>
  2635. /// Sets attributes to control how visual styles are applied to a specified window.
  2636. /// </summary>
  2637. /// <param name="hwnd">
  2638. /// Handle to a window to apply changes to.
  2639. /// </param>
  2640. /// <param name="eAttribute">
  2641. /// Value of type WINDOWTHEMEATTRIBUTETYPE that specifies the type of attribute to set.
  2642. /// The value of this parameter determines the type of data that should be passed in the pvAttribute parameter.
  2643. /// Can be the following value:
  2644. /// <list>WTA_NONCLIENT (Specifies non-client related attributes).</list>
  2645. /// pvAttribute must be a pointer of type WTA_OPTIONS.
  2646. /// </param>
  2647. /// <param name="pvAttribute">
  2648. /// A pointer that specifies attributes to set. Type is determined by the value of the eAttribute value.
  2649. /// </param>
  2650. /// <param name="cbAttribute">
  2651. /// Specifies the size, in bytes, of the data pointed to by pvAttribute.
  2652. /// </param>
  2653. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2654. [DllImport("uxtheme.dll", PreserveSig = false)]
  2655. public static extern void SetWindowThemeAttribute([In] IntPtr hwnd, [In] WINDOWTHEMEATTRIBUTETYPE eAttribute, [In] ref WTA_OPTIONS pvAttribute, [In] uint cbAttribute);
  2656. [SuppressMessage("Microsoft.Interoperability", "CA1400:PInvokeEntryPointsShouldExist")]
  2657. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2658. [DllImport("user32.dll", EntryPoint = "GetWindowLong", SetLastError = true)]
  2659. private static extern int GetWindowLongPtr32(IntPtr hWnd, GWL nIndex);
  2660. [SuppressMessage("Microsoft.Interoperability", "CA1400:PInvokeEntryPointsShouldExist")]
  2661. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2662. [DllImport("user32.dll", EntryPoint = "GetWindowLongPtr", SetLastError = true)]
  2663. private static extern IntPtr GetWindowLongPtr64(IntPtr hWnd, GWL nIndex);
  2664. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2665. [DllImport("user32.dll", SetLastError = true)]
  2666. [return: MarshalAs(UnmanagedType.Bool)]
  2667. private static extern bool GetWindowPlacement(IntPtr hwnd, WINDOWPLACEMENT lpwndpl);
  2668. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2669. public static WINDOWPLACEMENT GetWindowPlacement(IntPtr hwnd)
  2670. {
  2671. WINDOWPLACEMENT wndpl = new WINDOWPLACEMENT();
  2672. if (GetWindowPlacement(hwnd, wndpl))
  2673. {
  2674. return wndpl;
  2675. }
  2676. throw new Win32Exception();
  2677. }
  2678. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2679. [DllImport("user32.dll", EntryPoint = "GetWindowRect", SetLastError = true)]
  2680. [return: MarshalAs(UnmanagedType.Bool)]
  2681. private static extern bool _GetWindowRect(IntPtr hWnd, out RECT lpRect);
  2682. public static RECT GetWindowRect(IntPtr hwnd)
  2683. {
  2684. RECT rc;
  2685. if (!_GetWindowRect(hwnd, out rc))
  2686. {
  2687. HRESULT.ThrowLastError();
  2688. }
  2689. return rc;
  2690. }
  2691. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2692. [DllImport("gdiplus.dll")]
  2693. public static extern Status GdipCreateBitmapFromStream(IStream stream, out IntPtr bitmap);
  2694. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2695. [DllImport("gdiplus.dll")]
  2696. public static extern Status GdipCreateHBITMAPFromBitmap(IntPtr bitmap, out IntPtr hbmReturn, Int32 background);
  2697. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2698. [DllImport("gdiplus.dll")]
  2699. public static extern Status GdipCreateHICONFromBitmap(IntPtr bitmap, out IntPtr hbmReturn);
  2700. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2701. [DllImport("gdiplus.dll")]
  2702. public static extern Status GdipDisposeImage(IntPtr image);
  2703. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2704. [DllImport("gdiplus.dll")]
  2705. public static extern Status GdipImageForceValidation(IntPtr image);
  2706. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2707. [DllImport("gdiplus.dll")]
  2708. public static extern Status GdiplusStartup(out IntPtr token, StartupInput input, out StartupOutput output);
  2709. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2710. [DllImport("gdiplus.dll")]
  2711. public static extern Status GdiplusShutdown(IntPtr token);
  2712. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2713. [DllImport("user32.dll")]
  2714. [return: MarshalAs(UnmanagedType.Bool)]
  2715. public static extern bool IsWindowVisible(IntPtr hwnd);
  2716. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2717. [DllImport("kernel32.dll", EntryPoint = "LocalFree", SetLastError = true)]
  2718. private static extern IntPtr _LocalFree(IntPtr hMem);
  2719. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2720. [DllImport("user32.dll")]
  2721. public static extern IntPtr MonitorFromWindow(IntPtr hwnd, uint dwFlags);
  2722. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2723. [DllImport("user32.dll", EntryPoint = "PostMessage", SetLastError = true)]
  2724. [return: MarshalAs(UnmanagedType.Bool)]
  2725. private static extern bool _PostMessage(IntPtr hWnd, WM Msg, IntPtr wParam, IntPtr lParam);
  2726. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2727. public static void PostMessage(IntPtr hWnd, WM Msg, IntPtr wParam, IntPtr lParam)
  2728. {
  2729. if (!_PostMessage(hWnd, Msg, wParam, lParam))
  2730. {
  2731. throw new Win32Exception();
  2732. }
  2733. }
  2734. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2735. [DllImport("user32.dll", SetLastError = true, EntryPoint = "RegisterClassExW")]
  2736. private static extern short _RegisterClassEx([In] ref WNDCLASSEX lpwcx);
  2737. // Note that this will throw HRESULT_FROM_WIN32(ERROR_CLASS_ALREADY_EXISTS) on duplicate registration.
  2738. // If needed, consider adding a Try* version of this function that returns the error code since that
  2739. // may be ignorable.
  2740. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2741. public static short RegisterClassEx(ref WNDCLASSEX lpwcx)
  2742. {
  2743. short ret = _RegisterClassEx(ref lpwcx);
  2744. if (ret == 0)
  2745. {
  2746. HRESULT.ThrowLastError();
  2747. }
  2748. return ret;
  2749. }
  2750. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2751. [DllImport("user32.dll", EntryPoint = "RegisterWindowMessage", SetLastError = true, CharSet = CharSet.Unicode)]
  2752. private static extern uint _RegisterWindowMessage([MarshalAs(UnmanagedType.LPWStr)] string lpString);
  2753. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2754. public static WM RegisterWindowMessage(string lpString)
  2755. {
  2756. uint iRet = _RegisterWindowMessage(lpString);
  2757. if (iRet == 0)
  2758. {
  2759. HRESULT.ThrowLastError();
  2760. }
  2761. return (WM)iRet;
  2762. }
  2763. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2764. [DllImport("user32.dll", EntryPoint = "SetActiveWindow", SetLastError = true)]
  2765. private static extern IntPtr _SetActiveWindow(IntPtr hWnd);
  2766. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2767. public static IntPtr SetActiveWindow(IntPtr hwnd)
  2768. {
  2769. Verify.IsNotDefault(hwnd, "hwnd");
  2770. IntPtr ret = _SetActiveWindow(hwnd);
  2771. if (ret == IntPtr.Zero)
  2772. {
  2773. HRESULT.ThrowLastError();
  2774. }
  2775. return ret;
  2776. }
  2777. // This is aliased as a macro in 32bit Windows.
  2778. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2779. public static IntPtr SetClassLongPtr(IntPtr hwnd, GCLP nIndex, IntPtr dwNewLong)
  2780. {
  2781. if (8 == IntPtr.Size)
  2782. {
  2783. return SetClassLongPtr64(hwnd, nIndex, dwNewLong);
  2784. }
  2785. return new IntPtr(SetClassLongPtr32(hwnd, nIndex, dwNewLong.ToInt32()));
  2786. }
  2787. [SuppressMessage("Microsoft.Interoperability", "CA1400:PInvokeEntryPointsShouldExist")]
  2788. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2789. [DllImport("user32.dll", EntryPoint = "SetClassLong", SetLastError = true)]
  2790. private static extern int SetClassLongPtr32(IntPtr hWnd, GCLP nIndex, int dwNewLong);
  2791. [SuppressMessage("Microsoft.Interoperability", "CA1400:PInvokeEntryPointsShouldExist")]
  2792. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2793. [DllImport("user32.dll", EntryPoint = "SetClassLongPtr", SetLastError = true)]
  2794. private static extern IntPtr SetClassLongPtr64(IntPtr hWnd, GCLP nIndex, IntPtr dwNewLong);
  2795. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2796. [DllImport("kernel32.dll", SetLastError = true)]
  2797. public static extern ErrorModes SetErrorMode(ErrorModes newMode);
  2798. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2799. [DllImport("kernel32.dll", SetLastError = true, EntryPoint = "SetProcessWorkingSetSize")]
  2800. [return: MarshalAs(UnmanagedType.Bool)]
  2801. private static extern bool _SetProcessWorkingSetSize(IntPtr hProcess, IntPtr dwMinimiumWorkingSetSize, IntPtr dwMaximumWorkingSetSize);
  2802. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2803. public static void SetProcessWorkingSetSize(IntPtr hProcess, int dwMinimumWorkingSetSize, int dwMaximumWorkingSetSize)
  2804. {
  2805. if (!_SetProcessWorkingSetSize(hProcess, new IntPtr(dwMinimumWorkingSetSize), new IntPtr(dwMaximumWorkingSetSize)))
  2806. {
  2807. throw new Win32Exception();
  2808. }
  2809. }
  2810. // This is aliased as a macro in 32bit Windows.
  2811. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2812. public static IntPtr SetWindowLongPtr(IntPtr hwnd, GWL nIndex, IntPtr dwNewLong)
  2813. {
  2814. if (8 == IntPtr.Size)
  2815. {
  2816. return SetWindowLongPtr64(hwnd, nIndex, dwNewLong);
  2817. }
  2818. return new IntPtr(SetWindowLongPtr32(hwnd, nIndex, dwNewLong.ToInt32()));
  2819. }
  2820. [SuppressMessage("Microsoft.Interoperability", "CA1400:PInvokeEntryPointsShouldExist")]
  2821. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2822. [DllImport("user32.dll", EntryPoint = "SetWindowLong", SetLastError = true)]
  2823. private static extern int SetWindowLongPtr32(IntPtr hWnd, GWL nIndex, int dwNewLong);
  2824. [SuppressMessage("Microsoft.Interoperability", "CA1400:PInvokeEntryPointsShouldExist")]
  2825. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2826. [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr", SetLastError = true)]
  2827. private static extern IntPtr SetWindowLongPtr64(IntPtr hWnd, GWL nIndex, IntPtr dwNewLong);
  2828. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2829. [DllImport("user32.dll", EntryPoint = "SetWindowRgn", SetLastError = true)]
  2830. private static extern int _SetWindowRgn(IntPtr hWnd, IntPtr hRgn, [MarshalAs(UnmanagedType.Bool)] bool bRedraw);
  2831. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2832. public static void SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool bRedraw)
  2833. {
  2834. int err = _SetWindowRgn(hWnd, hRgn, bRedraw);
  2835. if (0 == err)
  2836. {
  2837. throw new Win32Exception();
  2838. }
  2839. }
  2840. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2841. [DllImport("user32.dll", EntryPoint = "SetWindowPos", SetLastError = true)]
  2842. [return: MarshalAs(UnmanagedType.Bool)]
  2843. private static extern bool _SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, SWP uFlags);
  2844. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2845. public static bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, SWP uFlags)
  2846. {
  2847. if (!_SetWindowPos(hWnd, hWndInsertAfter, x, y, cx, cy, uFlags))
  2848. {
  2849. // If this fails it's never worth taking down the process. Let the caller deal with the error if they want.
  2850. return false;
  2851. }
  2852. return true;
  2853. }
  2854. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2855. [DllImport("shell32.dll", SetLastError = false)]
  2856. public static extern Win32Error SHFileOperation(ref SHFILEOPSTRUCT lpFileOp);
  2857. [DllImport("user32.dll")]
  2858. [return: MarshalAs(UnmanagedType.Bool)]
  2859. public static extern bool ShowWindow(IntPtr hwnd, SW nCmdShow);
  2860. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2861. [DllImport("user32.dll", EntryPoint = "SystemParametersInfoW", SetLastError = true)]
  2862. [return: MarshalAs(UnmanagedType.Bool)]
  2863. private static extern bool _SystemParametersInfo_String(SPI uiAction, int uiParam, [MarshalAs(UnmanagedType.LPWStr)] string pvParam, SPIF fWinIni);
  2864. /// <summary>Overload of SystemParametersInfo for getting and setting NONCLIENTMETRICS.</summary>
  2865. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2866. [DllImport("user32.dll", EntryPoint = "SystemParametersInfoW", SetLastError = true, CharSet = CharSet.Unicode)]
  2867. [return: MarshalAs(UnmanagedType.Bool)]
  2868. private static extern bool _SystemParametersInfo_NONCLIENTMETRICS(SPI uiAction, int uiParam, [In, Out] ref NONCLIENTMETRICS pvParam, SPIF fWinIni);
  2869. /// <summary>Overload of SystemParametersInfo for getting and setting HIGHCONTRAST.</summary>
  2870. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2871. [DllImport("user32.dll", EntryPoint = "SystemParametersInfoW", SetLastError = true, CharSet = CharSet.Unicode)]
  2872. [return: MarshalAs(UnmanagedType.Bool)]
  2873. private static extern bool _SystemParametersInfo_HIGHCONTRAST(SPI uiAction, int uiParam, [In, Out] ref HIGHCONTRAST pvParam, SPIF fWinIni);
  2874. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2875. public static void SystemParametersInfo(SPI uiAction, int uiParam, string pvParam, SPIF fWinIni)
  2876. {
  2877. if (!_SystemParametersInfo_String(uiAction, uiParam, pvParam, fWinIni))
  2878. {
  2879. HRESULT.ThrowLastError();
  2880. }
  2881. }
  2882. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2883. public static NONCLIENTMETRICS SystemParameterInfo_GetNONCLIENTMETRICS()
  2884. {
  2885. var metrics = Utility.IsOSVistaOrNewer
  2886. ? NONCLIENTMETRICS.VistaMetricsStruct
  2887. : NONCLIENTMETRICS.XPMetricsStruct;
  2888. if (!_SystemParametersInfo_NONCLIENTMETRICS(SPI.GETNONCLIENTMETRICS, metrics.cbSize, ref metrics, SPIF.None))
  2889. {
  2890. HRESULT.ThrowLastError();
  2891. }
  2892. return metrics;
  2893. }
  2894. [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
  2895. public static HIGHCONTRAST SystemParameterInfo_GetHIGHCONTRAST()
  2896. {
  2897. var hc = new HIGHCONTRAST { cbSize = Marshal.SizeOf(typeof(HIGHCONTRAST)) };
  2898. if (!_SystemParametersInfo_HIGHCONTRAST(SPI.GETHIGHCONTRAST, hc.cbSize, ref hc, SPIF.None))
  2899. {
  2900. HRESULT.ThrowLastError();
  2901. }
  2902. return hc;
  2903. }
  2904. // This function is strange in that it returns a BOOL if TPM_RETURNCMD isn't specified, but otherwise the command Id.
  2905. // Currently it's only used with TPM_RETURNCMD, so making the signature match that.
  2906. [DllImport("user32.dll")]
  2907. public static extern uint TrackPopupMenuEx(IntPtr hmenu, uint fuFlags, int x, int y, IntPtr hwnd, IntPtr lptpm);
  2908. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2909. [DllImport("gdi32.dll", EntryPoint = "SelectObject", SetLastError = true)]
  2910. private static extern IntPtr _SelectObject(SafeDC hdc, IntPtr hgdiobj);
  2911. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2912. public static IntPtr SelectObject(SafeDC hdc, IntPtr hgdiobj)
  2913. {
  2914. IntPtr ret = _SelectObject(hdc, hgdiobj);
  2915. if (ret == IntPtr.Zero)
  2916. {
  2917. HRESULT.ThrowLastError();
  2918. }
  2919. return ret;
  2920. }
  2921. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2922. [DllImport("gdi32.dll", EntryPoint = "SelectObject", SetLastError = true)]
  2923. private static extern IntPtr _SelectObjectSafeHBITMAP(SafeDC hdc, SafeHBITMAP hgdiobj);
  2924. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2925. public static IntPtr SelectObject(SafeDC hdc, SafeHBITMAP hgdiobj)
  2926. {
  2927. IntPtr ret = _SelectObjectSafeHBITMAP(hdc, hgdiobj);
  2928. if (ret == IntPtr.Zero)
  2929. {
  2930. HRESULT.ThrowLastError();
  2931. }
  2932. return ret;
  2933. }
  2934. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2935. [DllImport("user32.dll", SetLastError = true)]
  2936. public static extern int SendInput(int nInputs, ref INPUT pInputs, int cbSize);
  2937. // Depending on the message, callers may want to call GetLastError based on the return value.
  2938. [DllImport("user32.dll", SetLastError = true)]
  2939. public static extern IntPtr SendMessage(IntPtr hWnd, WM Msg, IntPtr wParam, IntPtr lParam);
  2940. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2941. [DllImport("user32.dll", EntryPoint = "UnregisterClass", SetLastError = true)]
  2942. [return: MarshalAs(UnmanagedType.Bool)]
  2943. private static extern bool _UnregisterClassAtom(IntPtr lpClassName, IntPtr hInstance);
  2944. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2945. [DllImport("user32.dll", EntryPoint = "UnregisterClass", CharSet = CharSet.Unicode, SetLastError = true)]
  2946. [return: MarshalAs(UnmanagedType.Bool)]
  2947. private static extern bool _UnregisterClassName(string lpClassName, IntPtr hInstance);
  2948. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2949. public static void UnregisterClass(short atom, IntPtr hinstance)
  2950. {
  2951. if (!_UnregisterClassAtom(new IntPtr(atom), hinstance))
  2952. {
  2953. HRESULT.ThrowLastError();
  2954. }
  2955. }
  2956. public static void UnregisterClass(string lpClassName, IntPtr hInstance)
  2957. {
  2958. if (!_UnregisterClassName(lpClassName, hInstance))
  2959. {
  2960. HRESULT.ThrowLastError();
  2961. }
  2962. }
  2963. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2964. [DllImport("user32.dll", SetLastError = true, EntryPoint = "UpdateLayeredWindow")]
  2965. [return: MarshalAs(UnmanagedType.Bool)]
  2966. private static extern bool _UpdateLayeredWindow(
  2967. IntPtr hwnd,
  2968. SafeDC hdcDst,
  2969. [In] ref POINT pptDst,
  2970. [In] ref SIZE psize,
  2971. SafeDC hdcSrc,
  2972. [In] ref POINT pptSrc,
  2973. int crKey,
  2974. ref BLENDFUNCTION pblend,
  2975. ULW dwFlags);
  2976. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2977. [DllImport("user32.dll", SetLastError = true, EntryPoint = "UpdateLayeredWindow")]
  2978. [return: MarshalAs(UnmanagedType.Bool)]
  2979. private static extern bool _UpdateLayeredWindowIntPtr(
  2980. IntPtr hwnd,
  2981. IntPtr hdcDst,
  2982. IntPtr pptDst,
  2983. IntPtr psize,
  2984. IntPtr hdcSrc,
  2985. IntPtr pptSrc,
  2986. int crKey,
  2987. ref BLENDFUNCTION pblend,
  2988. ULW dwFlags);
  2989. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  2990. public static void UpdateLayeredWindow(
  2991. IntPtr hwnd,
  2992. SafeDC hdcDst,
  2993. ref POINT pptDst,
  2994. ref SIZE psize,
  2995. SafeDC hdcSrc,
  2996. ref POINT pptSrc,
  2997. int crKey,
  2998. ref BLENDFUNCTION pblend,
  2999. ULW dwFlags)
  3000. {
  3001. if (!_UpdateLayeredWindow(hwnd, hdcDst, ref pptDst, ref psize, hdcSrc, ref pptSrc, crKey, ref pblend, dwFlags))
  3002. {
  3003. HRESULT.ThrowLastError();
  3004. }
  3005. }
  3006. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  3007. public static void UpdateLayeredWindow(
  3008. IntPtr hwnd,
  3009. int crKey,
  3010. ref BLENDFUNCTION pblend,
  3011. ULW dwFlags)
  3012. {
  3013. if (!_UpdateLayeredWindowIntPtr(hwnd, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, crKey, ref pblend, dwFlags))
  3014. {
  3015. HRESULT.ThrowLastError();
  3016. }
  3017. }
  3018. #region Win7 declarations
  3019. [DllImport("shell32.dll", EntryPoint = "SHAddToRecentDocs")]
  3020. private static extern void _SHAddToRecentDocs_String(SHARD uFlags, [MarshalAs(UnmanagedType.LPWStr)] string pv);
  3021. // This overload is required. There's a cast in the Shell code that causes the wrong vtbl to be used
  3022. // if we let the marshaller convert the parameter to an IUnknown.
  3023. [DllImport("shell32.dll", EntryPoint = "SHAddToRecentDocs")]
  3024. private static extern void _SHAddToRecentDocs_ShellLink(SHARD uFlags, IShellLinkW pv);
  3025. public static void SHAddToRecentDocs(string path)
  3026. {
  3027. _SHAddToRecentDocs_String(SHARD.PATHW, path);
  3028. }
  3029. // Win7 only.
  3030. public static void SHAddToRecentDocs(IShellLinkW shellLink)
  3031. {
  3032. _SHAddToRecentDocs_ShellLink(SHARD.LINK, shellLink);
  3033. }
  3034. // #define DWM_SIT_DISPLAYFRAME 0x00000001 // Display a window frame around the provided bitmap
  3035. [DllImport("dwmapi.dll", EntryPoint="DwmGetCompositionTimingInfo")]
  3036. private static extern HRESULT _DwmGetCompositionTimingInfo(IntPtr hwnd, ref DWM_TIMING_INFO pTimingInfo);
  3037. public static DWM_TIMING_INFO? DwmGetCompositionTimingInfo(IntPtr hwnd)
  3038. {
  3039. if (!Utility.IsOSVistaOrNewer)
  3040. {
  3041. // API was new to Vista.
  3042. return null;
  3043. }
  3044. var dti = new DWM_TIMING_INFO { cbSize = Marshal.SizeOf(typeof(DWM_TIMING_INFO)) };
  3045. HRESULT hr = _DwmGetCompositionTimingInfo(hwnd, ref dti);
  3046. if (hr == HRESULT.E_PENDING)
  3047. {
  3048. // The system isn't yet ready to respond. Return null rather than throw.
  3049. return null;
  3050. }
  3051. hr.ThrowIfFailed();
  3052. return dti;
  3053. }
  3054. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  3055. [DllImport("dwmapi.dll", PreserveSig = false)]
  3056. public static extern void DwmInvalidateIconicBitmaps(IntPtr hwnd);
  3057. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  3058. [DllImport("dwmapi.dll", PreserveSig = false)]
  3059. public static extern void DwmSetIconicThumbnail(IntPtr hwnd, IntPtr hbmp, DWM_SIT dwSITFlags);
  3060. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  3061. [DllImport("dwmapi.dll", PreserveSig = false)]
  3062. public static extern void DwmSetIconicLivePreviewBitmap(IntPtr hwnd, IntPtr hbmp, RefPOINT pptClient, DWM_SIT dwSITFlags);
  3063. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  3064. [DllImport("shell32.dll", PreserveSig = false)]
  3065. public static extern void SHGetItemFromDataObject(IDataObject pdtobj, DOGIF dwFlags, [In] ref Guid riid, [Out, MarshalAs(UnmanagedType.Interface)] out object ppv);
  3066. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  3067. [DllImport("shell32.dll", PreserveSig = false)]
  3068. public static extern HRESULT SHCreateItemFromParsingName([MarshalAs(UnmanagedType.LPWStr)] string pszPath, IBindCtx pbc, [In] ref Guid riid, [Out, MarshalAs(UnmanagedType.Interface)] out object ppv);
  3069. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  3070. [DllImport("shell32.dll")]
  3071. [return: MarshalAs(UnmanagedType.Bool)]
  3072. public static extern bool Shell_NotifyIcon(NIM dwMessage, [In] NOTIFYICONDATA lpdata);
  3073. /// <summary>
  3074. /// Sets the User Model AppID for the current process, enabling Windows to retrieve this ID
  3075. /// </summary>
  3076. /// <param name="AppID"></param>
  3077. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  3078. [DllImport("shell32.dll", PreserveSig = false)]
  3079. public static extern void SetCurrentProcessExplicitAppUserModelID([MarshalAs(UnmanagedType.LPWStr)] string AppID);
  3080. /// <summary>
  3081. /// Retrieves the User Model AppID that has been explicitly set for the current process via SetCurrentProcessExplicitAppUserModelID
  3082. /// </summary>
  3083. /// <param name="AppID"></param>
  3084. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  3085. [DllImport("shell32.dll")]
  3086. public static extern HRESULT GetCurrentProcessExplicitAppUserModelID([Out, MarshalAs(UnmanagedType.LPWStr)] out string AppID);
  3087. #endregion
  3088. }
  3089. }