笔记

·聊天窗设计要点

  //实现IManagedForm<string>接口 
    public partial class ChatForm: Form, IManagedForm<string>
    {
        private string friendID;       
        private MainForm mainForm;  

        public string FormID
        {
            get { return this.friendID; }
        }

        public ChatForm( string _friendID, MainForm _mainForm)
        {
            InitializeComponent();     
            this.friendID = _friendID;
            this.mainForm = _mainForm;
            //预定接收事件            
            this.mainForm.ChatMsgReceived += new CbGeneric<ChatMsg>(this.formMain_chatMsgReceived);
        }  
       
        private void ChatForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            //退订接收事件
            this.mainForm.ChatMsgReceived -= new CbGeneric<ChatMsg>(this.formMain_chatMsgReceived);
        }

        //滚动条靠下显示
        private void richTextBox_display_TextChanged(object sender, EventArgs e)
        {
            this.richTextBox_display.ScrollToCaret();
        }
    }
View Code

·显示聊天信息

       private void ShowChatMsg(ChatMsg chatMsg)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new CbGeneric<ChatMsg>(this.ShowChatMsg), chatMsg);
            }
            else
            {
                this.richTextBox_display.AppendText(chatMsg.SourceUserID + "   " + chatMsg.TimeSent.ToString() + "\r\n");
                this.richTextBox_display.AppendText(chatMsg.MsgText + "\r\n");
                this.richTextBox_Write.Clear();
            }
        }
View Code

·ReachTextBox滚动条靠下显示

 private void richTextBox_display_TextChanged(object sender, EventArgs e)
        {
            this.richTextBox_display.ScrollToCaret();
        }
View Code

·ListView点击其中一个item

   private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button != System.Windows.Forms.MouseButtons.Left)
            {
                return;
            }
            ListViewHitTestInfo info = this.listView1.HitTest(e.Location);
            if (info.Item != null)
            {
                this.ShowChatForm(info.Item.Text);
            }
        }
View Code

· 多栏目Listview的显示项的添加与删除

       private void RemoveItem(UserInfo info)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new CbGeneric<UserInfo>(this.RemoveItem), info);
            }
            else
            {
                this.listView1.Items.Remove(info.ListViewItem);
            }
        }

       private void AddItem(UserInfo info)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new CbGeneric<UserInfo>(this.AddItem), info);
            }
            else
            {
                string[] subItems = { info.UserID, info.AddressText, info.LogonTimeText };
                ListViewItem item = new ListViewItem(subItems);
                info.ListViewItem = item;
                this.listView1.Items.Add(item);
            }
        } 
View Code

·获取在线好友列表

 private void InitializeFriends()
        {
            List<string> list = this.rapidPassiveEngine.BasicOutter.GetAllOnlineUsers();
            foreach (string friendID in list)
            {
                if (friendID != this.userID && !this.ListViewContains(friendID))
                {
                    this.listView1.Items.Add(friendID, 0);
                }
            }
        }
View Code

·配置文件格式

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <!--监听端口-->
    <add key="Port" value="4540"/>   
  </appSettings>
</configuration>

ConfigurationManager.AppSettings["Port"]
View Code

·VS各版本下载 

·Switch语法

           switch (logonResult)
            {
                case LoginResultType.Succeed:                    
                    this.DialogResult = DialogResult.OK;
                    break;
                case LoginResultType.ConnectServerFailed:
                    MessageBox.Show("尝试连接服务器失败!");
                    break;
                case LoginResultType.IDPasswordError:
                    MessageBox.Show("账号密码有误!");
                    break;
                case LoginResultType.Timeout:
                    MessageBox.Show("请求超时!");
                    break;
                case LoginResultType.WrongServerPort:
                    MessageBox.Show("服务端端口配置错误!");
                    break;
            } 
View Code

·try-catch常用格式

            try
            {                            
                 //·······  
            }
            catch (Exception ee)
            {             
                MessageBox.Show(ee.Message);
            }
View Code

·聊天消息显示示例

private void ShowChatMsg(ChatMsg chatMsg)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new CbGeneric<ChatMsg>(this.ShowChatMsg), chatMsg);
            }
            else
            {
                //显示用户ID、时间等信息
                this.richTextBox_display.AppendText(chatMsg.SourceUserID + "   " + chatMsg.TimeSent.ToString() + "\r\n");
                //显示消息内容
                this.richTextBox_display.AppendText(chatMsg.MsgText + "\r\n");
                this.richTextBox_Write.Clear();//清空输入框
            }
        }
View Code

·拼接二进制数组

public static class BufferJointer
    {  
       
        public static byte[] Joint(byte[] buffer1, byte[] buffer2)
        {
            byte[] bufferWhole = new byte[buffer1.Length + buffer2.Length];
            Buffer.BlockCopy(buffer1, 0, bufferWhole, 0, buffer1.Length);
            Buffer.BlockCopy(buffer2, 0, bufferWhole, buffer1.Length, buffer2.Length);
            return bufferWhole;
        }

        public static byte[] Joint(byte[] buffer1, byte[] buffer2, byte[] buffer3)
        {
            return BufferJointer.Joint(BufferJointer.Joint(buffer1, buffer2), buffer3);
        }

        public static byte[] Joint(byte[] buffer1, byte[] buffer2, byte[] buffer3, byte[] buffer4)
        {
            return BufferJointer.Joint(BufferJointer.Joint(buffer1, buffer2, buffer3), buffer4);
        }

        public static byte[] Joint(byte[] buffer1, byte[] buffer2, byte[] buffer3, byte[] buffer4, byte[] buffer5, byte[] buffer6)
        {
            return BufferJointer.Joint(BufferJointer.Joint(buffer1, buffer2, buffer3, buffer4), buffer5, buffer6);
        }     
    }
