麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > .NET > 正文

Dotnet總結(3)--打印

2024-07-21 02:17:02
字體:
來源:轉載
供稿:網友

eg:
private datagridprinter m_odatagridprinter;
protected system.windows.forms.printpreviewdialog printpreviewdialog1;
private system.windows.forms.printdialog printdialog1;
public system.drawing.printing.printdocument printdocument1;
// 預覽
try
            {   
m_odatagridprinter = new datagridprinter(this.datagrid_brand, printdocument1, (datatable)this.datagrid_brand.datasource);
                this.printpreviewdialog1.showdialog();
catch
            {
                messagebox.show("沒有找到打印機,不能預覽!");
            }


// 打印
        public dialogresult print()
        {
            try
            {
//                datatable odttmp = (datatable)this.datagrid_brand.datasource;
//                if (odttmp==null || odttmp.rows.count<=0)
//                {
//                    messagebox.show("主窗口中沒有數據,請進行/"查詢/"或者/"顯示所有/"等操作,將您要打印的數據顯示在主窗口中,然后再進行打印!");
//                    return dialogresult.cancel;                   
//                }
                m_odatagridprinter = new datagridprinter(this.datagrid_brand, printdocument1, (datatable)this.datagrid_brand.datasource);
               
                if (this.printdialog1.showdialog() == dialogresult.ok)
                {
                    this.printdocument1.print();
                    return dialogresult.ok;
                }
                else
                {
                    return dialogresult.cancel;
                }
            }
            catch
            {
                messagebox.show("沒有找到打印機,不能打印!");
                return dialogresult.cancel;
            }
            return dialogresult.ok;
        }

// 事件定義
this.printdocument1.documentname = "brand";
            this.printdocument1.printpage += new system.drawing.printing.printpageeventhandler(this.printdocument1_printpage);

// 事件響應
private void printdocument1_printpage(object sender, system.drawing.printing.printpageeventargs e)
        {                   
            e.hasmorepages = m_odatagridprinter.drawdatagrid(e.graphics);
            if (e.hasmorepages)
            {
                m_odatagridprinter.pagenumber += 1;
            }
            else
            {
                m_odatagridprinter.pagenumber = 1;
                m_odatagridprinter.rowcount = 0;
            }



// 基類定義
imports system.drawing.printing
imports system.windows.forms
imports system.drawing



public class datagridprinter

    public rowcount as integer = 0
    public pagenumber as integer = 1

    private m_datatable as datatable
    private m_datagrid as datagrid
    private m_imagearray(2) as image

    private m_pagewidth as integer
    private m_pagewidthminusmargins as integer
    private m_pageheight as integer
    private m_adjcolumnby as integer
    private m_istoowide as boolean
    private m_datagridwidth as integer

    private m_sselectstring as string

    private const c_topmargin as integer = 50
    private const c_bottommargin as integer = 50
    private const c_leftmargin as integer = 50
    private const c_rightmargin as integer = 50
    private const c_verticalcellleeway as integer = 10

    public sub new(byval dg as datagrid, byval pd as printdocument, byval dt as datatable)
        m_sselectstring = ""

        m_datagrid = dg
        m_datatable = dt

        'set the document as landscape
        pd.defaultpagesettings.landscape = true

        'extract our width and height values
        m_pageheight = pd.defaultpagesettings.papersize.width
        m_pagewidth = pd.defaultpagesettings.papersize.height
        m_pagewidthminusmargins = m_pagewidth - (c_leftmargin + c_rightmargin)

        'hard-coded images
        'm_imagearray(0) = image.fromfile("images/major.gif")
        'm_imagearray(1) = image.fromfile("images/medium.gif")
        'm_imagearray(2) = image.fromfile("images/minor.gif")

        m_datagridwidth = getdatagridwidth()

        'set up some adjustments to scale the output later
        if m_datagrid.width > m_pagewidthminusmargins then
            m_adjcolumnby = m_datagrid.width - m_pagewidthminusmargins
            m_datagridwidth = m_datagridwidth - m_adjcolumnby
            m_istoowide = true
        else
            m_adjcolumnby = m_pagewidthminusmargins - m_datagrid.width
            m_datagridwidth = m_datagridwidth + m_adjcolumnby
            m_istoowide = false
        end if
    end sub


    public sub new(byval dg as datagrid, byval pd as printdocument, byval dt as datatable, byval headstring as string)
        m_sselectstring = headstring

        m_datagrid = dg
        m_datatable = dt

        'set the document as landscape
        pd.defaultpagesettings.landscape = true

        'extract our width and height values
        m_pageheight = pd.defaultpagesettings.papersize.width
        m_pagewidth = pd.defaultpagesettings.papersize.height
        m_pagewidthminusmargins = m_pagewidth - (c_leftmargin + c_rightmargin)

        'hard-coded images
        'm_imagearray(0) = image.fromfile("images/major.gif")
        'm_imagearray(1) = image.fromfile("images/medium.gif")
        'm_imagearray(2) = image.fromfile("images/minor.gif")

        m_datagridwidth = getdatagridwidth()

        'set up some adjustments to scale the output later
        if m_datagrid.width > m_pagewidthminusmargins then
            m_adjcolumnby = m_datagrid.width - m_pagewidthminusmargins
            m_datagridwidth = m_datagridwidth - m_adjcolumnby
            m_istoowide = true
        else
            m_adjcolumnby = m_pagewidthminusmargins - m_datagrid.width
            m_datagridwidth = m_datagridwidth + m_adjcolumnby
            m_istoowide = false
        end if
    end sub


    public function drawdatagrid(byval g as graphics) as boolean
        try
            drawpageheader(g)
            return drawpagerows(g)
        catch ex as exception
            '      messagebox.show(ex.message.tostring())
            return false
        end try
    end function

    private sub drawpageheader(byval g as graphics)

        'use this format when drawing later
        dim cellformat as new stringformat
        cellformat.trimming = stringtrimming.word
        cellformat.formatflags = stringformatflags.nowrap or stringformatflags.linelimit

        if m_sselectstring.length > 0 then

            'temp width to draw this column
            dim columnwidth as integer = m_pagewidthminusmargins - 10

            'create a layout rectangle to draw within.
            dim cellbounds as new rectanglef(c_leftmargin + 12, c_topmargin - 9, columnwidth, m_datagrid.preferredrowheight + c_verticalcellleeway - 3)

            g.drawstring(m_sselectstring, m_datagrid.font, new solidbrush(m_datagrid.headerforecolor), cellbounds, cellformat)

            'create the header rectangle
            'dim headerbounds as new rectanglef(c_leftmargin, c_topmargin, m_pagewidthminusmargins, m_datagrid.headerfont.sizeinpoints + c_verticalcellleeway)
            dim headerbounds as new rectanglef(c_leftmargin + 12, c_topmargin - 3 - 9 - 9 + m_datagrid.preferredrowheight + c_verticalcellleeway, m_pagewidthminusmargins - 10, m_datagrid.preferredrowheight + c_verticalcellleeway - 3)

            'draw the header rectangle
            g.fillrectangle(new solidbrush(m_datagrid.headerbackcolor), headerbounds)
        else
            'create the header rectangle
            'dim headerbounds as new rectanglef(c_leftmargin, c_topmargin, m_pagewidthminusmargins, m_datagrid.headerfont.sizeinpoints + c_verticalcellleeway)
            dim headerbounds as new rectanglef(c_leftmargin + 12, c_topmargin - 9, m_pagewidthminusmargins - 10, m_datagrid.preferredrowheight + c_verticalcellleeway - 3)

            'draw the header rectangle
            g.fillrectangle(new solidbrush(m_datagrid.headerbackcolor), headerbounds)

        end if



        dim xposition as single = c_leftmargin + 12 ' +12 for some padding

        'find the column names from the tablestyle
        dim cs as datagridcolumnstyle
        for each cs in m_datagrid.tablestyles(0).gridcolumnstyles
            if cs.width > 0 then

                'temp width to draw this column
                dim columnwidth as integer = cs.width

                'scale the summary column width
                'note: just a quick way to fit the text to the page width
                'this is not the best way to do this but it handles the most
                'common ui path for this demo app
                if cs.mappingname = "tasksummary" and m_istoowide then
                    columnwidth -= m_adjcolumnby
                elseif cs.mappingname = "tasksummary" then
                    columnwidth += m_adjcolumnby
                end if

                if m_sselectstring.length > 0 then
                    'create a layout rectangle to draw within.
                    dim cellbounds as new rectanglef(xposition, c_topmargin - 9 + m_datagrid.preferredrowheight + c_verticalcellleeway - 3, columnwidth, m_datagrid.headerfont.sizeinpoints + c_verticalcellleeway)

                    'draw the column name
                    g.drawstring(cs.headertext, m_datagrid.headerfont, new solidbrush(m_datagrid.headerforecolor), cellbounds, cellformat)
                else
                    'create a layout rectangle to draw within.
                    dim cellbounds as new rectanglef(xposition, c_topmargin, columnwidth, m_datagrid.headerfont.sizeinpoints + c_verticalcellleeway)

                    'draw the column name
                    g.drawstring(cs.headertext, m_datagrid.headerfont, new solidbrush(m_datagrid.headerforecolor), cellbounds, cellformat)
                end if

                'adjust the next x pos
                xposition += columnwidth
            end if
        next


    end sub

    private function drawpagerows(byval g as graphics) as boolean

        'dim yposition as single = c_topmargin + m_datagrid.headerfont.sizeinpoints + (c_verticalcellleeway * 2)
        dim yposition as single = c_topmargin + m_datagrid.preferredrowheight
        if m_sselectstring.length > 0 then
            yposition += m_datagrid.preferredrowheight
        end if

        'use this format when drawing later
        dim cellformat as new stringformat
        cellformat.trimming = stringtrimming.word
        cellformat.formatflags = stringformatflags.nowrap or stringformatflags.linelimit

        try
            if m_datatable.defaultview.count <= 0 then
                dim xposition as single = c_leftmargin + 12 ' +12 for some padding
                dim cellbounds as new rectanglef(xposition, yposition, c_leftmargin - c_rightmargin, m_datagrid.preferredrowheight)
                g.drawstring("對不起,沒有找到您需要打印的數據,請您確認程序主窗口中有您需要打印的數據!", m_datagrid.font, new solidbrush(m_datagrid.headerforecolor), cellbounds, cellformat)
            end if
        catch ex as exception
            dim xposition as single = c_leftmargin + 12 ' +12 for some padding
            dim cellbounds as new rectanglef(xposition, yposition, c_leftmargin - c_rightmargin, m_datagrid.preferredrowheight)
            g.drawstring("對不起,沒有找到您需要打印的數據,請您確認程序主窗口中有您需要打印的數據!", m_datagrid.font, new solidbrush(m_datagrid.headerforecolor), cellbounds, cellformat)
            return false
        end try



            'loop each visible row
            dim i as integer = 0
            for i = rowcount to (m_datatable.defaultview.count - 1)

                dim xposition as single = c_leftmargin + 12 ' +12 for some padding
                'g.drawline(new pen(m_datagrid.gridlinecolor, 1), xposition - 3, yposition - 3, xposition - 3, yposition + m_datagrid.preferredrowheight + c_verticalcellleeway - 1)

                'loop the columns of this row, if the column is visible
                'then print the cell value
                dim cs as datagridcolumnstyle
                for each cs in m_datagrid.tablestyles(0).gridcolumnstyles
                    if cs.width > 0 then

                        'temp width to draw this column
                        dim columnwidth as integer = cs.width

                        'scale the summary column width
                        'note: just a quick way to fit the text to the page width
                        'this is not the best way to do this but it handles the most
                        'common ui path for this demo app
                        if cs.mappingname = "tasksummary" and m_istoowide then
                            columnwidth -= m_adjcolumnby
                        elseif cs.mappingname = "tasksummary" then
                            columnwidth += m_adjcolumnby
                        end if

                        'create a layout rectangle to draw within.
                        'dim cellbounds as new rectanglef(xposition, yposition, columnwidth, m_datagrid.font.sizeinpoints + c_verticalcellleeway)
                        dim cellbounds as new rectanglef(xposition, yposition, columnwidth, m_datagrid.preferredrowheight)

                        'draw the item value
                        if cs.mappingname = "商標圖片" then
                            try
                                'draw image


                                dim by as byte() = ctype(m_datatable.defaultview.item(i).item(cs.mappingname), byte())

                                dim imagetodraw as image
                                dim omemorystream as system.io.memorystream
                                omemorystream = new system.io.memorystream(by, 0, by.length)
                                'omemorystream = new system.io.memorystream((byte[])this.getcolumnvalueatrow(source, rownum),  0, ctype(m_datatable.defaultview.item(i).item(cs.mappingname), byte))
                                'omemorystream = new system.io.memorystream(ctype(m_datatable.defaultview.item(i).item(cs.mappingname), [] byte), 0, ctype(m_datatable.defaultview.item(i).item(cs.mappingname), byte))

                                'memorystream omemorystream=new memorystream((byte[])this.getcolumnvalueatrow(source, rownum),0,((byte[])this.getcolumnvalueatrow(source, rownum)).length);

                                imagetodraw = system.drawing.image.fromstream(omemorystream)
                                g.drawimage(imagetodraw, new rectangle(xposition, yposition, 125, 36))
                                'select case m_datatable.defaultview.item(i).item("prioritytext")
                                '    case "major"
                                '        g.drawimage(m_imagearray(0), new point(convert.toint32(cellbounds.x) - 5, convert.toint32(cellbounds.y)))
                                '    case "medium"
                                '        g.drawimage(m_imagearray(1), new point(convert.toint32(cellbounds.x) - 5, convert.toint32(cellbounds.y)))
                                '    case "minor"
                                '        g.drawimage(m_imagearray(2), new point(convert.toint32(cellbounds.x) - 5, convert.toint32(cellbounds.y)))
                                'end select

                            catch ex as exception
                            end try
                        else
                            'draw as short date format or regular string
                            'if m_datatable.defaultview.item(i).item(cs.mappingname).gettype() is gettype(datetime) then
                            '    g.drawstring(string.format("{0:d}", m_datatable.defaultview.item(i).item(cs.mappingname)), m_datagrid.font, new solidbrush(m_datagrid.headerforecolor), cellbounds, cellformat)
                            'else
                        cellbounds.y = cellbounds.y + 8
                        'cellbounds.height = cellbounds.height - 8
                            try
                                g.drawstring(ctype(m_datatable.defaultview.item(i).item(cs.mappingname), string), m_datagrid.font, new solidbrush(m_datagrid.headerforecolor), cellbounds, cellformat)
                                'end if
                            catch ex as exception
                            end try
                        cellbounds.y = cellbounds.y - 8
                        'cellbounds.height = cellbounds.height + 8
                        end if

                        g.drawline(new pen(m_datagrid.gridlinecolor, 1), xposition, yposition - 4, xposition, yposition + m_datagrid.preferredrowheight + c_verticalcellleeway - 1)

                        'adjust the next x pos
                        xposition += columnwidth

                    end if
                next


                'set the rowcount (which is used for a possible next page)
                rowcount += 1

                g.drawline(new pen(m_datagrid.gridlinecolor, 1), m_pagewidthminusmargins + c_leftmargin - 1, yposition - 4, m_pagewidthminusmargins + c_leftmargin - 1, yposition + m_datagrid.preferredrowheight + c_verticalcellleeway - 1)

                'finished with this row, draw a nice line
                g.drawline(new pen(m_datagrid.gridlinecolor, 1), c_leftmargin + 12, yposition + m_datagrid.preferredrowheight + c_verticalcellleeway - 2, m_pagewidthminusmargins + c_leftmargin, yposition + m_datagrid.preferredrowheight + c_verticalcellleeway - 2)

                'adjust the next y pos
                'yposition += m_datagrid.headerfont.sizeinpoints + c_verticalcellleeway + 3
                yposition += m_datagrid.preferredrowheight + c_verticalcellleeway + 3

                'if at end of page exit out for next page
                if yposition * pagenumber > pagenumber * (m_pageheight - (c_bottommargin + c_topmargin)) then
                    return true
                end if
            next


        return false
    end function

    private function getdatagridwidth() as integer
        try
            dim cs as datagridcolumnstyle
            dim dgwidth as integer = 0
            for each cs in m_datagrid.tablestyles(0).gridcolumnstyles
                if cs.width <> 0 then
                    dgwidth = dgwidth + cs.width
                end if
            next
            return dgwidth
        catch ex as exception
            throw ex
        end try
    end function
end class
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产成人高清成人av片在线看 | 国产资源在线观看视频 | 国产91久久精品一区二区 | 亚洲欧美成aⅴ人在线观看 av免费在线播放 | 日韩精品中文字幕一区二区三区 | 亚洲视频综合网 | 久久久久久久久久久久久九 | www.99热精品 | 欧美18一12sex性处hd | 欧美黄色一级片视频 | 91成人免费视频 | 日韩专区在线 | 中文字幕在线观看1 | 成人午夜免费看 | www国产成人免费观看视频 | av电影在线网站 | 久久精品亚洲欧美日韩精品中文字幕 | 国产精品免费一区二区三区都可以 | 本色视频aaaaaa一级网站 | 成人毛片视频免费看 | 特一级毛片 | 麻豆视频在线播放 | 国产色视频一区 | 蜜桃视频在线播放 | 精品一区二区三区网站 | 国产在线a | 精品欧美一区二区精品久久 | 精品久久久久久亚洲精品 | 香蕉久久久精品 | 精品国产91久久久久久久 | 羞羞网站入口 | 免费一级在线观看 | 欧美一级视频免费看 | 99最新网址| 久久777国产线看观看精品 | 99精彩视频在线观看 | 久久综合婷婷香五月 | 国产成人在线免费视频 | 91在线视频导航 | 337p粉嫩大胆噜噜噜亚瑟影院 | 久草在线视频网 |