C#如何使显示器变为镜像显示呢?

yalan 2011-04-18 10:20:58
如何用C#将显示器的图像变为镜像显示呢?就是好像是在镜子里看一样。

如果这个实现不了,那么如何使C#编写的整个窗体是镜像显示的呢?就是别的都正常,就C#窗体是镜像的(包括窗体中打开的任何文字、图片等都是镜像的)


最好能实现第一个要求,我想将C#软件做成自动运行的,进入系统后整个显示器变为镜像显示了。
搞了好几天,不知道怎么弄,相关资料也很少,希望高手能给个源码,分不够再加100分呵呵~~~~~~~ ^_^
...全文
2583 56 打赏 收藏 转发到动态 举报
写回复
用AI写文章
56 条回复
切换为时间正序
请发表友善的回复…
发表回复
天堂镇的幽灵 2013-03-29
  • 打赏
  • 举报
回复
楼主我也遇到同样的问题了。求解决。309461987@qq.com
startstartsvip 2012-03-27
  • 打赏
  • 举报
回复
[Quote=引用 51 楼 yalan 的回复:]

也是自己搞定的呵呵,使用WPF完全改写了程序,还能动画翻转呢呵呵
[/Quote]

嘿嘿,新WPF 里面 + 效果就OK 啦
youngweili320 2012-01-04
  • 打赏
  • 举报
回复
楼主,你好,我也正在研究改造我们单位的提词器,能否交流一下?呵呵
lingkong198 2011-11-11
  • 打赏
  • 举报
回复
版主 可以直接购买一台镜像液晶显示器 就可以解决了。有这样的成品显示器 按键可以切换镜像和正像,可以联系我们 13521073323 葛先生
viki117 2011-07-28
  • 打赏
  • 举报
回复
最后还是用WPF...WPF界面效果的确相当厉害..就是那个消耗内存和CPU阿
ykdy100 2011-06-18
  • 打赏
  • 举报
回复
如果还没弄好的话联系我的QQ:78470769
ykdy100 2011-06-18
  • 打赏
  • 举报
回复
我不是高手,但是我搞得定,很简单的问题,却难倒怎么多高手,真是怪。当初我请教了好多高手,都说无法解决,最后我自己搞定
yalan 2011-06-18
  • 打赏
  • 举报
回复
也是自己搞定的呵呵,使用WPF完全改写了程序,还能动画翻转呢呵呵
yalan 2011-05-15
  • 打赏
  • 举报
回复
如果加上 frm.WindowState = FormWindowState.Maximized;的话按F4镜像窗体就出不来了。搞不定呵呵
yalan 2011-05-15
  • 打赏
  • 举报
回复
你运行一下看看。
我的测试步骤:
1,新建项目,在窗体上拖一个RTB控件,Dock属性Fill;
2,参考你的代码,修改。同时为了区别镜像窗体,我在镜像窗体上放了一个Label标签
3,F5运行,结果:RTB不能全屏,按F4只能看到镜像窗体的标签,镜像窗体的RTB不能显示,如果修改 case Keys.F4:
DestForm.MirrorState = !DestForm.MirrorState ;
//Left = DestForm.MirrorState?600:0;
Left = DestForm.MirrorState ? 0 : Screen.PrimaryScreen.WorkingArea.Width;
这里代码可以让镜像窗体的RTB显示,但是我控制不好,不熟悉GDI,希望你能指点一下
yalan 2011-05-15
  • 打赏
  • 举报
回复
下面是我测试的全部代码

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()));

}
}

}


你的代码越来越有意思了,看来分数要给你了
ChrisAK 2011-05-15
  • 打赏
  • 举报
回复
[Quote=引用 44 楼 yalan 的回复:]

还是无法全屏显示镜像,估计是你没考虑分辨率呵呵
[/Quote]不能全屏?什么情况?
分辨率考虑了的,我是用Screen取的主显示器的分辨率.
yalan 2011-05-15
  • 打赏
  • 举报
