仅仅是一个测试而已!

test
using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.ComponentModel.Design;
using  System.Drawing;
using  System.Drawing.Printing;
using  System.Data;
using  System.Text;
using  System.Windows.Forms;

public   delegate   void  printPropertiesChanged();

namespace  PrintBar
{
    [ToolboxBitmap(
typeof(ucPrintBar), "PrintBar.ucPrintBar.bmp")]
    [Designer(
"System.Windows.Forms.Design.ParentControlDesigner, System.Design"typeof(IDesigner))]
    
public partial class ucPrintBar : ToolStrip
    
{
        
public event printPropertiesChanged PrintPropertiesChanged;
        
private PrintDocument document;
        
private PrintPreviewControl previewControl;
        
private bool isDesign = true;
        
private ucFormManagement ucfm;
        
private int noOfPages = 0;
        
private bool systemChange = false;
        
private bool isLandscape = false;

        
public ucPrintBar()
        
{
            InitializeComponent();
        }


        
public bool IsLandscape
        
{
            
get
            
{
                
return this.isLandscape;
            }

        }


        
private void checkButtons()
        
{
            
bool nullDoc = (this.document == null);
            
this.tsbPrint.Enabled = !nullDoc;
            
this.tsbPrintProperties.Enabled = !nullDoc;
            
bool nullCtl = (this.previewControl == null);
            
this.tscmbZoom.Enabled = !nullCtl;
            
if (this.previewControl != null)
            
{
                
if (this.previewControl.StartPage == 0)
                
{
                    
this.tsbFirstPage.Enabled = false;
                    
this.tsbPrevPage.Enabled = false;
                }

                
else
                
{
                    
this.tsbFirstPage.Enabled = true;
                    
this.tsbPrevPage.Enabled = true;
                }

                
if (this.previewControl.StartPage == (this.noOfPages - 1))
                
{
                    
this.tsbNextPage.Enabled = false;
                    
this.tsbLastPage.Enabled = false;
                }

                
else
                
{
                    
this.tsbNextPage.Enabled = true;
                    
this.tsbLastPage.Enabled = true;
                }

            }

            
else
            
{
                
this.tsbFirstPage.Enabled = false;
                
this.tsbPrevPage.Enabled = false;
                
this.tsbNextPage.Enabled = false;
                
this.tsbLastPage.Enabled = false;
            }

        }


        
public PrintDocument Document
        
{
            
get
            
{
                
return this.document;
            }

            
set
            
{
                
this.document = value;
                
this.checkButtons();
                
if (this.document != null)
                
{
                    
this.document.BeginPrint += new PrintEventHandler(document_BeginPrint);
                    
this.document.PrintPage += new PrintPageEventHandler(document_PrintPage);
                    
this.document.EndPrint += new PrintEventHandler(document_EndPrint);
                }

            }

        }


        
void document_EndPrint(object sender, PrintEventArgs e)
        
{
            
if (this.previewControl == null)
            
{
                
throw new Exception("PrintBar does not have a reference to the preview control");
            }

            
this.previewControl.StartPage = 0;
            
this.tstxtPageNo.Text = "1";
            
this.setZoom();
            
this.checkButtons();
        }


        
void document_PrintPage(object sender, PrintPageEventArgs e)
        
{
            
this.noOfPages++;
        }


        
void document_BeginPrint(object sender, PrintEventArgs e)
        
{
            
this.noOfPages = 0;
        }


        
public PrintPreviewControl PreviewControl
        
{
            
get
            
{
                
return this.previewControl;
            }

            
set
            
{
                
this.previewControl = value;
                
this.checkButtons();
                
if (this.previewControl != null)
                
{
                    
double zoom = this.previewControl.Zoom * 100;
                    
this.tscmbZoom.Text = zoom + "%";
                }

            }

        }


        
public double Zoom
        
{
            
get
            
{
                
if (this.previewControl != null)
                
{
                    
return this.previewControl.Zoom;
                }

                
else
                
{
                    
return 0;
                }

            }

            
set
            
{
                
this.tscmbZoom.Text = Convert.ToString(value * 100);
                
this.setZoom();
            }

        }


        
public ucFormManagement FormManagement
        
{
            
get
            
{
                
return this.ucfm;
            }

            
set
            
{
                
this.ucfm = value;
            }

        }


        
private void tsbPrint_Click(object sender, EventArgs e)
        
{
            
try
            
{
                
this.document.Print();
            }

            
catch (Exception ex)
            
{
                MessageBox.Show(
"Could not print document " + ex.Message);
            }


        }


        
private void tsbPrintProperties_Click(object sender, EventArgs e)
        
{
            
//Shouldn't reach here unless the document is set (see checkButtons)
            PageSetupDialog setup = new PageSetupDialog();
            setup.Document 
= this.document;
            setup.EnableMetric 
= true;
            
if (setup.ShowDialog() == DialogResult.OK)
                
{
                
if (this.ucfm != null)
                
{
                    
this.ucfm.addExtra("LeftMargin"this.document.DefaultPageSettings.Margins.Left);
                    
this.ucfm.addExtra("RightMargin"this.document.DefaultPageSettings.Margins.Right);
                    
this.ucfm.addExtra("TopMargin"this.document.DefaultPageSettings.Margins.Top);
                    
this.ucfm.addExtra("BottomMargin"this.document.DefaultPageSettings.Margins.Bottom);
                    
this.ucfm.addExtra("IsLandscape"this.document.DefaultPageSettings.Landscape);
                }

                
if (this.PrintPropertiesChanged != null)
                
{
                    
this.PrintPropertiesChanged();
                }

            }

        }


        
private void setZoom()
        
{
            
if (this.previewControl != null)
            
{
                
string zoomText = this.tscmbZoom.Text;
                
double zoom = 1;
                
if (zoomText == "Fit")
                
{
                    
this.previewControl.AutoZoom = true;
                    
if (this.ucfm != null)
                    
{
                        
this.ucfm.addExtra("ZoomFit"true);
                    }

                }

                
else if ((zoomText != ""&& (zoomText != "."))
                
{
                    
this.previewControl.AutoZoom = false;
                    
if (zoomText.EndsWith("%"))
                    
{
                        zoomText 
= zoomText.Substring(0, zoomText.Length - 1);
                    }

                    zoom 
= Convert.ToDouble(zoomText) / 100;
                    
this.previewControl.Zoom = zoom;
                    
if (this.ucfm != null)
                    
{
                        
this.ucfm.addExtra("ZoomFit"false);
                        
this.ucfm.addExtra("Zoom", zoom);
                    }

                }

            }

        }


        
private void tscmbZoom_TextUpdate(object sender, EventArgs e)
        
{
            
if (!this.systemChange)
            
{
                
this.setZoom();
            }

        }


        
private void number_KeyPress(object sender, KeyPressEventArgs e)
        
{
            
this.isDesign = (this.Parent != null&& (this.Parent.ToString().Equals("System.Windows.Forms.Design.DesignerFrame+OverlayControl", StringComparison.OrdinalIgnoreCase));
            
if (this.isDesign)
            
{
                e.Handled 
= true;
                
return;
            }

            
string key = new string(e.KeyChar, 1);
            
bool validKey = (e.KeyChar >= 48 && e.KeyChar <= 57)
                
|| (e.KeyChar == 8);
            
if (!validKey)
            
{
                e.Handled 
= true;
            }

        }


        
private void tscmbZoom_SelectedIndexChanged(object sender, EventArgs e)
        
{
            
if (!this.systemChange)
            
{
                
this.setZoom();
            }

        }


        
private void tscmbZoom_Leave(object sender, EventArgs e)
        
{
            
string zoomText = this.tscmbZoom.Text;
            
if ((zoomText.ToLower() != "fit"&& (!zoomText.EndsWith("%")))
            
{
                
this.systemChange = true;
                
this.tscmbZoom.Text += "%";
                
this.systemChange = false;
            }

        }


        
public void loadDefaults()
        
{
            
if (this.ucfm == null)
            
{
                
throw new Exception("No form management user control set.");
            }

            
if (this.document == null)
            
{
                
throw new Exception("No pribt document set.");
            }

            
int leftMargin = 50;
            
int rightMargin = 50;
            
int topMargin = 50;
            
int bottomMargin = 50;
            
this.isLandscape = false;
            
if (this.ucfm.Extras != null)
            
{
                
if (this.ucfm.Extras.ContainsKey("LeftMargin"))
                
{
                    leftMargin 
= Convert.ToInt32(this.ucfm.Extras["LeftMargin"]);
                }

                
if (this.ucfm.Extras.ContainsKey("RightMargin"))
                
{
                    rightMargin 
= Convert.ToInt32(this.ucfm.Extras["RightMargin"]);
                }

                
if (this.ucfm.Extras.ContainsKey("TopMargin"))
                
{
                    topMargin 
= Convert.ToInt32(this.ucfm.Extras["TopMargin"]);
                }

                
if (this.ucfm.Extras.ContainsKey("BottomMargin"))
                
{
                    bottomMargin 
= Convert.ToInt32(this.ucfm.Extras["BottomMargin"]);
                }

                
if (this.ucfm.Extras.ContainsKey("IsLandscape"))
                
{
                    
this.isLandscape = Convert.ToBoolean(this.ucfm.Extras["IsLandscape"]);
                }

                
bool fit = false;
                
double zoom = 100;
                
if (this.ucfm.Extras.ContainsKey("ZoomFit"))
                
{
                    fit 
= Convert.ToBoolean(this.ucfm.Extras["ZoomFit"]);
                }

                
if (fit)
                
{
                    
this.tscmbZoom.Text = "Fit";
                }

                
else if (this.ucfm.Extras.ContainsKey("Zoom"))
                
{
                    zoom 
= Convert.ToDouble(this.ucfm.Extras["Zoom"]) * 100;
                    
this.tscmbZoom.Text = zoom.ToString("0.##"+ "%";
                }

                Margins margins 
= new Margins(leftMargin, rightMargin, topMargin, bottomMargin);
                
this.document.DefaultPageSettings.Margins = margins;
                
this.document.DefaultPageSettings.Landscape = this.isLandscape;
            }

        }


        
private void tsbFirstPage_Click(object sender, EventArgs e)
        
{
            
this.tstxtPageNo.Text = "1";
            
this.checkButtons();
        }


        
private void tsbLastPage_Click(object sender, EventArgs e)
        
{
            
this.tstxtPageNo.Text = this.noOfPages.ToString();
            
this.checkButtons();
        }


        
private void tsbNextPage_Click(object sender, EventArgs e)
        
{
            
int pageNo = Convert.ToInt32(this.tstxtPageNo.Text);
            pageNo
++;
            
this.tstxtPageNo.Text = pageNo.ToString();
            
this.checkButtons();
        }


        
private void tsbPrevPage_Click(object sender, EventArgs e)
        
{
            
int pageNo = Convert.ToInt32(this.tstxtPageNo.Text);
            pageNo
--;
            
this.tstxtPageNo.Text = pageNo.ToString();
            
this.checkButtons();
        }


        
private void tstxtPageNo_TextChanged(object sender, EventArgs e)
        
{
            
if (!this.systemChange)
            
{
                
this.systemChange = true;
                
int pageNo = Convert.ToInt32(this.tstxtPageNo.Text);
                
if (pageNo > this.noOfPages)
                
{
                    pageNo 
= this.noOfPages;
                }

                
if (pageNo < 1)
                
{
                    pageNo 
= 1;
                }

                
this.tstxtPageNo.Text = pageNo.ToString();
                
this.systemChange = false;
                
this.previewControl.StartPage = pageNo - 1;
                
this.checkButtons();
            }

        }


        
private void tsbSinglePage_Click(object sender, EventArgs e)
        
{
            
this.tsbSinglePage.Checked = true;
            
this.tsbTwoPages.Checked = false;
            
this.tsbMultiplePages.Checked = false;
            
this.previewControl.Rows = 1;
            
this.previewControl.Columns = 1;
            
this.checkButtons();
        }


        
private void tsbTwoPages_Click(object sender, EventArgs e)
        
{
            
this.tsbSinglePage.Checked = false;
            
this.tsbTwoPages.Checked = true;
            
this.tsbMultiplePages.Checked = false;
            
this.previewControl.Rows = 1;
            
this.previewControl.Columns = 2;
            
this.checkButtons();
        }


        
private void tsbMultiplePages_Click(object sender, EventArgs e)
        
{
            
this.tsbSinglePage.Checked = false;
            
this.tsbTwoPages.Checked = false;
            
this.tsbMultiplePages.Checked = true;
            
this.previewControl.Rows = 2;
            
this.previewControl.Columns = 2;
            
this.checkButtons();
        }

    }

}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值