本人自己制作了一个基于cdialog based的窗口,窗口比较宽,超出了我笔记本的宽度,所以自己添加了一个 横方向的滚动条
,经过一些列的代码添加,现在滚动挑可以滚动,但是窗口界面不移动,超出的界面看不到,请问是否是还要在OnPaint()事件里面添加什么么?首先我列出我修改的代码
一,新建一个基于对话框的工程Ex081122。
二,在上面增加一个滚动条控件,并为其关联一个水平滚动条控件,命名为(IDC_SCROLLBAR2
三,在CEx081122Dlg::OnInITDialog的初始化函数中加上如下代码:
CScrollBar* pSB=(CScrollBar*)GetDlgItem(IDC_SCROLLBAR2); //这里用于选择是那个进度条
pSB->SetScrollRange(0,100);//n
m_scroll.SetScrollPos(0);
四,利用类向导为主对话框增加WM_HSCROLL消息的响应函数。
void CEx081122Dlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
if(SB_LINERIGHT == nSBCode)
{//点击右键头
int iCurPos = pScrollBar->GetScrollPos();
pScrollBar->SetScrollPos(iCurPos+1);
CString strTITle ;
strTITle.Format("滚动条的值是:%d",iCurPos+1);
SetWindowText(strTITle);
}
else if( SB_THUMBPOSITION == nSBCode )
{//拖动
pScrollBar->SetScrollPos(nPos);
CString strTITle ;
strTITle.Format("滚动条的值是:%d",nPos);
SetWindowText(strTITle);
} CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
现在添加完后,滚动条是可以滚动了,但是那个主窗口dialog不移动,导致右边超出部分界面看不到,各位高手请帮忙
,经过一些列的代码添加,现在滚动挑可以滚动,但是窗口界面不移动,超出的界面看不到,请问是否是还要在OnPaint()事件里面添加什么么?首先我列出我修改的代码
一,新建一个基于对话框的工程Ex081122。
二,在上面增加一个滚动条控件,并为其关联一个水平滚动条控件,命名为(IDC_SCROLLBAR2
三,在CEx081122Dlg::OnInITDialog的初始化函数中加上如下代码:
CScrollBar* pSB=(CScrollBar*)GetDlgItem(IDC_SCROLLBAR2); //这里用于选择是那个进度条
pSB->SetScrollRange(0,100);//n
m_scroll.SetScrollPos(0);
四,利用类向导为主对话框增加WM_HSCROLL消息的响应函数。
void CEx081122Dlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
if(SB_LINERIGHT == nSBCode)
{//点击右键头
int iCurPos = pScrollBar->GetScrollPos();
pScrollBar->SetScrollPos(iCurPos+1);
CString strTITle ;
strTITle.Format("滚动条的值是:%d",iCurPos+1);
SetWindowText(strTITle);
}
else if( SB_THUMBPOSITION == nSBCode )
{//拖动
pScrollBar->SetScrollPos(nPos);
CString strTITle ;
strTITle.Format("滚动条的值是:%d",nPos);
SetWindowText(strTITle);
} CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
现在添加完后,滚动条是可以滚动了,但是那个主窗口dialog不移动,导致右边超出部分界面看不到,各位高手请帮忙
1.我的窗口里面放了30多个按钮,其余没什么,您的意思是我把这些按钮都放到edit控件里面么?
2 请问edit控件放上去之后,我把按钮都放到这个大的eidt控件里面,之后应该怎样弄呢?能否详细说明下呢?给个代码,辛苦啦!
前辈,有2个问题
1.我的窗口里面放了30多个按钮,其余没什么,您的意思是我把这些按钮都放到edit控件里面么?
2 请问edit控件放上去之后,我把按钮都放到这个大的eidt控件里面,之后应该怎样弄呢?能否详细说明下呢?给个代码,辛苦啦!
这是没有改动的代码,大家看下该怎么改?假设这里从偏离对话框最左边40像素开始绘图
void CMy2017Dlg::OnPaint()
{ if (IsIconic())
{
CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle中心在客户端矩形图标
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon
dc.DrawIcon(x, y, m_hIcon); }
else
{
CDialog::OnPaint();
}
}
对话框的OnPaint是没用的,因为它只能重画对话框窗口本身,而你的控件是对话框的子窗口,它们如何重画跟对话框一点关系都没有。
{
// TODO: Add your message handler code here and/or call default
//
int now=m_SbVert.GetScrollPos();
int vert=0;
switch (nSBCode)
{
case SB_LINEUP:
vert=-10;
break;
case SB_LINEDOWN:
vert=10;
break;
case SB_PAGEUP:
vert=-40;
break;
case SB_PAGEDOWN:
vert=40;
break;
}
m_SbVert.SetScrollPos(now+vert);
//clean dlg
Invalidate();//清除对话框
//
int ID=0;
CWnd *hwndChild = this->GetWindow(GW_CHILD);
HWND hChild = ::GetWindow(hwndChild->m_hWnd,GW_HWNDFIRST);
while(hChild)
{//枚举所有子窗口,因为有几个是动态产生的,随意的ID
ID=::GetDlgCtrlID(hChild);
if(IDC_SCROLLBAR_VERT==ID)
{//滚动条不能移动
hChild = ::GetWindow(hChild,GW_HWNDNEXT);
continue;
}
// char txt[260];
// ::GetWindowText(hChild,txt,260);
// afxDump << txt << " " << ID << "\n";;
CRect rc;
::GetWindowRect(hChild,&rc);//screen
ScreenToClient(rc);
rc.top += vert;
rc.bottom += vert;
::MoveWindow(hChild,rc.left,rc.top,rc.Width(),rc.Height(),FALSE);
//
hChild = ::GetWindow(hChild,GW_HWNDNEXT);
}
}
http://www.codeproject.com/KB/dialog/scrolling_support.aspx
(一):修改你的第二步:
“二,在上面增加一个滚动条控件,并为其关联一个水平滚动条控件,命名为IDC_SCROLLBAR2”
这是不必的,你将对话框的属性“Horizontal scroll”选上,窗口就出现了水平滚动条控件。再增加WM_HSCROLL消息的响应函数就可以了。
(二):也采用你的第二步:
“二,在上面增加一个滚动条控件,并为其关联一个水平滚动条控件,命名为IDC_SCROLLBAR2”
(1)接着在classWizard中为水平滚动条控件添加变量CScrollBar m_prejilu;
(2)在OnInitDialog函数中加m_prejilu.SetScrollRange(0,250);
(3)增加WM_HSCROLL消息的响应函数:
void CMy1Dlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
int minpos;
int maxpos;
pScrollBar->GetScrollRange(&minpos, &maxpos);
maxpos = pScrollBar->GetScrollLimit(); // Get the current position of scroll box.
int curpos = pScrollBar->GetScrollPos(); // Determine the new position of scroll box.
switch (nSBCode)
{
case SB_LEFT: // Scroll to far left.
curpos = minpos;
break; case SB_RIGHT: // Scroll to far right.
curpos = maxpos;
break; case SB_ENDSCROLL: // End scroll.
break; case SB_LINELEFT: // Scroll left.
if (curpos > minpos)
curpos--;
break; case SB_LINERIGHT: // Scroll right.
if (curpos < maxpos)
curpos++;
break; case SB_PAGELEFT: // Scroll one page left.
{
// Get the page size.
SCROLLINFO info;
pScrollBar->GetScrollInfo(&info, SIF_ALL);
if (curpos > minpos)
curpos = max(minpos, curpos - (int) info.nPage);
}
break; case SB_PAGERIGHT: // Scroll one page right.
{
// Get the page size.
SCROLLINFO info;
pScrollBar->GetScrollInfo(&info, SIF_ALL); if (curpos < maxpos)
curpos = min(maxpos, curpos + (int) info.nPage);
}
break; case SB_THUMBPOSITION: // Scroll to absolute position. nPos is the position
curpos = nPos; // of the scroll box at the end of the drag operation.
break; case SB_THUMBTRACK: // Drag scroll box to specified position. nPos is the
curpos = nPos; // position that the scroll box has been dragged to.
break;
} // Set the new position of the thumb (scroll box).
pScrollBar->SetScrollPos(curpos);
m_prezuobiao=curpos; //qianshendraw();
CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
//m_prejilu.SetScrollPos(nPos);
}