110,578
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace 显示器镜像
{
public partial class Form1 : Form
{
public Control SourceControl
{
get
{
return richTextBox1;
}
}
public DestForm DestForm
{
get;
set;
}
public Form1()
{
this.FullScreen();
FormBorderStyle = FormBorderStyle.None;
InitializeComponent();
}
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Escape:
DestForm.Close();
break;
case Keys.F4:
DestForm.MirrorState = !DestForm.MirrorState ;
//Left = DestForm.MirrorState?600:0;
Left = DestForm.MirrorState ? 0 : Screen.PrimaryScreen.WorkingArea.Width;
break;
}
}
}
public class DestForm : Form
{
[DllImport("user32.dll")]
static extern IntPtr GetDC(IntPtr wnd);
[DllImport("user32.dll")]
static extern bool ReleaseDC(IntPtr wnd, IntPtr hdc);
[DllImport("gdi32.dll")]
static extern bool StretchBlt(IntPtr hdcDest, int nXOriginDest, int nYOriginDest,
int nWidthDest, int nHeightDest,
IntPtr hdcSrc, int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc,
uint dwRop);
const uint SRCCOPY = 0x00CC0020;
Timer timer;
Form1 srcForm;
public DestForm(Form1 srcForm)
{
Label lb1=new Label();
lb1.Text="镜像窗体";
this.Controls.Add(lb1);
this.srcForm = srcForm;
srcForm.DestForm = this;
srcForm.Show();
this.FullScreen();
TopMost = false;
timer = new Timer();
Text = "Mirror";
GotFocus += (s, e) => srcForm.SourceControl.Focus();
timer.Interval = 1;
timer.Enabled = true;
timer.Tick += (s, e) => CapSrcFormAndMirror();
}
public bool MirrorState
{
get;
set;
}
void CapSrcFormAndMirror()
{
var width = srcForm.SourceControl.Width;
var height = srcForm.SourceControl.Height;
srcForm.SourceControl.Invalidate();
using (var g = CreateGraphics())
{
var srcDc = GetDC(srcForm.SourceControl.Handle);
var dstDc = g.GetHdc();
if (MirrorState)
StretchBlt(dstDc, 0, 0, width, height, srcDc, width - 1, 0, -width, height, SRCCOPY);
else
StretchBlt(dstDc, 0, 0, width, height, srcDc, 0, 0, width, height, SRCCOPY);
g.ReleaseHdc();
ReleaseDC(srcForm.SourceControl.Handle, srcDc);
}
}
}
public static class Program
{
public static void FullScreen(this Form frm)
{
frm.StartPosition = FormStartPosition.Manual;
frm.FormBorderStyle = FormBorderStyle.None;
frm.Size = Screen.PrimaryScreen.Bounds.Size;
frm.Location = new Point(0, 0);
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// Application.Run(new Form1());
Application.Run(new DestForm(new Form1()));
}
}
}
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing;
class SourceForm:Form
{
RichTextBox txtBox;
public Control SourceControl{
get{return txtBox;}
}
public DestForm DestForm{
get;
set;
}
public SourceForm()
{
txtBox = new RichTextBox();
txtBox.Dock = DockStyle.Fill;
this.FullScreen();
Left = 0;
FormBorderStyle = FormBorderStyle.None;
txtBox.KeyUp += (s,e)=>{
switch (e.KeyCode)
{
case Keys.Escape:
DestForm.Close();
break;
case Keys.F4:
DestForm.MirrorState = !DestForm.MirrorState ;
Left = DestForm.MirrorState?4000:0;
break;
}
};
Text = "Source";
Controls.Add (txtBox);
}
}
class DestForm:Form
{
[DllImport("user32.dll")]
static extern IntPtr GetDC(IntPtr wnd);
[DllImport("user32.dll")]
static extern bool ReleaseDC(IntPtr wnd,IntPtr hdc);
[DllImport("gdi32.dll")]
static extern bool StretchBlt(IntPtr hdcDest, int nXOriginDest, int nYOriginDest,
int nWidthDest, int nHeightDest,
IntPtr hdcSrc, int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc,
uint dwRop);
const uint SRCCOPY = 0x00CC0020;
Timer timer;
void SetDock (Control ctrl){
ctrl.Height = Height - 60;
ctrl.Width = Width;
ctrl.Anchor = AnchorStyles.Top|AnchorStyles.Bottom|AnchorStyles.Left|AnchorStyles.Right;
}
SourceForm srcForm;
public DestForm(SourceForm srcForm)
{
this.srcForm = srcForm;
srcForm.DestForm = this;
srcForm.Show ();
this.FullScreen();
TopMost = false;
timer = new Timer();
Text = "Mirror";
GotFocus += (s,e)=> srcForm.SourceControl.Focus();
timer.Interval =1;
timer.Enabled = true;
timer.Tick += (s, e) =>CapSrcFormAndMirror();
}
public bool MirrorState {
get;
set;
}
void CapSrcFormAndMirror()
{
var width = srcForm.SourceControl.Width;
var height = srcForm.SourceControl.Height;
srcForm.SourceControl.Invalidate ();
using (var g = CreateGraphics())
{
var srcDc = GetDC(srcForm.SourceControl.Handle);
var dstDc = g.GetHdc();
if (MirrorState)
StretchBlt(dstDc, 0, 0, width , height, srcDc, width - 1, 0, -width , height, SRCCOPY);
else
StretchBlt(dstDc, 0, 0, width, height, srcDc, 0, 0, width , height, SRCCOPY);
g.ReleaseHdc();
ReleaseDC(srcForm.SourceControl.Handle, srcDc);
}
}
}
static class program
{
public static void FullScreen (this Form frm){
frm.StartPosition = FormStartPosition.Manual;
frm.FormBorderStyle = FormBorderStyle.None;
frm.Size = Screen.PrimaryScreen.Bounds .Size;
frm.Location = new Point (0,0);
}
static void Main()
{
Application.Run(new DestForm(new SourceForm()));
}
}
按照你的要求又改了一下.using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing;
class SourceForm:Form
{
RichTextBox txtBox;
public Control SourceControl{
get{return txtBox;}
}
public SourceForm()
{
txtBox = new RichTextBox();
txtBox.Dock = DockStyle.Fill;
Text = "Source";
Controls.Add (txtBox);
}
}
class DestForm:Form
{
[DllImport("user32.dll")]
static extern IntPtr GetDC(IntPtr wnd);
[DllImport("user32.dll")]
static extern bool ReleaseDC(IntPtr wnd,IntPtr hdc);
[DllImport("gdi32.dll")]
static extern bool StretchBlt(IntPtr hdcDest, int nXOriginDest, int nYOriginDest,
int nWidthDest, int nHeightDest,
IntPtr hdcSrc, int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc,
uint dwRop);
const uint SRCCOPY = 0x00CC0020;
Panel panel;
Timer timer;
void SetDock (Control ctrl){
ctrl.Height = Height - 60;
ctrl.Width = Width;
ctrl.Anchor = AnchorStyles.Top|AnchorStyles.Bottom|AnchorStyles.Left|AnchorStyles.Right;
}
SourceForm srcForm;
public DestForm(SourceForm srcForm)
{
this.srcForm = srcForm;
srcForm.Show ();
timer = new Timer();
panel = new Panel ();
var checkBox = new CheckBox();
Text = "Mirror";
checkBox.Checked = false;
checkBox.Text = "Mirror On/Off";
checkBox.Dock = DockStyle.Bottom;
checkBox.CheckedChanged += (s,e)=>MirrorState = checkBox.Checked;
SetDock (panel);
Controls.Add (panel);
Controls.Add (checkBox);
timer.Interval =1;
timer.Enabled = true;
timer.Tick += (s, e) =>CapSrcFormAndMirror();
}
bool MirrorState {
get;
set;
}
void CapSrcFormAndMirror()
{
Bitmap bmp = new Bitmap(srcForm.SourceControl.Width, srcForm.SourceControl.Height);
using (var g = panel.CreateGraphics())
{
var srcDc = GetDC(srcForm.SourceControl.Handle);
var dstDc = g.GetHdc();
if (MirrorState)
StretchBlt(dstDc, 0, 0, bmp.Width, bmp.Height, srcDc, bmp.Width - 1, 0, -bmp.Width , bmp.Height, SRCCOPY);
else
StretchBlt(dstDc, 0, 0, bmp.Width, bmp.Height, srcDc, 0, 0, bmp.Width , bmp.Height, SRCCOPY);
g.ReleaseHdc();
ReleaseDC(srcForm.SourceControl.Handle, srcDc);
}
}
}
class program
{
static void Main()
{
Application.Run(new DestForm(new SourceForm()));
}
}