您的位置:首页 > 其它

VS2013 MFC Windows 程序设计之Accel

2015-01-14 11:56 435 查看
1.新建一个项目Accel,修改属性。创建头文件Accel.h和源文件Accel.cpp。

2.



#include <afxwin.h>
#include "Accel.h"



class CMyWinApp : public CWinApp
{

};

class CMyFrameWnd : public CFrameWnd
{

};
添加InitInstance函数:



3.



this->m_pMainWnd = new CMyFrameWnd;
this->m_pMainWnd->ShowWindow(this->m_nCmdShow);
this->m_pMainWnd->UpdateWindow();

return TRUE;
构造函数的声明:



class CMyFrameWnd : public CFrameWnd
{
public:
CMyFrameWnd();

};
构造函数的定义,写都源文件里



CMyWinApp app;
CMyFrameWnd::CMyFrameWnd()
{
this->Create(NULL, TEXT("Accel"), WS_OVERLAPPEDWINDOW);

}
4.单元格的高度、宽度和视图:
在类视图中,双击CMyFrameWnd,右击--添加--添加变量。







protected:
int m_nCellWidth;
int m_nCellHeight;
int m_nRibbonWidth;
int m_nViewWidth;//最大的显示。
int m_nViewHeight;
6.我们要初始化上面的数据,初始化数据在创建窗口的时候。





CClientDC dc(this);
m_nCellWidth = dc.GetDeviceCaps(LOGPIXELSX);//运用dc的方法。
m_nCellHeight = dc.GetDeviceCaps(LOGPIXELSX) / 4;
m_nRibbonWidth = m_nCellWidth / 2;
m_nViewWidth = (26 * m_nCellWidth) + m_nRibbonWidth;
m_nViewHeight = m_nCellHeight * 100;7.添加OnPaint:










CPen pen(PS_SOLID, 0, RGB(192, 192, 192));//宽度是0。
CPen *pOldPen = dc.SelectObject(&pen);

for (int i = 0; i < 99; i++){
int y = (i * m_nCellHeight) + m_nCellHeight;
dc.MoveTo(0, y);
dc.LineTo(m_nViewWidth, y);
}
//上面是横线,我们在画上竖线。

for (int j = 0; j < 26; j++){
int x = (j*m_nCellWidth) + m_nRibbonWidth;
dc.MoveTo(x, 0);
dc.LineTo(x, m_nViewHeight);
}

dc.SelectObject(&pOldPen);

//接下来,画行头。
CBrush brush;
brush.CreateStockObject(LTGRAY_BRUSH);

CRect rcTop(0, 0, m_nViewWidth, m_nCellHeight);
dc.FillRect(rcTop, &brush);
CRect rcLeft(0, 0, m_nRibbonWidth, m_nViewHeight);
dc.FillRect(rcLeft, &brush);

dc.MoveTo(0, m_nCellHeight);
dc.LineTo(m_nViewWidth, m_nCellHeight);

dc.MoveTo(m_nRibbonWidth, 0);
dc.LineTo(m_nRibbonWidth, m_nViewHeight);

dc.SetBkMode(TRANSPARENT);

for (int i = 0; i < 99; i++)
{
int y = (i * m_nCellHeight) + m_nCellHeight;
dc.MoveTo(0, y);
dc.LineTo(m_nRibbonWidth, y);

CString string;
string.Format(TEXT("%d"), i + 1);

CRect rect(0, y, m_nRibbonWidth, y + m_nCellHeight);
dc.DrawText(string, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);

rect.top++;
dc.Draw3dRect(rect, RGB(255, 255, 255), RGB(128, 128, 128));
}

for (int j = 0; j < 26; j++){
int x = (j* m_nCellWidth) + m_nRibbonWidth;
dc.MoveTo(x, 0);
dc.LineTo(x, m_nCellHeight);

CString string;
string.Format(TEXT("%c"), j + 'A');

CRect rect(x, 0, x + m_nCellWidth, m_nCellHeight);
dc.DrawText(string, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);

rect.left++;
dc.Draw3dRect(rect, RGB(255, 255, 255), RGB(128, 128, 128));
} CPen pen(PS_SOLID, 0, RGB(192, 192, 192));//宽度是0。
CPen *pOldPen = dc.SelectObject(&pen);

for (int i = 0; i < 99; i++){
int y = (i * m_nCellHeight) + m_nCellHeight;
dc.MoveTo(0, y);
dc.LineTo(m_nViewWidth, y);
}
//上面是横线,我们在画上竖线。

for (int j = 0; j < 26; j++){
int x = (j*m_nCellWidth) + m_nRibbonWidth;
dc.MoveTo(x, 0);
dc.LineTo(x, m_nViewHeight);
}

dc.SelectObject(&pOldPen);

//接下来,画行头。
CBrush brush;
brush.CreateStockObject(LTGRAY_BRUSH);

