VC实例创建的详细步骤
下面是我学习整理的东西;希望跟大家共享!希望学VC的朋友顺利!
AfxMessageBox("Copy");表示弹出一个消息框 消息是Copy;
1.启动界面的步骤
a.创建一个单文档框架
b.在工程菜单里选择添加工程
c.在添加工程里选择Components and Controls Gallery
d.Components and Controls Gallery里选Visuan C++ Components
e.Visuan C++ Components里选择Splash Screen控件
f.如果想改动启动界面只需要加入新的位图替换原来的位图
g.如果想延长界面显示时间可以调节SetTimer(1, 1520, NULL);第一个参数对应nIDEvent
2.给菜单里添加位图的步骤
a.创建一个单文档框架
b.在视类里添加CBitmap类对象;需要几个位图就添加几个对象
c.在视类的构造函数里调用CBitmap类对象的成员函数LoadBitmap(位图ID);
初始化CBitmap类对象
d.在视类里的OnDraw(CDC* pDC)函数添加一下函数
e. CWnd* parent=GetParent();
CMenu* pmenubar=parent->GetMenu();
CMenu* pmenu=pmenubar->GetSubMenu(0);
pmenu->SetMenuItemBitmaps(0,MF_BYPOSITION,&bm_New,&bm_New);
pmenu->SetMenuItemBitmaps(1,MF_BYPOSITION,&bm_Open,&bm_Open);
pmenu->SetMenuItemBitmaps(2,MF_BYPOSITION,&bm_Save,&bm_Save);
3. 改变主窗口的大小形状的步骤!
a.创建一个单文档框架
b在MainFrm类中添加消息函数WM_SIZE(OnSize())
c在OnSize()中添加一下函数就可以了:
d CRgn m_rgn;
CRect rc;
GetWindowRect(&rc);
rc -= rc.TopLeft();
m_rgn.CreateRoundRectRgn(rc.left,rc.top,rc.right,rc.bottom,100,100);
SetWindowRgn(m_rgn,TRUE);
4 把主窗口分成几部分的步骤
a.创建一个单文档框架
b在MainFrm类中重载虚拟函数OnCreateClient(在类向导里可以找到该虚拟函数)
c.在MainFrm类中添加CSplitterWnd m_Splitter类对象;
d.OnCreateClient虚拟函数中添加如下代码
CRect cr;
GetClientRect(&cr);
CSize paneSize(cr.Width()/3, cr.Height() /3);
m_Splitter.CreateStatic(this,3,1);
m_Splitter.CreateView(0,0,RUNTIME_CLASS(CExampleView),paneSize,pContext);
m_Splitter.CreateView(1,0,RUNTIME_CLASS(CExampleView),paneSize,pContext);
m_Splitter.CreateView(2,0,RUNTIME_CLASS(CExampleView),paneSize,pContext);
return TRUE;
5.在主窗口的两边创建滚动条的步骤
a.创建一个单文档框架
b在MainFrm类中重载虚拟函数OnCreateClient(在类向导里可以找到该虚拟函数)
c.在MainFrm类中添加CSplitterWnd m_Splitter类对象;
d.OnCreateClient虚拟函数中添加如下代码
return m_Splitter.Create(this,2,2,CSize(10,10),pContext);
6.创建透明图形的步骤
a.创建一个对话框框架
b.在OnInitDialog()函数中添加以下代码,画出图形
HRGN m_wndRgn;
CClientDC dc(this);
CFont mFont;
mFont.CreateFont(200, 50, 0, 0, FW_HEAVY, FALSE, FALSE, 0, ANSI_CHARSET,
OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,DEFAULT_PITCH | FF_SWISS, "宋体");
//open a path brackets , an application can begin calling GDI
//drawing functions to define the points that lie in the path.
dc.BeginPath();
dc.SetBkMode(TRANSPARENT);
CFont * pOldFont;
pOldFont = dc.SelectObject( &mFont );
dc.TextOut(0, 0, "◎◎");
dc.EndPath();
m_wndRgn =::PathToRegion(dc);
this->SetWindowRgn(m_wndRgn, TRUE);
dc.SelectObject(pOldFont );
c.进入MFC ClassWizard,在Class Info下的Message filter选择Window,
然后在回到Message Maps中找到(WM_NCHITTEST)添加该消息函数
图形就会随着鼠标移动
d.在该消息函数中添加以下代码
UINT nHitTest = CDialog::OnNcHitTest(point) ;
return (nHitTest==HTCLIENT)?HTCAPTION:nHitTest;
7.创建界面缓缓进入,缓缓退出的效果
a.创建一个单文档框架
b.在StdAfx.h文件的开始部分添加以下代码
#undef WINVER
#define WINVER 0x500
c.在OnCreate()消息函数中添加以下代码
AnimateWindow(GetSafeHwnd(),1000,AW_SLIDE|AW_HOR_POSITIVE);
d.创建OnClose()消息函数并添加以下代码
AnimateWindow(GetSafeHwnd(),1000,AW_HIDE|AW_CENTER);
8.给某一个控件设置鼠标,比如给button按钮设置鼠标的步骤
a.创建一个对话框框架
b.在MFC ClassWizard创建(WM_SETCURSOR)消息函数
c.在该函数下添加以下代码
CString sClassName;
HCURSOR m_Cursor;
::GetClassName(pWnd->GetSafeHwnd(),sClassName.GetBuffer(80),80);
if (sClassName=="Button")
{
m_Cursor = AfxGetApp()->LoadCursor(IDC_CURSOR1);
::SetCursor(m_Cursor);
return TRUE;
}
9.在对话框中添加menu菜单的步骤
a.创建一个对话框框架
b.在 ResourseView中建立Menu菜单,
c.在 ResourseView中打开Dialog对话框在属性对话框里在MENU选项下选择
10.在对话框中添加 ToolBar的步骤
a.创建一个对话框框架
b.在 ResourseView中建立Toolbar工具条
c.在Dialog类的OnInitDialog()函数中添加以下代码
CRect rect;
GetClientRect(rect);
if( !m_wndToolBar.Create(this) )
{
TRACE0("Failed to create tool bar\n");
return -1; // fail to create
}
m_wndToolBar.LoadToolBar(IDR_TOOLBAR1);
m_wndToolBar.MoveWindow(100, 0, rect.right, 32);
d.根据Toolbar里的图形ID号!创建一个消息函数!添加以下代码
AfxMessageBox("Click!");单击该图型就会有个消息框!
e.
11.在对话框中添加 StatusBar的步骤
a.创建一个对话框框架
b.在String Table中添加两个状态值
c.在对话框类.h里添加CStatusBar类对象;
d.在对话框类.cpp里添加以下代码:
static UINT indicators[] =
{
IDS_PANEL1,//为String Tabl里添加的ID值
IDS_PANEL2,
};
和OnInitDialog()函数里添加
CRect rect;
GetClientRect(rect);
//状态条
if (!m_wndStatusBar.Create(this)||!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("未能创建状态栏\n");
return -1; // 未能创建
}
m_wndStatusBar.MoveWindow(0, rect.bottom-20, rect.right, 20 );
12.添加CDialogBar的步骤
a.创建一个单文档框架
b.在MainFrm.h中添加一个public:型CDialogBar m_wndDialogBar对象;
c.在MainFrm.cpp中的OnCreate(LPCREATESTRUCT lpCreateStruct)函数中添加如下代码
m_wndDialogBar.Create( this, IDD_DIALOG1,CBRS_TOP|CBRS_GRIPPER|
CBRS_TOOLTIPS | CBRS_FLYBY |
CBRS_SIZE_DYNAMIC,
IDD_DIALOG1);
m_wndDialogBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndDialogBar);
13.制作橡皮球的步骤
a.创建一个单文档框架
b.在视图类.h中添加CRectTracker m_tracker;类对象,和左键消息
c.在视图类的初始化函数中添加以下代码
m_tracker.m_rect.SetRect(0, 0, 30, 30);
m_tracker.m_nStyle=CRectTracker::resizeInside | CRectTracker::dottedLine;
d.在视图类的OnDraw()函数中添加以下代码
CBrush brush (RGB (0, 255, 255));
CBrush* pOldBrush=pDC->SelectObject(&brush);
CRect rcEllipse;
m_tracker.GetTrueRect(rcEllipse);
pDC->Ellipse(rcEllipse);
m_tracker.Draw(pDC);
pDC->SelectObject(pOldBrush);
c.在视图类的OnLButtonDown()消息函数中添加以下代码
CExampleDoc* pDoc=GetDocument();
ASSERT_VALID(pDoc);
BOOL bResult=m_tracker.HitTest(point)!= CRectTracker::hitNothing;
if (bResult)
{
m_tracker.Track (this,point,true);
pDoc->SetModifiedFlag();
pDoc->UpdateAllViews(NULL);
}
else
m_tracker.TrackRubberBand(this,point,true);
14。给右键添加属性的步骤
a.创建一个对话框框架
b.加入一个Menu资源
c.创建WM_CONTEXTMENU消息函数并加入以下代码
CMenu menu;
menu.LoadMenu(IDR_MENU1);
menu.GetSubMenu(0)->TrackPopupMenu(
TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_RIGHTBUTTON,
point.x, point.y, this);
单击右键就可以出现属性条
15.改变对话框图标的方法
a.创建一个对话框框架
b.创建两个图标其ID值分别为(IDI_ICON1);(IDI_ICON2);
c.添加WM_LBUTTONDOWN消息并添加如下代码
HICON hIcon=AfxGetApp()->LoadIcon(IDI_ICON1);
ASSERT(hIcon);
AfxGetMainWnd()->SendMessage(WM_SETICON,TRUE,(LPARAM)hIcon);
//修改大图标
AfxGetMainWnd()->SendMessage(WM_SETICON,FALSE,(LPARAM)hIcon);
//修改小图标
d.添加WM_RBUTTONDOWN消息并添加如下代码
HICON hIcon=AfxGetApp()->LoadIcon(IDI_ICON2);
ASSERT(hIcon);
AfxGetMainWnd()->SendMessage(WM_SETICON,TRUE,(LPARAM)hIcon);
//修改大图标
AfxGetMainWnd()->SendMessage(WM_SETICON,FALSE,(LPARAM)hIcon);
//修改小图标
16.切换菜单语言的步骤
a.创建一个单文档框架
b.创建两个一样菜单,分别用中英文表示
c.在MainFrm.h中创建两个菜单对象用于保存中英文资源
d.根据menu_id号创建两个消息函数分别加入以下号码:
SetMenu(&m_pChinese);// 把菜单设置为汉语
SetMenu(&m_pEnglish);// 把菜单设置为英语
e.在OnCreate()函数中加入如下代码
m_pChinese.LoadMenu(IDR_MAINFRAME_CN);//调入中文菜单资源
m_pEnglish.LoadMenu(IDR_MAINFRAME_EN);//调入英文菜单资源
SetMenu(&m_pEnglish);
17。在状态栏里显示时间的步骤
a.创建一个单文档框架
b.在string Table里添加ID_INDICATOR_CLOCK标识号 标题为0000-00-00 00:00:00
c.创建WM_TIMER 消息函数并如以下代码
if (nIDEvent==1)
{
CTime t;
t=CTime::GetCurrentTime();
CString str=t.Format("%Y-%m-%d %H:%M:%S");
m_wndStatusBar.SetPaneText(m_wndStatusBar.CommandToIndex(
ID_INDICATOR_CLOCK),str);
}
d.创建WM_CLOSE消息函数并加入以下代码
KillTimer(1);
e.OnCreate()函数里添加以下代码
SetTimer(1,500,NULL);
f在MainFrm.h的开头处加入以下代码替换原来的代码
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
ID_INDICATOR_CLOCK,
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
18.创建透明背景的步骤
a.创建一个对话框框架
b。在OnInitDialog()函数里添加以下代码
SetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE,GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE)^0x80000);
//加入WS_EX_LAYERED扩展属性
HINSTANCE hInst = LoadLibrary("User32.DLL");
if(hInst)
{
typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD);
MYFUNC fun = NULL;
fun=(MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes");
//取得SetLayeredWindowAttributes函数指针
if(fun)fun(this->GetSafeHwnd(),0,129,2);
FreeLibrary(hInst);
}
19.画直线的步骤
a.创建一个单文档框架
b.在视图类.h中添加 CPoint m_StartPoint;类对象
c.添加WM_LBUTTONDOWN消息 添加如下代码
m_StartPoint=point;
SetCapture();
d.添加WM_LBUTTONUP消息,加如下代码
if(GetCapture()==this)
{
CClientDC dc(this);
ReleaseCapture();
dc.MoveTo(m_StartPoint);
dc.LineTo(point);
}
20。另一种画直线的步骤
a.创建一个单文档框架
b.在视图类.h中添加 CPoint m_StartPoint,m_LastEndPoint;类对象
c.添加WM_LBUTTONDOWN消息 添加如下代码
m_StartPoint=point;
m_LastEndPoint=point;
SetCapture();
d.添加WM_LBUTTONUP消息,加如下代码
if(GetCapture()==this)
{
CClientDC dc(this);
ReleaseCapture();
dc.MoveTo(m_StartPoint);
dc.LineTo(point);
}
e.添加WM_MOUSEMOVE消息,加入如下代码
if(nFlags && MK_LBUTTON)
{
CClientDC dc(this);
int nDrawMode=dc.GetROP2();
dc.SetROP2(R2_NOTXORPEN);
dc.MoveTo(m_StartPoint);
dc.LineTo(m_LastEndPoint);
dc.MoveTo(m_StartPoint);
dc.LineTo(point);
m_LastEndPoint=point;
dc.SetROP2(nDrawMode);
}
21.在对话框中显示位图的步骤
a.创建一个对话框框架
b.先创建两个位图
c.在OnPaint()函数下else语句块里添加如下代码
CPaintDC dc(this);
CBitmap BackBMP;
BackBMP.LoadBitmap(IDB_BITMAP1);
BITMAP bm;
BackBMP.GetBitmap(&bm);
CDC ImageDC;
ImageDC.CreateCompatibleDC(&dc);
CBitmap *pOldImageBMP = ImageDC.SelectObject(&BackBMP);
dc.StretchBlt(0,0,bm.bmWidth,bm.bmHeight,
&ImageDC,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);
ImageDC.SelectObject(pOldImageBMP);
CBitmap ForeBMP;
ForeBMP.LoadBitmap(IDB_BITMAP2);
ForeBMP.GetBitmap(&bm);
pOldImageBMP = ImageDC.SelectObject(&ForeBMP);
TransparentBlt(dc.GetSafeHdc(),0,0,bm.bmWidth,bm.bmHeight,
ImageDC.GetSafeHdc(),0,0,bm.bmWidth,bm.bmHeight,RGB(0xff,0xff,0xff));
ImageDC.SelectObject(pOldImageBMP);
22.创建艺术字的步骤:
a.创建一个对话框框架
b.在对话框类.cpp下OnPaint()函数里添加代码
CPaintDC dc(this); // 用于绘制的设备上下文
LOGFONT m_logfnt;
m_logfnt.lfCharSet=DEFAULT_CHARSET;
m_logfnt.lfClipPrecision=CLIP_DEFAULT_PRECIS;
m_logfnt.lfEscapement=0;
strcpy(m_logfnt.lfFaceName,"Times New Roman");
m_logfnt.lfHeight=80;
m_logfnt.lfItalic=false;
m_logfnt.lfOrientation=0;
m_logfnt.lfPitchAndFamily=FF_SWISS;
m_logfnt.lfQuality=DEFAULT_QUALITY;
m_logfnt.lfStrikeOut=false;
m_logfnt.lfUnderline=true;
m_logfnt.lfWeight=800;
m_logfnt.lfWidth=20;
m_logfnt.lfOutPrecision=OUT_DEFAULT_PRECIS;
CFont m_font;
m_font.CreateFontIndirect(&m_logfnt);
CFont *pOldFont = dc.SelectObject(&m_font);
CString lpszString1="Hello VC";
// dc.SetBkMode(TRANSPARENT);
dc.SetBkColor(RGB(255,125,0));
dc.TextOut(0,0,lpszString1,lstrlen(lpszString1));
dc.SelectObject(pOldFont);
CDialog::OnPaint();
23.在单文档框架里显示位图
a.创建单文档框架
b.在视图类.cpp里的OnDraw(CDC* pDC)函数下加入代码
CBitmap bitmap;
bitmap.LoadBitmap(IDB_BITMAP1);
CDC MemDC;
MemDC.CreateCompatibleDC(pDC);
CBitmap *pOldBitmap=MemDC.SelectObject(&bitmap);
BITMAP bm;
bitmap.GetBitmap(&bm);
pDC->BitBlt(0,0,bm.bmWidth,bm.bmHeight,&MemDC,0,0,SRCCOPY);
MemDC.SelectObject(pOldBitmap);
24。在单文档里打开一个位图
a.创建单文档框架
b.在视图类.cpp里的OnDraw(CDC* pDC)函数下加入代码
if (GetDocument()->GetPathName()!="" )
{
HBITMAP bitmap;
bitmap=(HBITMAP)LoadImage(AfxGetInstanceHandle(),
GetDocument()->GetPathName(),IMAGE_BITMAP,0,0,
LR_LOADFROMFILE|LR_CREATEDIBSECTION);
HBITMAP OldBitmap;
CDC MemDC;
MemDC.CreateCompatibleDC(pDC);
CRect rect;
GetClientRect(rect);
OldBitmap=(HBITMAP)MemDC.SelectObject(bitmap);
pDC->BitBlt(0,0,rect.Width(),rect.Height(),&MemDC,0,0,SRCCOPY);
MemDC.SelectObject(OldBitmap);
}
25.控制List Box的步骤
a.创建一个对话框框架
b.在对话框模块里添加一个List Box控件
c.给List Box控件创建一个Member Variables;Control IDs:IDC_LIST1;Type:CListBox,Member:m_list
d.在对话框类.cpp 里END_MESSAGE_MAP()后面添加以下代码
int index;
//The TEXTMETRIC structure contains basic
//information about a physical font.
//LPARAM A 32-bit value passed as a parameter to
// a window procedure or callback function.
bool CALLBACK EnumFontsCallback(CONST LOGFONT *lplf,
CONST TEXTMETRIC *lptm,
DWORD dwType, LPARAM aFontName)
{
CString far * aiFontName = (CString far *) aFontName;
index++;
aiFontName[index]=lplf->lfFaceName;
return TRUE;
}
e.在对话框类.cpp 中OnInitDialog()里添加以下代码
index=-1;
CString aFontName[MAX_PATH];
CPaintDC dc(this);
EnumFonts(dc.m_hDC,(LPCTSTR)NULL,(FONTENUMPROC)EnumFontsCallback,
(LPARAM) aFontName);
for (int i=0;i<index;i++)
m_list.AddString(aFontName[i]);
1.创建滚动小车的步骤
a.创建一个对话框框架
b.在对话框类.h里添加以下代码
int m_yx;小车的位置
int m_yy;
void DrawCar(int yx,int yy);//画小车
c.添加WM_TIMER消息响应函数,并加如下代码
if (nIDEvent == 99)
{
m_yx += 10;
if (m_yx > 580)
{
m_yx = 30;
}
DrawCar(m_yx,m_yy);
}
d.在对话框类.cpp里OnInitDialog()函数中添加如下代码
m_yx = 30;
m_yy = 120;//初始化位置
SetTimer(99,100,NULL);//设置时间参数,第一个为nIDEvent
e.在对话框类.cpp创建DrawCar(int yx,int yy)函数,代码如下
{
if(yx<=0 || yy<0) return;
this->RedrawWindow(CRect(yx-50,yy,yx+50,yy+100));
CBrush pBrush;
pBrush.CreateSolidBrush(RGB(10,10,10));
CDC *pDC = GetDC();
pDC->FillRect(CRect(yx,yy,yx+20,yy+10),&pBrush);
pDC->FillRect(CRect(yx-10,yy+10,yx+40,yy+20),&pBrush);
pDC->Ellipse(yx,yy+20,yx+10,yy+30);
pDC->Ellipse(yx+20,yy+20,yx+30,yy+30);
}
2.创建一个运行的时钟步骤
a.创建一个单文档框架
b.在视图类.cpp中的OnDraw(CDC* pDC)函数里加入以下代码 绘制时钟
// 计算椭圆中心位置
int CenterX = Rect.right/2;
int CenterY = Rect.bottom/2;
//取当前时间
CTime Time = CTime::GetCurrentTime();
CString str;
int i,x,y;
CSize size;
//创建一只黄色的笔,用来 画椭圆
CPen pen(PS_SOLID,5,RGB(255,255,0));
//设置当前画笔,并记下以前的笔
CPen *OldPen = pDC->SelectObject(&pen);
// 绘制钟面椭圆
pDC->Ellipse(5,5,Rect.right-5,Rect.bottom-5);
double Radians;
//设置字体颜色为红色
pDC->SetTextColor(RGB(255,0,0));
for (i=1; i <= 12; i++)
{
//格式化钟点值
str.Format("%d",i);
//GetTextExtent method computes the width and height of a line of text using the current font to determine the dimensions
size = pDC->GetTextExtent(str,str.GetLength());
Radians = (double)i*6.28/12.0;//???
// 计算钟点放置的位置
x = CenterX - (size.cx/2) + (int)((double)(CenterX - 20)* sin(Radians));
y = CenterY - (size.cy/2) - (int)((double)(CenterY - 20)* cos(Radians));
//绘制钟点
pDC->TextOut(x,y,str);
}
//计算时钟指示的夹角
Radians = (double)Time.GetHour() + (double)Time.GetMinute()/60.0 +
(double)Time.GetSecond()/3600.0;
Radians *= 6.28/12.0;
//创建时钟指针画笔
CPen HourPen(PS_SOLID,5,RGB(0,255,0));
pDC->SelectObject(&HourPen);
//绘制时钟指针
pDC->MoveTo(CenterX,CenterY);
pDC->LineTo(CenterX+(int)((double)(CenterX/3)*sin(Radians)),
CenterY - (int)((double)(CenterY/3)*cos(Radians)));
Radians = (double)Time.GetMinute() + (double)Time.GetSecond()/60.0;
Radians *= 6.28/60.0;
//创建分钟指针画笔
CPen MinutePen(PS_SOLID,3,RGB(0,0,255));
pDC->SelectObject(&MinutePen);
//绘制分钟指针
pDC->MoveTo(CenterX,CenterY);
pDC->LineTo(CenterX+(int)((double)(CenterX*2/3)*sin(Radians)),
CenterY - (int)((double)(CenterY*2/3)*cos(Radians)));
Radians = (double)Time.GetSecond();
Radians *= 6.28/60.0;
//创建秒表指针画笔
CPen SecondPen(PS_SOLID,1,RGB(0,255,255));
pDC->SelectObject(&SecondPen);
//绘制秒表指针
pDC->MoveTo(CenterX,CenterY);
pDC->LineTo(CenterX+(int)((double)(CenterX*4/5)*sin(Radians)),
CenterY - (int)((double)(CenterY*4/5)*cos(Radians)));
pDC->SelectObject(OldPen);
c.在视图类里添加WM_TIMER消息函数;并加入以下代码
InvalidateRect(NULL,true);
UpdateWindow();
d.在视图类里添加WM_CREATE消息函数,加入以下代码
//设置计时器 1秒发送一次时间
SetTimer(1,1000,NULL);//1000
3.将一个窗口分成两部分;一边可以接受输入;一边显示画的图形
a.创建一个单文档框架, 继承CEditView类;在xView.h视图类前申明一个文挡类 class xDoc;
b.创建一个ArtView类, 继承CView类
c.ArtView类.cpp中的OnDraw(CDC* pDC)函数里添加代码 画一个图形
CBrush *pOldBrush=pDC->GetCurrentBrush();
//Create a solid blue brush
CBrush br;
br.CreateSolidBrush(RGB(255,120,0));
//**Select the blue brush in to the device context
pDC->SelectObject(&br);
pDC->Ellipse(10,10,300,300);
//Call this function to detach m_hDC (the output device context) from
//the CDC object and set both m_hDC and m_hAttribDC to NULL.
br.Detach();
br.CreateHatchBrush(HS_FDIAGONAL,RGB(255,255,0));
pDC->SelectObject(&br);
pDC->Ellipse(50,50,200,200);
//*Restore the current brush
pDC->SelectObject(pOldBrush);
d.在CMainFrame类.h添加CSplitterWnd m_wndSplitter;成员类
e.在CMainFrame类前包含ArtView类和视图类;并重载OnCreateClient()虚拟函数,加入以下代码
if (!m_wndSplitter.CreateStatic(this,1,2))
return false;
//**Create two views and insert in to the splitter panes
if (!m_wndSplitter.CreateView(0,0,RUNTIME_CLASS(CSplitViewView),//CSplitViewView为视图类名
CSize(150,100),pContext) ||
!m_wndSplitter.CreateView(0,1,RUNTIME_CLASS(CArtView),
CSize(150,100),pContext))
{
m_wndSplitter.DestroyWindow();
return false;
}
//**Return successful
return true;
4.安装向导的创建步骤
a.创建一个对话框框架
b.在ResourceView里添加三个对话框
第一对话框里添加静态文本: 欢迎你安装本程序!请安下一步继续,如果你想放弃,请安取消按钮放弃
第二个对话框添加 1.静态文本:请选择要添加的项 2.添加三个CheckBox控件
第三个对话框添加 静态文本:欢迎新用户使用本软件,按完成结束安装工作
c.以三个对话框的ID号创建三个类,都继承自CPropertyPage类
d.创建一个MySheet类,继承自CPropertySheet类 并在头文件里添加以下代码
#include "MyPage1.h"
#include "MyPage2.h"
#include "MyPage3.h"
#define FINISH 12325
#define NEXT 12324
#define BACK 12323
e.在MySheet类里添加OnInitDialog()虚拟函数,并加入以下代码:
//为安装向导的第一页设置了Next 按钮
SetWizardButtons(PSWIZB_NEXT);
f.在MySheet类里添加OnCmdMsg()虚拟函数,并加入以下代码:
//GetActiveIndexHA函数获得选项卡对话框的索引值存到iPage里!
int iPage = GetActiveIndex();
if (((iPage == 0)&&(nID == NEXT))||((iPage == 2)&&(nID == BACK)))
{
SetWizardButtons(PSWIZB_BACK|PSWIZB_NEXT);
}
else if (iPage == 1)
{
if (nID == NEXT)
{
SetWizardButtons(PSWIZB_BACK|PSWIZB_FINISH);
}
else if (nID == BACK)
{
SetWizardButtons(PSWIZB_NEXT);
}
}
g.在MySheet类.h里添加
CMyPage1 m_Page1;
CMyPage2 m_Page2;
CMyPage3 m_Page3;
MySheet();
h.在MySheet类.cpp里添加
MySheet::MySheet():CPropertySheet()
{
//添加三个选项卡到MySheet类中
AddPage(&m_Page1);
AddPage(&m_Page2);
AddPage(&m_Page3);
//为向导对话框设置风格
m_psh.dwFlags |= PSH_WIZARD97;
}
5.创建属性对话框的步骤
a.创建一个单文档框架
b.在ResourceView里添加三个对话框
c.第一个对话框创建一个控制它的 MySheet类,继承自CPropertySheet类
f.在MySheet类.h中添加 以下代码
#include "MyPage1.h"
#include "MyPage2.h"
public:
CMyPage1 mypage1;
CMyPage2 mypage2;
CMySheet();
g.在MySheet类.cpp中添加 以下代码
CMySheet::CMySheet()
{
AddPage(&mypage1);//把各页添加到对话框中!
AddPage(&mypage2);
}
d.第二个对话框创建一个控制它的 MyPage1类,继承自CPropertyPage类
MyPage1类添加两个静态文本1.姓名选项卡,2.姓名 和一个Edit Box其ID号为IDC_NAME
为IDC_NAME创建成员变量m_strName,类型为CString
为IDC_NAME添加消息EN_CHANGE函数,并在函数里添加以下代码
SetModified(true);//是 Apply按钮允许
UpdateData(true);
e.第三个对话框创建一个控制它的 MyPage2类,继承自CPropertyPage类
MyPage1类添加三个静态文本1.ID选项卡,2.学号,3身份证号 和
两个Edit Box其ID号为IDC_SNUM和IDC_PNUM
为IDC_SNUM创建成员变量m_strSnum,类型为CString
为IDC_PNUM创建成员变量m_strPnum,类型为CString
为IDC_PNUM和IDC_SNUM分别添加消息添加消息EN_CHANGE函数,并在函数里添加以下代码
SetModified(true);//是 Apply按钮允许
UpdateData(true);
f.在视图类里添加WM_RBUTTONDOWN消息,在其.cpp中包含#include"MySheet.h";并给消息函数加入代码
CMySheet mysheet;
mysheet.DoModal();
6.列表控件的使用方法
a.创建一个单文档框架
b.在ResourceView里添加一个对话框
c.在一个对话框里添加List Control控件;其控件View:里选择List;Align:里选择Top;
在添加一个静态文本其ID:为IDC_STATIC_LIST1
d.创建八个Icon;其ID为
e.创建一个控制对话框类xDlg;;添加一个CImageList m_imageList;类成员;
f.创建一个控制对话框类xDlg;添加一个WM_INITDIALOG消息函数;加入代码
HICON hIcon[8];//用于显示在List Control控件里
int n;
//BOOL Create( int cx, int cy, UINT nFlags, int nInitial, int nGrow );
m_imageList.Create(16,16,0,8,8);
hIcon[0] = AfxGetApp()->LoadIcon(IDI_WHITE);//IDI_WHITE为Icon的ID值
hIcon[1] = AfxGetApp()->LoadIcon(IDI_BLACK);
hIcon[2] = AfxGetApp()->LoadIcon(IDI_RED);
hIcon[3] = AfxGetApp()->LoadIcon(IDI_BLUE);
hIcon[4] = AfxGetApp()->LoadIcon(IDI_YELLOW);
hIcon[5] = AfxGetApp()->LoadIcon(IDI_CYAN);
hIcon[6] = AfxGetApp()->LoadIcon(IDI_PURPLE);
hIcon[7] = AfxGetApp()->LoadIcon(IDI_GREEN);
for (n = 0; n < 8; ++n )
{
m_imageList.Add(hIcon[n]);
}
static char *color[] = {"white","black","red","blue",
"yellow","cyan","purple","green"};
CListCtrl *pList = (CListCtrl*)GetDlgItem(IDC_LIST1);
pList->SetImageList(&m_imageList,LVSIL_SMALL);//
for (n = 0; n < 8;++n)
{
// int InsertItem( int nItem, LPCTSTR lpszItem, int nImage );
pList->InsertItem(n,color[n],n);
}
pList->SetBkColor(RGB(0,255,255));
pList->SetTextBkColor(RGB(0,255,255));
g.创建一个控制对话框类xDlg;添加一个LVN_ITEMCHANGED消息函数
CListCtrl *pList = (CListCtrl*)GetDlgItem(IDC_LIST1);
int nSelected = pNMListView->iItem;
if (nSelected >= 0)
{
//GetItemText(int nItem, int nSubItem ) const函数Retrieves the text of a list view item or subitem
//If nSubItem is zero, this function retrieves the item label
// if nSubItem is nonzero, it retrieves the text of the subitem
CString strItem = pList->GetItemText(nSelected,0);
SetDlgItemText(IDC_STATIC_LIST1,strItem);
}
h.在视图类里添加WM_LBUTTONDOWN消息处理函数;在其.cpp中加#include "TestDlg.h"
CxDlg dlg;
dlg.DoModal();
7.更改图片显示的步骤
a.创建一个对话框框架
b.在Dialog显示界面里添加三个Picture控件;其ID值分别为IDC_ST_PICTURE,IDC_ST_BACK,IDC_ST_OK
Type:都为Bitmap;并将IDC_ST_PICTURE控件的Extend styles的Transparent属性选中;
添加Combo Box控件 ID值为IDC_COMBO_FA;Type:Drop List;Owner draw:NO;并选中
Vertical scroll属性;添加Button控件,在styles里将Flag选中
c.在Dialog类里添加Member Variables;IDC_COMBO_FA;Type:CComboBox Member:m_falist
IDC_ST_BACK;Type:CStatic, Member:m_back;IDC_ST_OK;Type:CStatic,Member:m_ok;
IDC_ST_PICTURE;Type:CStatic,Member:m_pic;
d.在Dialog类里添加IDC_BTN_CHANGE的处理函数;代码如下
int nSel = m_falist.GetCurSel();
char AppPathName[100];
GetModuleFileName(NULL,AppPathName,100);
CString FileName=(CString)AppPathName;
if(FileName.ReverseFind('\\')>0)
FileName=FileName.Left(FileName.ReverseFind('\\')+1);
switch(nSel){
case 0:
m_pbbrush=CreateSolidBrush(RGB(191,223,255));
FileName = FileName + "\\Pic\\01.bmp";
break;
case 1:
m_pbbrush=CreateSolidBrush(RGB(255,208,232));
FileName = FileName + "\\Pic\\02.bmp";
break;
case 2:
m_pbbrush=CreateSolidBrush(RGB(255,255,210));
FileName = FileName + "\\Pic\\03.bmp";
break;
case 3:
m_pbbrush=CreateSolidBrush(RGB(190,125,255));
FileName = FileName + "\\Pic\\04.bmp";
break;
case 4:
m_pbbrush=CreateSolidBrush(RGB(210,255,222));
FileName = FileName + "\\Pic\\05.bmp";
break;
}
HBITMAP bitmap;
bitmap=(HBITMAP)LoadImage(AfxGetInstanceHandle(), FileName,IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
BITMAP bm;
GetObject(bitmap, sizeof(bm), &bm);
GetDlgItem(IDC_ST_PICTURE)->MoveWindow(CRect(10,15,bm.bmWidth,bm.bmHeight));
m_pic.SetBitmap(bitmap);
this->RedrawWindow();
e.在Dialog类里添加WM_CTLCOLOR消息函数;加入代码如下
switch(nCtlColor)
{
case CTLCOLOR_LISTBOX:
case CTLCOLOR_STATIC:
case CTLCOLOR_DLG:
case CTLCOLOR_MSGBOX :
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(RGB(0,0,0));
return m_pbbrush;
break;
default:
return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
break;
}
f.在Dialog类里OnInitDialog()函数;加入代码如下
m_falist.AddString("方案1(蓝色)");
m_falist.AddString("方案2(粉红色)");
m_falist.AddString("方案3(黄色)");
m_falist.AddString("方案4(紫色)");
m_falist.AddString("方案5(绿色)");
char AppPathName[100];
GetModuleFileName(NULL,AppPathName,100);
CString PathName=(CString)AppPathName;
CString FileName = "";
if(PathName.ReverseFind('\\')>0)
PathName=PathName.Left(PathName.ReverseFind('\\')+1);
FileName = PathName + "\\Pic\\back.bmp";
backmap=(HBITMAP)LoadImage(AfxGetInstanceHandle(), FileName,IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
m_back.SetBitmap(backmap);
GetDlgItem(IDC_ST_BACK)->MoveWindow(CRect(9,11,572,289));
FileName = PathName + "\\Pic\\ok.bmp";
okmap=(HBITMAP)LoadImage(AfxGetInstanceHandle(), FileName,IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
GetDlgItem(IDC_BTN_CHANGE)->GetWindowRect(&ret);
ScreenToClient(&ret);
m_ok.SetBitmap(okmap);
GetDlgItem(IDC_ST_OK)->MoveWindow(ret);
g.在Dialog类里添加WM_LBUTTONUP消息函数;加入代码如下
if(ret.PtInRect(point))
{
OnBtnChange();
}
h.在Dialog类.h里添加以下代码
public:
HBRUSH m_pbbrush;
HBITMAP backmap;
HBITMAP okmap;
CRect ret;
8.