Visual Basic, .NET, ASP, VBScript
 

   
   
     

Форум - .NET

Страница: 1 |

 

  Вопрос: Я хочу быть как Тултип Добавлено: 08.03.10 17:36  

Автор вопроса:  VβÐUηìt | Web-сайт: смекаешь.рф
C#
Когда вася кидает на форму ToolTip, то у всех контролов на форме добавляется свойство ToolTip на toolTip1. Мне нужно, чтобы мой контрол делал так же, но привинчивал к контролам не string, а мой класс. Как это сделать?

Заранее благодарен.

Ответить

  Ответы Всего ответов: 2  

Номер ответа: 1
Автор ответа:
 EROS



Вопросов: 58
Ответов: 4255
 Профиль | | #1 Добавлено: 08.03.10 17:43
Возьми Рефлектор и распотроши ToolTip.. заодно и нам потом поведаешь.. ))

Ответить

Номер ответа: 2
Автор ответа:
 VβÐUηìt



Вопросов: 246
Ответов: 3333
 Web-сайт: смекаешь.рф
 Профиль | | #2
Добавлено: 08.03.10 18:33
И таки нашел:

IExtenderProvider - интерфейс
Определяет интерфейс для расширения свойств других компонентов в контейнере.

  1. namespace Microsoft.Samples.WinForms.Cs.HelpLabel
  2. {
  3. using System;
  4. using System.Collections;
  5. using System.ComponentModel;
  6. using System.ComponentModel.Design;
  7. using System.Drawing;
  8. using System.Windows.Forms;
  9. using System.Windows.Forms.Design;
  10.  
  11. //
  12. // <doc>
  13. // <desc>
  14. // Help Label offers an extender property called
  15. // "HelpText".  It monitors the active control
  16. // and displays the help text for the active control.
  17. // </desc>
  18. // </doc>
  19. //
  20. [
  21. ProvideProperty("HelpText",typeof(Control)),
  22. Designer(typeof(HelpLabel.HelpLabelDesigner))
  23. ]
  24. public class HelpLabel : Control, System.ComponentModel.IExtenderProvider
  25. {
  26. /// <summary>
  27. ///    Required designer variable.
  28. /// </summary>
  29. private System.ComponentModel.Container components;
  30. private Hashtable helpTexts;
  31. private System.Windows.Forms.Control activeControl;
  32.  
  33. //
  34. // <doc>
  35. // <desc>
  36. //      Creates a new help label object.
  37. // </desc>
  38. // </doc>
  39. //
  40. public HelpLabel()
  41. {
  42. //
  43. // Required for Windows Form Designer support
  44. //
  45. InitializeComponent();
  46.  
  47. helpTexts = new Hashtable();
  48. }
  49.  
  50. /// <summary>
  51. ///    Clean up any resources being used.
  52. /// </summary>
  53. protected override void Dispose(bool disposing)
  54. {
  55. if (disposing)
  56. {
  57. components.Dispose();
  58. }
  59. base.Dispose(disposing);
  60. }
  61.  
  62. /// <summary>
  63. ///    Required method for Designer support - do not modify
  64. ///    the contents of this method with the code editor.
  65. /// </summary>
  66. private void InitializeComponent()
  67. {
  68. this.components = new System.ComponentModel.Container ();
  69. this.BackColor = System.Drawing.SystemColors.Info;
  70. this.ForeColor = System.Drawing.SystemColors.InfoText;
  71. this.TabStop = false;
  72. }
  73.  
  74. //
  75. // <doc>
  76. // <desc>
  77. //      Overrides the text property of Control.  This label ignores
  78. //      the text property, so we add additional attributes here so the
  79. //      property does not show up in the properties window and is not
  80. //      persisted.
  81. // </desc>
  82. // </doc>
  83. //
  84. [
  85. Browsable(false),
  86. EditorBrowsable(EditorBrowsableState.Never),
  87. DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
  88. ]
  89. public override string Text
  90. {
  91. get
  92. {
  93. return base.Text;
  94. }
  95. set
  96. {
  97. base.Text = value;
  98. }
  99. }
  100.  
  101. //
  102. // <doc>
  103. // <desc>
  104. //      This implements the IExtenderProvider.CanExtend method.  The
  105. //      help label provides an extender property, and the design time
  106. //      framework will call this method once for each component to determine
  107. //      if we are interested in providing our extended properties for the
  108. //      component.  We return true here if the object is a control and is
  109. //      not a HelpLabel (since it would be silly to add this property to
  110. //      ourselves).
  111. // </desc>
  112. // </doc>
  113. //
  114. bool IExtenderProvider.CanExtend(object target)
  115. {
  116. if (target is Control &&
  117. !(target is HelpLabel))
  118. {
  119.  
  120. return true;
  121. }
  122. return false;
  123. }
  124.  
  125. //
  126. // <doc>
  127. // <desc>
  128. //      This is the extended property for the HelpText property.  Extended
  129. //      properties are actual methods because they take an additional parameter
  130. //      that is the object or control to provide the property for.
  131. // </desc>
  132. // </doc>
  133. //
  134. [
  135. DefaultValue(""),
  136. ]
  137. public string GetHelpText(Control control)
  138. {
  139. string text = (string)helpTexts[control];
  140. if (text == null)
  141. {
  142. text = string.Empty;
  143. }
  144. return text;
  145. }
  146.  
  147. //
  148. // <doc>
  149. // <desc>
  150. //      This is an event handler that responds to the OnControlEnter
  151. //      event.  We attach this to each control we are providing help
  152. //      text for.
  153. // </desc>
  154. // </doc>
  155. //
  156. private void OnControlEnter(object sender, EventArgs e)
  157. {
  158. activeControl = (Control)sender;
  159. Invalidate();
  160. }
  161.  
  162. //
  163. // <doc>
  164. // <desc>
  165. //      This is an event handler that responds to the OnControlLeave
  166. //      event.  We attach this to each control we are providing help
  167. //      text for.
  168. // </desc>
  169. // </doc>
  170. //
  171. private void OnControlLeave(object sender, EventArgs e)
  172. {
  173. if (sender == activeControl)
  174. {
  175. activeControl = null;
  176. Invalidate();
  177. }
  178. }
  179.  
  180. //
  181. // <doc>
  182. // <desc>
  183. //      This is the extended property for the HelpText property.
  184. // </desc>
  185. // </doc>
  186. //
  187. public void SetHelpText(Control control, string value)
  188. {
  189. if (value == null)
  190. {
  191. value = string.Empty;
  192. }
  193.  
  194. if (value.Length == 0)
  195. {
  196. helpTexts.Remove(control);
  197.  
  198. control.Enter -= new EventHandler(OnControlEnter);
  199. control.Leave -= new EventHandler(OnControlLeave);
  200. }
  201. else
  202. {
  203. helpTexts[control] = value;
  204.  
  205. control.Enter += new EventHandler(OnControlEnter);
  206. control.Leave += new EventHandler(OnControlLeave);
  207. }
  208.  
  209. if (control == activeControl)
  210. {
  211. Invalidate();
  212. }
  213. }
  214.  
  215. //
  216. // <doc>
  217. // <desc>
  218. //      Overrides Control.OnPaint.  Here we draw our
  219. //      label.
  220. // </desc>
  221. // </doc>
  222. //
  223. protected override void OnPaint(PaintEventArgs pe)
  224. {
  225.  
  226. // Let the base draw.  This will cover our back
  227. // color and set any image that the user may have
  228. // provided.
  229. //
  230. base.OnPaint(pe);
  231.  
  232. // Draw a rectangle around our control.
  233. //
  234. Rectangle rect = ClientRectangle;
  235.  
  236. Pen borderPen = new Pen(ForeColor);
  237. pe.Graphics.DrawRectangle(borderPen, rect);
  238. borderPen.Dispose();
  239.  
  240. // Finally, draw the text over the top of the
  241. // rectangle.
  242. //
  243. if (activeControl != null)
  244. {
  245. string text = (string)helpTexts[activeControl];
  246. if (text != null && text.Length > 0)
  247. {
  248. rect.Inflate(-2, -2);
  249. Brush brush = new SolidBrush(ForeColor);
  250. pe.Graphics.DrawString(text, Font, brush, rect);
  251. brush.Dispose();
  252. }
  253. }
  254. }
  255.  
  256. // <doc>
  257. // <desc>
  258. //     Returns true if the backColor should be persisted in code gen.  We
  259. //      override this because we change the default back color.
  260. // </desc>
  261. // <retvalue>
  262. //     true if the backColor should be persisted.
  263. // </retvalue>
  264. // </doc>
  265. //
  266. public bool ShouldSerializeBackColor()
  267. {
  268. return(!BackColor.Equals(SystemColors.Info));
  269. }
  270.  
  271. // <doc>
  272. // <desc>
  273. //     Returns true if the foreColor should be persisted in code gen.  We
  274. //      override this because we change the default foreground color.
  275. // </desc>
  276. // <retvalue>
  277. //     true if the foreColor should be persisted.
  278. // </retvalue>
  279. // </doc>
  280. //
  281. public bool ShouldSerializeForeColor()
  282. {
  283. return(!ForeColor.Equals(SystemColors.InfoText));
  284. }
  285.  
  286. //
  287. // <doc>
  288. // <desc>
  289. //      This is a designer for the HelpLabel.  This designer provides
  290. //      design time feedback for the label.  The help label responds
  291. //      to changes in the active control, but these events do not
  292. //      occur at design time.  In order to provide some usable feedback
  293. //      that the control is working the right way, this designer listens
  294. //      to selection change events and uses those events to trigger active
  295. //      control changes.
  296. // </desc>
  297. // </doc>
  298. //
  299.         [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
  300. public class HelpLabelDesigner : System.Windows.Forms.Design.ControlDesigner
  301. {
  302.  
  303. private bool trackSelection = true;
  304.  
  305. /// <summary>
  306. /// This property is added to the control's set of properties in the method
  307. /// PreFilterProperties below.  Note that on designers, properties that are
  308. /// explictly declared by TypeDescriptor.CreateProperty can be declared as
  309. /// private on the designer.  This helps to keep the designer's publi
  310. /// object model clean.
  311. /// </summary>
  312. private bool TrackSelection
  313. {
  314. get
  315. {
  316. return trackSelection;
  317. }
  318. set
  319. {
  320. trackSelection = value;
  321. if (trackSelection)
  322. {
  323. ISelectionService ss = (ISelectionService)GetService(typeof(ISelectionService));
  324. if (ss != null)
  325. {
  326. UpdateHelpLabelSelection(ss);
  327. }
  328. }
  329. else
  330. {
  331. HelpLabel helpLabel = (HelpLabel)Control;
  332. if (helpLabel.activeControl != null)
  333. {
  334. helpLabel.activeControl = null;
  335. helpLabel.Invalidate();
  336. }
  337. }
  338. }
  339. }
  340.  
  341. public override DesignerVerbCollection Verbs
  342. {
  343. get
  344. {
  345. DesignerVerb[] verbs = new DesignerVerb[] {
  346.   new DesignerVerb("Sample Verb", new EventHandler(OnSampleVerb))
  347.   };
  348. return new DesignerVerbCollection(verbs);
  349. }
  350. }
  351.  
  352. //
  353. // <doc>
  354. // <desc>
  355. //      Overrides Dispose.  Here we remove our handler for the selection changed
  356. //      event.  With designers, it is critical that they clean up any events they
  357. //      have attached.  Otherwise, during the course of an editing session many
  358. //      designers may get created and never destroyed.
  359. // </desc>
  360. // </doc>
  361. //
  362. protected override void Dispose(bool disposing)
  363. {
  364. if (disposing)
  365. {
  366. ISelectionService ss = (ISelectionService)GetService(typeof(ISelectionService));
  367. if (ss != null)
  368. {
  369. ss.SelectionChanged -= new EventHandler(OnSelectionChanged);
  370. }
  371. }
  372.  
  373. base.Dispose(disposing);
  374. }
  375.  
  376. //
  377. // <doc>
  378. // <desc>
  379. //       Overrides initialize.  Here we add an event handler to the selection service.
  380. //      Notice that we are very careful not to assume that the selection service is
  381. //      available.  It is entirely optional that a service is available and you should
  382. //      always degrade gracefully if a service could not be found.
  383. // </desc>
  384. // </doc>
  385. //
  386. public override void Initialize(IComponent component)
  387. {
  388. base.Initialize(component);
  389.  
  390. ISelectionService ss = (ISelectionService)GetService(typeof(ISelectionService));
  391. if (ss != null)
  392. {
  393. ss.SelectionChanged += new EventHandler(OnSelectionChanged);
  394. }
  395. }
  396.  
  397. private void OnSampleVerb(object sender, EventArgs e)
  398. {
  399. MessageBox.Show("You have just invoked a sample verb.  Normally, this would do something interesting.");
  400. }
  401.  
  402. //
  403. // <doc>
  404. // <desc>
  405. //      Our handler for the selection change event.  Here we update the active control within
  406. //      the help label.
  407. // </desc>
  408. // </doc>
  409. //
  410. private void OnSelectionChanged(object sender, EventArgs e)
  411. {
  412. if (trackSelection)
  413. {
  414. ISelectionService ss = (ISelectionService)sender;
  415. UpdateHelpLabelSelection(ss);
  416. }
  417. }
  418.  
  419. protected override void PreFilterProperties(IDictionary properties)
  420. {
  421. // Always call base first in PreFilter* methods, and last in PostFilter*
  422. // methods.
  423. base.PreFilterProperties(properties);
  424.  
  425. // We add a design-time property called "TrackSelection" that is used to track
  426. // the active selection.  If the user sets this to true (the default), then
  427. // we will listen to selection change events and update the control's active
  428. // control to point to the current primary selection.
  429. properties["TrackSelection"] = TypeDescriptor.CreateProperty(
  430. this.GetType(),        // the type this property is defined on
  431. "TrackSelection",    // the name of the property
  432. typeof(bool),        // the type of the property
  433. new Attribute[] {CategoryAttribute.Design});    // attributes
  434. }
  435.  
  436. /// <summary>
  437. /// This is a helper method that, given a selection service, will update the active control
  438. /// of our help label with the currently active selection.
  439. /// </summary>
  440. /// <param name="ss"></param>
  441. private void UpdateHelpLabelSelection(ISelectionService ss)
  442. {
  443. Control c = ss.PrimarySelection as Control;
  444. HelpLabel helpLabel = (HelpLabel)Control;
  445. if (c != null)
  446. {
  447. helpLabel.activeControl = c;
  448. helpLabel.Invalidate();
  449. }
  450. else
  451. {
  452. if (helpLabel.activeControl != null)
  453. {
  454. helpLabel.activeControl = null;
  455. helpLabel.Invalidate();
  456. }
  457. }
  458. }
  459. }
  460. }
  461. }

Ответить

Страница: 1 |

Поиск по форуму



© Copyright 2002-2011 VBNet.RU | Пишите нам