⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 background.c

📁 一个类似windows
💻 C
📖 第 1 页 / 共 2 页
字号:
     * use the contents of szFile to initialize itself */
    ofn.lpstrFile[0] = TEXT('\0');
    ofn.nMaxFile = MAX_PATH;
    ofn.lpstrFilter = filter;
    ofn.nFilterIndex = 0;
    ofn.lpstrFileTitle = fileTitle;
    ofn.nMaxFileTitle = 256;
    ofn.lpstrInitialDir = NULL;
    ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

    if(GetOpenFileName(&ofn) == TRUE)
    {   
        /* Check if there is already a entry that holds this filename */
        if(CheckListBoxFilename(g_hBackgroundList, filename) == TRUE)
            return;
        
        if(g_listViewItemCount > (MAX_BACKGROUNDS - 1))
            return;
        
        SHGetFileInfo(filename,
                      0,
                      &sfi,
                      sizeof(sfi),
                      SHGFI_SYSICONINDEX | SHGFI_SMALLICON | SHGFI_DISPLAYNAME);
        
        backgroundItem = &g_backgroundItems[g_listViewItemCount];
        
        backgroundItem->bWallpaper = TRUE;
        
        _tcscpy(backgroundItem->szDisplayName, sfi.szDisplayName);
        _tcscpy(backgroundItem->szFilename, filename);
        
        ZeroMemory(&listItem, sizeof(LV_ITEM));
        listItem.mask       = LVIF_TEXT | LVIF_PARAM | LVIF_STATE | LVIF_IMAGE;
        listItem.state      = 0;
        listItem.pszText    = backgroundItem->szDisplayName;
        listItem.iImage     = sfi.iIcon;
        listItem.iItem      = g_listViewItemCount;
        listItem.lParam     = g_listViewItemCount;
        
        (void)ListView_InsertItem(g_hBackgroundList, &listItem);
        
        g_listViewItemCount++;
    }
}

void ListViewItemChanged(int itemIndex)
{
    BackgroundItem *backgroundItem = NULL;
    
    g_backgroundSelection = itemIndex;
    backgroundItem = &g_backgroundItems[g_backgroundSelection];
    
    if(g_pWallpaperBitmap != NULL)
    {
        DibFreeImage(g_pWallpaperBitmap);
        g_pWallpaperBitmap = NULL;
    }
    
    if(backgroundItem->bWallpaper == TRUE)
    {   
        g_pWallpaperBitmap = DibLoadImage(backgroundItem->szFilename);
        
        if(g_pWallpaperBitmap == NULL)
        {
            return;
        }
    }
    
    InvalidateRect(g_hBackgroundPreview, NULL, TRUE);
    
    EnableWindow(g_hColorButton, (backgroundItem->bWallpaper == FALSE ? TRUE : FALSE));
    EnableWindow(g_hPlacementCombo, backgroundItem->bWallpaper);
    
    PropSheet_Changed(GetParent(g_hBackgroundPage), g_hBackgroundPage);
}

void DrawBackgroundPreview(LPDRAWITEMSTRUCT draw)
{
	float scaleX;
	float scaleY;
	int scaledWidth;
	int scaledHeight;
	int posX;
	int posY;

    if(g_backgroundItems[g_backgroundSelection].bWallpaper == FALSE)
    {
        FillRect(draw->hDC, &draw->rcItem, GetSysColorBrush(COLOR_BACKGROUND));
        return;
    }
    
    if(g_pWallpaperBitmap == NULL)
        return;

    scaleX = ((float)GetSystemMetrics(SM_CXSCREEN) - 1) / (float)draw->rcItem.right;
    scaleY = ((float)GetSystemMetrics(SM_CYSCREEN) - 1) / (float)draw->rcItem.bottom;

    scaledWidth = g_pWallpaperBitmap->width / scaleX;
    scaledHeight = g_pWallpaperBitmap->height / scaleY;
    
    posX = (draw->rcItem.right / 2) - (scaledWidth / 2);
    posY = (draw->rcItem.bottom / 2) - (scaledHeight / 2);
    
    FillRect(draw->hDC, &draw->rcItem, GetSysColorBrush(COLOR_BACKGROUND));
    
    SetStretchBltMode(draw->hDC, COLORONCOLOR);
    
    if(g_placementSelection == PLACEMENT_CENTER)
    {
        StretchDIBits(draw->hDC,
                      posX,
                      posY,
                      scaledWidth,
                      scaledHeight,
                      0,
                      0,
                      g_pWallpaperBitmap->width,
                      g_pWallpaperBitmap->height,
                      g_pWallpaperBitmap->bits,
                      g_pWallpaperBitmap->info,
                      DIB_RGB_COLORS,
                      SRCCOPY);
    }
    
    if(g_placementSelection == PLACEMENT_STRETCH)
    {
        StretchDIBits(draw->hDC,
                      0,
                      0,
                      draw->rcItem.right,
                      draw->rcItem.bottom,
                      0,
                      0,
                      g_pWallpaperBitmap->width,
                      g_pWallpaperBitmap->height,
                      g_pWallpaperBitmap->bits,
                      g_pWallpaperBitmap->info,
                      DIB_RGB_COLORS,
                      SRCCOPY);
    }

    if(g_placementSelection == PLACEMENT_TILE)
    {
        int x;
        int y;
        
        for(y = 0; y < draw->rcItem.bottom; y += scaledHeight)
        {
            for(x = 0; x < draw->rcItem.right; x += scaledWidth)
            {
                StretchDIBits(draw->hDC,
                              x,
                              y,
                              scaledWidth,
                              scaledHeight,
                              0,
                              0,
                              g_pWallpaperBitmap->width,
                              g_pWallpaperBitmap->height,
                              g_pWallpaperBitmap->bits,
                              g_pWallpaperBitmap->info,
                              DIB_RGB_COLORS,
                              SRCCOPY);
            }
        }
    }
}

