VC界面开发
1.让控件随着窗口的大小变化而变化
重载OnSize函数:
[cpp]
// TODO: 在此处添加消息处理程序代码
if(nType==SIZE_RESTORED || nType==SIZE_MAXIMIZED) //窗体大小发生变动。处理函数resize
{
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/oldPoint.x; //计算长度的新旧比
fsp[1]=(float)Newp.y/oldPoint.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]); //用比例得出左上角的x坐标
TLPoint.y = long(OldTLPoint.y*fsp[1]); //用比例得出左上角的y坐标
OldBRPoint = Rect.BottomRight();
BRPoint.x = long(OldBRPoint.x *fsp[0]); //用比例得出右下角的x坐标
BRPoint.y = long(OldBRPoint.y *fsp[1]); //用比例得出右下角的y坐标
Rect.SetRect(TLPoint,BRPoint); //设置最新的Rect
GetDlgItem(woc)->MoveWindow(Rect,TRUE);
hwndChild=::GetWindow(hwndChild, GW_HWNDNEXT); //获得下一个控件的句柄
}
oldPoint=Newp;
}
2.为Static控件加载图片
[cpp]
CBitmap bm1;
bm1.LoadBitmap(IDB_BACKGROUND);
BITMAP structBm1;
bm1.GetBitmap(&structBm1);
HBITMAP hBitmap=::LoadBitmap(AfxGetApp()-> m_hInstance,MAKEINTRESOURCE(IDB_BACKGROUND));
m_StaticTop.ModifyStyle(0,SS_BITMAP|SS_REALSIZEIMAGE);
m_StaticTop.SetBitmap(hBitmap);
m_StaticTop.MoveWindow(0,0,dlgWidth,structBm1.bmHeight);
3.点击dialog任意位置,都可移动dialog
重载OnLButtonDown函数:
[cpp]
void CInte易做图ceDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
PostMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x, point.y));
//向系统发送HTCAPTION消息,让系统以为鼠标点在标题栏上
CDialog::OnLButtonDown(nFlags, point);
}
4.为dialog添加背景图片
重载OnPaint函数:
[cpp]
{
//CDialog::OnPaint();
CPaintDC dc(this);
CBitmap m_bmpBackground;
m_bmpBackground.LoadBitmap(IDB_BACKGROUND);
CRect rect;
GetClientRect(&rect);
CDC dcMem;
dcMem.CreateCompatibleDC(&dc);
BITMAP bitMap;
m_bmpBackground.GetBitmap(&bitMap);
CBitmap *pbmpOld=dcMem.SelectObject(&m_bmpBackground);
dc.StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem,0,0,bitMap.bmWidth,bitMap.bmHeight,SRCCOPY);
//dc.BitBlt(0,0,rect.Width(),rect.Height(),&dcMem,0,0,SRCCOPY);
}
5.设置控件的背景颜色和字体颜色
重载OnCtlColor函数:
[cpp]
HBRUSH CRecNineDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
if (pWnd->GetDlgCtrlID()==IDC_BACKTOP)
{
HBRUSH brush = CreateSolidBrush(RGB(200,200,200));
pDC->SetTextColor(RGB(60,60,60));
pDC->SetBkMode(TRANSPARENT);
//pDC->SetBkColor(RGB(255,0,0));
return brush;
}
return CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
}
补充:软件开发 , Vc ,