GDI操作基础(1)——拷贝A窗体的内容到B窗体上

来源:互联网 发布:充话费软件利润 编辑:程序博客网 时间:2024/06/04 01:20

 

 

 

Form2中的代码:

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace DrawOnDesktop
  10. {
  11.     public partial class Form2 : Form
  12.     {
  13.         private Form1 f1 = new Form1();
  14.         private IntPtr f1_hdc = IntPtr.Zero;
  15.         private IntPtr f2_hdc = IntPtr.Zero;
  16.         public Form2()
  17.         {
  18.             InitializeComponent();
  19.             f1.Show();
  20.         }
  21.         private void timer1_Tick(object sender, EventArgs e)
  22.         {
  23.             f1_hdc = Win.GetDC(f1.Handle);
  24.             f2_hdc = Win.GetDC(this.Handle);
  25.             DrawOnDesktop.Win.Rect rect;
  26.             Win.GetClientRect(f1.Handle, out rect);
  27.             Win.BitBlt(f2_hdc, 0, 0, rect.right, rect.bottom, f1_hdc, 0, 0, 13369376);
  28.             Win.ReleaseDC(f1.Handle, f1_hdc);
  29.             Win.ReleaseDC(this.Handle, f2_hdc);
  30.         }
  31.     }
  32. }

 

Win.cs

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.InteropServices;
  6. namespace DrawOnDesktop
  7. {
  8.     public static class Win
  9.     {
  10.         [StructLayout(LayoutKind.Sequential) ]
  11.         public struct Rect
  12.         {
  13.             public int left;
  14.             public int top;
  15.             public int right;
  16.             public int bottom; 
  17.         }
  18.         [DllImport("user32")]
  19.         public static extern IntPtr GetDC(IntPtr hwnd);
  20.         [DllImport("user32")]
  21.         public static extern bool GetClientRect(IntPtr hWnd, out Rect rect);
  22.         [DllImport("gdi32")]
  23.         public static extern bool BitBlt(
  24.           IntPtr hdcDest, // handle to destination DC
  25.           int nXDest,  // x-coord of destination upper-left corner
  26.           int nYDest,  // y-coord of destination upper-left corner
  27.           int nWidth,  // width of destination rectangle
  28.           int nHeight, // height of destination rectangle
  29.           IntPtr hdcSrc,  // handle to source DC
  30.           int nXSrc,   // x-coordinate of source upper-left corner
  31.           int nYSrc,   // y-coordinate of source upper-left corner
  32.           int dwRop  // raster operation code
  33.         );
  34.         [DllImport("user32")]
  35.         public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
  36.     }
  37. }

Form1没有写代码,在Form中有一个定时器,触发事件为100ms。

原创粉丝点击