CRect rcTop(0, 0, m_nViewWidth, m_nCellHeight);
dc.FillRect(rcTop, &brush);
CRect rcLeft(0, 0, m_nRibbonWidth, m_nViewHeight);
dc.FillRect(rcLeft, &brush);

dc.MoveTo(0, m_nCellHeight);
dc.LineTo(m_nViewWidth, m_nCellHeight);

dc.MoveTo(m_nRibbonWidth, 0);
dc.LineTo(m_nRibbonWidth, m_nViewHeight);

dc.SetBkMode(TRANSPARENT);

for (int i = 0; i < 99; i++)
{
int y = (i * m_nCellHeight) + m_nCellHeight;
dc.MoveTo(0, y);
dc.LineTo(m_nRibbonWidth, y);

CString string;
string.Format(TEXT("%d"), i + 1);

CRect rect(0, y, m_nRibbonWidth, y + m_nCellHeight);
dc.DrawText(string, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);

rect.top++;
dc.Draw3dRect(rect, RGB(255, 255, 255), RGB(128, 128, 128));
}

for (int j = 0; j < 26; j++){
int x = (j* m_nCellWidth) + m_nRibbonWidth;
dc.MoveTo(x, 0);
dc.LineTo(x, m_nCellHeight);

CString string;
string.Format(TEXT("%c"), j + 'A');

CRect rect(x, 0, x + m_nCellWidth, m_nCellHeight);
dc.DrawText(string, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);

rect.left++;
dc.Draw3dRect(rect, RGB(255, 255, 255), RGB(128, 128, 128));
}8.下面是创建滚动条:







滚动条有不同的通知码,下面是编写各种通知码:



int m_nHScrollPos;//保存水平滚动条的位置。
int m_nVScrollPos;//保存垂直滚动条的位置。



//在创建窗口的时候,指定滚动条的位置。
m_nHScrollPos = m_nVScrollPos = 0;

int nDelta;

switch (nSBCode)
{
case SB_LINEDOWN:
nDelta = m_nCellHeight;
break;
}

m_nVScrollPos += nDelta;

SetScrollPos(SB_VERT, m_nVScrollPos, TRUE);


#define LINESIZE 8

int nDelta;

switch (nSBCode)
{
case SB_LINEUP:
nDelta = -LINESIZE;
break;
case SB_LINEDOWN:
nDelta = LINESIZE;
break;


int m_nHPageSize;
int m_nVPageSize;




int nHScrollMax = 0;//水平可以滚动的最大距离。
m_nHPageSize = 0;

if (cx < m_nViewWidth)
{
m_nHPageSize = cx;
nHScrollMax = m_nViewWidth - 1;

}

SCROLLINFO si;
si.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
si.nMin = 0;
si.nMax = nHScrollMax;
si.nPos = m_nHScrollPos;
si.nPage = m_nHPageSize;

SetScrollInfo(SB_HORZ, &si, TRUE);

int nVScrollMax = 0;
m_nVPageSize = 0;

if (cy < m_nViewHeight)
{
nVScrollMax = m_nViewHeight - 1;
m_nVPageSize = cy;

}

si.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
si.nMin = 0;
si.nMax = nVScrollMax;
si.nPos = m_nVScrollPos;
si.nPage = m_nVPageSize;

SetScrollInfo(SB_VERT,&si, TRUE);

效果图:





<span style="white-space:pre">	</span>case SB_THUMBTRACK:
<span style="white-space:pre">		</span>nDelta = (int)nPos - m_nVScrollPos;
<span style="white-space:pre">		</span>break;
    case SB_PAGEUP:
<span style="white-space:pre">		</span>nDelta = -m_nVPageSize;
<span style="white-space:pre">	</span>    break;
    case SB_PAGEDOWN:
<span style="white-space:pre">		</span>nDelta = m_nVPageSize;
<span style="white-space:pre">	</span>    break;
<span style="white-space:pre">	</span>default:
<span style="white-space:pre">		</span>return;
下面写水平滚动条:


int nDelta;
switch (nSBCode)
{
case SB_LINELEFT:
nDelta = -LINESIZE;
break;
case SB_LINERIGHT:
nDelta = LINESIZE;
break;
case SB_PAGELEFT:
nDelta = -m_nHPageSize;
break;
case SB_PAGERIGHT:
nDelta = m_nHPageSize;
break;
case SB_THUMBTRACK:
nDelta = (int)nPos - m_nHScrollPos;
break;
default:
break;
}
if (nDelta != 0){
m_nHScrollPos += nDelta;
SetScrollPos(SB_HORZ, m_nHScrollPos, TRUE);
}


ScrollWindow(0, -nDelta);



<span style="white-space:pre">	</span>dc.SetWindowOrg(m_nHScrollPos, m_nVScrollPos);//图上是错误的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: