C\#winform 사용자 정의 페이지 넘 기기 컨트롤 상세 설명

C#  winform 에서 사용자 정의 페이지 넘 기기 컨트롤,자체 디자인,참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.
1.주로 컨트롤 바 인 딩 클릭 이벤트 사용  

사용 하 는 컨트롤 은 각각 picturebox 입 니 다.   lable  이전 페이지 pbPagePrev    다음 페이지 pbPageNext  홈 페이지 pbPageBegin   끝 페이지 pbPageEnd  picturebox 컨트롤 에 배경 그림 입 니 다. 
제 1 과  모두  페이지 는 lable"labPageInfo"입 니 다.    lable 에 숨겨 진 textbox 컨트롤"txtPageInfo"를 추가 하 였 습 니 다.
2.이 페이지 넘 기기 기능 을 사용자 컨트롤 uPageTurn 에 따로 적 습 니 다.  그리고 페이지 마다 바로 적용 해 주시 면 됩 니 다. 
다음은 ucPageTurn 만 썼어 요.
winform 페이지 에 도 적용 해 야 합 니 다.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Demo
{
 public partial class ucPageTurn : UserControl, IMessageFilter
 {
  private const int SIZE_HEIGHT = 40;
  private const int SIZE_MINWIDTH = 84;
  private const int SIZE_INFO_MINWIDTH = 188;

  public ucPageTurn()
  {
   Application.AddMessageFilter(this); 

   InitializeComponent();
   this.BorderStyle = System.Windows.Forms.BorderStyle.None;
   this.MinimumSize = new Size(SIZE_MINWIDTH, 0);
   this.Disposed += new EventHandler(ucPages_Disposed);
   
   //this.MouseClick += new MouseEventHandler(ucKeyboard_Close);

   PageChanged += new PageChangedHandle(new PageChangedHandle((oldPage, newPage, e) => { }));
   InputGotFocus += new InputFocusHandle(new InputFocusHandle((sender, e) => { })); 
   //InputLostFocus += new InputFocusHandle(new InputFocusHandle((sender, e) => { }));

   InputGotFocus += new InputFocusHandle(new InputFocusHandle((sender, e) => { })); 
   InputLostFocus += new InputFocusHandle(new InputFocusHandle((sender, e) => { }));

   InputMouseDown += new InputMouseHandle(new InputMouseHandle((sender, e) => { }));
   InputMouseUp += new InputMouseHandle(new InputMouseHandle((sender, e) => { }));
   InputTextClick += new EventHandler(new EventHandler((sender, e) => { }));
   InputKeyDown += new InputKeyHandle(new InputKeyHandle((sender, e) => { }));
   InputKeyUp += new InputKeyHandle(new InputKeyHandle((sender, e) => { }));
   InputKeyPress += new InputKeyPressHandle(new InputKeyPressHandle((sender, e) => { }));
   InputTextChanged += new EventHandler(new EventHandler((sender, e) => { }));

   this.BackColor = Color.White;
   labPageInfo.BackColor = this.BackColor;

   this.Resize += new EventHandler(ucPages_Resize);

   //labPageInfo.MouseDoubleClick += new MouseEventHandler(labPageInfo_MouseDoubleClick);

   pbPage_Prev.MouseDown += new MouseEventHandler(pbPage_Prev_MouseDown);
   pbPage_Prev.MouseUp += new MouseEventHandler(pbPage_Prev_MouseUp);

   pbPage_Next.MouseDown += new MouseEventHandler(pbPage_Next_MouseDown);
   pbPage_Next.MouseUp += new MouseEventHandler(pbPage_Next_MouseUp);

   pbPage_Begin.MouseDown += new MouseEventHandler(pbPage_Begin_MouseDown);
   pbPage_Begin.MouseUp += new MouseEventHandler(pbPage_Begin_MouseUp);

   pbPage_End.MouseDown += new MouseEventHandler(pbPage_End_MouseDown);
   pbPage_End.MouseUp += new MouseEventHandler(pbPage_End_MouseUp);

   txtPageInfo.TextChanged += new EventHandler(txtPageInfo_TextChanged);
   txtPageInfo.GotFocus += new EventHandler(txtPageInfo_GotFocus);
   txtPageInfo.Click += new EventHandler(txtPageInfo_Click);
   txtPageInfo.Text = m_strText;
   txtPageInfo.Visible = m_blnShowTxtPageInfo;

   m_blnIsAutoJump = false;
   m_timerAutoPage.Enabled = false;
   m_timerAutoPage.Interval = WAIT_FOR_AUTOJUMP;
   m_timerAutoPage.Tick += new EventHandler(timerAutoPage_Tick);
  }

  private void ucPages_Load(object sender, EventArgs e)
  {
   setStatus();
  }

  private void ucPages_Disposed(object sender, EventArgs e)
  {
   Application.RemoveMessageFilter(this);
  }

  public bool PreFilterMessage(ref System.Windows.Forms.Message MyMessage)
  {
   if (MyMessage.Msg == 0x204 || MyMessage.Msg == 0x205)
   {
    return true;
   }
   return false;
  }

  //          
  private void ucPages_Resize(object sender, EventArgs e)
  {
   this.Height = SIZE_HEIGHT;

   pbPage_Begin.Location = new Point(0, 0);
   pbPage_Begin.Size = new Size(SIZE_HEIGHT, SIZE_HEIGHT);

   pbPage_Prev.Location = new Point(pbPage_Begin.Width + 2, pbPage_Begin.Top);
   pbPage_Prev.Size = pbPage_Begin.Size;

   pbPage_End.Location = new Point(this.Width - pbPage_End.Width, pbPage_Begin.Top);
   pbPage_End.Size = pbPage_Begin.Size;

   pbPage_Next.Location = new Point(this.Width - pbPage_Next.Width - pbPage_End.Width - 2, pbPage_Begin.Top);
   pbPage_Next.Size = pbPage_Begin.Size;

   if (this.Width < SIZE_INFO_MINWIDTH)
   {
    labPageInfo.Visible = false;
    txtPageInfo.Visible = false;
   }
   else
   {
    labPageInfo.Location = new Point(pbPage_Prev.Width + pbPage_Prev.Width + 3, 2);
    labPageInfo.Size = new Size(pbPage_Next.Left - labPageInfo.Left - 3, pbPage_Prev.Height);
    
    txtPageInfo.Location = new Point(pbPage_Prev.Left + pbPage_Prev.Width + 5, 11);
    //txtPageInfo.Size = new Size(pbPage_Next.Left - labPageInfo.Left , 15);

    if (m_blnShowLabel && !labPageInfo.Visible) labPageInfo.Visible = true;
    if (m_blnShowLabel && !txtPageInfo.Visible) txtPageInfo.Visible = true;
   }

   if (m_blnShowTxtPageInfo)
   {
    txtPageInfo.Size = new Size(79, labPageInfo.Height);
   }
   else
   {
    txtPageInfo.Size = new Size(0, labPageInfo.Height);
   }
  }

  //  lablelabPageInfo   txtPageInfo
  private void labPageInfo_Click(object sender, EventArgs e)
  {
   if (!txtPageInfo.Visible)
   {
    showJumpPageStatus(true);
    InputTextClick(txtPageInfo, new EventArgs());
   }
   else {
    showJumpPageStatus(false);
   }
  }

  public void showJumpPageStatus(Boolean isShow)
  {
   if (isShow)
   {
    txtPageInfo.Visible = true;
    txtPageInfo.Text = string.Empty;
    txtPageInfo.Focus();
   }
   else
   {
    txtPageInfo.Visible = false;
   }
  }

  //   
  private void pbPage_Prev_MouseDown(object sender, MouseEventArgs e)
  {
   pbPage_Prev.Image = global::Pku.CFM.Controls.Properties.Resources.Page_Prev_D;
   m_blnIsPrevDown = true;

   m_timerAutoPage.Enabled = true;
  }

  private void pbPage_Prev_MouseUp(object sender, MouseEventArgs e)
  {
   pbPage_Prev.Image = global::Pku.CFM.Controls.Properties.Resources.Page_Prev_N;
   m_blnIsPrevDown = false;

   if (m_blnIsAutoJump)
   {
    leaveAutoJumpMode();
    return;
   }
   m_timerAutoPage.Enabled = false;

   if (1 == m_intCurPageIndex) return;
   int intOldPage = m_intCurPageIndex;
   m_intCurPageIndex--;
   setStatus();
   PageChanged(intOldPage, m_intCurPageIndex, new EventArgs());
  }

  //   
  private void pbPage_Next_MouseDown(object sender, MouseEventArgs e)
  {
  
   pbPage_Next.Image = global::Pku.CFM.Controls.Properties.Resources.Page_Next_D;
   m_blnIsNextDown = true;

   m_timerAutoPage.Enabled = true;
  }

  private void pbPage_Next_MouseUp(object sender, MouseEventArgs e)
  {
   pbPage_Next.Image = global::Pku.CFM.Controls.Properties.Resources.Page_Next_N;
   m_blnIsNextDown = false;

   if (m_blnIsAutoJump)
   {
    leaveAutoJumpMode();
    return;
   }
   m_timerAutoPage.Enabled = false;

   if (m_intPageCount == m_intCurPageIndex) return;
   int intOldPage = m_intCurPageIndex;
   m_intCurPageIndex++;
   setStatus();
   PageChanged(intOldPage, m_intCurPageIndex, new EventArgs());
  }

  //  
  private void pbPage_Begin_MouseDown(object sender, MouseEventArgs e)
  {
   
   pbPage_Begin.Image = global::Pku.CFM.Controls.Properties.Resources.PageView_Begin_D;
   m_blnIsBeginDown = false;
   m_timerAutoPage.Enabled = true;
  }

  private void pbPage_Begin_MouseUp(object sender, MouseEventArgs e)
  {
   pbPage_Begin.Image = global::Pku.CFM.Controls.Properties.Resources.PageView_Begin_N;
   m_blnIsBeginDown = false;

   int intOldPage = m_intCurPageIndex;

   if (1 == m_intCurPageIndex) return;
   m_intCurPageIndex = 1;

   
   setStatus();
   PageChanged(intOldPage, m_intCurPageIndex, new EventArgs());

   m_blnIsAutoJump = false;
   m_timerAutoPage.Stop();
  }

  //  
  private void pbPage_End_MouseDown(object sender, MouseEventArgs e)
  {
   
   pbPage_End.Image = global::Pku.CFM.Controls.Properties.Resources.PageView_End_N;
   m_blnIsEndDown = true;

   m_timerAutoPage.Enabled = true;
  }

  private void pbPage_End_MouseUp(object sender, MouseEventArgs e)
  {
   pbPage_End.Image = global::Pku.CFM.Controls.Properties.Resources.PageView_End_D;
   m_blnIsEndDown = false;

   int intOldPage = m_intCurPageIndex;

   if (m_intCurPageIndex == m_intPageCount)return;
   m_intCurPageIndex = m_intPageCount;
   
   setStatus();
   PageChanged(intOldPage, m_intCurPageIndex, new EventArgs());

   m_blnIsAutoJump = false;
   m_timerAutoPage.Stop();
   
  }
  
  //       
  private void setStatus()
  {
   //     0,          
   if (m_intPageCount <= 0)
   {
    labPageInfo.Text = "";
    pbPage_Prev.Visible = false;
    pbPage_Next.Visible = false;
    pbPage_Begin.Visible = false;
    pbPage_End.Visible = false;
    txtPageInfo.Visible = false;
   }
   else
   {
    //     1,        
    if (1 == m_intPageCount)
    {
     labPageInfo.Text = "";
     pbPage_Prev.Visible = false;
     pbPage_Next.Visible = false;
     pbPage_Begin.Visible = false;
     pbPage_End.Visible = false;
     txtPageInfo.Visible = false;
    }
    else
    {
     //              
     if (m_intCurPageIndex <= 1)
     {
      m_intCurPageIndex = 1;

      pbPage_Prev.Visible = false;
      pbPage_Next.Visible = true;
      pbPage_Begin.Visible = false;
      pbPage_End.Visible = true;
      txtPageInfo.Visible = false;

     }
     //            
     else if (m_intCurPageIndex >= m_intPageCount)
     {
      m_intCurPageIndex = m_intPageCount;

      pbPage_Prev.Visible = true;
      pbPage_Next.Visible = false;
      pbPage_Begin.Visible = true ;
      pbPage_End.Visible = false;
      txtPageInfo.Visible = false;

     }
      //        
     else
     {
      pbPage_Prev.Visible = true;
      pbPage_Next.Visible = true;
      pbPage_Begin.Visible = true;
      pbPage_End.Visible = true;
      txtPageInfo.Visible = false;
     }

     labPageInfo.Text = String.Format(" {0}  / {1} ", m_intCurPageIndex, m_intPageCount);
     txtPageInfo.Text = String.Format("{0}", m_intCurPageIndex);
    }
   }
  }

  private void timerAutoPage_Tick(object sender, EventArgs e)
  {
   if ((m_blnIsNextDown && m_intCurPageIndex >= m_intPageCount) || (m_blnIsPrevDown && m_intCurPageIndex <= 1) || (m_blnIsEndDown && m_intCurPageIndex >=m_intPageCount) || (m_blnIsBeginDown && m_intCurPageIndex<=1))
   {
    leaveAutoJumpMode();
    return;
   }

   if (!m_blnIsAutoJump) m_timerAutoPage.Interval = AUTOJUMP_INV;

   int intOldPage = m_intCurPageIndex;

   if (m_blnIsNextDown) m_intCurPageIndex++;
   if (m_blnIsPrevDown) m_intCurPageIndex--;

   setStatus();

   PageChanged(intOldPage, m_intCurPageIndex, new EventArgs());

   if ((m_blnIsNextDown && m_intCurPageIndex >= m_intPageCount) || (m_blnIsPrevDown && m_intCurPageIndex <= 1)||(m_blnIsEndDown && m_intCurPageIndex >= m_intPageCount) || (m_blnIsBeginDown && m_intCurPageIndex <= 1))
   {
    leaveAutoJumpMode();
   }
   else
   {
    m_blnIsAutoJump = true;
   }
  }

  private void leaveAutoJumpMode()
  {
   m_blnIsAutoJump = false;
   m_timerAutoPage.Stop();
   m_timerAutoPage.Enabled = false;
   m_timerAutoPage.Interval = WAIT_FOR_AUTOJUMP;
  }

  private int m_intCurPageIndex = 0;

  //    
  public int CurPageIndex
  {
   get { return m_intCurPageIndex; }
   set
   {
    if (value < 0 || (m_intPageCount> 0 && value > m_intPageCount)) return;

    int intOldPage = m_intCurPageIndex;
    m_intCurPageIndex = value;

    setStatus();
    if (!m_blnIgnoreChange) PageChanged(intOldPage, m_intCurPageIndex, new EventArgs());

    m_blnIsAutoJump = false;
    m_timerAutoPage.Stop();
    m_timerAutoPage.Enabled = false;
   }
  }

  //     
  public int PageCount
  {
   get { return m_intPageCount; }
   set 
   { 
    m_intPageCount = value;

    if (m_intPageCount > 0)
    {
     if (m_intCurPageIndex <= 0) m_intCurPageIndex = 0;

     if (m_intCurPageIndex > m_intPageCount)
     {
      m_intCurPageIndex = m_intPageCount;
     }
    }
    else
    {
     m_intCurPageIndex = 0;
    }

    setStatus();
   }
  }

  private Boolean m_blnIgnoreChange = false;
  public Boolean IgnoreChange
  {
   get { return m_blnIgnoreChange; }
   set { m_blnIgnoreChange = value; }
  }

  private Boolean m_blnShowLabel = true;
  public Boolean ShowLabel
  {
   get { return m_blnShowLabel; }
   set { m_blnShowLabel = value; labPageInfo.Visible = value; }
  }

  private void txtPageInfo_TextChanged(object sender, EventArgs e)
  {
   if (m_blnIgnTextChange) return;
   m_blnIgnTextChange = true;
   InputTextChanged(sender, e);
  }

  private void txtPageInfo_GotFocus(object sender, EventArgs e)
  {
   InputGotFocus(this, e);
  }

  private void txtPageInfo_LostFocus(object sender, EventArgs e)
  {
   InputLostFocus(this, e);
  }

  private void txtPageInfo_MouseDown(object sender, MouseEventArgs e)
  {
   InputMouseDown(sender, e);
  }

  private void txtPageInfo_MouseUp(object sender, MouseEventArgs e)
  {
   InputMouseUp(sender, e);
  }

  private void txtPageInfo_Click(object sender, EventArgs e)
  {
   InputTextClick(sender, e);
  }

  private void txtPageInfo_KeyDown(object sender, KeyEventArgs e)
  {
   InputKeyDown(sender, e);
  }

  private void txtPageInfo_KeyUp(object sender, KeyEventArgs e)
  {
   InputKeyUp(sender, e);
  }

  private void txtPageInfo_KeyPress(object sender, KeyPressEventArgs e)
  {
   InputKeyPress(sender, e);
  }

  private string m_strText = "";
  public String Text
  {
   get { return m_strText; }
   set { m_strText = value; }
  }

  private System.Windows.Forms.Timer m_timerAutoPage = new Timer();
  private bool m_blnIsPrevDown = false;
  private bool m_blnIsNextDown = false;

  private bool m_blnIsBeginDown = false;
  private bool m_blnIsEndDown = false;

  private bool m_blnIsAutoJump = false;
  public delegate void PageChangedHandle(int oldPage, int newPage, EventArgs e);
  public event PageChangedHandle PageChanged;

  private const int WAIT_FOR_AUTOJUMP = 500;
  private const int AUTOJUMP_INV = 500;

  private int m_intPageCount = 0;
  protected String m_strErrorText = "";
  public String ErrorText
  {
   get { return m_strErrorText; }
   set { m_strErrorText = value; }
  }

  /// <summary>
  ///         
  /// </summary>
  private Control m_keyboardParent = null;
  public Control KeyboardParent
  {
   get { return m_keyboardParent; }
   set { m_keyboardParent = value; }
  }
  
  /// <summary>
  ///            
  /// </summary>
  private Boolean m_blnShowTxtPageInfo = true;
  public Boolean ShowTxtPageInfo
  {
   get { return m_blnShowTxtPageInfo; }
   set { m_blnShowTxtPageInfo = value; txtPageInfo.Visible = value; }
  }

  //public bool IsPages { get; set; }

  private bool m_blnIgnTextChange = false;
  private Boolean m_blnIsInputFocus = false;
  
  //public event EventHandler InputEnterPressed;
  //public event EventHandler InputClearPressed;

  public delegate void InputFocusHandle(object sender, EventArgs e);
  public event InputFocusHandle InputGotFocus;
  public event InputFocusHandle InputLostFocus;

  public delegate void InputMouseHandle(object sender, MouseEventArgs e);
  public event InputMouseHandle InputMouseDown;
  public event InputMouseHandle InputMouseUp;

  public event EventHandler InputTextClick;

  public delegate void InputKeyHandle(object sender, KeyEventArgs e);
  public event InputKeyHandle InputKeyDown;
  public event InputKeyHandle InputKeyUp;
  //public event InputKeyHandle InputTextKeyBoardEnter;

  public delegate void InputKeyPressHandle(object sender, KeyPressEventArgs e);
  public event InputKeyPressHandle InputKeyPress;

  public event EventHandler InputTextChanged;
  

  public TextBox InputTextBox
  {
   set { txtPageInfo=value; }
   get { return txtPageInfo; }
  }

  public String InputText
  {
   get
   {
    if (m_strText == txtPageInfo.Text || String.IsNullOrWhiteSpace(txtPageInfo.Text))
    {
     return String.Empty;
    }
    else
    {
     return txtPageInfo.Text;
    }
   }
   set
   {
    if (m_blnIsInputFocus)
    {
     txtPageInfo.Text = value;
    }
    else
    {
     if (String.IsNullOrWhiteSpace(value))
     {
      m_blnIgnTextChange = true;
      txtPageInfo.Text = m_strText;
      m_blnIgnTextChange = false;
     }
    }
   }
  }

  public void setInputText(String text)
  {
   txtPageInfo.Text = text;
  }
 }
}
3.자원 관리 파일 에 생 성 된 control 폴 더 에 사용자 컨트롤 uPageTurn 을 만 들 었 습 니 다.도구 모음 에 방금 쓴 uPageTurn 구성 요 소 를 자동 으로 표시 합 니 다.페이지 에 끌 어 다 놓 고 이 구성 요소 의 이름 을 uPageTurn 이 라 고 합 니 다.그러면 ucpageturn 안의 컨트롤 을 편집 할 수 있 습 니 다.