void SetWallpaper()
{
    HKEY regKey;
    
    RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Control Panel\\Desktop"), 0, KEY_ALL_ACCESS, &regKey);

    if(g_placementSelection == PLACEMENT_TILE)
    {
        RegSetValueEx(regKey, TEXT("TileWallpaper"), 0, REG_SZ, (BYTE *)TEXT("1"), sizeof(TCHAR) * 2);
        RegSetValueEx(regKey, TEXT("WallpaperStyle"), 0, REG_SZ, (BYTE *)TEXT("0"), sizeof(TCHAR) * 2);
    }
    
    if(g_placementSelection == PLACEMENT_CENTER)
    {
        RegSetValueEx(regKey, TEXT("TileWallpaper"), 0, REG_SZ, (BYTE *)TEXT("0"), sizeof(TCHAR) * 2);
        RegSetValueEx(regKey, TEXT("WallpaperStyle"), 0, REG_SZ, (BYTE *)TEXT("0"), sizeof(TCHAR) * 2);
    }

    if(g_placementSelection == PLACEMENT_STRETCH)
    {
        RegSetValueEx(regKey, TEXT("TileWallpaper"), 0, REG_SZ, (BYTE *)TEXT("0"), sizeof(TCHAR) * 2);
        RegSetValueEx(regKey, TEXT("WallpaperStyle"), 0, REG_SZ, (BYTE *)TEXT("2"), sizeof(TCHAR) * 2);
    }
    
    RegCloseKey(regKey);
    
    if(g_backgroundItems[g_backgroundSelection].bWallpaper == TRUE)
    {
        SystemParametersInfo(SPI_SETDESKWALLPAPER,
                             0,
                             g_backgroundItems[g_backgroundSelection].szFilename,
                             SPIF_UPDATEINIFILE);
    }
    else
    {   
        SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, TEXT(""), SPIF_UPDATEINIFILE);
    }
}

INT_PTR CALLBACK BackgroundPageProc(HWND hwndDlg,
                                    UINT uMsg,
                                    WPARAM wParam,
                                    LPARAM lParam)
{
    g_hBackgroundPage = hwndDlg;

    switch(uMsg)
    {
        case WM_INITDIALOG:
            {
                InitBackgroundDialog();
            } break;
        
        case WM_COMMAND:
            {              
                DWORD controlId = LOWORD(wParam);
                DWORD command   = HIWORD(wParam);
                
                switch(controlId)
                {
                    case IDC_COLOR_BUTTON:
                        {
                            if(command == BN_CLICKED)
                                OnColorButton();
                            
                        } break;
                    
                    case IDC_BROWSE_BUTTON:
                        {
                            if(command == BN_CLICKED)
                                OnBrowseButton();
                            
                        } break;
                    
                    case IDC_PLACEMENT_COMBO:
                        {
                            if(command == CBN_SELCHANGE)
                            {
                                g_placementSelection = SendMessage(g_hPlacementCombo, CB_GETCURSEL, 0, 0);
                                
                                InvalidateRect(g_hBackgroundPreview, NULL, TRUE);
                                
                                PropSheet_Changed(GetParent(g_hBackgroundPage), g_hBackgroundPage);
                            }
                            
                        } break;
                }
            } break;

        case WM_DRAWITEM:
            {
                LPDRAWITEMSTRUCT drawItem;
                drawItem = (LPDRAWITEMSTRUCT)lParam;

                if(drawItem->CtlID == IDC_BACKGROUND_PREVIEW)
                {
                    DrawBackgroundPreview(drawItem);
                }

            } break;
        
        case WM_NOTIFY:
            {
                LPNMHDR lpnm = (LPNMHDR)lParam;
                
                switch(lpnm->code)
                {   
                    case PSN_APPLY:
                        {
                            SetWallpaper();

                            return TRUE;
                        } break;

                    case LVN_ITEMCHANGED:
                        {
                            LPNMLISTVIEW nm = (LPNMLISTVIEW)lParam;
                            
                            if((nm->uNewState & LVIS_SELECTED) == 0)
                                return FALSE;
                            
                            ListViewItemChanged(nm->iItem);

                        } break;
                    
                    default:
                        break;
                }
                
            } break;

        case WM_DESTROY:
            {
                if(g_pWallpaperBitmap != NULL)
                    DibFreeImage(g_pWallpaperBitmap);

            } break;
    }
    
    return FALSE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -