MFC绘图的问题
我运行的程序怎么会运行一段时间后,显示的画面就开始混乱了,而且我电脑的桌面主题会自己消失,一停止调试就好了 MFC绘图 桌面主题 调试
[解决办法]
你程序里面有设置主题的相关代码,或者你用了第三方界面库了
[解决办法]
目测应该不断的占用设备资源,而且没有释放
[解决办法]
void CMyGameDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
CBrush m_brushBackground;
CBitmap bmp;
bmp.LoadBitmap(IDB_BITMAP2); ///加载位图
m_brushBackground.CreatePatternBrush(&bmp); ///创建位图画刷
CRect rect1;
GetClientRect(rect1);///取得客户区域
GetDC()-> FillRect(rect1,&m_brushBackground);
DeleteObject(&m_brushBackground);
PAINTSTRUCT ps;
CRect rt;
m_pDrawWnd->GetClientRect(&rt);
CDC* pDC=m_pDrawWnd->BeginPaint(&ps);
DrawOnMem();
pDC->BitBlt(0, 0, rt.Width(), rt.Height(), &m_memDC, 0, 0, SRCCOPY);
if(m_bGameStart)
{
int i,j,x,y,nW,nH;
for (i=0;i<22;i++)
for (j=0;j<18;j++)
{
if (m_iGameStatus[i][j]==1)
{
//在游戏区域中状态为被占用状态的区域绘制小方块
x = j*30 +2;
y = i*30 +2;
nW = 30-3;
nH = 30-3;
CBitmap bitmap;
bitmap.LoadBitmap(IDB_BITMAP1);
CDC m_memRectDC;
m_memRectDC.CreateCompatibleDC(pDC);
m_memRectDC.SelectObject(&bitmap);
pDC->BitBlt(x,y,nW,nH,&m_memRectDC,0,0,SRCCOPY);
DeleteObject(&bitmap);
}
}
}
m_pDrawWnd->EndPaint(&ps);
}
::DeleteObject(m_Bmp.m_hObject);
}
HBRUSH CMyGameDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if(pWnd->GetDlgCtrlID()==IDC_STATIC
[解决办法]
pWnd->GetDlgCtrlID()==IDCANCEL)
{
pDC->SetTextColor(RGB(255,0,0));//设置文本颜色
pDC->SetBkMode(TRANSPARENT);//文字背景设为透明
pDC->SetBkColor(RGB(255,200,200));//设置背景色
return m_brush;
}
// TODO: Change any attributes of the DC here
// TODO: Return a different brush if the default is not desired
return hbr;
}
// 初始化和双缓冲相关的要素
void CMyGameDlg::InitialDBB()
{
CRect rt;
m_pDrawWnd->GetClientRect(&rt);
CDC* sDC = m_pDrawWnd->GetDC();
// 为屏幕DC创建兼容的内存DC
if(!m_memDC.CreateCompatibleDC(sDC))//
{
::PostQuitMessage(0);
}
// 创建位图,不能是m_memDC,否则无颜色
m_Bmp.CreateCompatibleBitmap(sDC, rt.Width(), rt.Height());//
// 相当于选择画布,m_pDrawWnd->
::SelectObject(m_memDC.GetSafeHdc(), m_Bmp);
m_pDrawWnd->ReleaseDC(sDC);
}
//双缓冲内存上绘图
void CMyGameDlg::DrawOnMem()
{
CRect rect;
m_pDrawWnd->GetClientRect(&rect);
// COLORREF crl = GetSysColor(COLOR_3DFACE);
// m_memDC.FillSolidRect(rect, crl);
m_memDC.FillSolidRect(&rect, 0x209050);// 填充底色
CPen pen(PS_SOLID,1,RGB(255,0,0));
m_memDC.SelectObject(&pen);
//画图部分
int i,j;
for( i=1;i<18;i++)
{
m_memDC.MoveTo(30*i,0);
m_memDC.LineTo(30*i,rect.bottom);
}
for( j=1;j<22;j++)
{
m_memDC.MoveTo(0,30*j);
m_memDC.LineTo(rect.right,30*j);
}
DeleteObject(&pen);
}
BOOL CMyGameDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
imagelist.Create(32,32,ILC_COLOR32
[解决办法]
ILC_MASK,0,0);
imagelist.Add(::LoadIcon(::AfxGetResourceHandle(),MAKEINTRESOURCE(IDI_ICON2)));
imagelist.Add(::LoadIcon(::AfxGetResourceHandle(),MAKEINTRESOURCE(IDI_ICON3)));
imagelist.Add(::LoadIcon(::AfxGetResourceHandle(),MAKEINTRESOURCE(IDI_ICON1)));
imagelist.Add(::LoadIcon(::AfxGetResourceHandle(),MAKEINTRESOURCE(IDI_ICON4)));
imagelist.Add(::LoadIcon(::AfxGetResourceHandle(),MAKEINTRESOURCE(IDI_ICON5)));
//imagelist.Add(::LoadIcon(::AfxGetResourceHandle(),MAKEINTRESOURCE(IDI_ICON6)));
CString s;\
TCHAR *pString;
menu.LoadMenu(IDR_MENU1);
toolbar.EnableAutomation();
toolbar.Create(WS_CHILD
[解决办法]
WS_VISIBLE,CRect(0,0,0,0),this,ID_TOOLBAR);
toolbar.SetImageList(&imagelist);
int i;
for(i=0;i<6;i++)
{
button[i].dwData=0;
button[i].fsState=TBSTATE_ENABLED;
button[i].fsStyle=TBSTYLE_BUTTON;
}
button[0].idCommand=ID_START;
button[0].iBitmap=0;
button[1].idCommand=ID_SUSPEND;
button[1].iBitmap=1;
button[2].idCommand=ID_STOP;
button[2].iBitmap=2;
button[3].idCommand=ID_SETUP;
button[3].iBitmap=3;
button[4].idCommand=ID_PAIMING;
button[4].iBitmap=4;
//button[6].idCommand=ID_EXIT;
//button[6].iBitmap=5;
for(i=0;i<6;i++)
{
s.LoadString(i+IDS_1);
int nStringLength= s.GetLength() + 1;
pString = s.GetBufferSetLength(nStringLength);
button[i].iString =toolbar.AddStrings(pString);
s.ReleaseBuffer();
}
toolbar.AddButtons(5,button);
toolbar.AutoSize();
toolbar.SetStyle(TBSTYLE_FLAT
[解决办法]
CCS_TOP);
m_brush.CreateSolidBrush(RGB(255,0,0));//初始化画刷
m_pDrawWnd= GetDlgItem(IDC_STATIC);//获得静态窗口对象指针
InitialDBB();// 初始化双缓冲相关的数据
return TRUE; // return TRUE unless you set the focus to a control
}
果然
你在OnPaint里怎么还调用CDialog::OnPaint();
而且还GetDC和PAINTSTRUCT混用
这也太乱了
我能加您QQ吗,刚开始学,有些问题不是不懂,我的QQ是603240637,我尽可能不耽误您时间,麻烦您了,谢谢!
for (j=0;j<18;j++)
{
if (m_iGameStatus[i][j]==1)
{
//在游戏区域中状态为被占用状态的区域绘制小方块
x = j*30 +2;
y = i*30 +2;
nW = 30-3;
nH = 30-3;
CBitmap bitmap;
bitmap.LoadBitmap(IDB_BITMAP1);
CDC m_memRectDC;
m_memRectDC.CreateCompatibleDC(pDC);
m_memRectDC.SelectObject(&bitmap);
pDC->BitBlt(x,y,nW,nH,&m_memRectDC,0,0,SRCCOPY);
DeleteObject(&bitmap);
}
}