基本信息
源码名称:C#winform打印指定区域---控件拖动---控件设置颜色字体
源码大小:0.11M
文件格式:.rar
开发语言:C#
更新时间:2019-04-13
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
C#winform打印指定区域---控件拖动---控件设置颜色字体
C#winform打印指定区域---控件拖动---控件设置颜色字体
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
namespace Print
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 打印
/// </summary>
/// <param name="hdcDest"></param>
/// <param name="nXDest"></param>
/// <param name="nYDest"></param>
/// <param name="nWidth"></param>
/// <param name="nHeight"></param>
/// <param name="hdcSrc"></param>
/// <param name="nXSrc"></param>
/// <param name="nYSrc"></param>
/// <param name="dwRop"></param>
/// <returns></returns>
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern bool BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, System.Int32 dwRop);
Bitmap memoryImage = null;
private void button2_Click_1(object sender, EventArgs e)
{
Graphics graphic = Panel1.CreateGraphics();
Size s = Panel1.Size;
Bitmap memImage = new Bitmap(s.Width, s.Height, graphic);
Graphics memGraphic = Graphics.FromImage(memImage);
IntPtr dc1 = graphic.GetHdc();
IntPtr dc2 = memGraphic.GetHdc();
BitBlt(dc2, 0, 0, Panel1.ClientRectangle.Width, Panel1.ClientRectangle.Height,dc1, 0, 0, 13369376);
memoryImage = (Bitmap)memImage.Clone();
graphic.ReleaseHdc(dc1);
memGraphic.ReleaseHdc(dc2);
graphic.Dispose();
memGraphic.Dispose();
memImage.Dispose();
PrintPreviewDialog dlg = new PrintPreviewDialog();
PrintDocument pd = new PrintDocument();
pd.PrintPage = new PrintPageEventHandler(this.pd_PrintPage);
pd.Print();
}
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage, 0, 0);
e.Graphics.DrawImage(memoryImage, Panel1.ClientRectangle.Width 10, 0);
e.Graphics.DrawImage(memoryImage, 0, Panel1.ClientRectangle.Height 14);
e.Graphics.DrawImage(memoryImage, Panel1.ClientRectangle.Width 10, Panel1.ClientRectangle.Height 14);
}
/// <summary>
/// 控件拖动
/// </summary>
public enum EnumMousePointPosition
{
MouseSizeNone = 0, //'无
MouseSizeRight = 1, //'拉伸右边框
MouseSizeLeft = 2, //'拉伸左边框
MouseSizeBottom = 3, //'拉伸下边框
MouseSizeTop = 4, //'拉伸上边框
MouseSizeTopLeft = 5, //'拉伸左上角
MouseSizeTopRight = 6, //'拉伸右上角
MouseSizeBottomLeft = 7, //'拉伸左下角
MouseSizeBottomRight = 8, //'拉伸右下角
MouseDrag = 9 // '鼠标拖动
}
const int Band = 5;
const int MinWidth = 10;
const int MinHeight = 10;
private EnumMousePointPosition m_MousePointPosition;
private Point p, p1;
private void button1_MouseDown_1(object sender, MouseEventArgs e)
{
p.X = e.X;
p.Y = e.Y;
p1.X = e.X;
p1.Y = e.Y;
}
private void button1_MouseMove(object sender, MouseEventArgs e)
{
Control lCtrl = (sender as Control);
if (e.Button == MouseButtons.Left)
{
switch (m_MousePointPosition)
{
case EnumMousePointPosition.MouseDrag:
lCtrl.Left = lCtrl.Left e.X - p.X;
lCtrl.Top = lCtrl.Top e.Y - p.Y;
break;
case EnumMousePointPosition.MouseSizeBottom:
lCtrl.Height = lCtrl.Height e.Y - p1.Y;
p1.X = e.X;
p1.Y = e.Y; //'记录光标拖动的当前点
break;
case EnumMousePointPosition.MouseSizeBottomRight:
lCtrl.Width = lCtrl.Width e.X - p1.X;
lCtrl.Height = lCtrl.Height e.Y - p1.Y;
p1.X = e.X;
p1.Y = e.Y; //'记录光标拖动的当前点
break;
case EnumMousePointPosition.MouseSizeRight:
lCtrl.Width = lCtrl.Width e.X - p1.X;
p1.X = e.X;
p1.Y = e.Y; //'记录光标拖动的当前点
break;
case EnumMousePointPosition.MouseSizeTop:
lCtrl.Top = lCtrl.Top (e.Y - p.Y);
lCtrl.Height = lCtrl.Height - (e.Y - p.Y);
break;
case EnumMousePointPosition.MouseSizeLeft:
lCtrl.Left = lCtrl.Left e.X - p.X;
lCtrl.Width = lCtrl.Width - (e.X - p.X);
break;
case EnumMousePointPosition.MouseSizeBottomLeft:
lCtrl.Left = lCtrl.Left e.X - p.X;
lCtrl.Width = lCtrl.Width - (e.X - p.X);
lCtrl.Height = lCtrl.Height e.Y - p1.Y;
p1.X = e.X;
p1.Y = e.Y; //'记录光标拖动的当前点
break;
case EnumMousePointPosition.MouseSizeTopRight:
lCtrl.Top = lCtrl.Top (e.Y - p.Y);
lCtrl.Width = lCtrl.Width (e.X - p1.X);
lCtrl.Height = lCtrl.Height - (e.Y - p.Y);
p1.X = e.X;
p1.Y = e.Y; //'记录光标拖动的当前点
break;
case EnumMousePointPosition.MouseSizeTopLeft:
lCtrl.Left = lCtrl.Left e.X - p.X;
lCtrl.Top = lCtrl.Top (e.Y - p.Y);
lCtrl.Width = lCtrl.Width - (e.X - p.X);
lCtrl.Height = lCtrl.Height - (e.Y - p.Y);
break;
default:
break;
}
if (lCtrl.Width < MinWidth) lCtrl.Width = MinWidth;
if (lCtrl.Height < MinHeight) lCtrl.Height = MinHeight;
}
else
{
m_MousePointPosition = MousePointPosition(lCtrl.Size, e); //'判断光标的位置状态
switch (m_MousePointPosition) //'改变光标
{
case EnumMousePointPosition.MouseSizeNone:
this.Cursor = Cursors.Arrow; //'箭头
break;
case EnumMousePointPosition.MouseDrag:
this.Cursor = Cursors.SizeAll; //'四方向
break;
case EnumMousePointPosition.MouseSizeBottom:
this.Cursor = Cursors.SizeNS; //'南北
break;
case EnumMousePointPosition.MouseSizeTop:
this.Cursor = Cursors.SizeNS; //'南北
break;
case EnumMousePointPosition.MouseSizeLeft:
this.Cursor = Cursors.SizeWE; //'东西
break;
case EnumMousePointPosition.MouseSizeRight:
this.Cursor = Cursors.SizeWE; //'东西
break;
case EnumMousePointPosition.MouseSizeBottomLeft:
this.Cursor = Cursors.SizeNESW; //'东北到南西
break;
case EnumMousePointPosition.MouseSizeBottomRight:
this.Cursor = Cursors.SizeNWSE; //'东南到西北
break;
case EnumMousePointPosition.MouseSizeTopLeft:
this.Cursor = Cursors.SizeNWSE; //'东南到西北
break;
case EnumMousePointPosition.MouseSizeTopRight:
this.Cursor = Cursors.SizeNESW; //'东北到南西
break;
default:
break;
}
}
}
private EnumMousePointPosition MousePointPosition(Size size, System.Windows.Forms.MouseEventArgs e)
{
if ((e.X >= -1 * Band) | (e.X <= size.Width) | (e.Y >= -1 * Band) | (e.Y <= size.Height))
{
if (e.X < Band)
{
if (e.Y < Band)
{
return EnumMousePointPosition.MouseSizeTopLeft;
}
else
{
if (e.Y > -1 * Band size.Height)
{
return EnumMousePointPosition.MouseSizeBottomLeft;
}
else
{
return EnumMousePointPosition.MouseSizeLeft;
}
}
}
else
{
if (e.X > -1 * Band size.Width)
{
if (e.Y < Band)
{
return EnumMousePointPosition.MouseSizeTopRight;
}
else
{
if (e.Y > -1 * Band size.Height)
{
return EnumMousePointPosition.MouseSizeBottomRight;
}
else
{
return EnumMousePointPosition.MouseSizeRight;
}
}
}
else
{
if (e.Y < Band)
{
return EnumMousePointPosition.MouseSizeTop;
}
else
{
if (e.Y > -1 * Band size.Height)
{
return EnumMousePointPosition.MouseSizeBottom;
}
else
{
return EnumMousePointPosition.MouseDrag;
}
}
}
}
}
else
{
return EnumMousePointPosition.MouseSizeNone;
}
}
private void button1_MouseLeave(object sender, EventArgs e)
{
m_MousePointPosition = EnumMousePointPosition.MouseSizeNone;
this.Cursor = Cursors.Arrow;
}
/// <summary>
/// 加载事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
textBox1.MouseDown = new MouseEventHandler(this.button1_MouseDown_1);
textBox1.MouseMove = new MouseEventHandler(this.button1_MouseMove);
textBox1.MouseLeave = new EventHandler(this.button1_MouseLeave);
textBox1.MouseDoubleClick = new MouseEventHandler(this.FontColor);
textBox2.MouseDown = new MouseEventHandler(this.button1_MouseDown_1);
textBox2.MouseMove = new MouseEventHandler(this.button1_MouseMove);
textBox2.MouseLeave = new EventHandler(this.button1_MouseLeave);
textBox2.MouseDoubleClick = new MouseEventHandler(this.FontColor);
textBox3.MouseDown = new MouseEventHandler(this.button1_MouseDown_1);
textBox3.MouseMove = new MouseEventHandler(this.button1_MouseMove);
textBox3.MouseLeave = new EventHandler(this.button1_MouseLeave);
textBox3.MouseDoubleClick = new MouseEventHandler(this.FontColor);
textBox4.MouseDown = new MouseEventHandler(this.button1_MouseDown_1);
textBox4.MouseMove = new MouseEventHandler(this.button1_MouseMove);
textBox4.MouseLeave = new EventHandler(this.button1_MouseLeave);
textBox4.MouseDoubleClick = new MouseEventHandler(this.FontColor);
textBox5.MouseDown = new MouseEventHandler(this.button1_MouseDown_1);
textBox5.MouseMove = new MouseEventHandler(this.button1_MouseMove);
textBox5.MouseLeave = new EventHandler(this.button1_MouseLeave);
textBox5.MouseDoubleClick = new MouseEventHandler(this.FontColor);
textBox6.MouseDown = new MouseEventHandler(this.button1_MouseDown_1);
textBox6.MouseMove = new MouseEventHandler(this.button1_MouseMove);
textBox6.MouseLeave = new EventHandler(this.button1_MouseLeave);
textBox6.MouseDoubleClick = new MouseEventHandler(this.FontColor);
}
/// <summary>
/// 设置字体样式
/// </summary>
private FontDialog MyFontDialog;
TextBox Mytext;
private void FontColor(object sender, MouseEventArgs e)
{
TextBox __src = (TextBox)sender;
MyFontDialog = new FontDialog();
MyFontDialog.AllowScriptChange = true;
MyFontDialog.ShowColor = true;
MyFontDialog.ShowApply = true;
MyFontDialog.Font = __src.Font;
MyFontDialog.Color = __src.ForeColor;
Mytext = __src;
MyFontDialog.Apply = new System.EventHandler(MyApplyDlg_Apply);
if (MyFontDialog.ShowDialog() == DialogResult.OK)
{
__src.Font = MyFontDialog.Font;
__src.ForeColor = MyFontDialog.Color;
}
}
/// <summary>
/// 保存样式状态
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MyApplyDlg_Apply(object sender, System.EventArgs e)
{
Mytext.ForeColor = MyFontDialog.Color;
Mytext.Font = MyFontDialog.Font;
}
}
}