Problem with IE style menu bar
- From: "banrenma23@xxxxxxxxxxxx" <banrenma23@xxxxxxxxxxxx>
- Date: 20 Sep 2005 03:46:57 -0700
Hi all,
I want to create a IE style menu bar. Below is my code that based on
sample from MSDN.
However,my menu can not display normally. I still do not know the
reason.
can any body help me?
// MenuBar.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
////////////////////////////////////////////////////////////
#include <commctrl.h>
#include <strsafe.h>
/////////////////////////////////////////////////////////////
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
/////////////////////////////////////////////////////////
#define ID_File 1000
#define ID_Edit 1001
#define ID_TOOLBAR 1002
#define MAX_LEN 16
#define NUM_BUTTONS 3
HWND hWndParent;
HWND hWndMB;
BOOL CreateMenubar(HWND hWnd);
LRESULT CALLBACK MenubarProc(HWND hWnd,UINT message,WPARAM
wParam,LPARAM lParam);
////////////////////////////////////////////////////////
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_MENUBAR, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_MENUBAR);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this
code
// to be compatible with Win32 systems prior to the
'RegisterClassEx'
// function that was added to Windows 95. It is important to call
this function
// so that the application will get 'well formed' small icons
associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_MENUBAR);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;//(LPCSTR)IDC_MENUBAR;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global
variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM
lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
switch (message)
{
case WM_CREATE:
//=======create menu bar =======//
CreateMenubar(hWnd);
//==============================//
break;
case WM_SIZE:
UpdateWindow(hWnd);
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM
lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
/*
message handler for Menubar
*/
///////////////////////////////////////////////////////
LRESULT CALLBACK MenubarProc(HWND hWnd,UINT message,WPARAM
wParam,LPARAM lParam)
{
LPNMHDR lpnm = (LPNMHDR)lParam;
LPNMTOOLBAR lpnmTB =(LPNMTOOLBAR)lParam;
RECT rc;
TPMPARAMS tpm;
HMENU hPopupMenu = NULL;
HMENU hMenuLoaded;
BOOL bRet = FALSE;
HMENU hMenu ;
HMENU hSubMenu;
switch (message)
{
// actually, i can not get this message . Why??????
case TBN_DROPDOWN:
hMenu = LoadMenu(hInst,(LPCTSTR)IDC_MENUBAR);
hSubMenu = GetSubMenu(hMenu,0);
TrackPopupMenu(hSubMenu,TPM_LEFTALIGN|TPM_RIGHTBUTTON,
10,10,0,hWnd,NULL);
break;
case WM_SIZE:
UpdateWindow(hWnd);
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
/*
response to menu item click event
*/
case ID_File:
hMenu = LoadMenu(hInst,(LPCTSTR)IDC_MENUBAR);
hSubMenu = GetSubMenu(hMenu,0);
TrackPopupMenu(hSubMenu,TPM_LEFTALIGN|TPM_RIGHTBUTTON,
10,10,0,hWnd,NULL);
break;
default:
break;
}
default:
return (DefWindowProc(hWnd, message, wParam, lParam));
}
return 0L;
}
/*
create menu bar
*/
BOOL CreateMenubar(HWND hWnd)
{
/*
step 1: create a child window as the base of menu
*/
RECT rect;
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = MenubarProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInst;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = //TOOLBARCLASSNAME;
"TBClass";
if (! RegisterClass (&wc))
return FALSE;
GetClientRect(hWnd, &rect);
hWndMB = CreateWindowEx(0,
//TOOLBARCLASSNAME,
"TBClass",
NULL,
WS_CHILD|WS_VISIBLE|WS_EX_STATICEDGE,
rect.left,rect.top,
rect.right-rect.left,
30,//23,
hWnd, NULL, hInst, NULL);
if (!hWndMB)
{
::MessageBox(NULL,"create fial","",0);
return FALSE;
}
ShowWindow(hWndMB, SW_SHOW);
/*
step 2: add buttons to the child window
*/
SendMessage(hWndMB,TB_SETIMAGELIST, 0, NULL);
INITCOMMONCONTROLSEX iccex;
iccex.dwSize = sizeof (INITCOMMONCONTROLSEX);
iccex.dwICC = ICC_BAR_CLASSES;
InitCommonControlsEx (&iccex);
//
char szBuf[10];//="File";
int iFile = -1;
HRESULT hr;
size_t cch;
LoadString(hInst, IDS_FILE, szBuf, 29);
hr = StringCchLength(szBuf, MAX_LEN, &cch);
if(SUCCEEDED(hr))
{
szBuf[cch + 2] = 0; //Double-null terminate.
}
else
{
MessageBox(NULL,"can not add string",NULL,0);
}
iFile = SendMessage(hWndMB, TB_ADDSTRING, 0, (LPARAM) (LPSTR)
szBuf);
TBBUTTON pMenuButton[2] ={
{I_IMAGENONE, ID_File, TBSTATE_ENABLED,
TBSTYLE_BUTTON/*|TBSTYLE_LIST|BTNS_DROPDOWN*/ , 0, iFile},
{I_IMAGENONE, ID_Edit, TBSTATE_ENABLED,
TBSTYLE_BUTTON/*|TBSTYLE_LIST|BTNS_DROPDOWN*/ , 0, iFile}
};
//BTNS_DROPDOWN
/*
create a tool bar that acts as a menu bar
*/
::CreateToolbarEx(hWndMB,
WS_CHILD|WS_VISIBLE,//|TBSTYLE_FLAT ,
ID_TOOLBAR,
0,
hInst,
NULL,
pMenuButton,
sizeof(pMenuButton)/sizeof(TBBUTTON),
30,
20,
0,
0,
sizeof(TBBUTTON));
return TRUE;
}
.
- Prev by Date: Re: Drawing in the NC area of a window
- Next by Date: Double Buffering a simple edit control
- Previous by thread: Disable "selection box" selection for Listview controls
- Next by thread: Re: Problem with IE style menu bar
- Index(es):
Loading