|
|
|
|
---===在Windows95/98中实现苹果窗口界面(vc6)===--- |
|
|
|
作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站 |
在Windows95/98中实现苹果窗口界面 高波 马惠业
| 有没有想过在Windows环境下实现苹果电脑窗口界面的风格?下面就以实现苹果电脑窗口风格为例,进行一次奇妙的旅行。 |
| 仔细观察苹果窗口,发现和Windows窗口的区别主要体现在标题栏和控制按钮(即最小化、恢复、关闭按钮)。所以我们应该把主要精力集中在这两点上,直接对Windows窗口已有的标题栏和控制按钮进行修改。 |
| 由于标题栏和控制按钮都属于非客户区,所以关键是获得非客户区的CDC,可以通过GetWindowDC()来获得。GetWindowDC()可以获得整个窗口的CDC,当然也包括非客户区的CDC,得到此CDC后,确定标题条的确切位置,就可以在标题栏上为所欲为了。如图1所示,在标题栏的位置装入一幅位图(截取了苹果窗口的一幅位图),在位图上加上文字标题(此标题具有3D效果,感觉还不错吧,其实就是把相同的字用不同的颜色和坐标写了两次)和控制按钮(实际也是一幅位图,只不过在鼠标单击时显示另一幅位图,看起来就像是一个按钮),由于控制按钮是自己加的,所以要由自己负责单击按钮的处理。到此为止,虽然准备好了窗口的标题栏和按钮,但还没有机会显示。我们知道要在窗口的客户区上画东东,只需响应Windows的客户区重画消息,在此消息处理函数中实现具体的操作。同理,要在非客户区上画东东,也只需响应Windows的非客户区重画消息,在消息处理函数中完成标题栏和按钮的绘制。 |
 |
