2017-08-16 9 views
0

ビットマップを表示する単純なスプラッシュ画面にテキストを描画しようとしています。以下は私のコードです。ビットマップは正しく表示されますが、テキストは表示されません。私は間違って何をしていますか?ビットマップスプラッシュスクリーン(MFC)でテキストを描く

これは主にコードであるため、投稿する前にもう少し「詳細」を追加する必要があります。著者がこの例を入手した場所がわかりません。これが行われる方法に適したビットマップ上にテキストを描画する例は見つからないので、いくつかの助けが必要です。 おかげ

Splash.cpp

#include "stdafx.h" 
#include "Splash.h" 

Splash::Splash() 
{ 
    bool b = false; //debugging 
} 

Splash::~Splash() 
{ 
DestroyWindow(hSplashWnd); 
} 

void Splash::Init(HWND hWnd,HINSTANCE hInst,int resid) 
{ 
hParentWindow=hWnd; 
hSplashWnd=CreateWindowEx(WS_EX_CLIENTEDGE,"STATIC","", 
    WS_POPUP|WS_DLGFRAME|SS_BITMAP,300,300,300,300,hWnd,NULL,hInst,NULL); 
SendMessage(hSplashWnd,STM_SETIMAGE,IMAGE_BITMAP,(LPARAM)LoadBitmap(hInst,MAKEINTRESOURCE(resid))); 
this->SHOWING = FALSE; 
} 

void Splash::Show() 
{ 
    //get size of hSplashWnd (width and height) 
    int  x,   y; 
    int  pwidth,  pheight; 
    int  tx,   ty; 
    HDWP windefer; 
    RECT rect,  prect; 

    GetClientRect(hSplashWnd,&rect); 
    x=rect.right; 
    y=rect.bottom; 

    //get parent screen coordinates 
    GetWindowRect(this->hParentWindow,&prect); 

    //center splash window on parent window 
    pwidth=prect.right-prect.left; 
    pheight=prect.bottom - prect.top; 

    int iScreenWidth, iScreenHeight, iSplashLeft, iSplashTop; 

    iScreenWidth = ::GetSystemMetrics(SM_CXSCREEN); 
    iScreenHeight = ::GetSystemMetrics(SM_CYSCREEN); 

    if(prect.left > iScreenWidth) 
    { 
     //On second monitor 
     iSplashLeft = iScreenWidth + (iScreenWidth/2) - ((rect.right - rect.left)/2); 
    } 
    else 
    { 
     //On first monitor 
     iSplashLeft = (iScreenWidth/2) - ((rect.right - rect.left)/2); 
    } 
    iSplashTop = (iScreenHeight/2) - ((rect.bottom - rect.top) /2); 

    tx = iSplashLeft; 
    ty = iSplashTop; 

    windefer=BeginDeferWindowPos(1); 
    DeferWindowPos(windefer,hSplashWnd,HWND_NOTOPMOST,tx,ty,50,50,SWP_NOSIZE|SWP_SHOWWINDOW|SWP_NOZORDER); 
    EndDeferWindowPos(windefer); 

    BOOL isValidWindow = IsWindow(hSplashWnd); 

    //##################### Draw text on the bitmap ############### 
    CBrush brush; 
    brush.CreateSolidBrush(RGB(255,0,0)); 
    HDC hdc = GetDC(hSplashWnd); 

    char *text = "HELLO HELLO HELLO HELLO HELLO HELLO"; 
    SelectObject(hdc, brush); 
    SetTextColor(hdc, RGB(0,255,0)); 
    DrawTextA(hdc, text, strlen(text), &rect, DT_SINGLELINE | DT_LEFT); //DT_CENTER | DT_VCENTER); 

    ShowWindow(hSplashWnd,SW_SHOWNORMAL); 
    UpdateWindow(hSplashWnd); 

    this->SHOWING = TRUE; 
} 

void Splash::Hide() 
{ 
    ShowWindow(hSplashWnd,SW_HIDE); 
    this->SHOWING = FALSE; 
} 

Splash.h

#if !defined(AFX_SPLASH_H_INCLUDED) 
#define AFX_SPLASH_H_INCLUDED 

#if _MSC_VER > 1000 
#pragma once 
#endif // _MSC_VER > 1000 

class Splash 
{ 
public: 
    void Hide(); 
    void Show(); 
    void Init(HWND hWnd,HINSTANCE hInst,int resid); 
    BOOL SHOWING; 
    Splash(); 
    virtual ~Splash(); 

private: 
    UINT TimerID; 
    HWND hParentWindow; 
    HWND hSplashWnd; 

}; 

#endif 

スプラッシュ画面描画:

Splash Splash1; 
Splash1.Init(m_pMainWnd->m_hWnd, this->m_hInstance, IDB_BITMAP_SPLASH); 
Splash.Show(); 
Sleep(3000); 
Splash1.Hide; 

答えて

0

移動をあなたのGetDC(..)

後に UpdateWindow(hSplashWnd);への呼び出し0

WM_PAINTメッセージは、ビットマップを描画し、その後にテキストを描画します。

HDC hdc = GetDC(hSplashWnd); 
UpdateWindow(hSplashWnd); 

wchar_t * text = L"HELLO"; 
//SelectObject(hdc, brush); 
SetTextColor(hdc, RGB(0, 255, 0)); 
DrawText(hdc, text, 200, &rect, DT_SINGLELINE | DT_LEFT); //DT_CENTER | DT_VCENTER); 
ShowWindow(hSplashWnd,SW_SHOWNORMAL); 
+0

テキストを描画した後にスプラッシュウィンドウにWM_PAINTが表示される場合はどうなりますか? – zett42

+0

ウィンドウをビットマップで更新します。しかし、あなたの描画テキストコードでそれを処理しない場合、あなたの "ハロー"が消えます。スプラッシュ画面の場合、心配したくないかもしれません。そこに良いコードがあります。 Microsoftの例をご覧ください。 – lakeweb

+0

これはあなたの質問に答えましたか? – lakeweb

関連する問題