【问题标题】:Draw text on bitmap splash screen (MFC)在位图闪屏 (MFC) 上绘制文本
【发布时间】:2017-08-16 16:17:37
【问题描述】:

我正在尝试在一个简单的启动屏幕上绘制文本,该屏幕仅显示位图,没有其他内容。下面是我的代码。位图正确显示,但没有文字。我做错了什么?

我需要在发布之前添加更多“细节”,因为这主要是代码:我不确定作者从哪里得到这个例子。我找不到适合这种方式的位图上绘制文本的示例,因此需要一些帮助。 谢谢

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;

【问题讨论】:

    标签: mfc


    【解决方案1】:

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

    【讨论】:

    • 如果在您绘制文本后启动窗口收到 WM_PAINT 怎么办?
    • 它将用位图刷新窗口。但是,如果您不使用绘图文本代码处理它,您的“HELLO”将会消失。对于启动画面,您可能不想担心。那里有更好的代码。查看 Microsoft 示例。
    • 这回答了你的问题吗?
    猜你喜欢
    • 1970-01-01
    • 2013-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多