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,右击--添加--添加变量。
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 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 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);
}
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);//图上是错误的。
相关文章推荐
- VS2013 MFC Windows 程序设计之窗口滚动条 他对,坚持,坚持,就对了。
- VS2013 MFC Windows 程序设计之消息框(里面没什么)
- MFC Windows 程序设计->WinMain 简单Windows程序 命令行编译
- MFC Windows 程序设计->GetDeviceCaps
- MFC Windows 程序设计 第四章 菜单
- Windows Embedded Compact 2013升级:VS2013也能编译
- MFC之HTTP POST请求 (VS2013环境 多字符编码)
- Windows程序设计与MFC先学哪个?
- VS2013 调试x64程序报"Windows Web服务框架错误"问题
- MFC Windows 程序设计 第二章 在窗口中绘图
- MFCWindows 程序设计 第一章 Hello MFC
- Windows 8.1 安装VS 2013失败
- MFC Windows 程序设计->取消对GDI对象的选定
- MFC Windows 程序设计->最顶层窗口
- MFC Windows 程序设计->实现客户区拖动
- MFC Windows 程序设计 第三章 鼠标与键盘
- MFC Windows 程序设计->hello world
- MFC Windows 程序设计->消息反射
- Windows程序设计__孙鑫C++Lesson3《MFC 程序框架的剖析》
- MFC Windows 程序设计 第六章 文件I/O与串行化