标签:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
private void frmSearch_Load(object sender, EventArgs e)
{ List<DictionaryEntry> list = new List<DictionaryEntry>(); list.Add(new DictionaryEntry("0","标题")); list.Add(new DictionaryEntry("1","演播")); list.Add(new DictionaryEntry("2","作者")); this.cmb_lei.DisplayMember = "Value"; this.cmb_lei.ValueMember = "Key"; this.cmb_lei.DataSource = list; //this.cmb_lei.SelectedValue = "1"; } //http异步请求的回调函数 public void ResponseCallBack(IAsyncResult result) { string Html = ""; HttpWebRequest req = (HttpWebRequest)result.AsyncState; try { using (HttpWebResponse response = (HttpWebResponse)req.EndGetResponse(result)) { Stream resStream = response.GetResponseStream(); StreamReader sr = new StreamReader(resStream, Encoding.GetEncoding("GB2312")); Html = sr.ReadToEnd(); } } catch(Exception ex) { if (IsDisposed || !this.IsHandleCreated) return; this.Invoke(new Action(() => { MessageBox.Show("查询时出现异常,原因:" + ex.Message); })); return; } //替换掉干扰代码 Html = Html.Replace(@" style=""background-color:#F3F3F3;""", "").Replace(@" style=""color:blue;""", ""); //动态生成Label的字体 Font font = new Font("微软雅黑", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); //正则表达式分析网页,查找查询结果 MatchCollection ms = Regex.Matches(Html, @"<li><a href=""(?<mainlei>[\w]*)/disp_(?<Number>[\d]*).htm""\s*>(?<Title>[\s\S]{1,20}?)</a>\([\d]*\)</li>", RegexOptions.IgnoreCase | RegexOptions.Multiline); if (ms.Count > 0) { //最大宽度 int MaxWidth = 0; Dictionary<string, string> list = new Dictionary<string, string>(); foreach (Match m in ms) { list.Add(string.Format("http://www.tingchina.com/{0}/disp_{1}.htm", m.Groups["mainlei"].Value, m.Groups["Number"].Value), m.Groups["Title"].Value); //获取字体的大小,找到最宽的那个条记录 Size size = TextRenderer.MeasureText(m.Groups["Title"].Value, font); if (size.Width > MaxWidth) { MaxWidth = size.Width; } } if (IsDisposed || !this.IsHandleCreated) return; this.Invoke(new Action(() => { //对结果进行排序 Dictionary<string, string> listAsc = list.OrderBy(o => o.Value).ToDictionary(o => o.Key, p => p.Value); //循环动态生成查询结果. foreach (KeyValuePair<string, string> kvp in listAsc) { Label lbl = new Label(); lbl.Text = kvp.Value; lbl.Tag = kvp.Key; lbl.Cursor = System.Windows.Forms.Cursors.Hand; lbl.Margin = new System.Windows.Forms.Padding(5, 0, 5, 10); lbl.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(192)))), ((int)(((byte)(0))))); lbl.Font = font; lbl.Width = MaxWidth; lbl.Click += new EventHandler(lbl_Click); this.fpnl_Content.Controls.Add(lbl); } })); } else { if (IsDisposed || !this.IsHandleCreated) return; this.Invoke(new Action(() => { MessageBox.Show("没有查找到数据,请更换关键词。"); })); } } private void btn_Search_Click(object sender, EventArgs e) { if (this.txt_key.Text.Trim() == "") { MessageBox.Show("请输入查询关键字."); return; } //循环获得分类编号 string mainlei = "0"; foreach (Control c in this.pnl_Search.Controls) { if (c.GetType().Equals(typeof(RadioButton)) && ((RadioButton)c).Checked) { mainlei = c.Name.Replace("rdo_mainlei", ""); break; } } //发送异步请求,根据关键字查询 string Url = string.Format("http://www.tingchina.com/search1.asp?mainlei={0}&lei={1}&keyword={2}", mainlei, this.cmb_lei.SelectedValue, HttpUtility.UrlEncode(this.txt_key.Text.Trim(),Encoding.GetEncoding("GB2312"))); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Url); request.Method = "GET"; request.BeginGetResponse(new AsyncCallback(ResponseCallBack), request); this.fpnl_Content.Controls.Clear(); } private void lbl_Click(object sender, EventArgs e) { //点击一个有声读物,进入其详细窗口 //待补充 } |