 |
【C++代碼】 #include <tchar.h> #include <Windows.h> #include <CommCtrl.h> #include <gdiplus.h> #include <winhttp.h> #include "resource.h" // 資源頭文件
// 啟用XP風格 #pragma comment(lib, "Comctl32.lib") #pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#pragma comment(lib, "gdiplus.lib") #pragma comment(lib, "winhttp.lib") using namespace Gdiplus;
HBITMAP hbmp; // 位圖句柄 TCHAR szBmpText[30]; // 提示框的內容
// 加載圖像 void LoadPicture(void) { // 連接伺服器 HINTERNET hSession = WinHttpOpen(L"My Test", NULL, NULL, NULL, NULL); HINTERNET hConnect = WinHttpConnect(hSession, L"zh.arslanbar.net", INTERNET_DEFAULT_HTTP_PORT, NULL); HINTERNET hRequest = WinHttpOpenRequest(hConnect, L"GET", L"Files/TopicImages/2012-7/8_2012-7-13_121400_609-318482.jpg", NULL, NULL, NULL, WINHTTP_FLAG_REFRESH); WinHttpSendRequest(hRequest, NULL, NULL, NULL, NULL, NULL, NULL); WinHttpReceiveResponse(hRequest, NULL);
// 下載圖像到內存中 HGLOBAL hMem = NULL; char *content = NULL; DWORD dwSize; DWORD dwRead = 0; DWORD dwBufSize = 0; while (WinHttpQueryDataAvailable(hRequest, &dwSize), dwSize > 0) { if (hMem == NULL) hMem = GlobalAlloc(GMEM_MOVEABLE, dwSize); else hMem = GlobalReAlloc(hMem, dwBufSize + dwSize, GMEM_MOVEABLE); content = (char *)GlobalLock(hMem); WinHttpReadData(hRequest, content + dwBufSize, dwSize, &dwRead); GlobalUnlock(hMem); dwBufSize += dwSize; } WinHttpCloseHandle(hRequest); WinHttpCloseHandle(hConnect); WinHttpCloseHandle(hSession);
// 轉換為Stream對象並創建位圖對象 IStream *is; CreateStreamOnHGlobal(hMem, TRUE, &is); Bitmap bmp(is); is->Release();
// 獲得位圖句柄 bmp.GetHBITMAP(Color::White, &hbmp); _stprintf_s(szBmpText, TEXT("Size: %ux%u"), bmp.GetWidth(), bmp.GetHeight()); }
// 創建提示框 void CreateTooltip(HWND hDlg) { HINSTANCE hInst = (HINSTANCE)GetWindowLongPtr(hDlg, GWLP_HINSTANCE); HWND hwndTip = CreateWindow(TOOLTIPS_CLASS, NULL, WS_POPUP | TTS_ALWAYSTIP, 0, 0, 0, 0, hDlg, NULL, hInst, NULL);
TOOLINFO ti; ZeroMemory(&ti, sizeof(ti)); ti.cbSize = sizeof(TOOLINFO); ti.hwnd = hDlg; ti.hinst = hInst; ti.lpszText = szBmpText; ti.uFlags = TTF_SUBCLASS;
// 獲取圖像控件相對於對話框的矩形區域 POINT pt; HWND hwndStatic = GetDlgItem(hDlg, IDC_BMP); GetWindowRect(hwndStatic, &ti.rect); pt.x = ti.rect.left; pt.y = ti.rect.top; ScreenToClient(hDlg, &pt); GetClientRect(hwndStatic, &ti.rect); OffsetRect(&ti.rect, pt.x, pt.y);
SendMessage(hwndTip, TTM_ADDTOOL, NULL, (LPARAM)&ti); }
INT_PTR CALLBACK DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) { int wmId; switch (uMsg) { case WM_INITDIALOG: SendMessage(hDlg, WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(NULL, IDI_APPLICATION)); // 設置對話框圖標 LoadPicture(); // 當對話框創建時加載圖像 SendDlgItemMessage(hDlg, IDC_BMP, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hbmp); CreateTooltip(hDlg); break; case WM_COMMAND: wmId = LOWORD(wParam); switch (wmId) { case IDOK: case IDCANCEL: DestroyWindow(hDlg); break; } break; case WM_DESTROY: DeleteObject(hbmp); // 當對話框關閉時刪除位圖句柄 PostQuitMessage(0); // 退出消息循環 break; } return FALSE; }
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { // 初始化GDI+ GdiplusStartupInput gdiplusStartupInput; ULONG_PTR gdiplusToken; GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
// 創建並顯示對話框 HWND hDlg = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, DlgProc); ShowWindow(hDlg, nCmdShow);
// 消息循環 MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); }
GdiplusShutdown(gdiplusToken); return msg.wParam; }
|
 |
 顯示半透明的png圖片: HINTERNET hConnect = WinHttpConnect(hSession, L"assets.onestore.ms", INTERNET_DEFAULT_HTTP_PORT, NULL); HINTERNET hRequest = WinHttpOpenRequest(hConnect, L"GET", L"cdnfiles/onestorerolling-1602-26000/shell/v3/images/logo/microsoft.png", NULL, NULL, NULL, WINHTTP_FLAG_REFRESH);
|