Current location - Recipe Complete Network - Complete cookbook of home-style dishes - How to combine menu bar and title bar into one
How to combine menu bar and title bar into one
You will find that more and more desktop applications now combine the menu bar with the title bar. In order to achieve this effect, there are usually two schemes:

Draw a menu on the title bar.

Remove the title bar and use the menu bar as the title.

Here is a brief description of the effect: on the interface of the application, the title bar and the menu bar are displayed in the same area, and the user clicks the menu area to pop up the corresponding menu; Clicking on other blank areas is like clicking on the title bar (I don't know, but I think everyone understands).

The second scheme, in fact, has two main points: remove the title bar and use the menu bar to simulate the title bar.

SetWindowPos

WM_NCHITTEST

. Through this news, we know that a window has been subdivided into more than 20 areas, two of which are of interest to us now, HTMENU and HTCAPTION. Now the idea is coming. When processing WM_NCHITTEST message, if we can convert part of the HTMENU returned by the system into HTCAPTION, can we achieve the effect of simulating the title bar? Facts tell us that it has basically been achieved. The code is as follows:1:uintcls _ onnchittest (hwndhwnd,

intx,

inty)2:{ 3:UINT uNcHitResult = FORWARD _ WM _ NCHITTEST(hwnd,x,y,DefWindowProc); 4:5:if(ht menu = = uNcHitResult)6:{ 7:hm enu hm enu = get menu(hwnd); 8:intnMenuItemCount = GetMenuItemCount(hMenu); 9: 10:RECT rcTheLastMenu = { 0,0,0,0 }; 11:if (getmenuitemrect (hwnd, hMenu, nmeuitemcount-1,& amprcthelastmenu))12: {13://When clicking in the blank area of the menu. right

After this process, clicking/double-clicking the blank area of the menu bar and dragging it are like operating the title bar.

After writing this code, you will find several problems:

Right-click an empty area of the menu bar to view the system menu.

When maximized, it is full screen, and the window covers the taskbar.

Therefore, this simulation method only basically realizes the function, and has not fully met the requirements. Now let's solve these problems step by step. Look at the system menu first.

intx,

Internationalorganizations (same as international organizations)

y,UINT code hittest)2:{ 3:if(ht caption = = code hittest)4:{ 5:HMENU hsys menu = get system menu(hwnd,

False); 6:intnMenuAlign = GetSystemMetrics(SM _ MENUDROPALIGNMENT); 7:8:// Please pay special attention to TPM_RETURNCMD. We need to know which system menu item the user clicked: boolbres = trackupmenuex (hsysmenu, nmenuualign | TPM _ returncmd, x, y, hwnd, null); 10:return forward _ WM _ sys command(hwnd,bRes,x,y,DefWindowProc); 1 1:} 12: 13:FORWARD _ WM _ NCRBUTTONUP(hwnd,x,y,codeHitTest,DefWindowProc); 14:}

So far, we seem to have solved all the menu problems. But in fact, the problem is still a little. There seems to be a problem with the status of the system menu item. In order to solve this problem, we need to use WM_INITMENU message to challenge the corresponding menu item state by maximizing or minimizing the current window state. Look at the code:1:void cls _ oninit menu (hwnd hwnd, hmenu hmenu) 2: {3: if (get system menu (hwnd,

FALSE)= = hMenu)4:{ 5:UINT uSysCmds[]= { SC _ SIZE,SC_MOVE,SC_MINIMIZE,SC_MAXIMIZE,SC_CLOSE,SC _ RESTORE }; 6:if(isi conic(hwnd))7:{ 8:for(inti = 0; I< _ countof (usyscmds); ++ I)9:{ 10:switch(uSysCmds[i]) 1 1:{ 12:caseSC _ MAXIMIZE: 13:caseSC _ RESTORE: 14:caseSC _ CLOSE: / kloc-0/5:EnableMenuItem(hMenu,uSysCmds[I],MF _ ENABLED); 16: broken; 17: 18: default value: 19:EnableMenuItem(hMenu, uSysCmds[i], MF _ disabled | MF _ grayed); 20: broken; 2 1:} 22:} 23:} 24:else if(iszolled(hwnd))25:{ 26:for(inti = 0; I< _ countof (usyscmds); ++ I)27:{ 28:switch(uSysCmds[i])29:{ 30:caseSC _ RESTORE:3 1:caseSC _ MINIMIZE:32:caseSC _ CLOSE:33:EnableMenuItem(hMenu,uSysCmds[I],MF _ ENABLED); 34: broken; 35:36: default value: 37:EnableMenuItem(hMenu, uSysCmds[i], MF _ disabled | MF _ grayed); 38: broken; 39:} 40:} 4 1:} 42:else 43:{ 44:for(inti = 0; I< _ countof (usyscmds); ++ I)45:{ 46:switch(uSysCmds[i])47:{ 48:caseSC _ RESTORE:49:enable menuitem(hMenu,uSysCmds[I],MF _ DISABLED | MF _ GRAYED); 50: broken; 5 1:52: default value: 53:EnableMenuItem(hMenu, uSysCmds[i], MF _ enabled); 54: broken; 55:} 56:} 57:} 58:} 59:else 60:{ 6 1:FORWARD _ WM _ init menu(hwnd,hMenu,DefWindowProc); 62:}63:}

You're screwed.. Well, there seems to be something wrong. There seems to be something wrong with the status of the system menu that pops up for the first time. If it pops up again, it seems right. What happened? Blame GetSystemMenu. To solve this problem, just call GetSystemMenu to copy before displaying the system menu.

At this point, the problems of the system menu should be solved. The following is to solve the problem of full screen.