int ActualImageWidth = 1000;
int ActualImageHeight = 1200;
int WindowWidth = 800;
int WindowHeight = 500;
float resizedPercent;
为了进行水平最佳拟合,我想在不改变实际图像宽高比的情况下将实际图像宽度减小或增加到窗口宽度。此外,我还应根据新调整大小的图像(Window Resized)维护滚动条拇指轨道长度。为此,我做了以下计算
switch(FitType)
{
case FITHORIZONTAL:
//find out whether resize percentage is decrease or increase
if(ActualImageWidth > WindowWidth) //resize decreasing
{
//find out the pecentage of decreased value
resizedPercent = WindowWidth/ActualImageWidth;
} else //resize increasing
{
//find out the pecentage of increased value
resizedPercent = ActualImageWidth/WindowWidth;
}
ResizedWidth = iWndwidth;
ResizedHeight = ActualImageHeight * resizedPercent;
break;
}
为了设置滚动条,在wm_size事件中我使用下面的代码
LRESULT CALLBACK my_wnd_proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static SCROLLINFO si;
static RECT rect;
int WndWidth = 0;
int WndHeight = 0;
case WM_CREATE :
{
//getting image data from here.
return 0;
}
case WM_SIZE :
{
GetWindowRect(hWnd, &rect);
WndWidth = rect.right - rect.left;
WndHeight = rect.bottom - rect.top;
GetScrollInfo(hWnd, SB_VERT, &si);
int yMaxScroll = max((int)ResizedHeight - WndHeight, 0);
int yCurrentScroll = min(si.nPos, yMaxScroll);
si.cbSize = sizeof(si);
si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
si.nMin = 0;
si.nMax = ResizedHeight;
si.nPage = WndHeight;
si.nPos = 0;
SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
InvalidateRect(hWnd, &rect, true);
return 0;
}
case WM_PAINT :
{
//try to draw image here..
return 0;
}
}
我的问题是我无法获得确切的滚动条拇指轨道,因此我无法滚动图像直到图像结束。请用样本
建议正确的逻辑答案 0 :(得分:0)
注意:如果您使用@o_weisman开始发表评论,我会收到通知给我的评论。在我的代码中,我也像这样重写OnVScroll并且它适用于我(你需要调整它来处理窗口的滚动消息:
void CMagDialog::OnVScroll( UINT nSBCode, UINT nPos, CScrollBar* pScrollBar )
{
int nDelta;
int nMaxPos = yMaxScroll;
if(pScrollBar == NULL)
{
switch (nSBCode)
{
case SB_LINEDOWN:
if (m_vScrollPos >= nMaxPos)
return;
nDelta = min(nMaxPos/100,nMaxPos-m_vScrollPos);
break;
case SB_LINEUP:
if (m_vScrollPos <= 0)
return;
nDelta = -min(nMaxPos/100,m_vScrollPos);
break;
case SB_PAGEDOWN:
if (m_vScrollPos >= nMaxPos)
return;
nDelta = min(nMaxPos/5,nMaxPos-m_vScrollPos);
break;
case SB_THUMBPOSITION:
nDelta = (int)nPos - m_vScrollPos;
break;
case SB_PAGEUP:
if (m_vScrollPos <= 0)
return;
nDelta = -min(nMaxPos/5, m_vScrollPos);
break;
default:
return;
}
m_vScrollPos += nDelta;
SetScrollPos(SB_VERT,m_vScrollPos,TRUE);
ScrollWindow(0,-nDelta);
}
}