当前位置: 移动技术网 > IT编程>开发语言>.net > 在控制台中绘制图像

在控制台中绘制图像

2019年03月24日  | 移动技术网IT编程  | 我要评论

花都兽医无弹窗,陈艾森勃起,家长网

在控制台中绘制图像是一件非常有趣的事,不知情的人看到了会感觉很惊讶,如果你有gdi+绘制基础,那么继续往下看。

imports system.drawing
imports system.runtime.interopservices

module module1
    '引用win32api,该函数用来获取当前命令行的窗口句柄,以便后面从该句柄创建画布
    <dllimport("kernel32.dll", setlasterror:=true)>
    private function getconsolewindow() as intptr
    end function
    dim h as intptr = getconsolewindow()

    sub main()
        '随便输出一行文本
        console.title = "脚本编译器"
        console.readkey()
        dim s as string = "正在将脚本编译为可执行exe文件..."
        console.write(s)
        console.foregroundcolor = consolecolor.white
        for i as integer = 1 to 100 step 2
            '依次从1到100绘制一个进度条
            drawpersion(i)
            threading.thread.sleep(100)
        next
        '最后再绘制一次100%时的进度条
        drawpersion(100)
        console.writeline()
        console.writeline()
        console.writeline()
        console.writeline("操作完成")
        while true
            console.readkey()
            console.writeline()
        end while
    end sub
    '绘制进度条过程
    private sub drawpersion(i as integer)
        dim t as integer = console.cursortop
        dim s as string = "正在将脚本编译为可执行exe文件..."
        using g as graphics = graphics.fromhwnd(h)
            g.smoothingmode = drawing2d.smoothingmode.highquality
            '设置光标位置,计算出位置应该在文本结尾
            console.setcursorposition(s.length * 2, t)
            '通过当前命令行文本行数计算出最末行的起始位置,用以绘制图像
            dim n as integer = (t + 1) * console.cursorsize - 5
            '绘制一个图片上去
            g.drawimage(my.resources.compile, new rectangle(0, n, 16, 16), new rectangle(0, 0, 16, 16), graphicsunit.pixel)
            '由于图片大小已知,又可计算出进度条左上角的起始位置,以下为绘制一个线性渐变的进度条
            using lg as new drawing2d.lineargradientbrush(new point(0, n), new point(0, n + console.cursorsize), color.steelblue, color.dodgerblue)
                '模拟值范围为0到300,并乘以当前传入的循环i值
                dim l as integer = 300 / 100 * i
                '用线性渐变填充一个矩形,该矩形是上面计算出的从图片右侧和同行y值,长度为当前i比例的矩形
                g.fillrectangle(lg, new rectangle(20, n, l, 16))
                '得到进度文本的左上角,其实就是进度条矩形的左上角
                dim pt as point = new point(20, n)
                '绘制黑色文本模拟投影
                g.drawstring(string.format("{0}%", i), new font("宋体", 12, fontstyle.bold), brushes.black, pt)
                '坐标往左上角偏移1像素绘制白色文本
                pt += new point(-1, -1)
                g.drawstring(string.format("{0}%", i), new font("宋体", 12, fontstyle.bold), brushes.white, pt)
            end using
        end using
    end sub
end module

  运行效果

代码中用到的素材图片

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网