异常抛出0x0131EB06 Visual Studio

时间:2015-06-08 14:34:15

标签: c++ visual-studio direct2d

我是Directx的新手,我想测试一个在窗口中绘制圆圈的简单direct2d应用程序。我正在使用Visual Studio Professional。当我使用Local Windows Debugger运行名为D2D Test Engine的程序时,我会收到以下错误:

Exception thrown at 0x0140EB06 in D2D Test Engine.exe: 0xC0000005: Access violation reading location 0x00000008.
Exception thrown at 0x0140EB06 in D2D Test Engine.exe: 0xC0000005: Access violation reading location 0x00000008.
Exception thrown at 0x0140EB06 in D2D Test Engine.exe: 0xC0000005: Access violation reading location 0x00000008.

我无法弄清楚我的程序有什么问题。 这是我的代码:

#include <Windows.h>
#include "Graphics.h"
Graphics* graphics;
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
if (uMsg == WM_DESTROY) {
    PostQuitMessage(0);
    return 0;
}
graphics->BeginDraw();
graphics->ClearScreen(0.0f, 0.0f, 0.5f);
graphics->DrawShape(100, 100, 50, 1.0f, 0.0, 0.0, 1.0);
graphics->EndDraw();
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE prevInstance, LPWSTR cmd, int nCmdShow) {
WNDCLASSEX windowclass;
ZeroMemory(&windowclass, sizeof(WNDCLASSEX));
windowclass.cbSize = sizeof(WNDCLASSEX);
windowclass.hbrBackground = (HBRUSH) COLOR_WINDOW;
windowclass.hInstance = hInstance;
windowclass.lpfnWndProc = WindowProc;
windowclass.lpszClassName = "MainWindow";
windowclass.style = CS_HREDRAW | CS_VREDRAW;
RegisterClassEx(&windowclass);
RECT rect = { 0, 0, 800, 600 };
AdjustWindowRectEx(&rect, WS_EX_OVERLAPPEDWINDOW, false, WS_EX_OVERLAPPEDWINDOW);
HWND windowHandle = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, "MainWindow", "D2D Test Engine", WS_OVERLAPPEDWINDOW, 0, 0, rect.right - rect.left,      rect.bottom - rect.top, NULL, NULL, hInstance, 0);
    if (!windowHandle) {
        return -1;
    }
    graphics = new Graphics();
    if (graphics->Init(windowHandle) == false) {
        delete graphics;
        return -1;
    }
    ShowWindow(windowHandle, nCmdShow);
    MSG message;
    while (GetMessage(&message, NULL, 0, 0)) {
        DispatchMessage(&message);
    }
    return 0;
}

下一个文件:

#pragma once
#include <Windows.h>
#include <d2d1.h>
class Graphics
{
ID2D1Factory* factory;
ID2D1HwndRenderTarget* renderTarget;
public:
Graphics();
virtual ~Graphics();
bool Init(HWND windowHandle);
void BeginDraw() {renderTarget->BeginDraw();}
void EndDraw() {renderTarget->EndDraw();}
void ClearScreen(float r, float g, float b);
void DrawShape(float x, float y, float radius, float r, float g, float b, float a);
};

下一个文件:

#include "Graphics.h"
#include <d2d1.h>
#include <Windows.h>
Graphics::Graphics()
{
factory = NULL;
renderTarget = NULL;
}
Graphics::~Graphics()
{
if (factory) {
    factory->Release();
}
if (renderTarget) {
    renderTarget->Release();
}
}
bool Graphics::Init(HWND windowHandle) {
HRESULT res = D2D1CreateFactory(D2D1_FACTORY_TYPE_MULTI_THREADED, &factory);
if (res != S_OK) {
    return false;
}
RECT rect;
GetClientRect(windowHandle, &rect);
res = factory->CreateHwndRenderTarget(D2D1::RenderTargetProperties(), D2D1::HwndRenderTargetProperties(windowHandle, D2D1::SizeU(rect.right, rect.bottom)), &renderTarget);
if (res != S_OK) {
    return false;
}
return true;
}
void Graphics::ClearScreen(float r, float g, float b) {
renderTarget->Clear(D2D1::ColorF(r, g, b));
}
void Graphics::DrawShape(float x, float y, float radius, float r, float g, float b, float a) {
ID2D1SolidColorBrush* brush;

renderTarget->CreateSolidColorBrush(D2D1::ColorF(r, g, b, a), &brush);
renderTarget->DrawEllipse(D2D1::Ellipse(D2D1::Point2F(x, y), radius, radius), brush, 3.0f);
brush->Release();
}

当我没有调用BeginDraw(),ClearScreen,DrawShape和EndDraw时,会出现一个窗口,它运行正常,所以我认为它与它们有关。

2 个答案:

答案 0 :(得分:0)

好像,你有WM_CREATE消息的问题。 当CreateWindowEx调用时,将发送此消息。

Graphics* graphics = NULL; // it's better to zero pointer in C++.

简单解决方案 - 将图形设置为null并在WindowProc函数中检查它。

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    if (uMsg == WM_DESTROY) {
      PostQuitMessage(0);
      return 0;
   } 

   if (graphics) {
     graphics->BeginDraw();
     graphics->ClearScreen(0.0f, 0.0f, 0.5f);
     graphics->DrawShape(100, 100, 50, 1.0f, 0.0, 0.0, 1.0);
     graphics->EndDraw();
   }
   return DefWindowProc(hwnd, msg, wParam, lParam);
}

答案 1 :(得分:0)

我想出了答案。我对这个directx的东西很新,而且我正在使用一个教程来构建它。我需要做的是将我调用的所有函数放在if语句中。这就是我所做的:

if (uMsg == WM_PAINT) {
    graphics->BeginDraw();
    graphics->ClearScreen(0.0f, 0.0f, 0.5f);
    graphics->DrawShape(100, 100, 50, 1.0f, 0.0, 0.0, 1.0);
    graphics->EndDraw();
}
抱歉所有麻烦,我是新手。