190717-3
스크롤은 별도의 커맨드 메세지가 발생되지 않는다.
void CScrollDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
    switch (nSBCode) {
        case SB_LINELEFT:
            m_nR = max(0, m_nR - 1); // 구간설정 시 팁 max,min을 사용한다.
            break;
        case SB_LINERIGHT:
            m_nR = min(255, m_nR + 1);
            break;
        case SB_PAGELEFT:
            m_nR -= max(0, m_nR - 5);
            break;
        case SB_PAGERIGHT:
            m_nR += min(255, m_nR + 5);
            break;
        case SB_THUMBTRACK://잡고 드래그
            m_nR = nPos; // nPos : 똇을 때의 위치
            break;
    }
    m_ctrlR.SetScrollPos(m_nR);
    CDialogEx::OnHScroll(nSBCode, nPos, pScrollBar);
}
- 스크롤시 값 설정
void CScrollDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
    UINT nT;
    if(&m_ctrlR==pScrollBar)
        nT = m_nR;
    else if (&m_ctrlB == pScrollBar)
        nT = m_nB;
    else if (&m_ctrlG == pScrollBar)
        nT = m_nG;
    switch (nSBCode) {
    case SB_LINELEFT:
        nT = max(0, nT - 1); // 구간설정 시 팁 max,min을 사용한다.
        break;
    case SB_LINERIGHT:
        nT = min(255, nT + 1);
        break;
    case SB_PAGELEFT:
        nT = max(0, nT - 5);
        break;
    case SB_PAGERIGHT:
        nT = min(255, nT + 5);
        break;
    case SB_THUMBTRACK://잡고 드래그
        nT = nPos; // nPos : 똇을 때의 위치
        break;
    }
    if (&m_ctrlR == pScrollBar)
        m_nR = nT;
    else if (&m_ctrlB == pScrollBar)
        m_nB = nT;
    else if (&m_ctrlG == pScrollBar)
        m_nG = nT;
    pScrollBar->SetScrollPos(nT);
    CDialogEx::OnHScroll(nSBCode, nPos, pScrollBar);
}
- 스크롤 이동시 값 설정
void CScrollDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
    int nT = 0;
    if(&m_ctrlR==pScrollBar)
        nT = m_nR;
    else if (&m_ctrlG == pScrollBar)
        nT = m_nG;
    else if (&m_ctrlB == pScrollBar)
        nT = m_nB;
    switch (nSBCode) {
        case SB_LINELEFT:
            nT = max(0, nT - 1); // 구간설정 시 팁 max,min을 사용한다.
            break;
        case SB_LINERIGHT:
            nT = min(255, nT + 1);
            break;
        case SB_PAGELEFT:
            nT = max(0, nT - 5);
            break;
        case SB_PAGERIGHT:
            nT = min(255, nT + 5);
            break;
        case SB_THUMBTRACK://잡고 드래그
            nT = nPos; // nPos : 똇을 때의 위치
            break;
    }
    if (&m_ctrlR == pScrollBar) {
        m_nR = nT;
        m_nER = nT;
    }
    else if (&m_ctrlG == pScrollBar) {
        m_nG = nT;
        m_nEG = nT;
    }
    else if (&m_ctrlB == pScrollBar) {
        m_nB = nT;
        m_nEB = nT;
    }
    UpdateData(FALSE); //변수의 값을 컨트롤로 전송
    pScrollBar->SetScrollPos(nT);
    CDialogEx::OnHScroll(nSBCode, nPos, pScrollBar);
}
- 스크롤시 각각의 스크롤 바가 움직이며 값이 변경되게함 
void CCBView::OnPaint()
{
    CPaintDC dc(this); // device context for painting
                     // TODO: 여기에 메시지 처리기 코드를 추가합니다.
                     // 그리기 메시지에 대해서는 CView::OnPaint()을(를) 호출하지 마십시오.
    CCBDoc* pDoc = GetDocument();
    CList<CRect>& lt = pDoc->m_rtList;
    POSITION pos = lt.GetHeadPosition(); // cf. MFC : 이터레이터를 쓰지 않음. (이중연결리스트)
    while (pos) { // if pos is not NULL
        const CRect& rt = lt.GetNext(pos); // and pos++
        dc.Rectangle(rt);
    }
}
- DC로 그림 그리기 리스트에 있는 사각형 전부 그리기
다이얼로그는 생성자가 이미 정해져있어서 원하는 인수를 넘기지 못함
초기화 함수를 작성하자.
우리 예제에서 프레임의 자식은 뷰다.
void CMainFrame::OnModalless()
{
    CCBView* pView = (CCBView*)this->GetActiveView(); // 자식을 얻어옴 (in this example)
    // if 이미 객체가 있다면 포커스 else 없다면 생성
    if (m_pDlg == NULL){
        m_pDlg = new CScrollDlg;
        m_pDlg->InitDialog(pView->m_brush);
        m_pDlg->Create(IDD_DIALOG2, this); // this : parent setting
        m_pDlg->ShowWindow(SW_SHOW);
    }
    else {
        m_pDlg->SetFocus();
    }
}
[이후 업데이트한 코드]
void CMainFrame::OnModalless()
{
    CCBView* pView = (CCBView*)this->GetActiveView(); // 자식을 얻어옴 (in this example)
    // if 이미 객체가 있다면 포커스 else 없다면 생성
    if (m_pDlg == NULL){
        m_pDlg = new CScrollDlg;
        m_pDlg->InitDialog(pView);
        m_pDlg->Create(IDD_DIALOG2, this); // this : parent setting
        m_pDlg->ShowWindow(SW_SHOW);
    }
    else {
        m_pDlg->SetFocus();
    }
}
pView를 통채로 넘겨서 Brush를 갱신한다.
[CScrollDlg.cpp]
void CScrollDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
    int nT = 0;
    if(&m_ctrlR==pScrollBar)
        nT = m_nR;
    else if (&m_ctrlG == pScrollBar)
        nT = m_nG;
    else if (&m_ctrlB == pScrollBar)
        nT = m_nB;
    switch (nSBCode) {
        case SB_LINELEFT:
            nT = max(0, nT - 1); // 구간설정 시 팁 max,min을 사용한다.
            break;
        case SB_LINERIGHT:
            nT = min(255, nT + 1);
            break;
        case SB_PAGELEFT:
            nT = max(0, nT - 5);
            break;
        case SB_PAGERIGHT:
            nT = min(255, nT + 5);
            break;
        case SB_THUMBTRACK://잡고 드래그
            nT = nPos; // nPos : 똇을 때의 위치
            break;
    }
    if (&m_ctrlR == pScrollBar) {
        m_nR = nT;
        m_nER = nT;
    }
    else if (&m_ctrlG == pScrollBar) {
        m_nG = nT;
        m_nEG = nT;
    }
    else if (&m_ctrlB == pScrollBar) {
        m_nB = nT;
        m_nEB = nT;
    }
    UpdateData(FALSE);
    pScrollBar->SetScrollPos(nT);
    m_pView->UpdateBrushColor(RGB(m_nR, m_nG, m_nB));
    CDialogEx::OnHScroll(nSBCode, nPos, pScrollBar);
}
[CBView.cpp]
void CCBView::UpdateBrushColor(COLORREF brush)
{
    m_brush = brush;
    Invalidate();
}
- 프레임에서 View를 사용
'MFC' 카테고리의 다른 글
| Hide Show Controls Using the Button, List View, Draw Rectangle (0) | 2019.11.09 | 
|---|---|
| [MFC] Creating a square information move dialog (0) | 2019.11.08 | 
| [MFC] Calling up mainframe, Modaless dialog (0) | 2019.11.06 | 
| [MFC] Modal Dialog, Databinding 직접해주기 (0) | 2019.11.05 | 
| [MFC] SetModifiedFlag, 새파일, 직렬화, 다형성을 이용한 도형그리기 (0) | 2019.11.04 |