经典Console案例
/*
下面的示例演示 WindowLeft、WindowTop、WindowWidth、WindowHeight、BufferWidth、BufferHeight 和 CursorVisible 属性以及
SetWindowPosition、SetBufferSize 和 ReadKey 方法。该示例在屏幕缓冲区中根据屏幕缓冲区的宽度绘制一个网格模式。然后,该示例在
按下向上键、向下键、向左键或向右键这四个控制台键时相应移动控制台窗口。网格模式有助于查看控制台窗口相对于屏幕缓冲区的移动。
*/
// This example demonstrates the Console.WindowLeft and
// Console.WindowTop properties.
using System;
using System.Text;
using System.IO;
//
class Sample
{
public static int saveBufferWidth;
public static int saveBufferHeight;
public static int saveWindowHeight;
public static int saveWindowWidth;
public static bool saveCursorVisible;
//
public static void Main()
{
string m1 = "1) Press the cursor keys to move the console window.\n" +
"2) Press any key to begin. When you're finished...\n" +
"3) Press the Escape key to quit.";
string g1 = "+----";
string g2 = "| ";
string grid1;
string grid2;
StringBuilder sbG1 = new StringBuilder();
StringBuilder sbG2 = new StringBuilder();
ConsoleKeyInfo cki;
int y;
//
try
{
saveBufferWidth = Console.BufferWidth;
saveBufferHeight = Console.BufferHeight;
saveWindowHeight = Console.WindowHeight;
saveWindowWidth = Console.WindowWidth;
saveCursorVisible = Console.CursorVisible;
//
Console.Clear();
Console.WriteLine(m1);
Console.ReadKey(true);
// Set the smallest possible window size before setting the buffer size.
Console.SetWindowSize(1, 1);
Console.SetBufferSize(80, 80);
Console.SetWindowSize(40, 20);
// Create grid lines to fit the buffer. (The buffer width is 80, but
// this same technique could be used with an arbitrary buffer width.)
for (y = 0; y < Console.BufferWidth/g1.Length; y++)
{
sbG1.Append(g1);
sbG2.Append(g2);
}
sbG1.Append(g1, 0, Console.BufferWidth%g1.Length);
sbG2.Append(g2, 0, Console.BufferWidth%g2.Length);
grid1 = sbG1.ToString();
grid2 = sbG2.ToString();
Console.CursorVisible = false;
Console.Clear();
for (y = 0; y < Console.BufferHeight-1; y++)
{
if (y%3 == 0)
Console.Write(grid1);
else
Console.Write(grid2);
}
Console.SetWindowPosition(0, 0);
do
{
cki = Console.ReadKey(true);
switch (cki.Key)
{
case ConsoleKey.LeftArrow:
if (Console.WindowLeft > 0)
Console.SetWindowPosition(
Console.WindowLeft-1, Console.WindowTop);
break;
case ConsoleKey.UpArrow:
if (Console.WindowTop > 0)
Console.SetWindowPosition(
Console.WindowLeft, Console.WindowTop-1);
break;
case ConsoleKey.RightArrow:
if (Console.WindowLeft < (Console.BufferWidth-Console.WindowWidth))
Console.SetWindowPosition(
Console.WindowLeft+1, Console.WindowTop);
break;
case ConsoleKey.DownArrow:
if (Console.WindowTop < (Console.BufferHeight-Console.WindowHeight))
Console.SetWindowPosition(
Console.WindowLeft, Console.WindowTop+1);
break;
}
}
while (cki.Key != ConsoleKey.Escape); // end do-while
} // end try
catch (IOException e)
{
Console.WriteLine(e.Message);
}
finally
{
Console.Clear();
Console.SetWindowSize(1, 1);
Console.SetBufferSize(saveBufferWidth, saveBufferHeight);
Console.SetWindowSize(saveWindowWidth, saveWindowHeight);
Console.CursorVisible = saveCursorVisible;
}
} // end Main
} // end Sample
/*
This example produces results similar to the following:
1) Press the cursor keys to move the console window.
2) Press any key to begin. When you're finished...
3) Press the Escape key to quit.
...
+----+----+----+-
| | | |
| | | |
+----+----+----+-
| | | |
| | | |
+----+----+----+-
*/
J#
// This example demonstrates the Console.WindowLeft and
// Console.WindowTop properties.
import System.*;
import System.Text.*;
import System.IO.*;
//
class Sample
{
public static int saveBufferWidth;
public static int saveBufferHeight;
public static int saveWindowHeight;
public static int saveWindowWidth;
public static boolean saveCursorVisible;
//
public static void main(String[] args)
{
String m1 = "1) Press the cursor keys to move the console window.\n"
+ "2) Press any key to begin. When you're finished...\n"
+ "3) Press the Escape key to quit.";
String g1 = "+----";
String g2 = "| ";
String grid1;
String grid2;
StringBuilder sbG1 = new StringBuilder();
StringBuilder sbG2 = new StringBuilder();
ConsoleKeyInfo cki;
int y;
//
try {
saveBufferWidth = Console.get_BufferWidth();
saveBufferHeight = Console.get_BufferHeight();
saveWindowHeight = Console.get_WindowHeight();
saveWindowWidth = Console.get_WindowWidth();
saveCursorVisible = Console.get_CursorVisible();
//
Console.Clear();
Console.WriteLine(m1);
Console.ReadKey(true);
// Set the smallest possible window size before setting the buffer
// size.
Console.SetWindowSize(1, 1);
Console.SetBufferSize(80, 80);
Console.SetWindowSize(40, 20);
// Create grid lines to fit the buffer. (The buffer width is 80, but
// this same technique could be used with an arbitrary buffer width.)
for (y = 0; y < Console.get_BufferWidth() / g1.get_Length(); y++) {
sbG1.Append(g1);
sbG2.Append(g2);
}
sbG1.Append(g1, 0, Console.get_BufferWidth() % g1.get_Length());
sbG2.Append(g2, 0, Console.get_BufferWidth() % g2.get_Length());
grid1 = sbG1.ToString();
grid2 = sbG2.ToString();
Console.set_CursorVisible(false);
Console.Clear();
for (y = 0; y < Console.get_BufferHeight() - 1; y++) {
if (y % 3 == 0) {
Console.Write(grid1);
}
else {
Console.Write(grid2);
}
}
Console.SetWindowPosition(0, 0);
do {
cki = Console.ReadKey(true);
switch (cki.get_Key()) {
case ConsoleKey.LeftArrow :
if (Console.get_WindowLeft() > 0) {
Console.SetWindowPosition(Console.get_WindowLeft()-1,
Console.get_WindowTop());
}
break;
case ConsoleKey.UpArrow :
if (Console.get_WindowTop() > 0) {
Console.SetWindowPosition(Console.get_WindowLeft(),
Console.get_WindowTop() - 1);
}
break;
case ConsoleKey.RightArrow :
if (Console.get_WindowLeft() < Console.get_BufferWidth()
- Console.get_WindowWidth()) {
Console.SetWindowPosition(Console.get_WindowLeft()+1,
Console.get_WindowTop());
}
break;
case ConsoleKey.DownArrow :
if (Console.get_WindowTop() < Console.get_BufferHeight()
- Console.get_WindowHeight()) {
Console.SetWindowPosition(Console.get_WindowLeft(),
Console.get_WindowTop() + 1);
}
break;
}
} while (!(cki.get_Key().Equals(ConsoleKey.Escape))); // end do-while
} // end try
catch (IOException e) {
Console.WriteLine(e.get_Message());
}
finally {
Console.Clear();
Console.SetWindowSize(1, 1);
Console.SetBufferSize(saveBufferWidth, saveBufferHeight);
Console.SetWindowSize(saveWindowWidth, saveWindowHeight);
Console.set_CursorVisible(saveCursorVisible);
}
} //end main
} //end Sample
/*
This example produces results similar to the following:
1) Press the cursor keys to move the console window.
2) Press any key to begin. When you're finished...
3) Press the Escape key to quit.
...
+----+----+----+-
| | | |
| | | |
+----+----+----+-
| | | |
| | | |
+----+----+----+-
*/
经典Console案例的更多相关文章
- 使用MapReduce实现一些经典的案例
在工作中,很多时候都是用hive或pig来自动化执行mr统计,但是我们不能忘记原始的mr.本文记录了一些通过mr来完成的经典的案例,有倒排索引.数据去重等,需要掌握. 一.使用mapreduce实现倒 ...
- PE经典DIY案例1:全解开方案让量产PE也能
更新说明:因未来的uefi似乎并不能识别并引导ud区,但能识别和引导量产和u+B+隐藏或高端隐藏区,故解决量产PE对u+B+隐藏区的支持,并增加对UEFI启动支持,已经成为PE制作的最主流技术. PE ...
- 18个awk的经典实战案例
介绍 这些案例是我收集起来的,大多都是我自己遇到过的,有些比较经典,有些比较具有代表性. 这些awk案例我也录了相关视频的讲解awk 18个经典实战案例精讲,欢迎大家去瞅瞅. 插入几个新字段 在&qu ...
- Spring框架-经典的案例和demo,一些可以直接用于生产,使用atomikos来处理多数据源的一致性事务等
Spring Examples Demo website:http://www.ityouknow.com/ 对Spring框架的学习,包括一些经典的案例和demo,一些可以直接用于生产. sprin ...
- 快要C语言考试了,大学生们收好这些经典程序案例,包你考试过关!
距离考试越来越近 编程大佬早已饥渴难耐 电脑小白还在瑟瑟发抖 但是不要怕! 来看看这些经典程序案例 包你考试过关! [程序1] 有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多 ...
- JAVA并发,经典死锁案例-哲学家就餐
转自:http://blog.csdn.net/tayanxunhua/article/details/38691005 死锁经典案例:哲学家就餐. 这个案例会导致死锁. 通过修改<Java编程 ...
- MySQL数据库“十宗罪”【十大经典错误案例】
原文作者:张甦 来源:http://blog.51cto.com/sumongodb 今天就给大家列举 MySQL 数据库中,最经典的十大错误案例,并附有处理问题的解决思路和方法,希望能给刚入行,或数 ...
- CISCO ASA 5505 经典配置案例
nterface Vlan2 nameif outside ----------------------------------------对端口命名外端口 security-level 0 -- ...
- C语言经典88案例,我文科妹妹说她都学会了!
案例ex01: 将字符串转换为一个整数 1 题目 函数:fun() 功能:将字符串转换为一个整数 描述: [不能使用C语言提供的字符串函数] 输入:字符串"-1234" 输出:整型 ...
随机推荐
- 通过一个工具类更深入理解动态代理和Threadlocal
动态代理和Threadlocal 一个代理类返回指定的接口,将方法调用指定的调用处理程序的代理类的实例.返回的是一个代理类,由指定的类装载器的定义和实现指定接口指定代理实例调用处理程序最近用到一个工具 ...
- pig强制转换(字符到整数):首位0怎么处理,‘01’到1的转化,
pig支持的类型转换(cast) Pig Latin supports casts as shown in this table. from / to bag tuple map int long f ...
- Accounting Flexfield Setup and Usage (Doc ID 124333.1)
APPLIES TO:Oracle General Ledger - Version 11.5.10.2 to 12.1.3 [Release 11.5.10 to 12.1] Information ...
- django练习——博客系统优化
一直准备使用Django搭建一个个人网站,最近终于开始动手,上周已经完成了基本博客功能的搭建(http://blog.csdn.net/hcx25909/article/details/2460133 ...
- Linux的文件系统及其硬盘分区挂载原理
如果您是一位新手,也许 您还不知道如何把文件从Windows拷贝到Linux上吧?下面,我们将说明Unix文件系统以及mount的工作过程,然后再比较详细地讨论. mount的使用和有关选项.如果您已 ...
- C 语言之银行ATM机界面
其实就是简单地对switch的用法,希望能给广大读者一些思路,写出自己的创意界面. #include <stdio.h> void main() { char SelectKey,Cred ...
- iOS9关键字的简单使用
在iOS 9 苹果推出了很多关键字, 目的其实很明确, 主要就是提高开发人员的效率, 有益于程序员之间的沟通与交流, 在开发中代码更加规范! 1. nullable 与 nonnull nullabl ...
- uploadify在火狐下上传不了的解决方案,java版(Spring+SpringMVC+MyBatis)详细解决方案
由于技术选型的原因,在一个产品中,我选择了uploadify,选择它的原因是它有完善的技术文档说明(http://www.uploadify.com/documentation/),唯一不足的是 ...
- android升级后错误:Unable to execute dex: java.nio.BufferOverflowException.Check
Android SDK Tools升级为22.3,Android SDK Platform-tools 升级为19后,编译工程出现错误: Unable to execute dex: java.nio ...
- 理解WebKit和Chromium: Chromium插件和扩展基础
转载请注明原文地址:http://blog.csdn.net/milado_nju ##概述 插件和扩展是一种扩充浏览器功能的技术,在之前我们介绍过NPAPI插件技术,在Chromium中,远远不只是 ...