2012-11-02 58 views
0

我想知道是否有一種方法將jpg打印到圖片控件矩形上(即使用ResEdit構建),應該打印圖片的動作爲IDC_BUTTON1:case針對我要觀看的圖像與ID的畫面控制:IDC_STATIC的win32 API在圖片控件C++中打開jpg

BOOL CALLBACK AppDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) 
{ 
    switch(uMsg) 
    { 

    case WM_INITDIALOG: 
     DragAcceptFiles(hDlg,true); 
    SetClassLongPtr(hDlg, GCLP_HICON, (long)LoadIcon(0, IDI_APPLICATION)); 
    return 1; 
    case WM_COMMAND: 
    switch(wParam) 
    { 
    case IDOK: 
    return 0; 
case IDCANCEL: 
    EndDialog(hDlg, 0); 
} 
switch(wParam) 
     { 
      case IDC_BUTTON1: 
       ShellExecute(hDlg, 
     "open", 
     "C:\immagine1.jpg", 
     NULL, 
     NULL, 
     SW_SHOWDEFAULT); 
      break; 
     } 

    switch(wParam) 
     { 
      case IDC_BUTTON4: 
       ShellExecute(hDlg, 
     "open", 
     "C:\log.txt", 
     NULL, 
     NULL, 
     SW_SHOWDEFAULT); 
      break; 
     } 






    } 
    return 0; 
} 

,而不是使用shell執行打開默認的瀏覽器謝謝大家

+1

的[OleLoadPicturePath](http://msdn.microsoft.com/en-us/library/windows/desktop/ms678485%28v=vs.85%29 .aspx)API函數可以加載一個JPEG文件。那麼這只是一個訪問位的問題。哦,它涉及到一些「閱讀文檔」,我沒有時間在這裏充實細節。我也不再爲此編碼。我只記得大約10年左右的時間。還有[Windows映像組件](http://msdn.microsoft.com/en-us/library/ee719902%28v=VS.85%29.aspx)API,但我沒有使用過。我懷疑它也可以,但是,一旦我加載了圖像,它可能會出現 –

+0

如何將消息發送到圖片控件? 我已經看到加載一個BMP的程序,沒有任何JPG格式... – Lorenzo

+0

嗯,讓我試試。可能需要一段時間。不是連續的空閒時間 –

回答

0

OleLoadPicturePath API函數可以加載一個JPEG文件。

然後它只是訪問位的問題。

還有Windows Imaging Component API,但我沒有使用過。

雖然我懷疑它也有效,但它可能比處理OLE的東西更簡單,但在這裏我例舉了OleLoadPicturePath

之前試圖適應下面的代碼,你應該:

  • 確保畫面控制資源的類型設置爲位圖(實質上,在.RC文本層面,它有SS_BITMAP風格)。

  • 將ID更改爲獨特的東西,而不是IDC_STATIC

enter image description here

#include <header_wrapper/olectl_h.h>  // IPicture 
#include <header_wrapper/windows_h.h> 
#include "resource.h"  // IDD_DEMO_DIALOG, IDC_PICTURE 

#include <progrock/cppx/throwx.h>   // hopefully, throwX, std::exception 
#include <progrock/cppx/size.h>    // size 
#include <progrock/winapi/path.h>   // * 
#include <progrock/winapi/ComPointer.h>  // ComPointer 
using namespace progrock; 

#include <assert.h>   // assert 
#include <iostream> 
#include <stdlib.h>   // EXIT_FAILURE, EXIT_SUCCESS 
using namespace std; 

using cppx::hopefully; 
using cppx::size; 
using cppx::throwX; 

using winapi::String; 

struct IsHrSuccess 
{ 
    friend bool operator>>(HRESULT const hr, IsHrSuccess const&) 
    { 
     return SUCCEEDED(hr); 
    } 
}; 

IsHrSuccess const isHrSuccess = IsHrSuccess(); 

short kindOf(IPicture const& pic) 
{ 
    short kind = 0; 
    const_cast< IPicture& >(pic).get_Type(&kind) 
     >> isHrSuccess || throwX("kindOf: IPicture::get_Type failed"); 
    return kind; 
} 

bool isBitmap(IPicture const& pic) 
{ 
    return (kindOf(pic) == PICTYPE_BITMAP); 
} 

OLE_HANDLE handleOf(IPicture const& pic) 
{ 
    OLE_HANDLE result = 0; 
    const_cast< IPicture& >(pic).get_Handle(&result) 
     >> isHrSuccess || throwX("handleOf: IPicture::get_Handle failed"); 
    return result; 
} 

HBITMAP bmpHandle(IPicture const& pic) 
{ 
    assert(isBitmap(pic)); 
    return reinterpret_cast<HBITMAP>(handleOf(pic)); 
} 

namespace g { 
    winapi::ComPointer<IPicture> pPicture; 
} // namespace g 

INT_PTR CALLBACK demoDialogProc(
    HWND const  window, 
    UINT const  messageId, 
    WPARAM const wParam, 
    LPARAM const lParam 
    ) 
{ 
    switch(messageId) 
    { 
    case WM_INITDIALOG: 
     ::SendDlgItemMessage(
      window, IDC_PICTURE, STM_SETIMAGE, 
      IMAGE_BITMAP, 
      reinterpret_cast<LPARAM>(bmpHandle(*g::pPicture)) 
      ); 
     break; 
    case WM_CLOSE: 
     ::EndDialog(window, IDCANCEL); 
     break; 
    case WM_COMMAND: 
     ::EndDialog(window, wParam); 
     break; 
    } 
    return 0; 
} 

struct Com 
{ 
    Com() { ::CoInitialize(0) >> isHrSuccess || throwX("::CoInitialize failed"); } 
    ~Com() { ::CoUninitialize(); } 
}; 

void cppMain() 
{ 
    Com usingCom; 
    String const picFileName  = L"image.jpg"; 
    String const picFilePath  = winapi::path::combine(winapi::exeFolder(), picFileName); 

    ::OleLoadPicturePath(
     const_cast< wchar_t* >(picFilePath.c_str()), 
     nullptr, 0, 0, 
     IID_IPicture, 
     g::pPicture.asOutArgument() 
     ) 
     >> isHrSuccess || throwX("OleLoadPicturePath failed"); 
    assert(isBitmap(*g::pPicture)); 

    ::DialogBox(::GetModuleHandle(0), MAKEINTRESOURCE(IDD_DEMO_DIALOG), 0, demoDialogProc); 
} 

int main() 
{ 
    try 
    { 
     cppMain(); 
     return EXIT_SUCCESS; 
    } 
    catch(exception const& x) 
    { 
     wcout << "!" << x.what() << endl; 
    } 
    return EXIT_FAILURE; 
}