C++ 在实现dwrite时,在c++;导致空PTR错误
我一直从以下来源为我的项目提供参考: 我的目标很简单: 按照下面给出的链接中的指导,以某种方式获得链接最终获得的输出。如果我至少能得到中间的结果,我会很高兴的 然而,不幸的是,我的项目似乎总是在编译时出现nullptr错误,到目前为止,这些都是我的发现。我在一个专业领域工作,我只能找到以下几点:C++ 在实现dwrite时,在c++;导致空PTR错误,c++,visual-studio,fonts,bitmap,directwrite,C++,Visual Studio,Fonts,Bitmap,Directwrite,我一直从以下来源为我的项目提供参考: 我的目标很简单: 按照下面给出的链接中的指导,以某种方式获得链接最终获得的输出。如果我至少能得到中间的结果,我会很高兴的 然而,不幸的是,我的项目似乎总是在编译时出现nullptr错误,到目前为止,这些都是我的发现。我在一个专业领域工作,我只能找到以下几点: 我不确定是否根据链接中给出的内容创建了SimpleText类 我可能没有将窗口处理程序正确链接到简单文本类对象 这是我的代码(看起来很大,但它只是类定义+基本窗口创建,老实说,没有别的) //cvvv
//cvvvv.cpp:定义应用程序的入口点。
//
#包括“framework.h”
#包括“cvvvv.h”
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
#定义最大加载字符串100
//全局变量:
HINSTANCE hInst;//当前实例
WCHAR szTitle[MAX_LOADSTRING];//标题栏文本
WCHAR szWindowClass[最大加载字符串];//主窗口类名称
//转发此代码模块中包含的函数声明:
ATOM MyRegisterClass(HINSTANCE HINSTANCE);
BOOL InitInstance(HINSTANCE,int);
LRESULT回调WndProc(HWND、UINT、WPARAM、LPARAM);
INT_PTR回调关于(HWND、UINT、WPARAM、LPARAM);
// *********************************************************************************
//HRESULT HasCharacter(
//UINT32将贬值,
//布尔*存在
//);
长常数uniLast=1114111;
//UINT32码点[uniLast];
//UINT32 codepointscont=uniLast;
//UINT16指数[uniLast];
朗朗赛拉莫尔;
// https://docs.microsoft.com/en-us/windows/win32/directwrite/getting-started-with-directwrite //
/////第1部分:声明DirectWrite和Direct2D资源/////
// 1. 在类头文件(SimpleText.h)中,将指向IDWriteFactory和IDWriteTextFormat接口的指针声明为私有成员。
//IDWriteFactory*pDWriteFactory\ux;
//IDWriteTextFormat*pTextFormat\;
// 3. 声明指向ID2D1Factory、ID2D1HwndRenderTarget和ID2D1SolidColorBrush接口的指针,用于使用Direct2D呈现文本。
//ID2D1Factory*PD2Factory;
//ID2D1HwndRenderTarget*pRT;
//ID2D1SOLIDCORBRUSH*pBlackBrush;
/////第2部分:创建独立于设备的资源/////
模板无效安全释放(T**ppT)
{
如果(*ppT)
{
(*ppT)->发布();
*ppT=NULL;
}
}
类SimpleText{
私人:
//2.声明成员以保存要呈现的文本字符串和字符串的长度。
IDWriteFactory*pDWriteFactory\ux;
IDWriteTextFormat*pTextFormat\;
常量wchar_t*wszText;
UINT32 cTextLength;
ID2D1Factory*PD2Factory;
ID2D1HwndRenderTarget*pRT;
ID2D1SOLIDCORBRUSH*pBlackBrush;
HRESULT-hr;
RECT-rc;
HWND HWND_uux;
浮动dpiScaleX_uuuuuuuuuuuuuuuuuuuuuuux,dpiScaleY_uuuuuuuux;
公众:
SimpleText(){
hr=CreateDeviceResources();
如果(成功(hr))
{
pRT_->BeginDraw();
pRT->SetTransform(D2D1::IdentityMatrix());
清除(D2D1::彩色F(D2D1::彩色F::白色));
//调用此类的DrawText方法。
hr=DrawText();
如果(成功(hr))
{
hr=pRT->EndDraw(
);
}
}
如果(失败(小时))
{
丢弃设备源();
}
}
void CreateDeviceIndependentResources(){
hr=D2D1CreateFactory(
D2D1工厂型单螺纹,
&PD2D;
如果(成功(hr))
{
hr=DWriteCreateFactory(
数据写入工厂类型共享,
__uuidof(IDWriteFactory),
重新解释强制转换(&pDWriteFactory)
);
}
wszText_uL=Hello World using DirectWrite;
cTextLength=(UINT32)wcslen(wszText);
如果(成功(hr))
{
hr=pDWriteFactory->CreateTextFormat(
L“Gabriola”,//字体姓氏。
NULL,//字体集合(NULL将其设置为使用系统字体集合)。
D书写字体重量规则,
DWRITE\u FONT\u STYLE\u NORMAL,
DWRITE\u FONT\u STRETCH\u NORMAL,
72.0f,
L“恩我们”,
&pTextFormat_
);
}
//将文本居中对齐(水平)。
如果(成功(hr))
{
hr=pTextFormat->SetTextAlignment(DWRITE\u TEXT\u ALIGNMENT\u CENTER);
}
如果(成功(hr))
{
hr=pTextFormat->SetParagraphAlignment(数据写入、段落对齐、居中对齐);
}
}
HRESULT CreateDeviceResources(){
GetClientRect(hwnd_和rc);
D2D1_SIZE_SIZE=D2D1::SizeU(rc.right-rc.left,rc.bottom-rc.top);
如果(!pRT_)
{
//创建Direct2D渲染目标。
hr=pD2DFactory->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(
hwnd_uuz,
大小
),
&pRT_
);
//创建一个黑色笔刷。
如果(成功(hr))
{
hr=pRT_u3;->CreateSolidColorBrush(
D2D1::ColorF(D2D1::ColorF::黑色),
&丁香蒲_
);
}
}
返回人力资源;
}
void discardeDeviceResources(){
安全释放(&pRT);
安全释放(&pBlackBrush);
}
HRESULT DrawText(){
D2D1_RECT_F layoutRect=D2D1::RectF(
静态铸件(钢筋混凝土左)/d
// cvvvv.cpp : Defines the entry point for the application.
//
#include "framework.h"
#include "cvvvv.h"
#include<dwrite.h>
#include<d2d1.h>
#include<iostream>
#include<fstream>
#include<stdio.h>
using namespace std;
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
// *********************************************************************************
//HRESULT HasCharacter(
// UINT32 unicodeValue,
// BOOL* exists
//);
long const uniLast = 1114111;
//UINT32 codePoints[uniLast];
//UINT32 codePointsCount = uniLast;
//UINT16 glyphIndices[uniLast];
long long SairamAll;
// https://docs.microsoft.com/en-us/windows/win32/directwrite/getting-started-with-directwrite //
///// Part 1: Declare DirectWrite and Direct2D Resources. /////
// 1. In your class header file(SimpleText.h), declare pointers to IDWriteFactoryand IDWriteTextFormat interfaces as private members.
//IDWriteFactory* pDWriteFactory_;
//IDWriteTextFormat* pTextFormat_;
// 3. Declare pointers to ID2D1Factory, ID2D1HwndRenderTarget, and ID2D1SolidColorBrush interfaces for rendering the text with Direct2D.
//ID2D1Factory* pD2DFactory_;
//ID2D1HwndRenderTarget* pRT_;
//ID2D1SolidColorBrush* pBlackBrush_;
///// Part 2: Create Device Independent Resources. /////
template <class T> void SafeRelease(T** ppT)
{
if (*ppT)
{
(*ppT)->Release();
*ppT = NULL;
}
}
class SimpleText {
private:
// 2. Declare members to hold the text string to render and the length of the string.
IDWriteFactory* pDWriteFactory_;
IDWriteTextFormat* pTextFormat_;
const wchar_t* wszText_;
UINT32 cTextLength_;
ID2D1Factory* pD2DFactory_;
ID2D1HwndRenderTarget* pRT_;
ID2D1SolidColorBrush* pBlackBrush_;
HRESULT hr;
RECT rc;
HWND hwnd_;
float dpiScaleX_, dpiScaleY_;
public:
SimpleText() {
hr = CreateDeviceResources();
if (SUCCEEDED(hr))
{
pRT_->BeginDraw();
pRT_->SetTransform(D2D1::IdentityMatrix());
pRT_->Clear(D2D1::ColorF(D2D1::ColorF::White));
// Call the DrawText method of this class.
hr = DrawText();
if (SUCCEEDED(hr))
{
hr = pRT_->EndDraw(
);
}
}
if (FAILED(hr))
{
DiscardDeviceResources();
}
}
void CreateDeviceIndependentResources() {
hr = D2D1CreateFactory(
D2D1_FACTORY_TYPE_SINGLE_THREADED,
&pD2DFactory_);
if (SUCCEEDED(hr))
{
hr = DWriteCreateFactory(
DWRITE_FACTORY_TYPE_SHARED,
__uuidof(IDWriteFactory),
reinterpret_cast<IUnknown**>(&pDWriteFactory_)
);
}
wszText_ = L"Hello World using DirectWrite!";
cTextLength_ = (UINT32)wcslen(wszText_);
if (SUCCEEDED(hr))
{
hr = pDWriteFactory_->CreateTextFormat(
L"Gabriola", // Font family name.
NULL, // Font collection (NULL sets it to use the system font collection).
DWRITE_FONT_WEIGHT_REGULAR,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
72.0f,
L"en-us",
&pTextFormat_
);
}
// Center align (horizontally) the text.
if (SUCCEEDED(hr))
{
hr = pTextFormat_->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER);
}
if (SUCCEEDED(hr))
{
hr = pTextFormat_->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER);
}
}
HRESULT CreateDeviceResources() {
GetClientRect(hwnd_, &rc);
D2D1_SIZE_U size = D2D1::SizeU(rc.right - rc.left, rc.bottom - rc.top);
if (!pRT_)
{
// Create a Direct2D render target.
hr = pD2DFactory_->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(
hwnd_,
size
),
&pRT_
);
// Create a black brush.
if (SUCCEEDED(hr))
{
hr = pRT_->CreateSolidColorBrush(
D2D1::ColorF(D2D1::ColorF::Black),
&pBlackBrush_
);
}
}
return hr;
}
void DiscardDeviceResources() {
SafeRelease(&pRT_);
SafeRelease(&pBlackBrush_);
}
HRESULT DrawText() {
D2D1_RECT_F layoutRect = D2D1::RectF(
static_cast<FLOAT>(rc.left) / dpiScaleX_,
static_cast<FLOAT>(rc.top) / dpiScaleY_,
static_cast<FLOAT>(rc.right - rc.left) / dpiScaleX_,
static_cast<FLOAT>(rc.bottom - rc.top) / dpiScaleY_
);
pRT_->DrawText(
wszText_, // The string to render.
cTextLength_, // The string's length.
pTextFormat_, // The text format.
layoutRect, // The region of the window where the text will be rendered.
pBlackBrush_ // The brush used to draw the text.
);
return hr;
}
};
// *********************************************************************************
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Place code here.
// Initialize global strings
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_CVVVV, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_CVVVV));
MSG msg;
SimpleText s;
// Main message loop:
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_CVVVV));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_CVVVV);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code that uses hdc here...
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}