---- Everyone is using some software, have some software to have some interesting things. For example, there is a button on the title bar of the main window. Such small controls are visible everywhere in the Internet. How can the button to join the non-client area?
---- Here, the most critical point is that you should not be misled by traditional knowledge: I really think it is a button. The Handle's control must not be placed on the title bar. Experienced programmers use Spy to track, immediately discover the secrets. It is not a button, but it is only a way to deal with a button.
---- Since we know, then we can't do it yourself, of course, no problem, let's use delphi to implement it, tell my annotation.
---- Before the specific example, we should know the important news about the title bar:
---- WM_NCPAINT: Heavy painting title bar message. We must intercept it, you can redraw buttons here;
---- WM_NCLBUTTONDOWN: Press the left mouse button on the title bar. We can intercept it, draw a button on the title bar, and you can do your own clicked events, make it like a button;
---- WM_NCLBUTTONUP: Release the left mouse button on the title bar. We can intercept it, draw the look on the button on the title bar;
---- WM_NCLBUTTONDBLCLK: Double-click the left mouse button on the title bar. We can intercept it, when we do it in the button area, we should make it invalid, so that the form performs maximum and restore operations.
---- wm_ncrbuttondown: Press the right mouse button on the title bar. We can intercept it, when we do it in the button area, we should make it invalid, thereby avoiding pop-up formula menu.
---- WM_NCMOUSEMOVE: Move mouse messages on the title bar. We can intercept it, when the mouse is removed from the button area, we must draw a button until it is pressed, that is, when the projection is raised.
---- WM_NCActivate: Receives the message when the title bar is switched between activation and non-activation. We can intercept it, when the window is processed, we can do something, such as turning the font on our title bar button or changing the current state of the window. Below I didn't join this feature. If you are interested, you can do it yourself.
---- (Everyone can find that the message on the title bar is the beginning of WM_NC)
Add buttons on the title bar 2001-10-30 · · Bull · Yesky
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 largely determined to the convenience of human-computer interaction. At present, the title bar of most programs is all, how to make your own procedure and uniqueness is the dream of each programmer, but the content of changing the title column is indeed quite 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:
// Chicked, m_hwnd is the window handle CbexTra.init (m_hWnd) we have to handle; // // Set the original button (maximize, minimize, and off) Cbeextra.setnumofdefaultcaptions (3 ); transparent color COLORREF crTransparent // the bitmap = RGB (255,0,255); cbExtra.SetTransparentColor (crTransparent); look after the bitmap // mouse to select a bitmap cbExtra.SetSelectionBitmap ((HBITMAP) LoadImage (AfxGetInstanceHandle (), MAKEINTRESOURCE (IDB_BITMAP7), IMAGE_BITMAP, 0, 0, LR_LOADMAP3DCOLORS | LR_DEFAULTCOLOR)); // the cursor to a bitmap, the bitmap change HBITMAP hMouseOverBitmap = (HBITMAP) LoadImage (AfxGetInstanceHandle (), MAKEINTRESOURCE (IDB_BITMAP2 ), Image_bitmap, 0, 0, lr_loadmap3dcolors | lr_defaultcolor;
// Set bitmap 1Hbitmap HcAptionAmbitmap = (HbitMap) loadImage (AfxGetInstanceHandle (), makeintResource (IDB_bitmap3), image_bitmap, 0, 0, lr_loadmap3dcolors | lr_defaultcolor);
Hbitmap hcaptionambitmaphilite = ccaptionButton :: CombineBitmaps (HcAptionambitmap, hmouseoverbitmap, crtransparent);
// Setting bitmap 2Hbitmap hcaption2bitmap = (hbitmap) loadImage (AfxGetInstanceHandle (), makeintresource (idb_bitmap6), image_bitmap, 0, 0, lr_loadmap3dcolors | lr_defaultcolor);
HbitMap hcaption2bitmaphilite = ccaptionButton :: CombineBitmap (HcAption2bitmap, hmouseoverbitmap, crtransparent);
// the bitmap three HBITMAP hCaption3Bitmap = (HBITMAP) LoadImage (AfxGetInstanceHandle (), MAKEINTRESOURCE (IDB_BITMAP1), IMAGE_BITMAP, 0, 0, LR_LOADMAP3DCOLORS | LR_DEFAULTCOLOR); HBITMAP hCaption3BitmapHilite = CCaptionButton :: (hCaption3Bitmap, hMouseOverBitmap, crTransparent) CombineBitmaps;
// Use the above definition to create a bitmap, a treasurer bitmap ID number on the title bar, the File: // transform bitmap when the mouse is passed, and 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 wParamcase 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 :: overdelete () {cbextra.delete (1);
Void CMAINFRAME :: OnChange () {Cbeextra.Replace (1, 1, HcAption4bitmaphilite, HcAption4bitmap, "pnewtooltext");}
Ok, the function is realized, it is still satisfactory, I hope to be useful to you.