win32 API在图片控件c ++中打开一个jpg

时间:2012-11-02 09:41:57

标签: c++ api winapi jpeg

我想知道是否有办法将jpg打印到图片控制矩形(我使用ResEdit构建)上应该打印图像的操作是IDC_BUTTON1:并且我想要查看图像的目标是图片控件的ID:IDC_STATIC

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 个答案:

答案 0 :(得分:1)

OleLoadPicturePath API函数可以加载JPEG文件。

然后只是访问位。

还有Windows Imaging Component API,但我没有使用它。

我怀疑它也有效,但它可能比处理OLE内容更简单,但在这里我举例说明OleLoadPicturePath

在尝试调整下面的代码之前,您应该:

  • 确保图片控件资源的类型设置为BITMAP(基本上,在.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;
}