windows-Customizing the application appearance

Source: Internet
Author: User

windows-Customizing the program appearance
Summary: Describes the operations of common application appearance properties
Modify the appearance of an application window
1. Modify before the window is created
You need to do this in the CMainFrame::P recreatewindow function of the frame window.
You can change the size and title of the window by adding the following code

#if 1    cs.cx400;    cs.cy300;    //设置自定义标题的两种方法    //cs.style &= ~FWS_ADDTOTITLE;    cs.style = WS_OVERLAPPEDWINDOW;    cs.lpszName = _T("chengzhi");#endif

2. Modify after the window is created
Need to operate in the CMainFrame::OnCreate function
Add the following code, remove the program title (document title),

#if 1    //窗口创建之后改变外观    SetWindowLong(m_hWnd, GWL_STYLE, WS_OVERLAPPEDWINDOW);#endif

If you want to modify the existing window style based on the following code can be used
The GetWindowLong function and the SetWindowLong correspond

#if 1    //在窗口创建之后去掉窗口的最大化按钮    SetWindowLong(m_hWnd, GWL_STYLE, GetWindowLong(m_hWnd, GWL_STYLE) & ~WS_MAXIMIZEBOX);#endif

Two modifying the window's cursor, icon and background
1. Modify before the window is created
Need to operate in CMainFrame::P Recreatewindow function
method One: you define a window class and register it, but this method is not recommended, too troublesome.

 #if 1  //does not recommend the use of methods, change icons, cursors and other properties  wndclass ws;    Ws.cbclsextra = 0 ;    Ws.cbwndextra = 0 ;    Ws.hbrbackground = static_cast  <HBRUSH> (Getstockobject (Black_brush));    Ws.hcursor = LoadCursor (nullptr , Idc_help);    Ws.hicon = LoadIcon (nullptr , Idi_error);    Ws.hinstance = AfxGetInstanceHandle ();    Ws.lpfnwndproc =::D efwindowproc;    Ws.lpszclassname = _t ( "Chengzhi" );    Ws.lpszmenuname = nullptr ; Ws.style = Cs_hredraw |    Cs_vredraw;    RegisterClass (&WS); Cs.lpszclass = _t ( "Chengzhi" );  #endif   

Note: Only icons can be modified in the frame class, and if you want to modify the background and cursor, you need to look in the view class.
Set a new window class in Cstyleview::P Recreatewindow to modify the background and cursor

#if 1    cs.lpszClass = _T("chengzhi");#endif

Method Two: Use global function AfxRegisterWndClass
To modify an icon in a frame class

#if 1    //建议使用的方法    00, LoadIcon(nullptr, IDI_WARNING));#endif

Modify the background and cursor in the view class

#if 1    //在view中修改客户区中的属性     cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW, LoadCursor(nullptrstatic_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)));#endif

2. Modify after the window is created
To modify the icon properties in the CMainFrame::OnCreate function of the framework class, you can view the MSDN Help for the specific API: Https://msdn.microsoft.com/library

#if 1    //窗口创建之后改变外观    SetWindowLong(m_hWnd, GWL_STYLE, WS_OVERLAPPEDWINDOW);#endif

modifying background and Cursor properties in the View class

#if 1    //在视图窗口创建完毕之后设置背景和鼠标指针的图标    SetClassLong(m_hWnd, GCL_HBRBACKGROUND, (LONG)GetStockObject(BLACK_BRUSH));    SetClassLong(m_hWnd, GCL_HCURSOR, (LONG)LoadCursor(nullptr, IDC_HELP));#endif

three-switch app icon
Since it is the icon to switch the application, it is necessary to operate in the framework class.
Define an array of icons first

private:    //存储图标的数组    HICON m_hIcons[3];

Switch in the CMainFrame::OnCreate function, makeintresource the macro to convert the specified ID into a string

#if 1    //Initialize the icon array, we use the3Method of m_hicons[0] = LoadIcon (AfxGetInstanceHandle (), Makeintresource (Idi_icon1));//The Theapp global object needs to be declared in the. cpp file for this function//extern Cstyleapp Theapp; m_hicons[2] = LoadIcon (AfxGetApp ()->m_hinstance, Makeintresource (Idi_icon3)); Set timer,id = 1, timedMs, 1s Toggle an icon settimer(1,     nullptr); Initialize sets the first icon setclasslong(M_hwnd, Gcl_hicon, (LONG) m_hicons[0]); #endif 

