MFC小程序

Posted Lune-Qiu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 CFileDialog窗口问题

Android获取各个应用程序的缓存文件代码小片段(使用AIDL)

MFC 小知识总结五

提效小技巧——记录那些不常用的代码片段

Android小部件,启动一个片段?