在位图启动画面(MFC)上绘制文本

时间:2017-08-16 16:17:37

标签: 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;

1 个答案:

答案 0 :(得分:0)

将来电UpdateWindow(hSplashWnd);移至GetDC(..)

之后

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);