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) { //點擊一個有聲讀物,進入其詳細窗口 //待補充 } |