一派掌门 二十级              | 
          
            
            
             
              【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; }              
             |