首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >所有者绘图按钮强制刷新

所有者绘图按钮强制刷新
EN

Stack Overflow用户
提问于 2015-06-15 16:50:11
回答 1查看 536关注 0票数 0

我有4个所有者画的按钮,作为我的程序的标签系统。我遇到的问题是,当我单击其中一个按钮时,我需要更改其他按钮的图像。但每当我尝试重置图像时,它只会更改我所单击的按钮上的图像。有没有办法在不被点击的情况下改变其他按钮上的图像?

代码语言:javascript
复制
INT_PTR CALLBACK Springboard::DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam){
    static DRAWITEMSTRUCT* pdis;

    switch (msg) {
        case WM_DRAWITEM:

            pdis = (DRAWITEMSTRUCT*) lParam;
            // (winuser.h) Maybe you also want to account for pdis->CtlType (ODT_MENU, ODT_LISTBOX, ODT_COMBOBOX, ODT_BUTTON, ODT_STATIC)
            switch(pdis->CtlID) {
            case IDC_TAB1:
                theSpringboard.myManageOwnerDrawIconButton(pdis, hInstance);
                break;
            case IDC_TAB2:
                theSpringboard.myManageOwnerDrawIconButton(pdis, hInstance);

                break;
            case IDC_TAB3:
                theSpringboard.myManageOwnerDrawIconButton(pdis, hInstance);
                break;
            case IDC_TAB4:
                theSpringboard.myManageOwnerDrawIconButton(pdis, hInstance);
                break;
            case IDC_TAB5:
                theSpringboard.myManageOwnerDrawIconButton(pdis, hInstance);
                break;
        default:
            break;
            }

int Springboard::myManageOwnerDrawIconButton(DRAWITEMSTRUCT* pdis, HINSTANCE hInstance) {
    static RECT rect;
    static HBITMAP hCurrIcon, hSTDc,  hSTDoff, hSTDon, hVFXc, hVFXoff, hVFXon, hCITYc, hCITYoff, hCITYon, hMAXc, hMAXoff, hMAXon, hSETc, hSEToff, hSETon;
    rect = pdis->rcItem;

    hSTDoff = (HBITMAP) LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_STDOFF));
    hSTDon = (HBITMAP) LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_STDON));
    hSTDc = (HBITMAP) LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_STDCLICK));
    hVFXoff = (HBITMAP) LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_VFXOFF));
    hVFXon = (HBITMAP) LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_VFXON));
    hVFXc = (HBITMAP) LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_VFXCLICK));
    hCITYoff = (HBITMAP) LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_CITYOFF));
    hCITYon = (HBITMAP) LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_CITYON));
    hCITYc = (HBITMAP) LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_CITYCLICK));
    hMAXoff = (HBITMAP) LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_MAXOFF));
    hMAXon = (HBITMAP) LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_MAXON));
    hMAXc = (HBITMAP) LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_MAXCLICK));
    hSEToff = (HBITMAP) LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_SETTINGOFF));
    hSETon = (HBITMAP) LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_SETTINGON));
    hSETc = (HBITMAP) LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_SETTINGCLICK));

    if (IDC_TAB1 == pdis->CtlID) {
        // If the button is selected, display the 
        // "active" icon, else the "inactive" icon:

        if (pdis->itemState & ODS_SELECTED) hCurrIcon = hSTDc;
        else hCurrIcon = hSTDoff;
        if(TabRoll == 1) hCurrIcon = hSTDon;
    }

    if (IDC_TAB2 == pdis->CtlID) {
        // If the button is selected, display the 
        // "active" icon, else the "inactive" icon:
        if (pdis->itemState & ODS_SELECTED) hCurrIcon = hVFXc;
        else hCurrIcon = hVFXoff;
        //if(TabRoll == 2) hCurrIcon = hVFXon;
    }

    if (IDC_TAB3 == pdis->CtlID) {
        // If the button is selected, display the 
        // "active" icon, else the "inactive" icon:
        if (pdis->itemState & ODS_SELECTED) hCurrIcon = hCITYc;
        else hCurrIcon = hCITYoff;
        //if(TabRoll == 3) hCurrIcon = hCITYon;
    }

    if (IDC_TAB4 == pdis->CtlID) {
        // If the button is selected, display the 
        // "active" icon, else the "inactive" icon:

        if (pdis->itemState & ODS_SELECTED) hCurrIcon = hMAXc;
        else hCurrIcon = hMAXoff;
        //if(TabRoll == 4) hCurrIcon = hMAXon;
    }

    if (IDC_TAB5 == pdis->CtlID) {
        // If the button is selected, display the 
        // "active" icon, else the "inactive" icon:
        if (pdis->itemState & ODS_SELECTED){ 
            hCurrIcon = hSETc;}
        else{
            hCurrIcon = hSEToff;}
        //if(TabRoll == 5) hCurrIcon = hSETon;
    }

    HDC hdc = CreateCompatibleDC(pdis->hDC);
    SelectObject(hdc, hCurrIcon);

BitBlt(pdis->hDC,0,
        0,ICON_WIDTH,
        ICON_HEIGHT, hdc, 0, 0, SRCCOPY);

        DeleteDC(hdc);

    return(RET_OK);

}
EN

回答 1

Stack Overflow用户

发布于 2015-06-15 22:53:31

您可以简化DlgProc

代码语言:javascript
复制
INT_PTR CALLBACK Springboard::DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
    case WM_DRAWITEM:
        theSpringboard.myManageOwnerDrawIconButton((DRAWITEMSTRUCT*)lp, hInstance);
        break;
    //...
    return FALSE;
}

myManageOwnerDrawIconButton中,您需要进行以下更改:

来自HDC hdc = CreateCompatibleDC(pdis->hDC);

至:

HDC hdc = pdis->hDC;,这样就不需要调用DeleteDC(hdc);

更改rect的声明,它不一定是静态的。

RECT rect = pdis->rcItem;

其他静态变量需要初始化一次,这就是static的全部意义所在:

代码语言:javascript
复制
static HBITMAP hCurrIcon = (HBITMAP) LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_something));
//...

ODS_SELECTED只表示按钮被按下。你可能想使用你自己的全局变量来手动跟踪按钮,设置选择和删除其他按钮的选择……

另外,我为你的问题添加了winapi标签。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30840949

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档