To add a timer Wm_timer event response to a framework class
Add the following code in the Cmainframe::ontimer response function:

#if 1    //因为初始化显示的是第一幅,所以这里设置index = 1,而不是0    staticintindex1;    //设置App图标    SetClassLong(m_hWnd, GCL_HICON, (LONG)m_hIcons[index]);    //使用取模运算, 将index限定在0-2之内    index = (++index3#endif

four toolbar (ToolBar) Programming
1. Note : The ID in toolbar corresponds to the ID of the menu item in the menus bar, we typically develop the menu bar first, and then provide the usual menu item actions to the toolbar.

2. Create Toolbar
Let's take a look at its inheritance level.

creating a toolbar still needs to be created in the Framework class
(1). Create a toolbar resource
Create a bitmap image like the system yourself

(2). Constructing CToolBar objects

private://自定义的新的工具栏    CToolBar m_newToolBar;

(3). Create ()/createex (), LoadToolBar

#if  1  //Create a custom toolbar  if  (!m_newtoolbar.createex (this , Tbstyle_flat, Ws_child | ws_visible | Cbrs_top | Cbrs_gripper | Cbrs_tooltips | cbrs_flyby | Cbrs_size_dynamic) | | !m_newtoolbar.loadtoolbar (IDR_TOOLBAR1)) {TRACE0 ( "failed to create toolbar \ n" ); return -1 ; //failed to create } //the Settings toolbar can be docked  m_newtoolbar.enabledocking (Cbrs_align_any); //setting the main frame window can be docked  EnableDocking (Cbrs_align_any); //docking the toolbar  DockControlBar (&m_newtoolbar); #endif   

3. Add a menu item to manage toolbars
Add a menu item and add an event response to add a code in the response function

