AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > VC编程

在Windows95/98中实现苹果窗口界面

51自学网 2015-08-30 http://www.wanshiok.com

 

然后画出标题栏:
void CTestDlg::DrawCaption()
{
if (!IsWindowVisible() || IsIconic())
return;
// get appropriate bitmap
CDC memDC;
CDC* pDC = GetWindowDC();
memDC.CreateCompatibleDC(pDC);
memDC.SelectObject(m_bitmapCaption);
// get button rect and convert it into non
-client area coordinates
CRect rect, rectWnd;
GetCaptionRect(rect);
GetWindowRect(rectWnd);
rect.OffsetRect(-rectWnd.left, -rectWnd.top);
// draw the caption
int width,height;
BITMAP *pBitMap;
pBitMap = new BITMAP;
m_bitmapCaption.GetBitmap(pBitMap);
width=pBitMap->bmWidth;
height=pBitMap->bmHeight;
pDC->StretchBlt( rect.left, rect.top, rect.Width(),
rect.Height(), &memDC, 0, 0, width,
height, SRCCOPY );
//get the the text of the caption and
draw it with 3D style
pDC->SetBkColor(RGB(209,209,209));
CString caption;
GetWindowText(caption);
caption = “ "+caption+“ ";
rect.OffsetRect(0,4);
//draw the text of the caption with gray color
pDC->SetTextColor(RGB(128,128,128));
pDC->DrawText
(caption,rect,DT_CENTER|DT_VCENTER);
//move the coordinate to left and up
rect.OffsetRect(-1,-1);
pDC->SetBkMode(TRANSPARENT);
//draw the text of the caption with white color
pDC->SetTextColor(RGB(255,255,255));
//255,255,255 128,128,128
pDC->DrawText
(caption,rect,DT_CENTER|DT_VCENTER);
memDC.DeleteDC();
ReleaseDC(pDC);
delete pBitMap;
}

6.处理鼠标点击按钮的消息。

要响应鼠标左键在标题栏上的单击,需要添加相应的消息映射函数。

通过在OnNcLButtonDown中给关闭按钮换一副图像,来实现按钮被按下的效果,同时完成窗口的关闭。

void CTestDlg::OnNcLButtonDown
(UINT nHitTest, CPoint point)
{
if (nHitTest == HTCAPTION)
{
// see if in area we reserved for button
CRect rect;
GetButtonRect(rect);
if (rect.PtInRect(point))
{
m_bPressed = !m_bPressed;
DrawButton();
CDialog::OnCancel();
}
}
CDialog::OnNcLButtonDown(nHitTest, point);
}

7.处理窗口非客户区的重画。

这里主要是对标题栏和按钮的重画。通过ClassWizard给对话框添加非客户区重画的消息映射函数。

void CTestDlg::OnNcPaint()
{
// draw caption first
CDialog::OnNcPaint();

// then draw button on top
DrawCaption();
DrawButton();
}

8.给对话框加上背景,在此为一幅位图:

void CTestDlg::OnPaint()
{
CPaintDC dc(this);
// device context for painting
CDC memDC;
memDC.CreateCompatibleDC(&dc);
memDC.SelectObject(m_bmpBk);
CRect rect, rectWnd
GetWindowRect(rect);
GetClientRect(rect);
int width,height;
BITMAP *pBitMap;
pBitMap = new BITMAP;
m_bmpBk.GetBitmap(pBitMap);
width=pBitMap->bmWidth;
height=pBitMap->bmHeight;
dc.StretchBlt( rect.left, rect.top, rect.Width(),
rect.Height(),
&memDC, 0, 0,width,height, SRCCOPY );
}

至此为止,一个具有苹果窗口风格的对话框就新鲜出炉了!怎么样?味道还不错吧?

但是,如果每次要用到对话框的时候都如此这般,岂不是太...那个了吧!不要惊慌,只需稍做改变就可一劳永逸了。将此对话框的构造函数的说明部分改为下面黑体所示即可,就这么简单。(用黑体表示强调的部分)

CTestDlg::CTestDlg(int nID, CWnd
* pParent /*=NULL*/)
: CDialog(nID, pParent)
{
//{{AFX_DATA_INIT(CTestDlg)
// NOTE: the ClassWizard will add
member initialization here
//}}AFX_DATA_INIT
m_bPressed=FALSE;
m_bitmapPressed.LoadBitmap
(IDB_BUTTONDOWN);
m_bitmapUnpressed.LoadBitmap
(IDB_BUTTONUP);
m_bitmapCaption.LoadBitmap(IDB_CAPTION);
m_bmpBk.LoadBitmap(IDB_BKGROUND);
m_brushButton.CreateSolidBrush
(RGB(192,192,255));
}

以后,凡是用到对话框的时候,在VC的资源编辑器中把对话框设置好,把父类改为此对话框类即可,当然,要把此对话框类包括在你的Project中,图1所示的对话框就是继承于此对话框。

在CMyDialog的.h文件中将CDialog 改为CTestDlg:

Class CMyDialog: public CTestDlg
void CNewapple1Doc::OnDialog1()
{
// TODO: Add your command handler code here
CMyDialog dlg;
dlg.DoModal();
}

 
 
说明
:本教程来源互联网或网友上传或出版商,仅为学习研究或媒体推广,wanshiok.com不保证资料的完整性。

上一篇:实现真正的Windows屏幕保护程序  下一篇:一段使窗口透明的代码(仅适用于2000)