MFC --- GDI like the DC Miscellany, a ruler to draw an example

GDI is an interface provided by the Windows operating system, the purpose of each type and other hardware devices, such as printers, monitors and other interactive, just to meet these hardware windows

Interface provides, you can interact with the operating system

DC class is a subclass of the class of GDI, DC class can not be used directly derived CClientDC CWindowDC CPaintDC CMetaFileDC etc. from him,

Which CClientDC CPaintDC more commonly, they are used to in (a region other than the header row, the navigation bar) to draw images and text MFC window "user area" (yes, the text also "draw out")

Note that only for CPaintDC On_Paint () method body.

  • The following example is a ruler in the picture CClientDC
  • Definition screen .h file
#pragma once
#include <afxwin.h>
class MyApp :public CWinApp
{
    virtual BOOL InitInstance();
};
class MyWnd:public CFrameWnd
{
public:
    MyWnd();
protected:
    DECLARE_MESSAGE_MAP();
    afx_msg void OnPaint();
    afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
    ~MyWnd();
};
  • Define a class of entities
#include "MyWnd.h"

MyApp app;

BOOL MyApp::InitInstance()
{
    this->m_pMainWnd = new MyWnd;
    this->m_pMainWnd->ShowWindow(this->m_nCmdShow);
    this->m_pMainWnd->UpdateWindow();
    return TRUE;
}

BEGIN_MESSAGE_MAP(MyWnd,CFrameWnd)
    ON_WM_PAINT()
END_MESSAGE_MAP()

MyWnd::MyWnd()
{
    CRect rect;
    this->Create(NULL,TEXT("一次征途,就是一首歌"),WS_OVERLAPPEDWINDOW|WS_VSCROLL);
}


MyWnd::~ MyWnd () 
{ 
} 


void MyWnd :: the OnPaint () 
{ 
    CPaintDC PDC ( the this ); 
    pdc.TextOutW ( 100 , 100 , the TEXT ( " all memories are engraved in the heart " )); 
    pdc.SetMapMode (the MM_LOENGLISH); / / set inches as the unit of measurement equal to one inch to about 25.4 mm, which is the beginning of the following cycle from 25 
    pdc.SetTextAlign (TA_CENTER | TA_TOP); // numbers on the scale display, centered, aligned with the upper scale 
    pdc.SetBkMode (TRANSPARENT) ; // a transparent background, or drawn figures will cover a portion of the rectangular region of 
    
    the CBrush Brush (the RGB ( 1 16 , 124 , 24 )); 
    the CBrush * oldbrush = pdc.SelectObject (&brush); // good programming practice to save the defined brush, the sentence can not, but proposed to retain 
    pdc.Rectangle ( 100 , - 100 , 1300 , - 200 ); because the top is set MapMode coordinate system is so MM_LOENGLISH : ruler at the top of the client area (0,0), the position of the ruler corresponds Videos axle negative y axis, following movement of brushes, all the objects as a starting point 
    pdc.SelectObject (oldbrush); // let DC brush holding device 
    for ( int I = 125 ; I < 1300 ; I = + 25 ) 
    { 
        pdc.MoveTo (I, - 100 ); 
        pdc.LineTo (I, - 108 ); 
    } 

    for ( int I = 150 ; I < 1300 ; I = + 50) 
    { 
        Pdc.MoveTo (I, - 100 ); 
        pdc.LineTo (I, - 1 16 ); 
    } 

    for ( int I = 200 is ; I < 1300 ; I + = 100 ) 
    { 
        pdc.MoveTo (I, - 100 ); 
        PDC .MoveTo (i, - 124 ); 
        CString STR; 
        str.format (the TEXT ( " % D " ), i / 100 - . 1 ); // loop variable i and the scale algorithm correspondence relationship, you know 
        pdc.TextOutW ( I, - 124 , STR);

    }
}

operation result:

 

 

 Summary: The idea of ​​painting is roughly DC

  • Examples of a DC class example: CClientDC cdc (this); this means that the object is attached to a window DC
  • Examples of a CPen CBrush or objects, initialization of these classes has its own specific instantiation parameters
  • DC object method call selectObject "hold" brush or brush implemented, painting, such as to draw a rectangle
pdc.Rectangle(100,-100,1300,-200);
  • Some will declare a class variable CRect rect rectangular area, dc painting objects when taking the address of a variable, drawing text, images, etc.

Guess you like

Origin www.cnblogs.com/saintdingspage/p/12399660.html