08 GDI练习
通过画一下表格练习GDI使用,函数都很简单,看名字就能知道用途,不懂的百度一下;直接贴效果图如下:
代码如下:
/*
*HelloMFC.h
*/
#ifndef _HELLO_MFC_
#define _HELLO_MFC_
class CMyApp : public CWinApp{
public:
virtual BOOL InitInstance();
};
class CMainWindow : public CFrameWnd{
public:
CMainWindow();
afx_msg void OnPaint();
DECLARE_MESSAGE_MAP()
};
#endif
/*
*HelloMFC.cpp
*/
#include <afxwin.h>
#include "HelloMFC.h"
CMyApp myApp;
BOOL CMyApp::InitInstance()
{
m_pMainWnd = new CMainWindow;
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}
BEGIN_MESSAGE_MAP(CMainWindow, CFrameWnd)
ON_WM_PAINT()
END_MESSAGE_MAP()
CMainWindow::CMainWindow()
{
Create(NULL, TEXT("画个表格"));
}
void CMainWindow::OnPaint()
{
CPaintDC dc(this);
CBrush brush;
CString str;
CRect rect(0, 0, 50, 30);
brush.CreateStockObject(LTGRAY_BRUSH);
dc.SelectObject(&brush);
CRect rectTmp = rect;
dc.SetBkMode(TRANSPARENT);
for (int i = 0; i < 26; i++){
dc.FillRect(&rectTmp, &brush);
dc.Draw3dRect(&rectTmp, RGB(255, 255, 255), RGB(128, 128, 128));
if (i != 0){
str.Format(_T("%c"), 'A' + i - 1);
dc.DrawText(str, &rectTmp, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
}
dc.MoveTo(rectTmp.right, 0);
dc.LineTo(rectTmp.right, 30 * 20);
rectTmp.left = rectTmp.right;
rectTmp.right = rectTmp.right + 50;
}
rectTmp = rect;
for (int i = 0; i < 20; i++){
dc.FillRect(&rectTmp, &brush);
dc.Draw3dRect(&rectTmp, RGB(255, 255, 255), RGB(128, 128, 128));
if (i != 0){
str.Format(_T("%d"), i - 1);
dc.SetBkColor(RGB(128, 128, 128));
dc.DrawText(str, &rectTmp, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
}
dc.MoveTo(0, rectTmp.bottom);
dc.LineTo(50 * 26, rectTmp.bottom);
rectTmp.top = rectTmp.bottom;
rectTmp.bottom = rectTmp.bottom + 30;
}
}