1. 程式人生 > >win32 實現右鍵選單

win32 實現右鍵選單

contextMenu.h

#ifndef _CONTEXTMENU_H_
#define _CONTEXTMENU_H_
#include <windows.h>

#define ITEM_MENU_FULLSCREEN	40001
#define ITEM_MENU_DOLLY			40002

class ContextMenu
{
public:
	ContextMenu();
	~ContextMenu();

	void PopMenu(HWND hwnd, int nX, int nY);
	void PopMenu(HWND hwnd, const POINT& pt);
	void EnableMenu(int nItem, bool nEnable);
private:
	HMENU m_pPopMenu;
};
#endif

contextMenu.cpp

#include "contextMenu.h"

ContextMenu::ContextMenu()
{
	m_pPopMenu = CreatePopupMenu();
	if (m_pPopMenu)
	{
		::InsertMenu(m_pPopMenu, (-1), MF_BYPOSITION, ITEM_MENU_FULLSCREEN, TEXT("全屏"));
		::InsertMenu(m_pPopMenu, (-1), MF_BYPOSITION, ITEM_MENU_DOLLY, TEXT("平移"));
		HBITMAP Hbitmap = NULL;
		Hbitmap = (HBITMAP)::LoadImage(NULL,"menu1.bmp",IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION|LR_LOADFROMFILE);
		ModifyMenu(m_pPopMenu, 0, MF_BYPOSITION | MF_BITMAP, ITEM_MENU_FULLSCREEN, (LPCSTR)Hbitmap);
		ModifyMenu(m_pPopMenu, 0, MF_BYPOSITION , ITEM_MENU_FULLSCREEN, TEXT("全屏"));

	}
}

ContextMenu::~ContextMenu()
{
	if (m_pPopMenu)
	{
		DestroyMenu(m_pPopMenu);
		m_pPopMenu = NULL;
	}
}

void ContextMenu::PopMenu(HWND hwnd, int nX, int nY)
{
	if (m_pPopMenu && hwnd)
	{
		TrackPopupMenu(m_pPopMenu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, nX, nY, 0, hwnd, NULL);
	}
}

void ContextMenu::PopMenu(HWND hwnd, const POINT& pt)
{
	PopMenu(hwnd, pt.x, pt.y);
}

main.cpp

#include <windows.h>
#include <stdio.h>
#include "contextMenu.h"

LRESULT CALLBACK WndSunProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	WNDCLASS wndclass;
	wndclass.cbClsExtra = 0;
	wndclass.cbWndExtra = 0;
	wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wndclass.hIcon = LoadIcon(NULL, IDI_ERROR);
	wndclass.hInstance = hInstance;
	wndclass.lpfnWndProc = WndSunProc;
	wndclass.lpszClassName = "liuzhichao2012";
	wndclass.lpszMenuName = NULL;
	wndclass.style = CS_HREDRAW | CS_VREDRAW;
	RegisterClass(&wndclass);

	HWND hwnd = CreateWindow(wndclass.lpszClassName, "liuzhcihao", WS_OVERLAPPEDWINDOW, 0, 0, 600, 400, NULL, NULL, hInstance, NULL);
	ShowWindow(hwnd, SW_SHOWNORMAL);
	UpdateWindow(hwnd);
	MSG msg;
	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return 0;
}

LRESULT CALLBACK WndSunProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
	case WM_CHAR:
		char sChar[20];
		sprintf_s(sChar, "char is %d", wParam);
		MessageBox(hwnd, sChar, "liu", 0);
		break;
	case WM_LBUTTONDOWN:
		MessageBox(hwnd, "mouse clicked", "liu", 0);
		HDC hdc;
		hdc = GetDC(hwnd);
		TextOut(hdc, 0, 50, "liuzhichaodasdfsadfsad", strlen("liuzhichaodasdfsadfsad"));
		ReleaseDC(hwnd, hdc);
		break;
	case WM_PAINT:
		HDC hDC;
		PAINTSTRUCT ps;
		hDC = BeginPaint(hwnd, &ps);
		TextOut(hDC, 0, 0, "維新培訓", strlen("維新培訓"));
		EndPaint(hwnd, &ps);
		break;
	case WM_CLOSE:
		if (IDYES == MessageBox(hwnd, "是否真的結束?", "weixin", MB_YESNO))
		{
			DestroyWindow(hwnd);
		}
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	case WM_RBUTTONUP:
		{
		POINT pt;
		::GetCursorPos(&pt);
		ContextMenu* pPopmenu = new ContextMenu();
		pPopmenu->PopMenu(hwnd, pt);
		delete pPopmenu;
		}
		break;
	case WM_COMMAND:
	{
		switch (LOWORD(wParam))
		{
		case ITEM_MENU_FULLSCREEN:
		{
			MessageBox(hwnd, "fullscreen", "info", 0);
		}
		break;
		}
	}
	default:
		return DefWindowProc(hwnd, uMsg, wParam, lParam);

	}
	return 0;
}