The Load button opens the documents folder and you select an image from the documents. The Show button displays vertical and horizontal scrolls, as well as an image. If the dialog box is larger than the image, then scrolls are not needed and will not be displayed. The problem is that the horizontal scroll behaves like a vertical one (that is, scroll up and down instead of scrolling left and right). And the vertical one lags, and sometimes does not scroll at all.
Help, please!
//When you click on the Load button, you can select an image from the files and upload it to the dialog box
void CНадеюсьЭтоСохранитьсяDlg::OnBnClickedLoad()
{
CFileDialog dlg(TRUE, NULL, NULL, OFN_FILEMUSTEXIST, _T(
"Image Files (*.bmp;*.jpg;*.png)|*.bmp;*.jpg;*.png|All Files (*/*)|*.*||"));
if (dlg.DoModal() == IDOK) {
CString path = dlg.GetPathName();
m_Image.Destroy();
HRESULT hr = m_Image.Load(path);
if (SUCCEEDED(hr)) {
AfxMessageBox(_T("Изображение загружено"));
}
else {
AfxMessageBox(_T("Не удалось загрузить изображение"));
}
}
}
//When you click on the Show button, scrollbars appear and an image (loaded using the Load button) is displayed
void CНадеюсьЭтоСохранитьсяDlg::OnBnClickedShow()
{
if (!m_Image.IsNull()) {
int width = m_Image.GetWidth();
int height = m_Image.GetHeight();
CRect rect;
GetClientRect(&rect);
if (height > rect.Height()) {
m_pScroll1->SetScrollRange(0, height - rect.Height());
m_pScroll1->SetScrollPos(0);
m_pScroll1->ShowWindow(SW_SHOW);
}
else {
m_pScroll1->ShowWindow(SW_HIDE);
}
if (width > rect.Width()) {
m_pScroll2->SetScrollRange(0, width - rect.Width());
m_pScroll2->SetScrollPos(0);
m_pScroll2->ShowWindow(SW_SHOW);
}
else {
m_pScroll2->ShowWindow(SW_HIDE);
}
Invalidate();
}
}
//Vertical Scrollbar Handler
void CНадеюсьЭтоСохранитьсяDlg::OnVScroll(UINT nSBCode1, UINT nPos1, CScrollBar* pScrollBar1)
{
CScrollBar* p1 = (CScrollBar*)GetDlgItem(IDC_SCROLLBAR1);
if (pScrollBar1->GetDlgCtrlID() == IDC_SCROLLBAR1) {
int pos = m_pScroll1->GetScrollPos();
switch (nSBCode1) {
case SB_LEFT:
pos = 0; break;
case SB_LINELEFT:
pos -= 1; break;
case SB_LINERIGHT:
pos += 1; break;
case SB_PAGELEFT:
pos -= 5; break;
case SB_PAGERIGHT:
pos += 5; break;
case SB_RIGHT:
pos = 100; break;
case SB_THUMBTRACK:
pos = nPos1; break;
defalt: break;
}
m_pScroll1->SetScrollPos(pos);
Invalidate();
}
CDialog::OnHScroll(nSBCode1, nPos1, pScrollBar1);
}
//Horizontal Scrollbar Handler
void CНадеюсьЭтоСохранитьсяDlg::OnHScroll(UINT nSBCode2, UINT nPos2, CScrollBar* pScrollBar2)
{
CScrollBar* p2 = (CScrollBar*)GetDlgItem(IDC_SCROLLBAR2);
if (pScrollBar2->GetDlgCtrlID() == IDC_SCROLLBAR2) {
int pos = m_pScroll2->GetScrollPos();
switch (nSBCode2) {
case SB_LEFT:
pos = 0; break;
case SB_LINELEFT:
pos -= 1; break;
case SB_LINERIGHT:
pos += 1; break;
case SB_PAGELEFT:
pos -= 5; break;
case SB_PAGERIGHT:
pos += 5; break;
case SB_RIGHT:
pos = 100; break;
case SB_THUMBTRACK:
pos = nPos2; break;
defalt: break;
}
m_pScroll2->SetScrollPos(pos);
Invalidate();
}
CDialog::OnHScroll(nSBCode2, nPos2, pScrollBar2);
}
//Надеюсь Это СохранитьсяDlg.h
// Реализация
protected:
HICON m_hIcon;
// Созданные функции схемы сообщений
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedLoad();
afx_msg void OnBnClickedShow();
afx_msg void OnVScroll(UINT nSBCode1, UINT nPos1, CScrollBar* pScrollBar1);
afx_msg void OnHScroll(UINT nSBCode2, UINT nPos2, CScrollBar* pScrollBar2);
CScrollBar* m_pScroll1;
CImage m_Image;
CScrollBar* m_pScroll2;
};
I don't speak English, so some phrases may be incorrect.
Med is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.