void  cmainframe::o Nshowmytoolbar () {#if  0  //a complex display method, and you cannot display the  if  in the last display (m_    Newtoolbar.iswindowvisible ()) {M_newtoolbar.showwindow (sw_hide);    } else  {M_newtoolbar.showwindow (sw_show);    //the active object on the frame window is notified by the layout changes  recalclayout (); DockControlBar (&m_newtoolbar); #endif      #if  1     //a simple way to re-display the  in the last hidden place. Showcontrolbar (&m_newtoolbar,!m_newtoolbar.iswindowvisible (), FALSE); #endif  }  

We refine this menu item, plus a check mark, add a command update message for the menu item you just added, and handle it in the message response function

void CMainFrame::OnUpdateShow(CCmdUI *pCmdUI){    // TODO:  在此添加命令更新用户界面处理程序代码#if 1    //设置复选标记的状态    pCmdUI->SetCheck(m_newToolBar.IsWindowVisible());#endif}

Five status bar (CStatusBar) programming

1. Add your own status bar to the framework class
The framework class already has a status bar member variable defined

public:    CStatusBar  m_wndStatusBar;

Add a new status indicator flag

//状态栏的指示器数组,资源ID是自己定在字符串资源中的static UINT indicators[] ={    ID_SEPARATOR,           // 状态行指示器    IDS_TIMER,              //时间指示器    IDS_PROGRESS,           //进度条指示器    ID_INDICATOR_CAPS,    ID_INDICATOR_NUM,    ID_INDICATOR_SCRL,};

Set our added status indicator in the CMainFrame::OnCreate function to display the current time of the system

...#if 1    //状态栏显示时间    SetTimer(11000, nullptr);    //设置状态栏时间    CTime t = CTime::GetCurrentTime();    CString str = t.Format("%H:%M:%S");    //知道IDS_TIMER的索引是1    //m_wndStatusBar.SetPaneText(1, str);    /*不知道状态栏网格的索引*/    CClientDC dc(this);    CSize sz = dc.GetTextExtent(str);    //根据ID获取网格的索引    int index = m_wndStatusBar.CommandToIndex(IDS_TIMER);    //设置状态栏网格的宽度    m_wndStatusBar.SetPaneInfo(index, IDS_TIMER, SBPS_NORMAL, sz.cx);    //设置网格内容    m_wndStatusBar.SetPaneText(index, str);#endif...

In the response function of the timer update time, here is only an instance, not extracted into a function, the actual development process, can not appear such a duplicate code

void CMainFrame::OnTimer(UINT_PTR nIDEvent) {...#if 1    //设置状态栏时间    CTime t = CTime::GetCurrentTime();    CString str = t.Format("%H:%M:%S");    //知道IDS_TIMER的索引是1    //m_wndStatusBar.SetPaneText(1, str);    /*不知道状态栏网格的索引*/    CClientDC dc(this);    CSize sz = dc.GetTextExtent(str);    //根据ID获取网格的索引    int index = m_wndStatusBar.CommandToIndex(IDS_TIMER);    //设置状态栏网格的宽度    m_wndStatusBar.SetPaneInfo(index, IDS_TIMER, SBPS_NORMAL, sz.cx);    //设置网格内容    m_wndStatusBar.SetPaneText(index, str);#endif...    CFrameWnd::OnTimer(nIDEvent);}

2. Display the mouse position in the status bar
We need to get the position of the mouse and we need to capture the WM_MOUSEMOVW message in the View class.
Here are a few of the methods available to implement the functionality

void Cstyleview:: OnMouseMove (UINT nflags, CPoint point) {// TODO: Add the message Handler code here and/or call the default value CString str; Str. Format (_t ("x =%d, y =%d"), Point.x, POINT.Y);#if 0     ((cmainframe*) GetParent ())M_wndstatusbar.setwindowtext (str);#endif#if 0    //Set the text of the first grid in the status bar ((cmainframe*) GetParent ())Setmessagetext (str);#endif#if 0     ((cmainframe*) GetParent ())Getmessagebar ()SetWindowText (str);#endif#if 1    //Get Descendants window GetParent ()Getdescendantwindow (afx_idw_status_bar)SetWindowText (str);#endif    CView:: OnMouseMove (nflags, point);}

6. Progress bar Programming

1. Create a progress bar in the oncreate of the Framework class

#if 0    //创建水平进度条    //m_progress.Create(WS_CHILD | WS_VISIBLE, CRect(100, 100, 200, 120), this, 123);    //创建垂直进度条    m_progress.Create(WS_CHILD | WS_VISIBLE | PBS_VERTICAL, CRect(100100120200this123);    m_progress.SetPos(50);#endif

2. Create a progress bar in the status bar
We create them in a way that sends custom messages.
1. Customize a message for your own users

MainFrm.h#define UM_PROGRESS WM_USER + 1

2. Add a message map declaration

// 生成的消息映射函数protected:     afx_msg  OnProgress(WPARAM wParam, LPARAM lParam);

3. Defining Message Mappings

MainFrm.cppBEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)    ...      ON_MESSAGE(UM_PROGRESS, &CMainFrame::OnProgress)    ...END_MESSAGE_MAP()

4. Defining Message response functions

LRESULT CMainFrame::OnProgress(WPARAM wParam, LPARAM lParam) {    //在状态栏中显示进度条    CRect rect;    m_wndStatusBar.GetItemRect(2, &rect);    m_progress.Create123);    m_progress.SetPos(50);    0;}

5. Make the progress bar move up
In the handler function of the timer set above, we increase the progress percentage of the progress bar to achieve the dynamic effect.

#if 1    //设置进度栏的步长    //m_progress.SetStep(1);    //增加进度栏的步长    m_progress.StepIt();#endif

6. in order to be able to change the window size, keep the position of the progress bar unchanged, you need to capture the frame class WM_PAINT message, in this message reset the position of the progress bar, but this error occurred, the reason has not been found, we found that the reason can be e-mail told me, study.

void  cmainframe::onpaint () {cpaintdc DC (this ); //device context for painting  //TODO: Add Message Handler code here  //do Not Call Cframewnd::onpaint ()  for drawing messages #if  0  //show progress bar in status bar, there is a problem  CRect rect; M_wndstatusbar.getitemrect (2 , &rect); if  (!m_progress.m_hwnd) {m_progress. Create (Ws_child | Ws_visible, Rect, &m_wndstatusbar, 123 ); } else  {m_progress. MoveWindow (rect); } m_progress. SetPos (50 ); #endif  } 

7. Summary
Information: Https://yunpan.cn/cSIuhhchmL7ZH access password bf13

Please correct me.

windows-Customizing the application appearance

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.