msn㊣Microshaoft.com 

naIBnaiM nOracle (大舌头) I Love download EditPlus.v3.11(340)!

© 0000 - 9999 ㊣ Microshaoft ™ ®
砍头不要紧 爱情价更高 欲穷千里目 粒粒皆辛苦 身披七彩祥云 脚踏金甲圣衣 卧鼠藏虫
http://microshaoft.googlepages.com/

博客园 首页 新随笔 联系 订阅 管理
  78 Posts :: 0 Stories :: 366 Comments :: 6 Trackbacks
.Net 1.1 下无法实现,终于在 .Net 2.0 可以解决了:

参阅了:
http://msdn2.microsoft.com/en-US/library/aa480477.aspx

增加了对: 方向键、Home键、End键 移动光标后插入字符的支持
增加了对: Delete键删除光标所在字符的支持

namespace Microshaoft
{
    
using System;
    
public class Class1
    
{
        
static void Main(string[] args)
        
{
            Console.WriteLine(
"Enter password:");
            
string password = ConsoleReadMaskLine('*',true);
            Console.WriteLine(
"\n" + password + "]");
            password 
= ConsoleReadMaskLine('%',false);
            Console.WriteLine(
"\n" + password + "]");
        }


        
public static string ConsoleReadMaskLine
            (
                
char PasswordChar
                , 
bool WithMask
            )
        
{
            
string password = "";
            ConsoleKey ck;
            
string s = @"~!@#$%^&*()_+`1234567890-="//可输入字符
            s += @"QWERTYUIOP{}|qwertyuiop[]\";
            s 
+= "ASDFGHJKL:\"asdfghjkl;'";
            s += "ZXCVBNM<>?zxcvbnm,./ ";

            
do
            
{
                ConsoleKeyInfo cki 
= Console.ReadKey(true);
                
char c = cki.KeyChar;
                ck 
= cki.Key;
                
int p = Console.CursorLeft;
                
if (ck == ConsoleKey.Backspace)
                
{
                    
string left = "";
                    
if (p > 0)
                    
{
                        left 
= password.Substring(0,p - 1);
                    }

                    
string right = password.Substring(p);
                    password  
= left + right;
                    Console.Write(c);

                    
string output = right;
                    
if (WithMask)
                    
{
                        output 
= GetPasswordChars(right, PasswordChar);
                    }


                    output 
+= "\0";
                    Console.Write(output);
                    
if (p > 0)
                    
{
                        p 
--;
                    }

                }

                
else if (ck == ConsoleKey.Delete)
                
{
                    
string left = "";
                    
if (p > 0)
                    
{
                        left 
= password.Substring(0, p);
                    }

                    
string right = "";
                    
if (p < password.Length)
                    
{
                        right 
= password.Substring(p + 1);
                    }

                    password 
= left + right;
                    
//Console.Write(right + " ");

                    
string output = right;

                    
if (WithMask)
                    
{
                        output 
= GetPasswordChars(right, PasswordChar);
                    }

                    output 
+= "\0";

                    Console.Write(output);
                }

                
else
                
{
                    
if (s.IndexOf(c) >= 0)
                    
{
                        
string left = password.Substring(0, p);
                        
string right = password.Substring(p);
                        password 
= left + c + right;

                        
string output = c + right;

                        
if (WithMask)
                        
{
                            output 
= GetPasswordChars(c + right, PasswordChar);
                        }

                        Console.Write(output);

                        p 
++;
                    }

                    
else
                    
{
                        
switch (ck)
                        
{
                            
case ConsoleKey.LeftArrow :
                                
if (p > 0)
                                
{
                                    p 
--;
                                }

                                
break;
                            
case ConsoleKey.RightArrow :
                                
if (p < password.Length)
                                
{
                                    p 
++;
                                }

                                
break;
                            
case ConsoleKey.Home :
                                p 
= 0;
                                
break;
                            
case ConsoleKey.End :
                                p 
= password.Length;
                                
break;
                            
default :
                                Console.Beep();
                                
break;
                        }

                    }

                }

                Console.CursorLeft 
= p;
            }
 while (ck != ConsoleKey.Enter);
            
return password;
        }

        
private static string GetPasswordChars(string s, char c)
        
{
            
string passwordChars = "";
            
for (int i = 0; i < s.Length; i++)
            
{
                passwordChars 
+= c;
            }

            
return passwordChars;
        }

    }

}
posted on 2006-12-07 13:48 Microshaoft 阅读(21992) 评论(4)  编辑 收藏 网摘

Feedback

#1楼 2006-12-07 16:36 代码乱了      
怎么用?
  回复  引用  查看    

#2楼 2006-12-07 16:54 木野狐      
不错,收藏了。本质是对 Console 的读取行为的一个模拟实现。
  回复  引用  查看    

#3楼 2007-01-17 22:31 超宝[未注册用户]
有几点不好。
一、如果上次输出未换行,则程序在第一个键输入时出错,因为CursorLeft初始不为0。
二、未判断回车键的情形,导致按回车会Beep一声。
三、在组合字符串时未使用StringBuilder,效率有所影响。
四、某些流程还有优化空间。

  回复  引用    

#4楼 2007-01-17 22:36 超宝[未注册用户]
建议密码字符串用字符数组存储较好,有空我也写一个。
  回复  引用    




发表评论

昵称: [登录] [注册]

主页:

邮箱:(仅博主可见)

评论内容:

  登录  注册

[使用Ctrl+Enter键快速提交评论]

0 585259




相关文章:

相关链接: