code_junkie

Is there any way to draw a control on some other control in C#

Nov 13th, 2011
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
  2. {
  3. e.DrawBackground();
  4.  
  5. if (e.Index > -1)
  6. {
  7. String itemText = String.Format("{0}", this.Items.Count > 0 ? this.Items[e.Index] : this.Name);
  8.  
  9. //Snip
  10.  
  11. System.Windows.Forms.ControlPaint.DrawButton(e.Graphics, e.Bounds, ButtonState.Normal);
  12.  
  13. e.Graphics.DrawString(itemText, this.Font, SystemBrushes.ControlText, e.Bounds);
  14. }
  15. }
  16.  
  17. public delegate void OnPaintDelegate( PaintEventArgs e );
  18. private void panel1_Paint( object sender, PaintEventArgs e ) {
  19. OnPaintDelegate paintDelegate = (OnPaintDelegate)Delegate.CreateDelegate(
  20. typeof( OnPaintDelegate )
  21. , this.button1
  22. , "OnPaint" );
  23. paintDelegate( e );
  24. }
  25.  
  26. [DllImport("gdi32.dll")]
  27. private static extern bool BitBlt(
  28. IntPtr hdcDest,
  29. int nXDest,
  30. int nYDest,
  31. int nWidth,
  32. int nHeight,
  33. IntPtr hdcSrc,
  34. int nXSrc,
  35. int nYSrc,
  36. int dwRop
  37. );
  38.  
  39. private const Int32 SRCCOPY = 0xCC0020;
  40.  
  41. ....
  42.  
  43. Graphics sourceGraphics = sourceControl.CreateGraphics();
  44. Graphics targetGraphics = targetControl.CreateGraphics();
  45. Size controlSize = sourceControl.Size;
  46. IntPtr sourceDc = sourceGraphics.GetHdc();
  47. IntPtr targerDc = targetGraphics.GetHdc();
  48. BitBlt(targerDc, 0, 0, controlSize.Width, controlSize.Height, sourceDc, 0, 0, SRCCOPY);
  49. sourceGraphics.ReleaseHdc(sourceDc);
  50. targetGraphics.ReleaseHdc(targerDc);
  51.  
  52. class MyPanel : System.Windows.Forms.Panel
  53. {
  54. protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
  55. {
  56. base.OnPaint(e);
  57. }
  58. }
  59.  
  60. Button btn = new Button();
  61. btn.Text = "Hey!";
  62. Bitmap bmp = new Bitmap(btn.Width, btn.Height);
  63. btn.DrawToBitmap(bmp, new Rectangle(0, 0, btn.Width, btn.Height));
  64. PictureBox pb = new PictureBox();
  65. pb.Size = btn.Size;
  66. pb.Image = bmp;
  67.  
  68. e.Graphics.DrawImage(bmp, 0, 0);
  69. bmp.Dispose();
Add Comment
Please, Sign In to add comment