2

我想知道是否有办法将 jpg 打印到图片控制矩形(我用 ResEdit 构建)应该打印图像的操作是 case 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执行谢谢大家

4

1 回答 1

2

OleLoadPicturePathAPI 函数可以加载 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;
}
于 2012-11-02T20:00:12.903 回答