回复
还是无法全屏显示镜像,估计是你没考虑分辨率呵呵
CSDNFucker 2011-05-14
  • 打赏
  • 举报
回复
思路:字体文件内保存的是每个字的GraphicsPath,如果可以读取并反转再另存一种新字体,我想这样应该是你想要的吧?
ChrisAK 2011-05-14
  • 打赏
  • 举报
回复
[Quote=引用 38 楼 yalan 的回复:]

帖子又加了50分,一共200分,加上我http://topic.csdn.net/u/20110513/11/dad59784-9d00-4a36-8ea2-aac5333e2c5d.html这个帖子总共500分啦呵呵~~~~

截图镜像这个我知道,可是测试了很多遍无法实现我写的控制方法

我的主要需求是:
软件默认全屏,点击定义的按键RTB文字滚动以使播音员播音,然后再次点击定义的按……
[/Quote]汗...原来是还想能同时编辑啊.这个会比较烦.但截图这个思路是没啥错的.
内存占用大肯定是有内存泄露了.最好仔细检查你的代码.(比如我上面那两段,都有一个很严重的内存泄露,估计是刚开始用GDI+做的改StretchBlt的时候疏忽了)
ChrisAK 2011-05-14
  • 打赏
  • 举报
回复
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()));
}
}
按照你的要求又改了一下.
仍然是截图;运行初始无镜像全屏,F4切换镜像,可以输
入文字,Esc退出(不要按Alt+F4否则会崩溃),修正了内
存泄露我连续跑了30多分钟内存耗用稳定在17M左右

缺点在于反转模式下鼠标点击无效(其实就是把源窗体
移到了屏幕外).我没有设字体,如果需要rtf格式化过的文
本的话可以用写字板编辑后粘贴进去.至于需要滚动啥的
你自己加了
yalan 2011-05-14
  • 打赏
  • 举报
回复
帖子又加了50分,一共200分,加上我http://topic.csdn.net/u/20110513/11/dad59784-9d00-4a36-8ea2-aac5333e2c5d.html这个帖子总共500分啦呵呵~~~~

截图镜像这个我知道,可是测试了很多遍无法实现我写的控制方法

我的主要需求是:
软件默认全屏,点击定义的按键RTB文字滚动以使播音员播音,然后再次点击定义的按键滚动字幕停止。
我用截图的方法虽然能做到,但是计时器每2msTick一次内存迅速增加,不一会儿就占用1G以上内存,因此该方法只能实现镜像截图而无法实际应用到软件中。

所以我认为,最好的方法就是直接使RTB中的文字变成镜像的,这个我不清楚如何做,其实如果有个镜像字体一下就搞定了,什么都不用做了,只要点击镜像按钮在事件中用镜像字体格式化一下RTB就OK了。
或者如何使RTB中的文字直接变成反的呢?我见过很多大头贴软件,它们的文字就是反的,可以直接编辑复制粘贴打字等操作的,不知道是如何做的?

能提供如黑体等标准镜像字体的,给分400分呵呵
yalan 2011-05-14
  • 打赏
  • 举报
回复
看来这个问题不容易啊,一共发了500分的帖子,还没有搞定~~~~~
http://topic.csdn.net/u/20110513/11/dad59784-9d00-4a36-8ea2-aac5333e2c5d.html
ChrisAK 2011-05-14
  • 打赏
  • 举报
回复

贴个效果图
ChrisAK 2011-05-14
  • 打赏
  • 举报
回复
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()));
}
}

稍微改了一下.
换用RichTextBox,你可以在写字板里排好格式粘贴进去.
加了个checkbox控制镜像反转
如果嫌SourceWindow很烦,可以把它弄到需要的大小后移出屏幕.
只要不是最小化或隐藏,放在哪儿都没有问题.
加载更多回复(35)

110,578

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