用VC++ MFC 修改外观和大小,图标、光标、背景

来源:互联网 发布:淘宝卖家生存现状 编辑:程序博客网 时间:2024/05/16 13:32
如何修改MFC AppWizard向导生成的框架程序的外观和大小,修改图标、光标、背景的三种方法。如何增加和删除工具栏按钮,如何给应用程序增加工具栏,如何显示和隐藏工具栏。定制状态栏,在状态栏中添加时钟显示,CTime类及其用法。在状态栏中添加进度条(主窗口产生后立即产生进度条的巧妙思想,不能在OnCreate函数中直接处理,要用到自定义消息的方法)。鼠标坐标显示,在CView中获取状态栏对象的几种方式。如何为应用程序添加启动画面。


CMainFrame 样式修改
1、在窗口创建前修改:根据多态性原理,在CMainFrame的PreCreateWindow函数中修改。
   In a single document interface (SDI) application, the default window style in the framework is a combination of the WS_OVERLAPPEDWINDOW and FWS_ADDTOTITLE styles. FWS_ADDTOTITLE is an MFC-specific style that instructs the framework to add the document title to the window’s caption. To change the window attributes in an SDI application, override the PreCreateWindow function in your class derived from CFrameWnd (which AppWizard names CMainFrame). For example:

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
    // Create a window without min/max buttons or sizable border
    cs.style = WS_OVERLAPPED | WS_SYSMENU | WS_BORDER;

    // Size the window to 1/3 screen size and center it
    cs.cy = ::GetSystemMetrics(SM_CYSCREEN) / 3;
    cs.cx = ::GetSystemMetrics(SM_CXSCREEN) / 3;
    cs.y = ((cs.cy * 3) - cs.cy) / 2;
    cs.x = ((cs.cx * 3) - cs.cx) / 2;

    // Call the base-class version
    return CFrameWnd::PreCreateWindow(cs);
}

This code creates a main frame window without
2、在窗口创建后修改:OnCreate函数中修改,里面加上函数SetWindowLong
The SetWindowLong function changes an attribute of the specified window. The function also sets a 32-bit (long) value at the specified offset into the extra window memory of a window.

index为GWL_STYLE;样式的意思 其他有
GWL_EXSTYLE    Retrieves the extended window styles.
GWL_STYLE      Retrieves the window styles.
GWL_WNDPROC    Retrieves the address of the window procedure, or a handle representing the                address of the window procedure. You must use the CallWindowProc function to                call the window procedure.
GWL_HINSTANCE  Retrieves the handle of the application instance.
GWL_HWNDPARENT Retrieves the handle of the parent window, if any.
GWL_ID         Retrieves the identifier of the window.
GWL_USERDATA   Retrieves the 32-bit value associated with the window. Each window has a                corresponding 32-bit va

LONG SetWindowLong(
  HWND hWnd,       // handle of window
  int nIndex,      // offset of value to set
  LONG dwNewLong   // new value
);
另外一个对应的函数
The GetWindowLong function retrieves information about the specified window. The function also retrieves the 32-bit (long) value at the specified offset into the extra window memory of a window.

LONG GetWindowLong(
  HWND hWnd,  // handle of window
  int nIndex  // offset of value to retrieve
);
2、如何修改光标、图标等
MFC默认的光标、图标是由MFC底层代码完成的。当然,我们是不能修改MFC的底层代码的。
创建之前
而应该:
方法1:
在PreCreateWindow函数,定义一个自己的窗口类
 HINSTANCE AfxGetInstanceHandle( );这个函数返回当前应用程序实例的句柄。
 DefWindowProc默认的窗口函数。注意,这里用的是全局API函数,前面要有::
 菜单不是这个时候加上去的,而是在应用程序类的初始化函数加上去的,所以暂时先设置为NULL
然后注册这个窗口类
改变cs的属性类名为刚刚定义的这个类。
在框架窗口中只能修改图标等没被View覆盖的。
光标修改:应该在View类中的PreCreateWindow修改,将cs属性类名为刚刚定义的这个类。
这个方法麻烦!
方法2:
使用下面这个函数:
LPCTSTR AFXAPI AfxRegisterWndClass( UINT nClassStyle, HCURSOR hCursor = 0, HBRUSH hbrBackground = 0, HICON hIcon = 0 );
nClassStyle 为样式 参看 WndClass的style成员。msdn
在框架类的PreCreateWindow函数中 用它修改图标。
在视图类的PreCreateWindow函数中,用它修改背景、光标、画刷。
创建之后
方法1:
SetClassLong函数
The SetClassLong function replaces the specified 32-bit (long) value at the specified offset into the extra class memory or the WNDCLASSEX structure for the class to which the specified window belongs.

DWORD SetClassLong(
  HWND hWnd,       // handle of window
  int nIndex,      // index of value to change
  LONG dwNewLong   // new value
);
用法查看msdn 注意类型转换。

3、获取当前应用程序与实例句柄
AfxGetApp() 它也有个成员是实例句柄
AfxGetInstanceHandle()
4、WM_TIMER消息使用
时间间隔设置 SetTimer()在构造函数里使用。


5、创建自定义工具栏的步骤
Visual C++ provides you with two methods to create a toolbar. To create a toolbar resource using the Resource Editor, follow these steps:
(1)Create a toolbar resource.
(2)Construct the CToolBar object.
(3)Call the Create (or CreateEx) function to create the Windows toolbar and attach it to the CToolBar object.
(4)Call LoadToolBar to load the toolbar resource.
参看默认生成步骤.
6、状态栏
添加新的状态
如何访问状态拦
7、进度栏
8、自定义消息
The WM_USER constant is used by applications to help define private messages, usually of the form WM_USER+X, where X is an integer value.

#define WM_USER       0x0400

使用:#define UM_PROGRESS WM_USER+1
原创粉丝点击