// initControls    textbox      
 private void initControls()
  
 {
  ucPages.InputTextBox.KeyPress +=new KeyPressEventHandler(ucPages_KeyPress);
   ucPages.PageChanged += new Pku.CFM.Controls.ucPageTurn.PageChangedHandle(ucPages_PageChanged);
}

 private void ucPages_KeyPress(object sender, KeyPressEventArgs e)
  {
   if (13 == e.KeyChar)
   {
    int intReturn = SysDefine.NOTHING;
    String strInputText = ucPages.Text.ToUpper();
    if (SysDefine.FAILED == intReturn)
    {
     MessageBox.Show(this.ErrorText, "  ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    }
    
    String pageInputText = ucPages.InputText;
    int page = 0;
    try
    {
     page = int.Parse(pageInputText);
    }
    catch
    {
     page = 1;
    }
    ucPages.CurPageIndex = page;
    if (SysDefine.FAILED == refreshList(page, strInputText))
    {
     MessageBox.Show(this.ErrorText, "  ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    }
   }
  }

 private void ucPages_PageChanged(int oldPage, int newPage, EventArgs e)
  {
   int intReturn = SysDefine.NOTHING;

   String strInputText = ucPages.Text.ToUpper();
   intReturn = refreshList(newPage, strInputText);
   if (SysDefine.FAILED == intReturn)
   {
    MessageBox.Show(this.ErrorText, "  ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    return;
   }
  }
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기