MFC函数

  • 格式:docx
  • 大小:44.84 KB
  • 文档页数:14

1

MFC常用函数

(为整理完全,待续)

华东理工大学 张耀

2012年8月

2

1.消息结构体

typedef struct tagMSG {

HWND hwnd;

UINT message;

WPARAM wParam;

LPARAM lParam;

DWORD time;

POINT pt;

} MSG;

2. int WINAPI WinMain(

HINSTANCE hInstance, // handle to current instance

HINSTANCE hPrevInstance, // handle to previous instance

LPSTR lpCmdLine, // pointer to command line

int nCmdShow // show state of window

)

3. typedef struct _WNDCLASS {

UINT style; 窗口样式 CS_HREDRAW等CS_

WNDPROC lpfnWndProc;

int cbClsExtra;

int cbWndExtra;

HANDLE hInstance;

HICON hIcon;

HCURSOR hCursor;

HBRUSH hbrBackground;

LPCTSTR lpszMenuName;

LPCTSTR lpszClassName;

} WNDCLASS;

要是想要改变窗口样式可以自己定义一个窗口类并且注册;在PreCreateWindow()中将cs结构中的lpszClass改为自定义的窗口类名字.

4. HICON LoadIcon(

HINSTANCE hInstance, // handle to application instance

LPCTSTR lpIconName // icon-name string or icon resource

// identifier

);

加载系统图标时第一个参数为NULL,如果加载自定义图标则要调用全局函数AfxGetInstanceHandle()来获取当前应用程序实例;或者定义外部声明theApp.m_hInstance的当前应用程序实例;还有通过AfxGetApp()先获取theApp指针.

5. HCURSOR LoadCursor( 3

HINSTANCE hInstance, // handle to application instance

LPCTSTR lpCursorName // name string or cursor resource identifier

);

使用方法与LoadIcon是相同的.

6. CWnd::ShowWindow

BOOL ShowWindow( int nCmdShow );

相关参数 SW_NORMAL ; SW_HIDE

7. LRESULT CALLBACK WindowProc(

HWND hwnd, // handle to window

UINT uMsg, // message identifier

WPARAM wParam, // first message parameter

LPARAM lParam // second message parameter

);

可以通过SetWindowLong();修改当前的窗口过程实现对窗口性质的修改.

8. CButton::Create

BOOL Create( LPCTSTR lpszCaption, DWORD dwStyle, const RECT& rect, CWnd*

pParentWnd, UINT nID );

通过该函数可以创建一个按钮.创建后需要调用成员函数ShowWindow();或者在第二个

参数或上SW_VISIBLE.

9. CWnd::MessageBox

int MessageBox( LPCTSTR lpszText, LPCTSTR lpszCaption = NULL, UINT nType

= MB_OK );

int AfxMessageBox( LPCTSTR lpszText, UINT nType = MB_OK, UINT nIDHelp =

0 );

第二个为全局API函数.

10. HDC GetDC(

HWND hWnd // handle to a window

);

获取当前窗口的DC.调用是要加上::,使用完后要调用::ReleaseDC()释放DC.

11. BOOL MoveToEx(

HDC hdc, // handle to device context

int X, // x-coordinate of new current position

int Y, // y-coordinate of new current position

LPPOINT lpPoint // pointer to old current position);

) 4

把坐标原点移动到当前点.实际绘图比较少用.

12. BOOL LineTo(

HDC hdc, // device context handle

int nXEnd, // x-coordinate of line's ending point

int nYEnd // y-coordinate of line's ending point

);

从原点做线条到当前坐标.

13. CDC::MoveTo

CPoint MoveTo( int x, int y );

CPoint MoveTo( POINT point );

把坐标原点移动到当前点.实际绘图比较少用.

14. CDC::LineTo

BOOL LineTo( int x, int y );

BOOL LineTo( POINT point );

从原点做线条到当前坐标.注:该对象使用完成后也要调用CWnd::ReleaseDC();

lientDC由CDC继承而来,区别在于不要调用GetDC();和ReleaseDC(CDC*),但是实例化对象时需要提供窗口指针.可以使用this,GetParent()或者是GetDesktopWindow();

16 CPen::CPen

CPen( );

CPen( int nPenStyle, int nWidth, COLORREF crColor );

CPen( int nPenStyle, int nWidth, const LOGBRUSH* pLogBrush, int nStyleCount

= 0, const DWORD* lpStyle = NULL );//不常用

笔迹样式有PS_SOLID,PS_等.

17.COLORREF RGB(0~255,0~255,0~255);

18. CRect::CRect

CRect( );

CRect( int l, int t, int r, int b );

CRect( const RECT& srcRect );

CRect( LPCRECT lpSrcRect );

CRect( POINT point, SIZE size );

CRect( POINT topLeft, POINT bottomRight );

定义一个矩形区域.

5

19. CDC::FillRect

void FillRect( LPCRECT lpRect, CBrush* pBrush );

确定一个由画刷填充的矩形区域.

20. CBrush::CBrush

CBrush( );

CBrush( COLORREF crColor );

CBrush( int nIndex, COLORREF crColor );

CBrush( CBitmap* pBitmap );

创建各种形式的画刷.

21. CBitmap::LoadBitmap

BOOL LoadBitmap( LPCTSTR lpszResourceName );

BOOL LoadBitmap( UINT nIDResource );

创建和加载位图资源.

22. HGDIOBJ GetStockObject(

int fnObject // type of stock object

);

GetStockObject函数检索的一个句柄,一个预定义的股票笔、刷子、字体、或调色板。返回句柄.

23. CBrush::FromHandle

static CBrush* PASCAL FromHandle( HBRUSH hBrush );

将句柄转化为一个画刷指针.

24. CDC::SelectObject

CPen* SelectObject( CPen* pPen );

CBrush* SelectObject( CBrush* pBrush );

virtual CFont* SelectObject( CFont* pFont );

CBitmap* SelectObject( CBitmap* pBitmap );

int SelectObject( CRgn* pRgn );

将创建的图像信息选入设备描述表,返回值为旧的信息.

25. CDC::SetROP2

int SetROP2( int nDrawMode );

通过模式的传参实现像素颜色的改变.

26. CWnd::CreateSolidCaret

void CreateSolidCaret( int nWidth, int nHeight );

创建光标.创建后要调用ShowCaret();对其进行显示.

CWnd::CreateCaret

void CreateCaret( CBitmap* pBitmap );用来创建位图光标.