1.主要是使用控制項綁定點擊事件 用到的控制項分別為picturebox lable 上一頁pbPage_Prev 下一頁 pbPage_Next 首頁 pbPage_Begin 尾頁pbPage_End 是picturebox控制項加背景圖 “第 頁/ 共 頁” 是一個lable “labPageInf ...
1.主要是使用控制項綁定點擊事件
用到的控制項分別為picturebox lable 上一頁pbPage_Prev 下一頁 pbPage_Next 首頁 pbPage_Begin 尾頁pbPage_End 是picturebox控制項加背景圖
“第 頁/ 共 頁” 是一個lable “labPageInfo” 在lable上面加了一個隱藏的textbox 控制項 “txtPageInfo”
2.將這個翻頁的功能單獨寫在用戶控制項 ucPageTurn 裡面 然後在每個頁面直接應用就可以了
下麵只是把ucPageTurn寫了出來
還需要在winform頁面上應用上
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Drawing; 5 using System.Data; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace Demo 11 { 12 public partial class ucPageTurn : UserControl, IMessageFilter 13 { 14 private const int SIZE_HEIGHT = 40; 15 private const int SIZE_MINWIDTH = 84; 16 private const int SIZE_INFO_MINWIDTH = 188; 17 18 public ucPageTurn() 19 { 20 Application.AddMessageFilter(this); 21 22 InitializeComponent(); 23 this.BorderStyle = System.Windows.Forms.BorderStyle.None; 24 this.MinimumSize = new Size(SIZE_MINWIDTH, 0); 25 this.Disposed += new EventHandler(ucPages_Disposed); 26 27 //this.MouseClick += new MouseEventHandler(ucKeyboard_Close); 28 29 PageChanged += new PageChangedHandle(new PageChangedHandle((oldPage, newPage, e) => { })); 30 InputGotFocus += new InputFocusHandle(new InputFocusHandle((sender, e) => { })); 31 //InputLostFocus += new InputFocusHandle(new InputFocusHandle((sender, e) => { })); 32 33 InputGotFocus += new InputFocusHandle(new InputFocusHandle((sender, e) => { })); 34 InputLostFocus += new InputFocusHandle(new InputFocusHandle((sender, e) => { })); 35 36 InputMouseDown += new InputMouseHandle(new InputMouseHandle((sender, e) => { })); 37 InputMouseUp += new InputMouseHandle(new InputMouseHandle((sender, e) => { })); 38 InputTextClick += new EventHandler(new EventHandler((sender, e) => { })); 39 InputKeyDown += new InputKeyHandle(new InputKeyHandle((sender, e) => { })); 40 InputKeyUp += new InputKeyHandle(new InputKeyHandle((sender, e) => { })); 41 InputKeyPress += new InputKeyPressHandle(new InputKeyPressHandle((sender, e) => { })); 42 InputTextChanged += new EventHandler(new EventHandler((sender, e) => { })); 43 44 this.BackColor = Color.White; 45 labPageInfo.BackColor = this.BackColor; 46 47 this.Resize += new EventHandler(ucPages_Resize); 48 49 //labPageInfo.MouseDoubleClick += new MouseEventHandler(labPageInfo_MouseDoubleClick); 50 51 pbPage_Prev.MouseDown += new MouseEventHandler(pbPage_Prev_MouseDown); 52 pbPage_Prev.MouseUp += new MouseEventHandler(pbPage_Prev_MouseUp); 53 54 pbPage_Next.MouseDown += new MouseEventHandler(pbPage_Next_MouseDown); 55 pbPage_Next.MouseUp += new MouseEventHandler(pbPage_Next_MouseUp); 56 57 pbPage_Begin.MouseDown += new MouseEventHandler(pbPage_Begin_MouseDown); 58 pbPage_Begin.MouseUp += new MouseEventHandler(pbPage_Begin_MouseUp); 59 60 pbPage_End.MouseDown += new MouseEventHandler(pbPage_End_MouseDown); 61 pbPage_End.MouseUp += new MouseEventHandler(pbPage_End_MouseUp); 62 63 txtPageInfo.TextChanged += new EventHandler(txtPageInfo_TextChanged); 64 txtPageInfo.GotFocus += new EventHandler(txtPageInfo_GotFocus); 65 txtPageInfo.Click += new EventHandler(txtPageInfo_Click); 66 txtPageInfo.Text = m_strText; 67 txtPageInfo.Visible = m_blnShowTxtPageInfo; 68 69 m_blnIsAutoJump = false; 70 m_timerAutoPage.Enabled = false; 71 m_timerAutoPage.Interval = WAIT_FOR_AUTOJUMP; 72 m_timerAutoPage.Tick += new EventHandler(timerAutoPage_Tick); 73 } 74 75 private void ucPages_Load(object sender, EventArgs e) 76 { 77 setStatus(); 78 } 79 80 private void ucPages_Disposed(object sender, EventArgs e) 81 { 82 Application.RemoveMessageFilter(this); 83 } 84 85 public bool PreFilterMessage(ref System.Windows.Forms.Message MyMessage) 86 { 87 if (MyMessage.Msg == 0x204 || MyMessage.Msg == 0x205) 88 { 89 return true; 90 } 91 return false; 92 } 93 94 //設置控制項的自適應大小 95 private void ucPages_Resize(object sender, EventArgs e) 96 { 97 this.Height = SIZE_HEIGHT; 98 99 pbPage_Begin.Location = new Point(0, 0); 100 pbPage_Begin.Size = new Size(SIZE_HEIGHT, SIZE_HEIGHT); 101 102 pbPage_Prev.Location = new Point(pbPage_Begin.Width + 2, pbPage_Begin.Top); 103 pbPage_Prev.Size = pbPage_Begin.Size; 104 105 pbPage_End.Location = new Point(this.Width - pbPage_End.Width, pbPage_Begin.Top); 106 pbPage_End.Size = pbPage_Begin.Size; 107 108 pbPage_Next.Location = new Point(this.Width - pbPage_Next.Width - pbPage_End.Width - 2, pbPage_Begin.Top); 109 pbPage_Next.Size = pbPage_Begin.Size; 110 111 if (this.Width < SIZE_INFO_MINWIDTH) 112 { 113 labPageInfo.Visible = false; 114 txtPageInfo.Visible = false; 115 } 116 else 117 { 118 labPageInfo.Location = new Point(pbPage_Prev.Width + pbPage_Prev.Width + 3, 2); 119 labPageInfo.Size = new Size(pbPage_Next.Left - labPageInfo.Left - 3, pbPage_Prev.Height); 120 121 txtPageInfo.Location = new Point(pbPage_Prev.Left + pbPage_Prev.Width + 5, 11); 122 //txtPageInfo.Size = new Size(pbPage_Next.Left - labPageInfo.Left , 15); 123 124 if (m_blnShowLabel && !labPageInfo.Visible) labPageInfo.Visible = true; 125 if (m_blnShowLabel && !txtPageInfo.Visible) txtPageInfo.Visible = true; 126 } 127 128 if (m_blnShowTxtPageInfo) 129 { 130 txtPageInfo.Size = new Size(79, labPageInfo.Height); 131 } 132 else 133 { 134 txtPageInfo.Size = new Size(0, labPageInfo.Height); 135 } 136 } 137 138 //點擊lablelabPageInfo 顯示txtPageInfo 139 private void labPageInfo_Click(object sender, EventArgs e) 140 { 141 if (!txtPageInfo.Visible) 142 { 143 showJumpPageStatus(true); 144 InputTextClick(txtPageInfo, new EventArgs()); 145 } 146 else { 147 showJumpPageStatus(false); 148 } 149 } 150 151 public void showJumpPageStatus(Boolean isShow) 152 { 153 if (isShow) 154 { 155 txtPageInfo.Visible = true; 156 txtPageInfo.Text = string.Empty; 157 txtPageInfo.Focus(); 158 } 159 else 160 { 161 txtPageInfo.Visible = false; 162 } 163 } 164 165 //上一頁 166 private void pbPage_Prev_MouseDown(object sender, MouseEventArgs e) 167 { 168 pbPage_Prev.Image = global::Pku.CFM.Controls.Properties.Resources.Page_Prev_D; 169 m_blnIsPrevDown = true; 170 171 m_timerAutoPage.Enabled = true; 172 } 173 174 private void pbPage_Prev_MouseUp(object sender, MouseEventArgs e) 175 { 176 pbPage_Prev.Image = global::Pku.CFM.Controls.Properties.Resources.Page_Prev_N; 177 m_blnIsPrevDown = false; 178 179 if (m_blnIsAutoJump) 180 { 181 leaveAutoJumpMode(); 182 return; 183 } 184 m_timerAutoPage.Enabled = false; 185 186 if (1 == m_intCurPageIndex) return; 187 int intOldPage = m_intCurPageIndex; 188 m_intCurPageIndex--; 189 setStatus(); 190 PageChanged(intOldPage, m_intCurPageIndex, new EventArgs()); 191 } 192 193 //下一頁 194 private void pbPage_Next_MouseDown(object sender, MouseEventArgs e) 195 { 196 197 pbPage_Next.Image = global::Pku.CFM.Controls.Properties.Resources.Page_Next_D; 198 m_blnIsNextDown = true; 199 200 m_timerAutoPage.Enabled = true; 201 } 202 203 private void pbPage_Next_MouseUp(object sender, MouseEventArgs e) 204 { 205 pbPage_Next.Image = global::Pku.CFM.Controls.Properties.Resources.Page_Next_N; 206 m_blnIsNextDown = false; 207 208 if (m_blnIsAutoJump) 209 { 210 leaveAutoJumpMode(); 211 return; 212 } 213 m_timerAutoPage.Enabled = false; 214 215 if (m_intPageCount == m_intCurPageIndex) return; 216 int intOldPage = m_intCurPageIndex; 217 m_intCurPageIndex++; 218 setStatus(); 219 PageChanged(intOldPage, m_intCurPageIndex, new EventArgs()); 220 } 221 222 //首頁 223 private void pbPage_Begin_MouseDown(object sender, MouseEventArgs e) 224 { 225 226 pbPage_Begin.Image = global::Pku.CFM.Controls.Properties.Resources.PageView_Begin_D; 227 m_blnIsBeginDown = false; 228 m_timerAutoPage.Enabled = true; 229 } 230 231 private void pbPage_Begin_MouseUp(object sender, MouseEventArgs e) 232 { 233 pbPage_Begin.Image = global::Pku.CFM.Controls.Properties.Resources.PageView_Begin_N; 234 m_blnIsBeginDown = false; 235 236 int intOldPage = m_intCurPageIndex; 237 238 if (1 == m_intCurPageIndex) return; 239 m_intCurPageIndex = 1; 240 241 242 setStatus(); 243 PageChanged(intOldPage, m_intCurPageIndex, new EventArgs()); 244 245 m_blnIsAutoJump = false; 246 m_timerAutoPage.Stop(); 247 } 248 249 //尾頁 250 private void pbPage_End_MouseDown(object sender, MouseEventArgs e) 251 { 252 253 pbPage_End.Image = global::Pku.CFM.Controls.Properties.Resources.PageView_End_N; 254 m_blnIsEndDown = true; 255 256 m_timerAutoPage.Enabled = true; 257 } 258 259 private void pbPage_End_MouseUp(object sender, MouseEventArgs e) 260 { 261 pbPage_End.Image = global::Pku.CFM.Controls.Properties.Resources.PageView_End_D; 262 m_blnIsEndDown = false; 263 264 int intOldPage = m_intCurPageIndex; 265 266 if (m_intCurPageIndex == m_intPageCount)return; 267 m_intCurPageIndex = m_intPageCount; 268 269 setStatus(); 270 PageChanged(intOldPage, m_intCurPageIndex, new EventArgs()); 271 272 m_blnIsAutoJump = false; 273 m_timerAutoPage.Stop(); 274 275 } 276 277 //翻頁按鈕的狀態 278 private void setStatus() 279 { 280 //如果頁數為0,翻頁的按鈕全部不顯示 281 if (m_intPageCount <= 0) 282 { 283 labPageInfo.Text = ""; 284 pbPage_Prev.Visible = false; 285 pbPage_Next.Visible = false; 286 pbPage_Begin.Visible = false; 287 pbPage_End.Visible = false; 288 txtPageInfo.Visible = false; 289 } 290 else 291 { 292 //如果頁數為1,翻頁的按鈕不顯示 293 if (1 == m_intPageCount) 294 { 295 labPageInfo.Text = ""; 296 pbPage_Prev.Visible = false; 297 pbPage_Next.Visible = false; 298 pbPage_Begin.Visible = false; 299 pbPage_End.Visible = false; 300 txtPageInfo.Visible = false; 301 } 302 else 303 { 304 //只顯示下一頁和最後一頁的按鈕 305 if (m_intCurPageIndex <= 1) 306 { 307 m_intCurPageIndex = 1; 308 309 pbPage_Prev.Visible = false; 310 pbPage_Next.Visible = true; 311 pbPage_Begin.Visible = false; 312 pbPage_End.Visible = true; 313 txtPageInfo.Visible = false; 314 315 } 316 //只顯示上一頁和首頁的按鈕 317 else if (m_intCurPageIndex >= m_intPageCount) 318 { 319 m_intCurPageIndex = m_intPageCount; 320 321 pbPage_Prev.Visible = true; 322 pbPage_Next.Visible = false; 323 pbPage_Begin.Visible = true ; 324 pbPage_End.Visible = false; 325 txtPageInfo.Visible = false; 326 327 } 328 //否則按鈕全部顯示 329 else 330 { 331 pbPage_Prev.Visible = true; 332 pbPage_Next.Visible = true; 333 pbPage_Begin.Visible = true; 334 pbPage_End.Visible = true; 335 txtPageInfo.Visible = false; 336 } 337 338 labPageInfo.Text = String.Format("第{0}頁 /共{1}頁", m_intCurPageIndex, m_intPageCount); 339 txtPageInfo.Text = String.Format("{0}", m_intCurPageIndex); 340 } 341 } 342 } 343 344 private void timerAutoPage_Tick(object sender, EventArgs e) 345 { 346 if ((m_blnIsNextDown && m_intCurPageIndex >= m_intPageCount) || (m_blnIsPrevDown && m_intCurPageIndex <= 1) || (m_blnIsEndDown && m_intCurPageIndex >=m_intPageCount) || (m_blnIsBeginDown && m_intCurPageIndex<=1)) 347 { 348 leaveAutoJumpMode(); 349 return; 350 } 351 352 if (!m_blnIsAutoJump) m_timerAutoPage.Interval = AUTOJUMP_INV; 353 354 int intOldPage = m_intCurPageIndex; 355 356 if (m_blnIsNextDown) m_intCurPageIndex++; 357 if (m_blnIsPrevDown) m_intCurPageIndex--; 358 359 setStatus(); 360 361 PageChanged(intOldPage, m_intCurPageIndex, new EventArgs()); 362 363 if ((m_blnIsNextDown && m_intCurPageIndex >= m_intPageCount) || (m_blnIsPrevDown && m_intCurPageIndex <= 1)||(m_blnIsEndDown && m_intCurPageIndex >= m_intPageCount) || (m_blnIsBeginDown && m_intCurPageIndex <= 1)) 364 { 365 leaveAutoJumpMode(); 366 } 367 else 368 { 369 m_blnIsAutoJump = true; 370 } 371 } 372 373 private void leaveAutoJumpMode() 374 { 375 m_blnIsAutoJump = false; 376 m_timerAutoPage.Stop(); 377 m_timerAutoPage.Enabled = false; 378 m_timerAutoPage.Interval = WAIT_FOR_AUTOJUMP; 379 } 380 381 private int m_intCurPageIndex = 0; 382 383 //當前頁面 384 public int CurPageIndex 385 { 386 get { return m_intCurPageIndex; } 387 set 388 { 389 if (value < 0 || (m_intPageCount> 0 && value > m_intPageCount)) return; 390 391 int intOldPage = m_intCurPageIndex; 392 m_intCurPageIndex = value; 393 394 setStatus(); 395 if (!m_blnIgnoreChange) PageChanged(intOldPage, m_intCurPageIndex, new EventArgs()); 396 397 m_blnIsAutoJump = false; 398 m_timerAutoPage.Stop(); 399 m_timerAutoPage.Enabled = false; 400 } 401 } 402 403 //計算總頁數 404 public int PageCount 405 { 406 get { return m_intPageCount; } 407 set 408 { 409 m_intPageCount = value; 410 411 if (m_intPageCount > 0) 412 { 413 if (m_intCurPageIndex <= 0) m_intCurPageIndex = 0; 414 415 if (m_intCurPageIndex > m_intPageCount) 416 { 417 m_intCurPageIndex = m_intPageCount; 418 } 419 } 420 else 421 { 422 m_intCurPageIndex = 0; 423 } 424 425 setStatus(); 426 } 427 } 428 429 private Boolean m_blnIgnoreChange = false; 430 public Boolean IgnoreChange 431 { 432 get { return m_blnIgnoreChange; } 433 set { m_blnIgnoreChange = value; } 434 } 435 436 private Boolean m_blnShowLabel = true; 437 public Boolean ShowLabel 438 { 439 get { return m_blnShowLabel; } 440 set { m_blnShowLabel = value; labPageInfo.Visible = value; } 441 } 442 443 private void txtPageInfo_TextChanged(object sender, EventArgs e) 444 { 445 if (m_blnIgnTextChange) return; 446 m_blnIgnTextChange = true; 447 InputTextChanged(sender, e); 448 } 449 450 private void txtPageInfo_GotFocus(object sender, EventArgs e) 451 { 452 InputGotFocus(this, e); 453 } 454 455 private void txtPageInfo_LostFocus(object sender, EventArgs e) 456 { 457 InputLostFocus(this, e); 458 } 459 460 private void txtPageInfo_MouseDown(object sender, MouseEventArgs e) 461 { 462 InputMouseDown(sender, e); 463 } 464 465 private void txtPageInfo_MouseUp(object sender, MouseEventArgs e) 466 { 467 InputMouseUp(sender, e); 468 } 469 470 private void txtPageInfo_Click(object sender, EventArgs e) 471 { 472 InputTextClick(sender, e); 473 } 474 475 private void txtPageInfo_KeyDown(object sender, KeyEventArgs e) 476 { 477 InputKeyDown(sender, e); 478 } 479 480 private void txtPageInfo_KeyUp(object sender, KeyEventArgs e) 481 { 482 InputKeyUp(sender, e); 483 } 484 485 private void txtPageInfo_KeyPress(object sender, KeyPressEventArgs e) 486 { 487 InputKeyPress(sender, e); 488 } 489 490 private string m_strText = ""; 491 public String Text 492 { 493 get { return m_strText; } 494 set { m_strText =