2017-08-16 55 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

如果在您绘制文本后,splash窗口收到WM_PAINT会怎么样? – zett42

+0

它将用位图刷新窗口。但是如果你不用你的绘制文本代码来处理它,你的“HELLO”将会消失。对于启动画面,您可能不需要担心。那里有更好的代码。查看微软示例。 – lakeweb

+0

这是否回答了您的问题? – lakeweb