1、修改应用程序外观修改应用程序外观建立一个单文档的MFC应用程序,修改一个应用程序的外观和大小,要在应用程序的窗口创建之前去修改,1.在CMainFrame类的PrecreateWindow()函数中去创建。BOOLCMainFrame:PreCreateWindow(CREATESTRUCT&cs)if(!CFrameWnd:PreCreateWindow(cs)returnFALSE;/TODO:ModifytheWindowclassorstylesherebymodifying/theCREATESTRUCTcscs.cx=300;/宽度cs.cy=200;/高度/cs.style&=F
2、WS_ADDTOTITLE;/cs.style=cs.style&FWS_ADDTOTITLE;/这两个表达式一样cs.style=WS_OVERLAPPEDWINDOW;/也可以直接给这个类型赋值cs.lpszName=维唯为为;/窗口标题returnTRUE;2.用SetWindowLong在窗口创建之后改变外观,在CMainFrame类中的OnCreate()函数中去编写代码。intCMainFrame:OnCreate(LPCREATESTRUCTlpCreateStruct)if(CFrameWnd:OnCreate(lpCreateStruct)=-1)return-1;if(!m
3、_wndToolBar.CreateEx(this,TBSTYLE_FLAT,WS_CHILD|WS_VISIBLE|CBRS_TOP|CBRS_GRIPPER|CBRS_TOOLTIPS|CBRS_FLYBY|CBRS_SIZE_DYNAMIC)|!m_wndToolBar.LoadToolBar(IDR_MAINFRAME)TRACE0(Failedtocreatetoolbarn);return-1;/failtocreateif(!m_wndStatusBar.Create(this)|!m_wndStatusBar.SetIndicators(indicators,sizeof(in
4、dicators)/sizeof(UINT)TRACE0(Failedtocreatestatusbarn);return-1;/failtocreate/TODO:Deletethesethreelinesifyoudontwantthetoolbarto/bedockablem_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);EnableDocking(CBRS_ALIGN_ANY);DockControlBar(&m_wndToolBar);/修改窗口的外观/SetWindowLong(m_hWnd,GWL_STYLE,WS_OVERLAPPEDWIND
5、OW);/修改现有窗口的类型SetWindowLong(m_hWnd,GWL_STYLE,GetWindowLong(m_hWnd,GWL_STYLE)&WS_MAXIMIZEBOX);/使最大化图标变灰return0;3.编写自己的窗口类在CMainFrame:PreCreateWindow(CREATESTRUCT& cs)函数中添加:WNDCLASSwndcls;/定义一个窗口类wndcls.cbClsExtra=0;/类的额外内存,0,不需要wndcls.cbWndExtra=0;/窗口的额外内存,0,不需要wndcls.hbrBackground=(HBRUSH)GetStockOb
6、ject(BLACK_BRUSH);/背景wndcls.hCursor=LoadCursor(NULL,IDC_HELP);/NULL,指定标准光标/注:要改变窗口的背景和光标要在View类中去改变,因View窗口覆盖在Frame窗口之上wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);/图标wndcls.hInstance=AfxGetInstanceHandle();/获取当前应用程序的句柄wndcls.lpfnWndProc=:DefWindowProc;/窗口过程用缺省的,函数名指函数的地址wndcls.lpszClassName=luowei.org;/类的
7、名字wndcls.lpszMenuName=NULL;wndcls.style=CS_HREDRAW|CS_VREDRAW;/水平重画,垂直重画RegisterClass(&wndcls);/注册窗口类cs.lpszClass=luowei.org;在CStyleView:PreCreateWindow(CREATESTRUCT& cs)函数中添加:BOOL CStyleView:PreCreateWindow(CREATESTRUCT& cs) / TODO: Modify the Window class or styles here by modifying / the CREATEST
8、RUCT cs /修改窗口类 cs.lpszClass=luowei.org;/修改成自己定义的类 return CView:PreCreateWindow(cs);4.利用AfxRegisterWnd函数修改在CMainFrame:PreCreateWindow 中添加:cs.lpszClass=AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW,0,0,LoadIcon(NULL,IDI_WARNING);/cs.lpszClass=AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW)/设置成缺省值AfxRegisterWnd
9、Class LPCTSTR AFXAPI AfxRegisterWndClass( UINT nClassStyle, HCURSOR hCursor = 0, HBRUSH hbrBackground = 0, HICON hIcon = 0 ); Return ValueA null-terminated string containing the class name. You can pass this class name to the Create member function in CWnd or other CWnd-derived classes to create a w
10、indow. The name is generated by the Microsoft Foundation Class Library.NoteThe return value is a pointer to a static buffer. To save this string, assign it to a CString variable. ParametersnClassStyleSpecifies the Windows class style or combination of styles, created by using the bitwise-OR (|) oper
11、ator, for the window class. For a list of class styles, see the WNDCLASS structure in the Win32 SDK documentation. If NULL, the defaults will be set as follows: Sets the mouse style to CS_DBLCLKS, which sends double-click messages to the window procedure when the user double-clicks the mouse.Sets th
12、e arrow cursor style to the Windows standard IDC_ARROW.Sets the background brush to NULL, so the window will not erase its background.Sets the icon to the standard, waving-flag Windows logo icon. hCursorSpecifies a handle to the cursor resource to be installed in each window created from the window
13、class. If you use the default of 0, you will get the standard IDC_ARROW cursor.hbrBackgroundSpecifies a handle to the brush resource to be installed in each window created from the window class. If you use the default of 0, you will have a NULL background brush, and your window will, by default, not
14、 erase its background while processing WM_ERASEBKGND.hIconSpecifies a handle to the icon resource to be installed in each window created from the window class. If you use the default of 0, you will get the standard, waving-flag Windows logo icon.RemarksThe Microsoft Foundation Class Library automati
15、cally registers several standard window classes for you. Call this function if you want to register your own window classes.The name registered for a class by AfxRegisterWndClass depends solely on the parameters. If you call AfxRegisterWndClass multiple times with identical parameters, it only regis
16、ters a class on the first call. Subsequent calls to AfxRegisterWndClass with identical parameters simply return the already-registered classname.If you call AfxRegisterWndClass for multiple CWnd-derived classes with identical parameters, instead of getting a separate window class for each class, eac
17、h class shares the same window class. This can cause problems if the CS_CLASSDC class style is used. Instead of multiple CS_CLASSDC window classes, you end up with one CS_CLASSDC window class, and all C+ windows that use that class share the same DC. To avoid this problem, call AfxRegisterClass to r
18、egister the class.在CStyleView:PreCreateWindow(CREATESTRUCT& cs)中修改:BOOLCStyleView:PreCreateWindow(CREATESTRUCT&cs)/TODO:ModifytheWindowclassorstylesherebymodifying/theCREATESTRUCTcs/修改窗口类/cs.lpszClass=luowei.org;/修改成自己定义的类/修改背景和光标cs.lpszClass=AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW,LoadCursor(NULL
19、,IDC_CROSS),(HBRUSH)GetStockObject(BLACK_BRUSH),0);/cs.lpszClass=AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW)/设置成缺省值returnCView:PreCreateWindow(cs);5.窗口创建之后,再创建图标,光标及背景 利用SetClassLong函数,在CMainFrame:OnCreate(LPCREATESTRUCT lpCreateStruct)函数中添加:/修改图标 SetClassLong(m_hWnd,GCL_HICON,(LONG)LoadIcon(NULL,IDI
20、_ERROR);修改光标和背景,对CStyleView类添加一个WM_CREATE消息响应,编辑:intCStyleView:OnCreate(LPCREATESTRUCTlpCreateStruct)if(CView:OnCreate(lpCreateStruct)=-1)return-1;/TODO:Addyourspecializedcreationcodehere/修改背景SetClassLong(m_hWnd,GCL_HBRBACKGROUND,(LONG)GetStockObject(BLACK_BRUSH);/修改光标SetClassLong(m_hWnd,GCL_HCURSOR
21、,(LONG)LoadCursor(NULL,IDC_HELP);return0;6.实现动态图标Insert - 资源 - 导入三个图标,在CMainFrame类上定义三个用于存放图标句柄的数组,对CMainFrame类添加一个HICON类型的私有成员变量m_hIcons3,并在CMainFrame:OnCreate函数中支去加载图标:externCStyleApptheApp;/声明这个变量是在外部定义的intCMainFrame:OnCreate(LPCREATESTRUCTlpCreateStruct)./加载图标m_hIcons0=LoadIcon(AfxGetInstanceHan
22、dle(),MAKEINTRESOURCE(IDI_ICON1);/方式一m_hIcons1=LoadIcon(theApp.m_hInstance,MAKEINTRESOURCE(IDI_ICON2);/方式二,theApp是外部的全局变量,在CMainFrame:OnCreate函数之前要声明m_hIcons2=LoadIcon(AfxGetApp()-m_hInstance,MAKEINTRESOURCE(IDI_ICON3); SetClassLong(m_hWnd,GCL_HICON,(LONG)m_hIcons0);/将图标设置成引入的第一个图标/设置定时器SetTimer(1,1
23、000,NULL);return0;再给CMainFrame类添加WM_TIMER消息处理,编辑:voidCMainFrame:OnTimer(UINTnIDEvent)/TODO:Addyourmessagehandlercodehereand/orcalldefaultstaticintindex=1;/静态的变量是存放在数据区当中,而不是分配在栈当中SetClassLong(m_hWnd,GCL_HICON,(LONG)m_hIconsindex);index=+index%3;/让图标不断的循环变幻CFrameWnd:OnTimer(nIDEvent);7.工具栏的编程在菜单Menu下
24、IDR_MAINFRAME中添加一个菜单项Test,在图标Toolbar下IDR_MAINFRAME中添加一个图标。ID号都取为:IDC_TEST。对Test菜单添加一个COMMAND消息响应函数,编辑:void CMainFrame:OnTest() / TODO: Add your command handler code here MessageBox(Test);在工具栏图标之间,添加分隔符,可以用鼠标左键拖住图标往左移一点即可; 可以用鼠标左键把图标拖出工具栏外边即可!8.创建工具栏在ToolBar上新建一个工具栏IDR_TOOLBAR1,并在CMainFrame.h中添加:prot
25、ected: / control bar embedded members CStatusBar m_wndStatusBar; CToolBar m_wndToolBar; CToolBar m_newToolBar;/创建一个ToolBar然后在CMainFrame:OnCreate(LPCREATESTRUCT lpCreateStruct)中编辑:intCMainFrame:OnCreate(LPCREATESTRUCTlpCreateStruct)if(CFrameWnd:OnCreate(lpCreateStruct)=-1)return-1;if(!m_wndToolBar.Cr
26、eateEx(this,TBSTYLE_FLAT,WS_CHILD|WS_VISIBLE|CBRS_TOP|CBRS_GRIPPER|CBRS_TOOLTIPS|CBRS_FLYBY|CBRS_SIZE_DYNAMIC)|!m_wndToolBar.LoadToolBar(IDR_MAINFRAME)TRACE0(Failedtocreatetoolbarn);return-1;/failtocreateif(!m_wndStatusBar.Create(this)|!m_wndStatusBar.SetIndicators(indicators,sizeof(indicators)/size
27、of(UINT)TRACE0(Failedtocreatestatusbarn);return-1;/failtocreate/TODO:Deletethesethreelinesifyoudontwantthetoolbarto/bedockablem_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);EnableDocking(CBRS_ALIGN_ANY);DockControlBar(&m_wndToolBar);/修改窗口的外观/SetWindowLong(m_hWnd,GWL_STYLE,WS_OVERLAPPEDWINDOW);/修改现有窗口的类型/SetWindowLong(m_hWnd,GWL_STYLE,GetWindowLong(m_hWnd,GWL_STYLE)&WS_MAXIMIZEBOX);/使最大化图标变灰/修改图标/SetClassLong(m_hWnd,GCL_HICON,(LONG)LoadIcon(NULL,IDI_ERROR);/加载图标m_hIcons0=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_ICON1);/方式一m_hIcons1=LoadIcon(theApp.m_hInstance,
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1