MFC里怎么样去掉工具栏?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MFC里怎么样去掉工具栏?相关的知识,希望对你有一定的参考价值。

作的一个程序,不需要用到工具栏,想把它去掉,但是在MainFrame中把定义工具栏的语句删掉则无法运行,希望高手解答,最好详细点

参考技术A 把这个函数改成这样:

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)

if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
/*
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))

TRACE0("Failed to create toolbar\n");
return -1; // fail to create

*/
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))

TRACE0("Failed to create status bar\n");
return -1; // fail to create


// TODO: Delete these three lines if you don't want the toolbar to
// be dockable
/*
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
*/
return 0;
参考技术B 创建框架的时候取消掉工具栏的选项就行了

MFC小程序

1.将菜单栏归零,工具栏放在窗口低部,加载自己新建的工具栏

CMainFrame::OnCreate()函数中 this->SetMenu(0);

2.将窗口初始化为最大化

APP类中:m_pMainWnd->ShowWindow(SW_MAXIMIZE);

 3.去掉边框

CMainFrame::PreCreateWindow()函数中 cs.style = WS_POPUP;
4.在FRAME类中添加工具栏命令消息,关闭    

通过ON_COMMAND(ToolBar下的图片ID,实现的函数)  this->PostMessage(WM_CLOSE);

5.在VIEW类中添加系统消息keydown

void CDRAWView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)

{

  if(nChar == VK_ESCAPE)

  {

    AfxGetMainWnd()->PostMessage(WM_CLOSE);

  }  

  CView::OnKeyDown(nChar, nRepCnt, nFlags);

}

6.将桌面背景贴到窗口上
在Frame.h中添加成员变量和成员函数

class CMainFrame : public CFrameWnd
{
    public:
    int m_ScreenX;
    int m_ScreenY;
    stack<CBitmap*> stk;//保存图片,实现撤销操作
}

Frame.cpp的构造函数和析构函数

CMainFrame::CMainFrame()
{
    m_ScreenX = GetSystemMetrics(SM_CXSCREEN);//屏幕长(调用函数为获取屏幕分辨率)
    m_ScreenY = GetSystemMetrics(SM_CYSCREEN);//屏幕宽
    CWindowDC dc(GetDesktopWindow());//获取桌面屏幕
    CDC cdc;
    cdc.CreateCompatibleDC(&dc);//兼容性DC相当于没有画纸的DC
    CBitmap* bitmap = new CBitmap;
    bitmap->CreateCompatibleBitmap(&dc,m_ScreenX,m_ScreenY);//创建一个兼容性位图,第一个参数
    cdc.SelectObject(bitmap);//将画纸放在DC上(只有兼容性DC才可以放画纸)
    cdc.BitBlt(0,0,m_ScreenX,m_ScreenY,&dc,0,0,SRCCOPY);
    stk.push(bitmap);
}

CMainFrame::~CMainFrame()
{
    if(stk.empty() == false)
    {
        delete(stk.top());
        stk.pop();
    }
}

 VIEW.cpp

void CDRAWView::OnDraw(CDC* /*pDC*/)
{
    CDRAWDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
        return;

    CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();//父类指针强转为子类,才可使用子类特有变量
    CClientDC dc(this);
    CDC cdc;
    cdc.CreateCompatibleDC(&dc);
    cdc.SelectObject(pFrame->stk.top());
    dc.BitBlt(0,0,pFrame->m_ScreenX,pFrame->m_ScreenY,&cdc,0,0,SRCCOPY);
    
}

7.添加工具栏

在资源视图中Toolbar下自行绘制,更改对应ID;

在resource.h中将宏改为连续数字

ON_COMMAND_RANGE(ID_PEN,ID_ELLIPSE,&CDRAWView::ONChangeDrawStyle)//连续宏可使用

声明枚举类型    enum{PEN,LINE,RECTANGLE,TRIANGLE,ELLIPSE};(铅笔,直线,长方形,三角形,椭圆形)与resource.h中的顺序对应

在View.h中
class CDRAWView : public CView
{
    public:
    int m_DrawStyle;
    void ONChangeDrawStyle(UINT uid);
    bool m_blsFlag;
    CPoint PointMouseDown;
};

在View.cpp中

void CDRAWView::ONChangeDrawStyle(UINT uid)
{
    m_DrawStyle = uid - ID_PEN;
}
CDRAWView::CDRAWView()
{
    m_DrawStyle = ID_PEN;
    m_blsFlag = false;
}
void CDRAWView::OnLButtonDown(UINT nFlags, CPoint point)
{
    m_blsFlag = true;
    PointMouseDown = point;
    CView::OnLButtonDown(nFlags, point);
}


void CDRAWView::OnLButtonUp(UINT nFlags, CPoint point)
{
    m_blsFlag = false;

    CView::OnLButtonUp(nFlags, point);
}


void CDRAWView::OnMouseMove(UINT nFlags, CPoint point)
{
    CClientDC dc(this);
    CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
    if(m_blsFlag)
    {
        if(m_DrawStyle == PEN)
        {
            dc.MoveTo(PointMouseDown.x,PointMouseDown.y);
            dc.LineTo(point.x,point.y);
            PointMouseDown = point;
            return ;
        }
        else
        {
            //======================================解决闪屏========================================
            CDC MemDC;
            MemDC.CreateCompatibleDC(&dc);
            CBitmap* bitmap = new CBitmap;
            bitmap->CreateCompatibleBitmap(&dc,pFrame->m_ScreenX,pFrame->m_ScreenY);
            MemDC.SelectObject(bitmap);
            //-------------------------------------栈顶图片贴图擦除痕迹------------------------------------------------
            CDC cdc;
            cdc.CreateCompatibleDC(&dc);
            cdc.SelectObject(pFrame->stk.top());
            MemDC.BitBlt(0,0,pFrame->m_ScreenX,pFrame->m_ScreenY,&cdc,0,0,SRCCOPY);
       //-------------------------------------------------------------------------------------------------------
switch(m_DrawStyle) { case LINE: MemDC.MoveTo(PointMouseDown.x,PointMouseDown.y); MemDC.LineTo(point.x,point.y); break; case TRIANGLE: { POINT points[3] = {{(PointMouseDown.x+point.x)/2,PointMouseDown.y},{PointMouseDown.x,point.y},{point.x,point.y}}; MemDC.Polygon(points,3); } break; case ELLIPSE: MemDC.Ellipse(PointMouseDown.x,PointMouseDown.y,point.x,point.y); break; case RECTANGLE: MemDC.Rectangle(PointMouseDown.x,PointMouseDown.y,point.x,point.y); break; } dc.BitBlt(0,0,pFrame->m_ScreenX,pFrame->m_ScreenY,&MemDC,0,0,SRCCOPY); //=================================================================================================== } } CView::OnMouseMove(nFlags, point); }

 

以上是关于MFC里怎么样去掉工具栏?的主要内容,如果未能解决你的问题,请参考以下文章

为啥我创建的MFC工具栏按钮都是灰的

新建一个MFC单文档程序含有菜单和工具栏,又建了一个对话框,如何直接显示对话框?

SQL server字符串字段怎么存储换行符?

怎么把照片上的字去掉?

MFC单文档程序的菜单栏怎么修改高度和宽度?

MFC小程序