将位图图像加载到按钮

Posted

技术标签:

【中文标题】将位图图像加载到按钮【英文标题】:Loading bitmap image to button 【发布时间】:2014-04-30 11:36:20 【问题描述】:

我正在处理遗留应用程序,我在对话框中添加了一个按钮(Switch/pushlike),我想向其中添加位图图像,以获取引用 getdlgitem(id) 但这里这个函数也需要 hwnd 对象, 在搜索我之前找到的代码时,他们使用了以下三个函数

environ_control->dialwindow
environ_get_active_dialogue()
environ_get_ActiveWindow()

代替hwnd,它们都不适合我,

我试过这样

CButton* pButton = (CButton*)GetDlgItem(environ_get_active_dialogue(),IDD_BTN_CUSTOMRANDC );
pButton->SetBitmap((HBITMAP)Bmp1);

它在第二行给出调试断言失败,请任何帮助,功能代码是

SYMBOLIC JOB_PSNUP::nup_init_nup_dialog( void )

    int arrTabs[8];

arrTabs[0] = 6;
arrTabs[1] = 28;
arrTabs[2] = 61;
arrTabs[3] = 97;
arrTabs[4] = 137;
arrTabs[5] = 179;
arrTabs[6] = 218;

// Reset information about Preview area
m_hwndPreview = NULL;
m_hdcPreview  = NULL;
m_rFront.top = m_rFront.bottom = m_rFront.left = m_rFront.right = 0;
m_rBack.top = m_rBack.bottom = m_rBack.left = m_rBack.right = 0;
CBitmap Bmp1, Bmp2;


m_bSetOrderMode = FALSE;
m_iCurrOrder    = 1;

environ_set_dlg_item_text_win( IDD_SNAME, "<click on page>" );

CDataExchange dx(CWnd::FromHandle(environ_get_active_dialogue()), FALSE);
ELX_INT BtnIndex;
comm->m_pXPBtn[BtnIndex = comm->GetIndexForNewBtn()] = new ECXPStyleButton;
comm->m_pXPBtn[comm->GetIndexForNewBtn()] = new ECXPStyleButton;

comm->initXPBtn(comm->m_pXPBtn[BtnIndex+0], IDD_NUMOVEUP,   &dx, IDB_MOVEUP,   "Move up");
comm->initXPBtn(comm->m_pXPBtn[BtnIndex+1], IDD_NUMOVEDOWN, &dx, IDB_MOVEDOWN, "Move down");



ECListCtrl* pListCtrl = comm->InitECListCtrl(IDD_NULIST, _T("Order,70;Side,50;Number,60;H.Offset,60;V.Offset,60;Constant,60;Forms,50"));    
pListCtrl->GetHeaderCtrl()->EnableWindow(FALSE);
comm->state.enSortBy2 = SORT_DOC_BY_NAME_ASC;

environ_set_resource_object_state( 0, IDD_NUCURRORDER, OBJECT_STATE_DISABLED, FALSE );
environ_set_resource_object_state( 0, IDD_NUORDERLAB, OBJECT_STATE_DISABLED, FALSE );

environ_message_handler( ENVIRON_LBSET_TAB_STOPS, (void*)IDD_NULIST, (void*)(&arrTabs));
environ_message_handler( ENVIRON_LBSET_HEXTENT, (void*)IDD_NULIST, (void*)(1000) );

comm->dial.dial_stack_fake[0] = '\0';
environ_set_string_into_resource_object( 0, IDD_NUHOFFSET, comm->dial.dial_stack_fake,  FALSE );


Bmp1.LoadBitmapA(IDB_BUTTON_ON);
Bmp2.LoadBitmapA(IDB_BUTTON_OFF);
if( environ_is_object_selected( 0, IDD_BTN_CUSTOMRANDC ))

    CButton* pButton = (CButton*)GetDlgItem(environ_get_active_dialogue(),IDD_BTN_CUSTOMRANDC );
    pButton->SetBitmap((HBITMAP)Bmp2);  

else

    CButton* pButton = (CButton*)GetDlgItem(environ_get_active_dialogue(),IDD_BTN_CUSTOMRANDC );
    pButton->SetBitmap((HBITMAP)Bmp1);  





// Total number of partitions
environ_set_dlg_item_int_win( IDD_NUTOTNUM, m_iTotal );

// On Back side
if( m_enPaperDuplex == JOB_SIMPLEX )

    environ_set_resource_object_state( 0, IDD_NUFRONTNUMLAB, OBJECT_STATE_HIDDEN, FALSE );

    environ_set_resource_object_state( 0, IDD_NUFRONTNUM, OBJECT_STATE_HIDDEN, FALSE );
    environ_set_resource_object_state( 0, IDD_NUBACKNUMLAB, OBJECT_STATE_HIDDEN, FALSE );
    environ_set_resource_object_state( 0, IDD_NUBACKNUM, OBJECT_STATE_HIDDEN, FALSE );
    environ_set_resource_object_state( 0, IDD_NUFRONTLAB, OBJECT_STATE_HIDDEN, FALSE );
    environ_set_resource_object_state( 0, IDD_NUBACKLAB, OBJECT_STATE_HIDDEN, FALSE );

    environ_set_dlg_item_text_win( IDD_NUTOTNUMLAB, "&Number of Partitions:" );

else

    environ_set_dlg_item_int_win( IDD_NUFRONTNUM, m_iOnFront );
    environ_set_dlg_item_int_win( IDD_NUBACKNUM, m_iOnBack );


// Offset units
comm->job_prop->jobprop_get_unit_name( comm->formdef.props.units, FALSE/*TRUE*/, comm->state.workstring ); // SMP
environ_set_dlg_item_text_win( IDD_UNITSTR0, comm->state.workstring );
environ_set_dlg_item_text_win( IDD_UNITSTR1, comm->state.workstring );


if( comm->job_config.flag.auto_offset_paper_partitions )

    environ_set_resource_object_on_or_off( 0, IDD_NUAUTOOFFSET, TRUE );


// List of Partitions
nup_fill_list();


m_NupDialExit = USER_CANCEL;
return( 0 );

【问题讨论】:

【参考方案1】:

采用两个参数的 GetDlgItem 版本返回 HWND,而不是 CWnd*。因此将其转换为 CButton* 是不正确的。相反,请使用 GetDlgItem(IDD_BTN_CUSTOMRANDC)。

【讨论】:

以上是关于将位图图像加载到按钮的主要内容,如果未能解决你的问题,请参考以下文章

将位图图像加载到 WINApi C++ 并显示

Android:如何在不加载完整位图的情况下将流式图像即时渲染到 ImageView?

如何将位图图像设置为按钮背景图像

将位图添加到 MFC 中的按钮

如何在android中使用imageloader释放位图内存?

如何在 Android 中使位图加载更快?