Date
Sep. 8th, 2024
 
2024年 8月 6日

Post: MFC: 对话框添加背景图片

MFC: 对话框添加背景图片

Published 12:06 Jun 03, 2010.

Created by @ezra. Categorized in #Programming, and tagged as #Windows.

Source format: Markdown

Table of Content

有两种实现方法,分别是:

  • 插入一个 Bitmap 的资源图片,假设资源名称为 IDC_BITMAP1
  • CXXXDialog::OnPaint() 中实现:
void CMyDialogDlg::OnPaint() {

    if (IsIconic()) {

        CPaintDC dc(this); // 用于绘制的设备上下文

        SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

        // 使图标在工作区矩形中居中

        int cxIcon = GetSystemMetrics(℠_CXICON);

        int cyIcon = GetSystemMetrics(℠_CYICON);

        CRect rect;

        GetClientRect(&rect);

        int x = (rect.Width() - cxIcon + 1) / 2;

        int y = (rect.Height() - cyIcon + 1) / 2;

        // 绘制图标

        dc.DrawIcon(x, y, m_hIcon);

    } else {

        //

        // 给窗体添加背景

        //

        CPaintDC dc(this);

        CRect rc;

        GetClientRect(&rc);

        CDC dcMem;

        dcMem.CreateCompatibleDC(&dc);

        CBitmap bmpBackground;

        bmpBackground.LoadBitmap(IDB_BITMAP1);

        BITMAP bitmap;

        bmpBackground.GetBitmap(&bitmap);

        CBitmap* pbmpPri = dcMem.SelectObject(&bmpBackground);

        dc.StretchBlt(0,0,rc.Width(), rc.Height(), &dcMem,0,0,bitmap.bmWidth, bitmap.bmHeight, SRCCOPY);
    }
}
Pinned Message
HOTODOGO
The Founder and CEO of Infeca Technology.
Developer, Designer, Blogger.
Big fan of Apple, Love of colour.
Feel free to contact me.
反曲点科技创始人和首席执行官。
程序猿、设计师、奇怪的博主。
苹果死忠、热爱色彩斑斓的世界。
如有意向请随时 与我联系