How to add buttons on the title bar
Since the Windows operating system has appeared, the idea and means of man-machine interaction in the application have changed fundamental changes, and the interface of the computer has become friendly. The quality of a program is very large to determine the convenience of human-computer interaction. Currently, the title bar of most programs is all, how to make your own procedures and different are the dreams of each programmer, but the contents of changing the title bar have fairly difficult. This article describes how to add an icon button on the title bar, and when the mouse passes and click on the icon, the mouse will have different reactions. Follow the steps below.
Step 1: Open the VC programming environment, generate a new single document-based engineering Temp, all options take defaults, below, we generate three buttons icons on the title bar of this project.
Step 2: Download resource files, there are three files: capenbutton.cpp, capenbutton.h and thunk.h. Add these three files to the project (adding the method does not have to be said).
Step 3: Define variables CCAPTIONBUTTON CBEXTRA in Mainfrm.h; and of course, you want to include header file #include "captionButton.h".
Step 4: For engineering load bitmap resources, ID numbers are IDB_bitmap1, IDB_bitmap2, idb_bitmap3, idb_bitmap6, idb_bitmap7. These bitmaps will be displayed on the title bar, as for what kind of bitmap, look at your preference.
Step 5: Add the following code to the oncreate function of Mainfrm.cpp:
// Chicculy, m_hwnd is the window handle we have to handle
CbexTra.init (m_hwnd);
// / / Set the number of original buttons (maximize, minimize, and off) on the title bar
CbexTra.setnumofdefaultcaptions (3);
/ / Set the transparent color of the bitmap
ColorRef Crtransparent = RGB (255, 0, 255);
Cbextra.setTransparentColor (Crtransparent);
// The mouse is selected from the one bitmap.
CbexTra.setSelectionBitmap ((Hbitmap) loading (AfxGetInstanceHandle (),
MakeintResource (idb_bitmap7),
Image_bitmap,
0,
0,
Lr_loadmap3dcolors | lr_defaultcolor);
// After the mouse moves to a bitmap, the change in this bitmap
Hbitmap hmouseoverbitmap = (hbitmap) loading (AfxGetInstanceHandle (),
MakeintResource (idb_bitmap2),
Image_bitmap,
0,
0,
Lr_loadmap3dcolors | lr_defaultcolor;
// Set bitmap 1
Hbitmap hcaptionambitmap = (hbitmap) loading (AfxGetInstanceHandle (),
MakeintResource (idb_bitmap3),
Image_bitmap,
0,
0,
Lr_loadmap3dcolors | lr_defaultcolor;
Hbitmap hcaptionambitmaphilite =
CCAPTIONBUTTON :: CombineBitmaps (HcAptionAmbitmap,
HMouseOverbitmap, crtransparent);
// Set bitmap 2
Hbitmap hcaption2bitmap = (hbitmap) loading (AfxGetInstanceHandle (), makeintResource (idb_bitmap6),
Image_bitmap,
0,
0,
Lr_loadmap3dcolors | lr_defaultcolor;
Hbitmap hcaption2bitmaphilite =
CCAPTIONBUTTON :: CombineBitmaps (HcAption2bitmap,
HMouseOverbitmap, crtransparent);
// Set the bit map three
Hbitmap hcaption3bitmap = (hbitmap) loading (AfxGetInstanceHandle (),
MakeintResource (IDB_bitmap1),
Image_bitmap,
0,
0,
Lr_loadmap3dcolors | lr_defaultcolor;
Hbitmap hcaption3bitmaphilite =
CCAPTIONBUTTON :: CombineBitmaps (HcAption3bitmap,
HMouseOverbitmap, crtransparent);
// Use the above definition to create the bitmap on the title bar, the ID number of the treasurer gathertra, when the mouse passes
File: // Transform bitmap, change bitmap and prompt text when the mouse is selected.
CbexTra.new (1, HcAptionAmbitmaphilite, HcAptionambitmap, "Guan");
CbexTra.new (2, HcAption2bitmaphilite, HcAption2bitmap, "xi");
CbexTra.new (3, HcAption3bitmaphilite, HcAption3bitmap, "Ming");
Step 6: Now we can stop first and see how our results are. Compiling our engineering, run, we can find that there is three buttons on our title bar, and the bitmap changes when the mouse moves or click on it. Just we have not added to the handset on the bitmap, don't worry, let's add the corresponding processing function.
Step 7: When we click on the icon on the title bar, there will be a message WM_CBLBUTTONCLICKED generation, and the parameter wparam refers to the ID number of the icon.
Define message mapping functions in MainFRM.H AFX_MSG LRESULT HIT (WPARAM WPARAM, LPARAM LPARAM);
Define functions in mainfrm.cpp:
ON_MESSAGE (WM_CBLBUTTONCLICKED, HIT)
Lresult CMAINFRAME :: Hit (WPARAM WPARAM, LPARAM LPARAM)
{
Switch (WPARAM)
{// begin wparam
Case 1:
AfxMessageBox ("First CAPTION");
Break;
Case 2:
AfxMessageBox ("Second CAPTION");
Break;
Case 3:
AfxMessageBox ("Third CAPTION");
Break;
}
Return 1;
}
This way, when we click on the icon, different prompt dialogs will pop up, this is just an example, as for what functionality to make it with you.
Step 8: The dynamic changes of the title bar. During the execution of the program, if you want to change the title bar, you can also implement it, the following gives how to delete an icon and change an icon.
Void CMAINFRAME :: OnDelete ()
{
CbexTra.delete (1);
Void CMAINFRAME :: OnChange ()
{
CbexTra.Replace (1,
1,
HcAption4bitmaphilite,
HcAption4bitmap,
"pnewtooltiptext");
}
Ok, the function is realized, it is still satisfactory, I hope to be useful to you.
By routine analysis status
Bull77 Yesky
The status strip is a control bar that contains information, which is usually used for information and status prompts. Here information is a prompt string for menu commands or tool commands and other instructions / help information, and the status is used to indicate scroll locks and num locates, etc. Some keys. Status section usually the bottom of the frame window. The information line of the status bar displays the information of the related program status or the tool button or menu item pointing to the mouse. Status bars can neither accept user input, nor does it generate command information.
In fact, from the perspective of programmers (at least those programmers using AppWizard), the status strip is so common, and they are not like allowing users to edit resources like the toolbar. When creating a program framework, the user simply tells AppWizard to include a status bar for the application. It can be said that the user's work is completed. However, if we use the toolbar, we will find it to help us implement a lot of features.
As followed: We implements the scroll string, the coordinates of the mouse, and dynamic clocks on the status bar.
first step:
Run AppWizard Generate an engineering MyStatus accepting all the default settings, except for the following steps: Single Document; Step 4 Remove the Hook before Docking Bar, then click the Advanced button, select the maximized option in Window Styles. Click the Finish button, at this time we have generated a project. Run We can discover the status bars generated by the program default, then we must modify this status bar.
Step 2:
In this step we will move the status strip to the lower side of the menu. In Mainfrm.cpp, we can see the defined part of the status bar.
IF (! m_wndstatusbar.create (this) ||
M_WndStatusBar.setindicators (Indicators,
SIZEOF (INDICATORS) / SIZEOF (UINT))))
{
Trace0 ("Failed to Create Status Bar / N);
Return -1; // fail to create
}
What we have to do is change the above definition to
IF (! m_wndstatusbar.create (this, ws_child | ws_visible | CBRS_TOP, AFX_IDW_STATUS_BAR) ||
M_WndStatusBar.setindicators (Indicators,
SIZEOF (INDICATORS) / SIZEOF (UINT))))
{
Trace0 ("Failed to Create Status Bar / N);
Return -1; // fail to create
}
Operating the program, then we can find that the status strip has moved to the upper side.
third step:
In this step, we have implemented scrolling text on the status strip.
<1> In Mainfrm.cpp, we can find the following definitions
Static uint indeicators [] =
{
ID_separator,
ID_indicator_caps,
ID_indicator_num,
ID_Indicator_scrl,
}
We change it to
Static uint indeicators [] =
{
ID_separator,
ID_STATUS1,
ID_STATUS2,
ID_STATUS3,
ID_indicator_caps,
ID_indicator_num,
ID_Indicator_scrl,
}
<2> Select Resource Symbols in Menu View, select New in the dialog, enter ID_STATUS1, VALUE in Name, and define ID_STATUS2, ID_STATUS3.
<3> Define String Resources, select String Table in ResouceView, in which string resources are "ME" for id_status1, and the same method defines ID_STATUS2, ID_STATUS3.
<4> Define the following variables in Mainfrm.h:
PUBLIC:
CSTRING STR, STR1;
<5> Modify the oncreate function in mainfrm.cpp as follows:
INT CMAINFRAME :: OnCreate (lpcreatestruct lpcreatestruct)
{
IF (cframewnd :: oncreate (lpcreatestruct) == -1)
Return -1;
IF (! m_wndstatusbar.create (this, ws_child | ws_visible | cbrs_top, afx_idw_status_bar) ||! m_wndstatusbar.setindicators (Indicators, Sizeof (Indicators) / Sizeof (UINT))))
{
Trace0 ("Failed to Create Status Bar / N);
Return -1; // fail to create
}
M_WndStatusBar.SetPaneInfo (1, ID_STATUS1, SBPS_POPOPOUT, 320);
M_WndStatusBar.SetPaneInfo (2, ID_STATUS2, SBPS_POPOPOPOUT, 100);
M_WndStatusBar.SetPaneInfo (3, ID_STATUS3, SBPS_POPOPOUT, 100);
Str1 = str = "Hello, welcome to use this program, I wish you a happy family, the career is!";
Settimer (1,200, null);
Settimer (2, 10, null);
Return 0;
}
<6> Rendering function onTimer () in class CMAINFRAME, and add the following code:
Void CMAINFRAME :: ONTIMER (uint nidevent)
{
File: // Todo: Add Your Message Handler Code Here and / OR Call Default
IF (nidevent == 1) {
IF (str.isempty ()) Str = str1;
Str = str.right (str.getLength () - 2);
M_WndStatusBar.SetPanetext (1, STR);
}
IF (nidevent == 2) {
SystemTime T;
:: getLocalTime (& T);
CString str2;
Str2.format ("% d:% D:% D:% D", T.WHOUR, T.WMINUTE, T.WSECOND, T.WMILLISECONDS);
M_WndStatusBar.SetPanetext (3, STR2);
}
CframeWnd :: ONTIMER (Nidevent);
}
<7> In Mainfrm.h, define the protected: Change to PUBLIC:
<8> Overloads WM_MOUSEMOVE in class CMYSTATUSVIEW through class wizard, and adds the following code in the implementation function:
Void CMYSTATUSVIEW :: ONMOUSEMOVE (UINT NFLAGS, CPOINT POINT)
{
// Todo: add your message handler code here and / or call default
CString str3;
Str3.format ("x:% d, y:% d", point.x, point.y);
((CMAINFRAME *) AFXGETMAINWND ()) -> M_WndStatusBar.SetPanetext (2, STR3);
CView :: OnMousemove (NFLAGS, POINT);
}