繁体   English   中英

Win32 API在图片控件中打开jpg C ++

[英]win32 API open a jpg in a picture control c++

我想知道是否有一种方法可以将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 execute打开默认查看器,谢谢大家

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

然后,只需访问这些位。

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

我怀疑它也可以工作,并且它可能比处理OLE更简单,但是在这里我以OleLoadPicturePath

在尝试修改以下代码之前,您应该:

  • 确保将图片控制资源的类型设置为BITMAP(基本上,在.rc文本级别上,它具有SS_BITMAP样式)。

  • 将ID更改为唯一的名称,而不是IDC_STATIC

在此处输入图片说明

#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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM