16,548
社区成员




//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#pragma once
#include "vector"
#include "PopMenuLine.h"
// PopMenu
class PopMenu : public CWnd
{
DECLARE_DYNAMIC(PopMenu)
public:
PopMenu();
virtual ~PopMenu();
void SetPopMenuLine(PopMenuLine* pComLine);
void CreatePopMenu(HWND hParen);
std::vector<PopMenuLine*> m_aComLine;
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg void OnPaint();
private:
public:
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
};
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// PopMenu.cpp : 实现文件
//
#include "stdafx.h"
#include "PopMenu.h"
#include <..\src\mfc\afximpl.h>
// PopMenu
IMPLEMENT_DYNAMIC(PopMenu, CWnd)
PopMenu::PopMenu()
{
}
PopMenu::~PopMenu()
{
}
BEGIN_MESSAGE_MAP(PopMenu, CWnd)
ON_WM_SIZE()
ON_WM_PAINT()
ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()
void PopMenu::SetPopMenuLine( PopMenuLine* pComLine )
{
if(pComLine)
{
m_aComLine.push_back(pComLine);
}
}
void PopMenu::CreatePopMenu(HWND hParen)
{
CWnd* pWnd = FromHandle(hParen);
RECT rc = {0,0,144,255};
WNDCLASS wndclass;
wndclass.hInstance = AfxGetInstanceHandle();
wndclass.lpszClassName = _T("ey");
BOOL bReg = AfxRegisterClass(&wndclass);
//BOOL bRet = CreateEx(WS_EX_TOOLWINDOW | WS_EX_TOPMOST, _T("#32770"), _T("ey"), WS_POPUP | WS_VISIBLE, rc, pWnd, NULL, NULL);
BOOL bRet = Create(NULL, NULL, WS_CHILD | WS_VISIBLE, rc, pWnd, 123);
if(bRet == 0)
{
DWORD dwErrer = GetLastError();
ASSERT(0);
}
}
// PopMenu 消息处理程序
void PopMenu::OnSize(UINT nType, int cx, int cy)
{
CWnd::OnSize(nType, cx, cy);
if(GetSafeHwnd())
{
//MoveWindow()
}
}
void PopMenu::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
// Do not call CWnd::OnPaint() for painting messages
CRect rect;
GetClientRect(rect);
CBrush brush(RGB(22, 222, 222));
dc.FillRect(rect, &brush);
int nSize = m_aComLine.size();
for (int nL = 0; nL < nSize; ++nL)
{
PopMenuLine* pLine = m_aComLine[nL];
if (pLine)
{
pLine->DrawLine(&dc);
}
}
}
void PopMenu::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
Invalidate();
CWnd::OnMouseMove(nFlags, point);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#pragma once
class PopMenuLine
{
public:
PopMenuLine(void);
~PopMenuLine(void);
void DrawLine(CDC*pDC);
CString m_strText;
CRect m_rect;
};
/////~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include "StdAfx.h"
#include "PopMenuLine.h"
PopMenuLine::PopMenuLine(void)
{
}
PopMenuLine::~PopMenuLine(void)
{
}
void PopMenuLine::DrawLine( CDC*pDC )
{
CBrush brush(RGB(20, 200, 20));
pDC->FillRect(m_rect, &brush);
pDC->SetTextColor(RGB(22, 22, 22));
pDC->DrawText(m_strText, m_rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~/
//一下主窗体调用
PopMenu * pCombox = new PopMenu;
void CMyMenuDlg::OnBnClickedOk()//点击按键后,菜单出现
{
// TODO: 在此添加控件通知处理程序代码
if (pCombox == NULL)
{
return;
}
const int count = 8;
static CString strText[count] = { _T("ceshi"), _T(" 测试0"), _T("测试1"), _T("测试2"), _T("测试3"),_T("测试4"), _T("测试5"), _T("测试6") };
int nHeight = 30;
int nTotalHei = nHeight * pCombox->m_aComLine.size();
CRect rcDlg;
GetDlgItem(IDOK)->GetClientRect(rcDlg);
CRect rect ;
rect.left = rcDlg.left ;
rect.top = rcDlg.top ;
rect.right = rect.left + 100;
rect.bottom = rect.top + nHeight * count;
CRect rcLine = rect;
for (int nL = 0; nL < count; ++nL)
{
PopMenuLine * pLine = new PopMenuLine;
pLine->m_strText = strText[nL];
CRect rcTemp = rcLine;
rcTemp.top = rcTemp.top + nL * nHeight;
rcTemp.bottom = rcTemp.top + nHeight;
pLine->m_rect = rcTemp;
pCombox->SetPopMenuLine(pLine);
}
if (!pCombox->GetSafeHwnd())
{
pCombox->CreatePopMenu(m_hWnd);
}
// ClientToScreen(rect);
pCombox->MoveWindow(rect);
//pCombox->SetWindowPos(this, rect.left, rect.top, rect.Width(), rect.Height(), /*SWP_NOSIZE | */SWP_NOREDRAW /*| SWP_NOZORDER*/);
pCombox->ShowWindow(SW_SHOW);
pCombox->UpdateWindow();
}
BOOL CWnd::Create(LPCTSTR lpszClassName,
LPCTSTR lpszWindowName, DWORD dwStyle,
const RECT& rect,
CWnd* pParentWnd, UINT nID,
CCreateContext* pContext)
{
// can't use for desktop or pop-up windows (use CreateEx instead)
ASSERT(pParentWnd != NULL);
ASSERT((dwStyle & WS_POPUP) == 0);
return CreateEx(0, lpszClassName, lpszWindowName,
dwStyle | WS_CHILD,
rect.left, rect.top,
rect.right - rect.left, rect.bottom - rect.top,
pParentWnd->GetSafeHwnd(), (HMENU)nID, (LPVOID)pContext);
}
BOOL bRet = Create(NULL, NULL, WS_POPUP | WS_VISIBLE, rc, pWnd, 12300);
试试