Function DirectX DrawText不';行不通

Function DirectX DrawText不';行不通,function,directx,drawtext,Function,Directx,Drawtext,我已经学习DirectX和Windows编程很短时间了。但是,DirectX函数DrawText有一个问题。不知何故,它不会在窗口中显示文本,也无法找出原因。这是我的密码: main.cpp //Include's #include"winwindow.hpp" #include<time.h> #include<iostream> //main int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,

我已经学习DirectX和Windows编程很短时间了。但是,DirectX函数
DrawText
有一个问题。不知何故,它不会在窗口中显示文本,也无法找出原因。这是我的密码:

main.cpp

//Include's
#include"winwindow.hpp"
#include<time.h>
#include<iostream>
//main
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){
    c_winwindow window(hInstance,nCmdShow);
    bool b_init=false;
    b_init=window.init(); 
    while(b_init){
        window.clear(D3DCOLOR_XRGB(0xEF,0xEF,0xEF));
        window.drawtext(0,0,L"Halo",D3DCOLOR_COLORVALUE(0xFF,0,0,0xFF));
        window.display();
        b_init=window.update();
    }
    return 0; 
}
winwindow.cpp

//Include's
#include"winwindow.hpp"
#include<iostream>
//c_winwindow memberfunction's
//konstruktor
c_winwindow::c_winwindow(const HINSTANCE &temphInstance,int tempnCmdShow){
    hInstance=temphInstance;
    nCmdShow=tempnCmdShow;
    hwnd=NULL;
    lpd3d=NULL;
    lpd3ddevice=NULL;
    b_init=false;
}
//destruktor
c_winwindow::~c_winwindow(){
    PostQuitMessage(0);
    hwnd=NULL;
    if(lpd3d!=NULL){
        lpd3d->Release();
        lpd3d=NULL;
    }
    if(lpd3ddevice!=NULL){
        lpd3ddevice->Release();
        lpd3ddevice=NULL;
    }
    if(p_d3dfont!=NULL){
        p_d3dfont->Release();
        p_d3dfont=NULL;
    }
}
//initwin
bool c_winwindow::initwin(){
    if(!b_init){
        WNDCLASS wc;
        ZeroMemory(&wc,sizeof(wc));
        wc.lpfnWndProc=Messagehandler;
        wc.hInstance=hInstance;
        wc.lpszClassName=L"wndc";
        RegisterClass(&wc);
        hwnd=CreateWindow(L"wndc",L"TestWindow",WS_SYSMENU,10,10,1200,800,NULL,NULL,hInstance,NULL);
        if(hwnd==NULL){
            std::cout << "failed to create Window!" << std::endl;
            return false;
        }else{
            b_init=true;
            ShowWindow(hwnd,nCmdShow);
            return true;
        }
    }else{
        return true;
    }
}
//initdx
bool c_winwindow::initdx(){
    lpd3d=Direct3DCreate9(D3D_SDK_VERSION);
    if(lpd3d==NULL){
        std::cout << "couldn't create direct3d Object" << std::endl;
        return false;
    }
    D3DPRESENT_PARAMETERS params;
    ZeroMemory(&params,sizeof(params));
    params.SwapEffect=D3DSWAPEFFECT_FLIP;
    params.hDeviceWindow=hwnd;
    params.Windowed=true;
    params.BackBufferWidth=1200;
    params.BackBufferHeight=800;
    params.BackBufferFormat= D3DFMT_A8R8G8B8;
    if(FAILED(lpd3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&params,&lpd3ddevice))){
        std::cout << "couldn't create direct3d device" << std::endl;
        return false;
    }
    if(D3D_OK!=D3DXCreateFont(lpd3ddevice,22,0,FW_NORMAL,1,false,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,ANTIALIASED_QUALITY,DEFAULT_PITCH|FF_DONTCARE,L"Arial",&p_d3dfont)){
        std::cout << "couldn't create font" << std::endl;
        return false;
    }
    return true;
} 
//init
bool c_winwindow::init(){
    return initwin()&&initdx();
}
//update
bool c_winwindow::update(){
    if(b_init){
        if(GetMessage(&msg,NULL,0,0)){
            TranslateMessage(&msg);
            DispatchMessage(&msg);
            return true;
        }else{
            if(lpd3d!=NULL){
                lpd3d->Release();
                lpd3d=NULL;
            }
            if(lpd3ddevice!=NULL){
                lpd3ddevice->Release();
                lpd3ddevice=NULL;
            }
            hwnd=NULL;
            b_init=false;
            return false;
        }
    }else{
        return false;
    }
}
//clear
void c_winwindow::clear(D3DCOLOR clr){
    lpd3ddevice->Clear(0,0,D3DCLEAR_TARGET,clr,0,0);
    lpd3ddevice->BeginScene();
}
//display 
void c_winwindow::display(){
    lpd3ddevice->EndScene();
    lpd3ddevice->Present(0,0,0,0);
}
void c_winwindow::drawtext(unsigned short int us_x,unsigned short int us_y,LPCWSTR text,D3DCOLOR textcolor){
    RECT r;
    SetRect(&r,us_x,us_y,0,0);
    p_d3dfont->DrawText(NULL,text,-1,&r,DT_LEFT,textcolor);
}
//WndProc
LRESULT CALLBACK c_winwindow::Messagehandler(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){
    switch(msg){
        case(WM_DESTROY):
            PostQuitMessage(0); 
            return 0;
        break;
        case(WM_PAINT):
            return 0;
        break;
    }
    return DefWindowProc(hwnd,msg,wParam,lParam);
}
//包含的
#包括“winwindow.hpp”
#包括
//c_winwindow成员函数的
//康斯特鲁克托
c_winwindow::c_winwindow(const HINSTANCE&temphInstance,int tempnCmdShow){
hInstance=temphInstance;
nCmdShow=tempnCmdShow;
hwnd=NULL;
lpd3d=NULL;
lpd3ddevice=NULL;
b_init=假;
}
//解压器
c_winwindow::~c_winwindow(){
PostQuitMessage(0);
hwnd=NULL;
如果(lpd3d!=NULL){
lpd3d->Release();
lpd3d=NULL;
}
如果(lpd3ddevice!=NULL){
lpd3ddevice->Release();
lpd3ddevice=NULL;
}
if(p_d3dfont!=NULL){
p_d3dfont->Release();
p_d3dfont=NULL;
}
}
//伊尼特温
bool c_winwindow::initwin(){
如果(!b_init){
WNDCLASS wc;
零内存(&wc,sizeof(wc));
lpfnWndProc=Messagehandler;
wc.hInstance=hInstance;
wc.lpszClassName=L“wndc”;
注册类(&wc);
hwnd=CreateWindow(L“wndc”,L“TestWindow”,WS_SYSMENU,10,101200800,NULL,NULL,hInstance,NULL);
if(hwnd==NULL){
std::cout DrawText(NULL,text,-1,&r,DT_LEFT,textcolor);
}
//WndProc
LRESULT回调c_winwindow::Messagehandler(HWND-HWND、UINT-msg、WPARAM-WPARAM、LPARAM-LPARAM){
开关(msg){
案例(WM_销毁):
PostQuitMessage(0);
返回0;
打破
案例(WM_油漆):
返回0;
打破
}
返回DefWindowProc(hwnd、msg、wParam、lParam);
}

窗口显示为灰色,没有文本。有人能解释为什么没有文本吗?

你现在应该学习DX11。DX9已经是过时的技术,随着时间的推移。因此,特别是在你刚刚学习的时候,选择DX11+D2D。
//Include's
#include"winwindow.hpp"
#include<iostream>
//c_winwindow memberfunction's
//konstruktor
c_winwindow::c_winwindow(const HINSTANCE &temphInstance,int tempnCmdShow){
    hInstance=temphInstance;
    nCmdShow=tempnCmdShow;
    hwnd=NULL;
    lpd3d=NULL;
    lpd3ddevice=NULL;
    b_init=false;
}
//destruktor
c_winwindow::~c_winwindow(){
    PostQuitMessage(0);
    hwnd=NULL;
    if(lpd3d!=NULL){
        lpd3d->Release();
        lpd3d=NULL;
    }
    if(lpd3ddevice!=NULL){
        lpd3ddevice->Release();
        lpd3ddevice=NULL;
    }
    if(p_d3dfont!=NULL){
        p_d3dfont->Release();
        p_d3dfont=NULL;
    }
}
//initwin
bool c_winwindow::initwin(){
    if(!b_init){
        WNDCLASS wc;
        ZeroMemory(&wc,sizeof(wc));
        wc.lpfnWndProc=Messagehandler;
        wc.hInstance=hInstance;
        wc.lpszClassName=L"wndc";
        RegisterClass(&wc);
        hwnd=CreateWindow(L"wndc",L"TestWindow",WS_SYSMENU,10,10,1200,800,NULL,NULL,hInstance,NULL);
        if(hwnd==NULL){
            std::cout << "failed to create Window!" << std::endl;
            return false;
        }else{
            b_init=true;
            ShowWindow(hwnd,nCmdShow);
            return true;
        }
    }else{
        return true;
    }
}
//initdx
bool c_winwindow::initdx(){
    lpd3d=Direct3DCreate9(D3D_SDK_VERSION);
    if(lpd3d==NULL){
        std::cout << "couldn't create direct3d Object" << std::endl;
        return false;
    }
    D3DPRESENT_PARAMETERS params;
    ZeroMemory(&params,sizeof(params));
    params.SwapEffect=D3DSWAPEFFECT_FLIP;
    params.hDeviceWindow=hwnd;
    params.Windowed=true;
    params.BackBufferWidth=1200;
    params.BackBufferHeight=800;
    params.BackBufferFormat= D3DFMT_A8R8G8B8;
    if(FAILED(lpd3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&params,&lpd3ddevice))){
        std::cout << "couldn't create direct3d device" << std::endl;
        return false;
    }
    if(D3D_OK!=D3DXCreateFont(lpd3ddevice,22,0,FW_NORMAL,1,false,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,ANTIALIASED_QUALITY,DEFAULT_PITCH|FF_DONTCARE,L"Arial",&p_d3dfont)){
        std::cout << "couldn't create font" << std::endl;
        return false;
    }
    return true;
} 
//init
bool c_winwindow::init(){
    return initwin()&&initdx();
}
//update
bool c_winwindow::update(){
    if(b_init){
        if(GetMessage(&msg,NULL,0,0)){
            TranslateMessage(&msg);
            DispatchMessage(&msg);
            return true;
        }else{
            if(lpd3d!=NULL){
                lpd3d->Release();
                lpd3d=NULL;
            }
            if(lpd3ddevice!=NULL){
                lpd3ddevice->Release();
                lpd3ddevice=NULL;
            }
            hwnd=NULL;
            b_init=false;
            return false;
        }
    }else{
        return false;
    }
}
//clear
void c_winwindow::clear(D3DCOLOR clr){
    lpd3ddevice->Clear(0,0,D3DCLEAR_TARGET,clr,0,0);
    lpd3ddevice->BeginScene();
}
//display 
void c_winwindow::display(){
    lpd3ddevice->EndScene();
    lpd3ddevice->Present(0,0,0,0);
}
void c_winwindow::drawtext(unsigned short int us_x,unsigned short int us_y,LPCWSTR text,D3DCOLOR textcolor){
    RECT r;
    SetRect(&r,us_x,us_y,0,0);
    p_d3dfont->DrawText(NULL,text,-1,&r,DT_LEFT,textcolor);
}
//WndProc
LRESULT CALLBACK c_winwindow::Messagehandler(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){
    switch(msg){
        case(WM_DESTROY):
            PostQuitMessage(0); 
            return 0;
        break;
        case(WM_PAINT):
            return 0;
        break;
    }
    return DefWindowProc(hwnd,msg,wParam,lParam);
}