VC界面开发1

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; 


void CVideoScheduleView::InitControlPos()

{

//开始初始化控件大小

m_IsInitialed = false;

CRect m_ClientRect;

this->GetClientRect(&m_ClientRect);

CSize m_Forsize;

m_Forsize = GetTotalSize();//在资源编辑器中定好大小后,程序运行时大小(不管最大化和最小化,该大小均为同一个值),客户区大于或等于显示的大小

double m_x = (double)m_ClientRect.Width() / m_Forsize.cx;//宽度方向发大倍数

double m_y = (double)m_ClientRect.Height() / m_Forsize.cy;//高度方向发大倍数

//调整控件的大小

CWnd *pWnd = NULL; 

pWnd = GetWindow(GW_CHILD);

while(pWnd)//判断是否为空,因为对话框创建时会调用此函数,而当时控件还未创建

{

CRect rect;   //获取控件变化前大小

pWnd->GetWindowRect(&rect);

ScreenToClient(&rect);//将控件大小转换为在对话框中的区域坐标

m_ControlRect.insert(pair<int, CRect>(pWnd->GetDlgCtrlID(), rect));//保存控件的初始大小,以便在OnSize函数中继续使用

int width = rect.Width();

int height = rect.Height();

TCHAR szBuf[256];

GetClassName(pWnd->m_hWnd,szBuf,256);         

//其它控件位置和大小均变化

rect.top = m_y * rect.top;

rect.left = m_x * rect.left;

rect.bottom = m_y * rect.bottom;

rect.right = m_x * rect.right;


pWnd->MoveWindow(&rect);//设置控件大小

pWnd = pWnd->GetWindow(GW_HWNDNEXT);

}


//控件初始化结束

m_IsInitialed = true;

}

onsize:

if (!IsIconic() && m_IsInitialed)

{

CSize m_Forsize;

m_Forsize = GetTotalSize();

double m_x = (double)cx / m_Forsize.cx;

double m_y = (double)cy / m_Forsize.cy;

//读取控件的初始大小

map<int, CRect>::iterator pos = m_ControlRect.begin(),itUL,tit;

CRect rcUL,rcUR;

for (; pos != m_ControlRect.end(); ++pos)

{

CRect rect = pos->second;


rect.top = m_y * rect.top;

rect.left = m_x * rect.left;


rect.bottom = m_y * rect.bottom;

rect.right = m_x * rect.right;

if(pos->first == IDC_BUTTON_VIDEO_COMMAND)

{

rect.bottom = rect.top + 24;

rect.right = rect.left + 24;

}


GetDlgItem(pos->first)->MoveWindow(rect);

}

}


bool m_IsInitialed;

map<int, CRect> m_ControlRect;


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 CInterfaceDlg::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); 


你可能感兴趣的:(开发)