/
image_view_dialog.cpp
231 lines (192 loc) · 7.02 KB
/
image_view_dialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// image_view_dialog.cpp : implementation file
//
#include "stdafx.h"
#include "image_view_dialog.h"
#include "graphics.h"
#include "gui_resources.h"
#include "run_cash_monitor.h"
#include "run_cash_monitor_inl.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// ImageViewDialog dialog
IMPLEMENT_DYNAMIC(ImageViewDialog, CDialog)
ImageViewDialog::ImageViewDialog(RunCashMonitor *monitor, int cashIndex, CWnd* pParent /*=NULL*/)
: CDialog(ImageViewDialog::IDD, pParent)
, monitor_(monitor)
, cashIndex_(cashIndex) {
}
ImageViewDialog::~ImageViewDialog() {
}
void ImageViewDialog::DoDataExchange(CDataExchange* pDX) {
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_STATIC_TITLE, labelTitle_);
DDX_Control(pDX, IDC_BUTTON_CLOSE, closeButton_);
}
BOOL ImageViewDialog::OnInitDialog() {
CDialog::OnInitDialog();
labelTitle_.SetFont(GuiResources::GetInstance()->GetFont(GuiResources::FONT_NORMAL), FALSE);
labelTitle_.SetBkColor(RGB(54, 133, 214));
labelTitle_.SetTextColor(RGB(255, 255, 255));
ButtonStyle buttonStyle;
// close button style
buttonStyle.bgColor = RGB(54, 133, 214);
buttonStyle.textColor = RGB(255, 255, 255);
buttonStyle.bitmap.LoadFromResource(IDB_BUTTON_CLOSE);
buttonStyle.bitmap.SetWithAlpha();
buttonStyle.bitmap.PreMultiplyAlpha();
closeButton_.SetStyle(BUTTON_STATE_NORMAL, buttonStyle);
buttonStyle.bgColor = RGB(255, 64, 64);
closeButton_.SetStyle(BUTTON_STATE_OVER, buttonStyle);
buttonStyle.bgColor = RGB(186, 0, 0);
closeButton_.SetStyle(BUTTON_STATE_DOWN, buttonStyle);
layout_.Init(m_hWnd);
layout_.AddDlgItem(IDC_PLACE_CAPTION, AnchorLayout::TOP_LEFT, AnchorLayout::TOP_RIGHT);
layout_.AddDlgItem(IDC_BUTTON_CLOSE, AnchorLayout::TOP_RIGHT, AnchorLayout::TOP_RIGHT);
layout_.AddDlgItem(IDC_PLACE_IMAGE, AnchorLayout::TOP_LEFT, AnchorLayout::BOTTOM_RIGHT);
ShowWindow(SW_MAXIMIZE);
SelectCash(cashIndex_);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void ImageViewDialog::DrawImage(CDC *dc, Bitmap *image, const CRect &rect) {
if (!image->IsValid()) {
return;
}
double scaleX = (double)rect.Width() / image->GetWidth();
double scaleY = (double)rect.Height() / image->GetHeight();
double scale = __min(__min(scaleX, scaleY), 1.0);
int dstWidth = (int)(scale * image->GetWidth());
int dstHeight = (int)(scale * image->GetHeight());
int left = rect.left + (rect.Width() - dstWidth) / 2;
int top = rect.top + (rect.Height() - dstHeight) / 2;
image->Draw(dc->GetSafeHdc(), CRect(left, top, left + dstWidth, top + dstHeight));
}
int ImageViewDialog::GetSelectedCashIndex() const {
return cashIndex_;
}
void ImageViewDialog::SelectCash(int index) {
cashIndex_ = index;
if (monitor_ != NULL && cashIndex_ != -1) {
int cashCount = monitor_->Freeze();
if (cashIndex_ < cashCount) {
Cash *cash = monitor_->GetCash(cashIndex_);
// draw image.
Bitmap *topImage = cash->GetTopImage();
Bitmap *bottomImage = cash->GetBottomImage();
CString title;
title.Format(IDS_VIEW_CASH_IMAGE, CA2T(cash->GetSN()));
labelTitle_.SetWindowText(title);
ASSERT(topImage != NULL && bottomImage != NULL);
topImage_ = *topImage;
bottomImage_ = *bottomImage;
}
monitor_->Unfreeze();
}
Invalidate();
}
BEGIN_MESSAGE_MAP(ImageViewDialog, CDialog)
ON_WM_ERASEBKGND()
ON_WM_PAINT()
ON_WM_SIZE()
ON_BN_CLICKED(IDC_BUTTON_CLOSE, &ImageViewDialog::OnBnClickedButtonClose)
ON_WM_KEYDOWN()
ON_WM_RBUTTONDOWN()
ON_WM_GETMINMAXINFO()
END_MESSAGE_MAP()
// ImageViewDialog message handlers
void ImageViewDialog::OnPaint() {
BufferredPaintDC dc(this);
CRect rectTitle;
GetDlgItem(IDC_PLACE_CAPTION)->GetWindowRect(&rectTitle);
ScreenToClient(&rectTitle);
dc.FillSolidRect(rectTitle, RGB(54, 133, 214));
CRect imageRect;
GetDlgItem(IDC_PLACE_IMAGE)->GetWindowRect(&imageRect);
ScreenToClient(&imageRect);
dc.FillSolidRect(imageRect, RGB(0, 0, 0));
CRect topRect(imageRect.left, imageRect.top,
imageRect.right, imageRect.top + imageRect.Height() / 2);
CRect bottomRect(imageRect.left, topRect.bottom, imageRect.right, imageRect.bottom);
dc.SetStretchBltMode(HALFTONE);
DrawImage(&dc, &topImage_, topRect);
DrawImage(&dc, &bottomImage_, bottomRect);
}
BOOL ImageViewDialog::OnEraseBkgnd(CDC* pDC) {
return TRUE;
}
void ImageViewDialog::OnBnClickedButtonClose() {
PostMessage(WM_CLOSE);
}
void ImageViewDialog::OnSize(UINT nType, int cx, int cy) {
CDialog::OnSize(nType, cx, cy);
if (::IsWindow(m_hWnd)) {
layout_.RecalcLayout();
}
}
void ImageViewDialog::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) {
CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}
BOOL ImageViewDialog::PreTranslateMessage(MSG* pMsg) {
if (pMsg->message == WM_KEYDOWN) {
UINT key = pMsg->wParam;
if (key == VK_DOWN || key == VK_UP || key == VK_LEFT || key == VK_RIGHT || key == VK_NEXT || key == VK_PRIOR || key == VK_SPACE) {
if (monitor_ != NULL && cashIndex_ != -1) {
int cashCount = monitor_->Freeze();
int cashIndex = cashIndex_;
if (key == VK_DOWN || key == VK_RIGHT || key == VK_NEXT || key == VK_SPACE) {
cashIndex++;
} else if (key == VK_UP || key == VK_LEFT || key == VK_PRIOR) {
cashIndex--;
}
if (cashIndex < 0) {
cashIndex = 0;
}
if (cashIndex >= cashCount) {
cashIndex = cashCount - 1;
}
if (cashIndex != cashIndex_) {
cashIndex_ = cashIndex;
Cash *cash = monitor_->GetCash(cashIndex_);
// draw image.
Bitmap *topImage = cash->GetTopImage();
Bitmap *bottomImage = cash->GetBottomImage();
CString title;
title.Format(IDS_VIEW_CASH_IMAGE, CA2T(cash->GetSN()));
labelTitle_.SetWindowText(title);
ASSERT(topImage != NULL && bottomImage != NULL);
topImage_ = *topImage;
bottomImage_ = *bottomImage;
Invalidate();
}
monitor_->Unfreeze();
}
}
}
return CDialog::PreTranslateMessage(pMsg);
}
void ImageViewDialog::OnRButtonDown(UINT nFlags, CPoint point) {
CMenu menu;
menu.LoadMenu(IDR_POPUP_IMAGE_VIEW);
CMenu *contextMenu = menu.GetSubMenu(0);
int choice = contextMenu->TrackPopupMenu(TPM_RETURNCMD, point.x, point.y, this);
if (choice == ID_SAVE_IMAGE) {
CFileDialog dlg(FALSE);
if (dlg.DoModal() == IDOK) {
CString filePath = dlg.GetPathName();
topImage_.SaveToFile(filePath + "_top.bmp");
bottomImage_.SaveToFile(filePath + "_bottom.bmp");
}
}
CDialog::OnRButtonDown(nFlags, point);
}
void ImageViewDialog::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI)
{
// TODO: Add your message handler code here and/or call default
//lpMMI->ptMaxSize.y = GetSystemMetrics(SM_CYFULLSCREEN) + GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYDLGFRAME);
CRect rt;
SystemParametersInfo(SPI_GETWORKAREA, 0, &rt, 0);
lpMMI->ptMaxSize.x = rt.Width();
lpMMI->ptMaxSize.y = rt.Height();
CDialog::OnGetMinMaxInfo(lpMMI);
}