Skip to content

Commit 04477dd

Browse files
committed
修正消息对话框调整大小时控件位置不正确的问题
1 parent a766a5e commit 04477dd

File tree

4 files changed

+50
-36
lines changed

4 files changed

+50
-36
lines changed

TrafficMonitor/BaseDialog.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,24 @@ void CBaseDialog::SetButtonIcon(UINT id, HICON hIcon)
260260
btn->SetIcon(hIcon);
261261
}
262262

263+
CRect CBaseDialog::GetControlRect(CWnd* pCtrl)
264+
{
265+
if (pCtrl != nullptr)
266+
{
267+
CRect rect;
268+
pCtrl->GetWindowRect(rect);
269+
ScreenToClient(rect);
270+
return rect;
271+
}
272+
return CRect();
273+
}
274+
275+
CRect CBaseDialog::GetControlRect(UINT id)
276+
{
277+
CWnd* pCtrl = GetDlgItem(id);
278+
return GetControlRect(pCtrl);
279+
}
280+
263281
void CBaseDialog::IterateControls(std::function<void(CWnd*)> func)
264282
{
265283
func(this);

TrafficMonitor/BaseDialog.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ class CBaseDialog : public CDialog
7979
void EnableDlgCtrl(UINT id, bool enable);
8080
void SetButtonIcon(UINT id, HICON hIcon);
8181
void SetRememberDlgSize(bool enable) { m_remember_dlg_size = enable; }
82+
CRect GetControlRect(CWnd* pCtrl);
83+
CRect GetControlRect(UINT id);
8284

8385
//遍历所有子控件
8486
void IterateControls(std::function<void(CWnd*)> func);

TrafficMonitor/MessageDlg.cpp

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,6 @@ void CMessageDlg::SetStandarnMessageIcon(StandardIcon standard_icon)
7171
SetMessageIcon(hIcon);
7272
}
7373

74-
void CMessageDlg::SetInfoStaticSize(int cx)
75-
{
76-
if (m_icon != NULL && m_info_static.GetSafeHwnd() != NULL)
77-
{
78-
CRect rc_info{ m_rc_info };
79-
//设置Static控件水平位置,为图标腾出空间
80-
rc_info.left = m_rc_info.left + MESSAGE_DLG_ICON_SIZE + theApp.DPI(8);
81-
if (cx > 0)
82-
rc_info.right = cx;
83-
//设置Static控件的垂直位置
84-
rc_info.MoveToY(rc_info.top + MESSAGE_DLG_ICON_VERTIAL_MARGIN);
85-
m_info_static.MoveWindow(rc_info);
86-
}
87-
}
88-
8974
void CMessageDlg::DoDataExchange(CDataExchange* pDX)
9075
{
9176
CBaseDialog::DoDataExchange(pDX);
@@ -133,31 +118,28 @@ BOOL CMessageDlg::OnInitDialog()
133118
//设置图标的位置
134119
if (m_icon != NULL)
135120
{
136-
CRect rc_edit;
137-
m_message_edit.GetWindowRect(rc_edit);
121+
CRect rc_info = GetControlRect(&m_info_static);
122+
//设置Static控件水平位置,为图标腾出空间
123+
rc_info.left = rc_info.left + MESSAGE_DLG_ICON_SIZE + theApp.DPI(8);
124+
//设置Static控件的垂直位置
125+
rc_info.MoveToY(rc_info.top + MESSAGE_DLG_ICON_VERTIAL_MARGIN);
126+
m_info_static.MoveWindow(rc_info);
127+
128+
CRect rc_edit = GetControlRect(&m_message_edit);
138129
rc_edit.top += (MESSAGE_DLG_ICON_VERTIAL_MARGIN * 2);
139-
ScreenToClient(rc_edit);
140-
m_icon_pos.x = rc_edit.left;
141-
m_icon_pos.y = (rc_edit.top - MESSAGE_DLG_ICON_SIZE) / 2;
130+
m_icon_pos.x = rc_edit.left;
131+
m_icon_pos.y = (rc_edit.top - MESSAGE_DLG_ICON_SIZE) / 2;
142132
m_message_edit.MoveWindow(rc_edit);
143-
144-
m_info_static.GetWindowRect(m_rc_info);
145-
ScreenToClient(m_rc_info);
146-
SetInfoStaticSize(0);
147133
}
148134

149135
//没有图标,且消息标题为空时,隐藏Static控件
150136
if (m_icon == NULL && m_info.IsEmpty())
151137
{
152-
CRect rc_edit;
153-
m_message_edit.GetWindowRect(rc_edit);
154-
ScreenToClient(rc_edit);
155-
m_info_static.GetWindowRect(m_rc_info);
156-
ScreenToClient(m_rc_info);
157-
rc_edit.top = m_rc_info.top;
138+
CRect rc_info = GetControlRect(&m_info_static);
139+
CRect rc_edit = GetControlRect(&m_message_edit);
140+
rc_edit.top = rc_info.top;
158141
m_message_edit.MoveWindow(rc_edit);
159142
m_info_static.ShowWindow(SW_HIDE);
160-
161143
}
162144

163145
return TRUE; // return TRUE unless you set the focus to a control
@@ -202,6 +184,21 @@ void CMessageDlg::OnSize(UINT nType, int cx, int cy)
202184
{
203185
CBaseDialog::OnSize(nType, cx, cy);
204186

205-
// TODO: 在此处添加消息处理程序代码
206-
SetInfoStaticSize(cx);
187+
if (m_info_static.GetSafeHwnd() != NULL && m_icon != NULL)
188+
{
189+
CRect rc_info = GetControlRect(&m_info_static);
190+
rc_info.right = cx;
191+
m_info_static.MoveWindow(rc_info);
192+
}
193+
if (m_message_edit.GetSafeHwnd() != NULL)
194+
{
195+
CRect rc_edit = GetControlRect(&m_message_edit);
196+
//设置Edit控件的垂直位置
197+
CRect rc_ok = GetControlRect(IDOK);
198+
if (!rc_ok.IsRectEmpty())
199+
{
200+
rc_edit.bottom = rc_ok.top - theApp.DPI(6);
201+
m_message_edit.MoveWindow(rc_edit);
202+
}
203+
}
207204
}

TrafficMonitor/MessageDlg.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,10 @@ class CMessageDlg : public CBaseDialog
4949

5050
HICON m_icon{};
5151
CPoint m_icon_pos{}; //图标的位置
52-
CRect m_rc_info{}; //错误信息Static控件的初始区域
5352

5453
//bool m_show_link_ctrl{ false };
5554

5655
protected:
57-
void SetInfoStaticSize(int cx); //如果设置了图标,则需要将错误信息Static控件向右移动一些
58-
5956
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
6057

6158
DECLARE_MESSAGE_MAP()

0 commit comments

Comments
 (0)