1、控件随着窗口自适应
首先在类视图里面添加OnSize()函数,然后添加一个ChangeSize()函数,头文件中添加如下
//控件自适应变量
POINT old;
CRect m_rect;
void ChangeSize(UINT nID, int x, int y);
afx_msg void OnSize(UINT nType, int cx, int cy);
在OnInitDialog中
//控件自适应
GetClientRect(&m_rect);
old.x = m_rect.right - m_rect.left;
old.y = m_rect.bottom - m_rect.top;
//窗口自适应
void CTestDlg::ChangeSize(UINT nID, int x, int y)
{
CWnd *pWnd;
pWnd = GetDlgItem(nID);
if (pWnd != NULL)
{
CRect rect;
pWnd->GetWindowRect(&rec);
ScreenToClient(&rec);
rect.left = rect.left*x / m_rect.Width();
rect.top = rect.top*y / m_rect.Height();
rect.bottom = rect.bottom*y / m_rect.Height();
rect.right = rect.right*x / m_rect.Width();
pWnd->MoveWindow(rec); //伸缩控件
}
}
//窗口自适应
int count = 0;
void CTestDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
// TODO: 在此处添加消息处理程序代码
if (nType != SIZE_MINIMIZED)
{
float fsp[2];
POINT Newp; //获取现在对话框的大小
CRect recta;
GetClientRect(&recta); //取客户区大小
Newp.x = recta.right - recta.left;
Newp.y = recta.bottom - recta.top;
fsp[0] = (float)Newp.x / old.x;
fsp[1] = (float)Newp.y / old.y;
CRect Rect;
int woc;
CPoint OldTLPoint, TLPoint; //左上角
CPoint OldBRPoint, BRPoint; //右下角
HWND hwndChild = ::GetWindow(m_hWnd, GW_CHILD); //列出所有控件
while (hwndChild){
woc = ::GetDlgCtrlID(hwndChild);//取得ID
GetDlgItem(woc)->GetWindowRect(Rect);
ScreenToClient(Rect);
OldTLPoint = Rect.TopLeft();
TLPoint.x = long(OldTLPoint.x*fsp[0]);
TLPoint.y = long(OldTLPoint.y*fsp[1]);
OldBRPoint = Rect.BottomRight();
BRPoint.x = long(OldBRPoint.x *fsp[0]);
BRPoint.y = long(OldBRPoint.y *fsp[1]);
Rect.SetRect(TLPoint, BRPoint);
GetDlgItem(woc)->MoveWindow(Rect, TRUE);
hwndChild = ::GetWindow(hwndChild, GW_HWNDNEXT);
}
old = Newp;
}
//主对话框需要,子对话框不需要
if (count!= 0) //用于计数,因为第一次运行的时候,不应该执行以下程序,否则会报错
{
CRect rectTab;
m_tab.GetClientRect(&rectTab); //m_tab是tab控件的变量
rectTab.top += 20;
rectTab.bottom += 4;
rectTab.left += 4;
rectTab.right -= 4;
a.MoveWindow(&rectTab, TRUE);//tab子界面a
b.MoveWindow(&rectTab, TRUE);//tab子界面b
}
count++;
}
想要使tab控件里面的子对话框跟随对话框自适应,主对话框(即有tab控件的对话框)中的程序是以上程序,而子对话框内的程序只需要去除以上tab那一段程序即可
注意:需要修改对话框中所有Static的ID,修改成不一样的ID即可。
2、给对话框添加背景图片
(1)资源视图右键->添加->资源->Bitmap->导入,将bmp图片放置在工程文件的Res文件夹中,将其导入Bitmap,然后修改其ID为IDB_BITMAP_BACKGROUND
(2)在类视图中添加OnPaint()函数
//添加背景颜色
void CTestDlg::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
{
CRect rect;
CPaintDC dc(this);
GetClientRect(&rect);
dc.FillSolidRect(rect, RGB(141, 182, 205));
dc.FillPath();
//背景图片 能够自适应窗口
GetClientRect(&rect);
CDC dcMem;
dcMem.CreateCompatibleDC(&dc);
CBitmap bmpBackground;
bmpBackground.LoadBitmap(IDB_BITMAP_BACKGROUND); //IDB_BITMAP_BACKGROUND是你自己的图对应的ID
BITMAP bitmap;
bmpBackground.GetBitmap(&bitmap);
CBitmap *pbmpOld = dcMem.SelectObject(&bmpBackground);
dc.StretchBlt(0, 0, rect.Width(), rect.Height(), &dcMem, 0, 0, bitmap.bmWidth, bitmap.bmHeight, SRCCOPY);
//CDialog::OnPaint();
}
}
我的背景图片已经包括了那个蓝框框
如果想要自己画蓝色框框,只需要在OnPaint()函数中添加以下程序即可
ScreenToClient(&rect);//转换为对话框上的客户坐标
CBrush brush(RGB(161, 224, 247));//中间的蓝色框
CRect rect(200, 200, 600, 380); //指定区域大小
dc.FillRect(&rect, &brush);
3、使Static背景变化
由于为了自适应改变了Static的ID,所以这里需要对不同的Static进行设置
//设置控件颜色
HBRUSH UserInfoDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: 在此更改 DC 的任何特性
if ((pWnd->GetDlgCtrlID() == IDC_IDTYPE_STATIC) || (pWnd->GetDlgCtrlID() == IDC_NAME_STATIC)
|| (pWnd->GetDlgCtrlID() == IDC_SEX_STATIC) || (pWnd->GetDlgCtrlID() == IDC_BIRTH_STATIC)
|| (pWnd->GetDlgCtrlID() == IDC_NATION_STATIC) || (pWnd->GetDlgCtrlID() == IDC_ADDRESS_STATIC)
|| (pWnd->GetDlgCtrlID() == IDC_AGENCY_STATIC) || (pWnd->GetDlgCtrlID() == IDC_STARTDATE_STATIC)
|| (pWnd->GetDlgCtrlID() == IDC_ENDDATE_STATIC) || (pWnd->GetDlgCtrlID() == IDC_IDNUM_STATIC))//
{
pDC->SetTextColor(RGB(0, 0, 0));//设置文本色
pDC->SetBkColor(RGB(141, 182, 205));//设置背景色
pDC->SetBkMode(TRANSPARENT);//透明
return (HBRUSH)::GetStockObject(NULL_BRUSH);
}
// TODO: 如果默认的不是所需画笔,则返回另一个画笔
return hbr;
}