| 采用VC6.0在Windows98下实现这种技术。 |
| 下面以生成一个苹果界面风格的对话框为例对这种方法进行详细的阐述。 |
| 1、用APPWIZARD(应用程序向导)生成一个新的应用程序(SDI与MDI均可),在 RESOURCE VIEW里面添加一个DIALOG资源。 |
| 2、添加一个新的对话框类。在ClassWizard中选择添加新类,输入类名CTestDlg并且选取基类CDIALOG。这样就建立起对话框类和对话框资源之间的联系。 |
| 3、在RESOURCE VIEW 中加入按钮和标题栏和背景位图。 |
| 在主菜单中的INSERT 菜单项下面选择RESOURCE,然后选择添加BITMAP,这样你就可以在RESOURCEVIEW 中看到BITMAP项,下面点击鼠标右键,选择IMPORT,从已有的位图文件中选出苹果风格的按钮和标题栏,并且分别赋予不同的ID。 |
| 4、在自己创建的CTestDlg类中添加新的按钮和新的标题栏。 |
| (1)首先在CTestDlg的构造函数中装入位图资源。程序如下: |
| CTestDlg::CTestDlg(int nID, |
| //{{AFX_DATA_INIT(CTestDlg) |
| // NOTE: the ClassWizard will add |
| member initialization here |
| //load the bitmap of button_down |
| m_bitmapPressed.LoadBitmap(IDB_BUTTONDOWN); |
| //load the bitmap of button_up |
| m_bitmapUnpressed.LoadBitmap(IDB_BUTTONUP); |
| //load the bitmap of caption |
| m_bitmapCaption.LoadBitmap(IDB_CAPTION); |
| //load the bitmap of background |
| m_bmpBk.LoadBitmap(IDB_BKGROUND); |
| void CTestDlg::GetButtonRect(CRect& rect) |
| // for small caption use SM_CYDLGFRAME |
| rect.top += GetSystemMetrics(SM_CYFRAME)+1; |
| // for small caption use SM_CYSMSIZE |
| rect.bottom =rect.top +GetSystemMetrics |
| // for small caption use SM_CXDLGFRAME |
| rect.left =rect.right -GetSystemMetrics |
| // for small caption use SM_CXSMSIZE |
| GetSystemMetrics(SM_CXSIZE))-1; |
| // for small caption use SM_CXSMSIZE |
| rect.right =rect.left +GetSystemMetrics |
| void CTestDlg::GetCaptionRect(CRect &rect) |
| // for small caption use SM_CYSMSIZE |
| rect.bottom =rect.top +GetSystemMetrics |
| 5.在按钮和标题栏对应的位置上分别画出苹果风格的按钮和标题栏。 |
| void CTestDlg::DrawButton() |
| // if window isn't visible or is minimized, skip |
| if (!IsWindowVisible() || IsIconic()) |
| // get appropriate bitmap |
| CDC* pDC = GetWindowDC(); |
| memDC.CreateCompatibleDC(pDC); |
| memDC.SelectObject(m_bPressed ? |
| &m_bitmapPressed : &m_bitmapUnpressed); |
| // get button rect and convert it into non |
| rect.OffsetRect(-rectWnd.left, -rectWnd.top); |
| m_bitmapPressed.GetBitmap(pBitMap); |
| m_bitmapUnpressed.GetBitmap(pBitMap); |
| height=pBitMap->bmHeight; |
| pDC->StretchBlt( rect.left, rect.top, rect.Width(), |
| rect.Height(), &memDC, 0, 0, |
| void CTestDlg::DrawCaption() |
| if (!IsWindowVisible() || IsIconic()) |
| // get appropriate bitmap |
| CDC* pDC = GetWindowDC(); |
| memDC.CreateCompatibleDC(pDC); |
| memDC.SelectObject(m_bitmapCaption); |
| // get button rect and convert it into non |
| rect.OffsetRect(-rectWnd.left, -rectWnd.top); |
| m_bitmapCaption.GetBitmap(pBitMap); |
| height=pBitMap->bmHeight; |
| pDC->StretchBlt( rect.left, rect.top, rect.Width(), |
| rect.Height(), &memDC, 0, 0, width, |
| //get the the text of the caption and |
| pDC->SetBkColor(RGB(209,209,209)); |
| caption = “ "+caption+“ "; |
| //draw the text of the caption with gray color |
| pDC->SetTextColor(RGB(128,128,128)); |
| (caption,rect,DT_CENTER|DT_VCENTER); |
| //move the coordinate to left and up |
| pDC->SetBkMode(TRANSPARENT); |
| //draw the text of the caption with white color |
| pDC->SetTextColor(RGB(255,255,255)); |
| //255,255,255 128,128,128 |
| (caption,rect,DT_CENTER|DT_VCENTER); |
| 要响应鼠标左键在标题栏上的单击,需要添加相应的消息映射函数。 |
| 通过在OnNcLButtonDown中给关闭按钮换一副图像,来实现按钮被按下的效果,同时完成窗口的关闭。 |
| void CTestDlg::OnNcLButtonDown |
| (UINT nHitTest, CPoint point) |
| if (nHitTest == HTCAPTION) |
| // see if in area we reserved for button |
| if (rect.PtInRect(point)) |
| m_bPressed = !m_bPressed; |
| CDialog::OnNcLButtonDown(nHitTest, point); |
| 这里主要是对标题栏和按钮的重画。通过ClassWizard给对话框添加非客户区重画的消息映射函数。 |
| void CTestDlg::OnNcPaint() |
| // then draw button on top |
| // device context for painting |
| memDC.CreateCompatibleDC(&dc); |
| memDC.SelectObject(m_bmpBk); |
| m_bmpBk.GetBitmap(pBitMap); |
| height=pBitMap->bmHeight; |
| dc.StretchBlt( rect.left, rect.top, rect.Width(), |
| &memDC, 0, 0,width,height, SRCCOPY ); |
| 至此为止,一个具有苹果窗口风格的对话框就新鲜出炉了!怎么样?味道还不错吧? |
| 但是,如果每次要用到对话框的时候都如此这般,岂不是太...那个了吧!不要惊慌,只需稍做改变就可一劳永逸了。将此对话框的构造函数的说明部分改为下面黑体所示即可,就这么简单。(用黑体表示强调的部分) |
| CTestDlg::CTestDlg(int nID, CWnd |
| //{{AFX_DATA_INIT(CTestDlg) |
| // NOTE: the ClassWizard will add |
| member initialization here |
| m_bitmapPressed.LoadBitmap |
| m_bitmapUnpressed.LoadBitmap |
| m_bitmapCaption.LoadBitmap(IDB_CAPTION); |
| m_bmpBk.LoadBitmap(IDB_BKGROUND); |
| m_brushButton.CreateSolidBrush |
| 以后,凡是用到对话框的时候,在VC的资源编辑器中把对话框设置好,把父类改为此对话框类即可,当然,要把此对话框类包括在你的Project中,图1所示的对话框就是继承于此对话框。 |
| 在CMyDialog的.h文件中将CDialog 改为CTestDlg: |
| Class CMyDialog: public CTestDlg |
| void CNewapple1Doc::OnDialog1() |
| // TODO: Add your command handler code here |

|
|
相关文章:相关软件: |
|