View Code

·StriveEngine基础类库

·显示聊天窗

  private void ShowChatForm(string friendUserID)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new CbGeneric<string>(this.ShowChatForm), friendUserID);
            }
            else
            {
                ChatForm form = this.chatFormManager.GetForm(friendUserID);
                if (form == null)
                {
                    form = new ChatForm(this.selfID, friendUserID, this, this.tcpPassiveEngine);                  
                    this.chatFormManager.Add(form);
                    form.Show();
                }
                form.Focus();
            }
        }
View Code

·属性无支持字段代码示例

class UserInfo
    {
        public string UserID { get; set; }
        public IPEndPoint Address { get; set; }
        public DateTime LogonTime { get; set; }
        public ListViewItem ListViewItem { get; set; }

        public string LogonTimeText
        {
            get { return this.LogonTime.ToString(); }
        }       

        public string AddressText
        {
            get { return this.Address.ToString(); }
        }

        public UserInfo(string userID, IPEndPoint addr)
        {
            this.UserID = userID;           
            this.Address = addr;
            this.LogonTime = DateTime.Now;           
        }             
    }
View Code

·ASP.Net视频教程 

·Winform视频教程 

·读取配置文件:1.添加引用System.Configuration

ConfigurationManager.AppSettings["oausServerIP"]
int.Parse(ConfigurationManager.AppSettings["oausServerPort"])
View Code

·直接引用类库源码的方法:1.在解决方案中添加现有项目 2.在项目中添加引用,点项目。

·避免foreach遍历集合时修改集合引起的异常:for循环,以集合长度往下递减循环。

·Listview.Items.tag可以存放与该ListviewItem实例所关联的信息,方便根据从Item找到关联信息。

·复制文本到剪切板的方法: Clipboard.SetText();

·控件图像资源

·判断文件是否打开

class FileHelper
    {
        // 首先引用API 函数  

        [DllImport("kernel32.dll")]
        private static extern IntPtr _lopen(string lpPathName, int iReadWrite);
        [DllImport("kernel32.dll")]
        private static extern bool CloseHandle(IntPtr hObject);
        private const int OF_READWRITE = 2;
        private const int OF_SHARE_DENY_NONE = 0x40;
        private readonly IntPtr HFILE_ERROR = new IntPtr(-1);


        /// <summary>   
        /// 检查文件是否已经打开   
        /// </summary>   
        /// <param name="strfilepath">要检查的文件路径</param>          
        /// <returns>-1文件不存在,1文件已经打开,0文件可用未打开</returns>   
        public FileStatus VerifyFileIsOpen(string strfilepath)
        {
            string vFileName = strfilepath;

            // 先检查文件是否存在,如果不存在那就不检查了   
            if (!File.Exists(vFileName))
            {
                return FileStatus.Inexistence;
            }

            // 打开指定文件看看情况   
            IntPtr vHandle = _lopen(vFileName, OF_READWRITE | OF_SHARE_DENY_NONE);
            if (vHandle == HFILE_ERROR)
            { // 文件已经被打开                   
                return FileStatus.Opened;
            }
            CloseHandle(vHandle);

            // 说明文件没被打开,并且可用  

            return FileStatus.Untapped;
        }
    }
View Code

·获取文件名,不含前缀

 string[] fileName = Directory.GetFiles("语音备忘");
            foreach (string name in fileName)
            {
                string shortName = name.Substring(name.LastIndexOf("\\") + 1, name.Length - 1 - name.LastIndexOf("\\"));
                this.toolStripComboBox1.Items.Add(shortName);
            }
View Code

·去掉文件扩展名

Path.GetFileNameWithoutExtension()
View Code

·Combox自动展开

this.toolStripComboBox1.DroppedDown = true;
View Code

·查看自己的公网IP 

·ipconfig查看内网IP 

·ipconfig /all查看物理地址(mac)

·网络编程学习资料 

·三元表达式

this.toolStripLabel_isHandled.Text = this.IsHandled ? "今日已处理" : "今日未处理";
View Code

·构造方法重载

   public AnalyzedFileName(string fullName)
            : this(Path.GetDirectoryName(fullName),
            Path.GetFileNameWithoutExtension(fullName),
            Path.GetExtension(fullName))
        {
        }
View Code

·打开文件夹

 Process.Start("explorer.exe", this.directoryPath); 
View Code

·最小化到托盘,右键退出

·源码分享网站

http://www.51aspx.com/

http://www.mycodes.net/
http://www.16aspx.com/
http://www.hicode.cn/
http://www.codesky.net/
http://www.sufeinet.com/ 

·获取文件夹大小

 public virtual ulong GetNetworkDiskSizeUsed(string clientUserID, string netDiskID)
        {
            ulong size = 0;
            try
            {
                string path = string.Format("{0}{1}\\", this.GetNetworkDiskRootPath(clientUserID ,netDiskID) , clientUserID);
                this.GetDirectorySize(path, ref size);
            }
            catch{ }

            return size;
        }

        private void GetDirectorySize(string dirPath ,ref ulong size)
        {
            string[] entries = System.IO.Directory.GetFileSystemEntries(dirPath);
            foreach (string entry in entries)
            {
                if (Directory.Exists(entry))
                {
                    this.GetDirectorySize(entry, ref size);
                }
                else
                {
                    if (!entry.EndsWith(".tmpe$"))
                    {
                        FileInfo fileInfo = new FileInfo(entry);
                        size += (ulong)fileInfo.Length;
                    }
                }
            }
        }
View Code

 

 

 

  

 

你可能感兴趣的:(笔记)