参考原型:
微软的loadpic.exe,
下载地址:http://support.microsoft.com/kb/218972

我能做到的最小的LoadPic:

#include <windows.h>
#include <olectl.h>

LPPICTURE      gpPicture;
HWND        hWnd;
HDC          hDC;
long        hmWidth,hmHeight;
int          SrcX,SrcY;

LRESULT CALLBACK MainWndProc(HWND, UINT,WPARAM, LPARAM);
void LoadPictureFile(char* point);

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{

  MSG      msg;
  WNDCLASS  wndclass;

  wndclass.style = 0;
  wndclass.cbClsExtra = 0;
  wndclass.cbWndExtra = 0;
  wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  wndclass.lpszMenuName =  NULL;
    hInstance=GetModuleHandle(NULL);
  wndclass.hInstance = hInstance;
  wndclass.hIcon = NULL;
  wndclass.lpfnWndProc = MainWndProc;
  wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW);
  wndclass.lpszClassName = "MyWindowClass";
  RegisterClass (&wndclass);
  hWnd = CreateWindow ("MyWindowClass","LoadPic",
             WS_MAXIMIZEBOX|WS_MINIMIZEBOX|WS_SYSMENU|WS_SIZEBOX,
             CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL,NULL,hInstance,NULL);
  hDC = GetDC(hWnd);
  ShowWindow(hWnd, SW_SHOWNORMAL);
  while (GetMessage(&msg, NULL, 0, 0)) 
  {
    DispatchMessage(&msg);
  }
  return 0;
}

void LoadPictureFile(char* point)
{
  char    szPath[255];
  OLECHAR    wszPath[255];
  int      i,j;
  
  if (gpPicture)
  {
    gpPicture->Release();
  }
  i=0;
  j=0;
  do
  {
    szPath[j]=point[i];
    if(szPath[j]=='\\')
    {
      j++;
      szPath[j]='\\';
    }
    i++;
    j++;
  }while(point[i-1]!='\0');
  MultiByteToWideChar(CP_ACP, 0, szPath, -1, wszPath, 255);  
  OleLoadPicturePath(wszPath, 0, 0, 0, IID_IPicture, (LPVOID *)&gpPicture);
  gpPicture->get_Width(&hmWidth);
  gpPicture->get_Height(&hmHeight);
  SrcX=hmWidth*GetDeviceCaps(hDC, LOGPIXELSX)/2540;
  SrcY=hmHeight*GetDeviceCaps(hDC, LOGPIXELSY)/2540;
  InvalidateRect(hWnd,NULL,1);
}

LRESULT CALLBACK MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  PAINTSTRUCT    ps;
  OPENFILENAME  ofn;
    char      szFileName[255];
  
  switch (message) 
  {
    case WM_RBUTTONUP:
      szFileName[0]='\0';
      ofn.lStructSize=sizeof(OPENFILENAME);
      ofn.hwndOwner=hWnd;
      ofn.hInstance=0;
      ofn.lpstrFilter="JPG Files (*.jpg)\0*.jpg\0";
      ofn.lpstrCustomFilter=0;
      ofn.nMaxCustFilter=0;
      ofn.nFilterIndex=0;
      ofn.lpstrFile=szFileName;
      ofn.nMaxFile=MAX_PATH;  
      ofn.lpstrFileTitle=0;
      ofn.nMaxFileTitle=MAX_PATH;      
      ofn.lpstrInitialDir=0;
      ofn.lpstrTitle=0;
      ofn.Flags=OFN_HIDEREADONLY|OFN_PATHMUSTEXIST;
      ofn.nFileOffset=0;
      ofn.nFileExtension=0;
      ofn.lpstrDefExt=NULL;
      ofn.lCustData=0;
      ofn.lpfnHook=0;
      ofn.lpTemplateName=0;
      if (GetOpenFileName(&ofn)) 
      {
        LoadPictureFile(szFileName);
      }
      break;
    case WM_PAINT:
      BeginPaint(hWnd, &ps);
      if (gpPicture)
      {
        gpPicture->Render(hDC, 0, 0, SrcX, SrcY, 0, hmHeight, hmWidth, -hmHeight, NULL);
      }
      EndPaint(hWnd, &ps);
      break;
    case WM_DESTROY:
      ExitProcess(0);  
      break;
    default:
      return DefWindowProc (hWnd, message, wParam, lParam);
   }
   return 0;
}

VC6编译后大小:2032字节,连2K都不到。

源程序下载:

上传的附件 LoadPic.zip

  • 标 题:答复
  • 作 者:xulay
  • 时 间:2011-07-18 11:28:13

几点说明:

1:link选项:
/entry:"WinMain" 
/align:16

2: 用CreateWindow,不用CreateWindowEx

3:消息循环:
  while (GetMessage(&msg, NULL, 0, 0)) 
  {
    DispatchMessage(&msg);
  }
配合
case WM_DESTROY:
      ExitProcess(0);

4:用OleLoadPicturePath,不用OleLoadPicture

5:目录单斜杠\转双斜杠\\:
  i=0;
  j=0;
  do
  {
    szPath[j]=point[i];
    if(szPath[j]=='\\')
    {
      j++;
      szPath[j]='\\';
    }
    i++;
    j++;
  }while(point[i-1]!='\0');

  • 标 题:答复
  • 作 者:xulay
  • 时 间:2011-07-19 09:06:04

增加以下功能:

1:启动画面不用默认,使用黑色背景,400*300大小,居中显示
  wndclass.hbrBackground = (HBRUSH)(GetStockObject (BLACK_BRUSH));

  dl=(GetSystemMetrics(SM_CXFULLSCREEN)-400)/2;
  dt=(GetSystemMetrics(SM_CYFULLSCREEN)-300)/2;
  dx=400;
  dy=300;
  hWnd = CreateWindow ("MyWindowClass","LoadPic",
             WS_MAXIMIZEBOX|WS_MINIMIZEBOX|WS_SYSMENU|WS_SIZEBOX,
             dl,dt,dx,dy,NULL,NULL,hInstance,NULL);
2:支持拖放
  DragAcceptFiles(hWnd,TRUE);


      if (!GetOpenFileName(&ofn)) 
      {
        break;
      }
    case WM_DROPFILES:
        if(message==WM_DROPFILES)
      {
        DragQueryFile((HDROP)wParam, 0, szFileName, 255);
      }
      LoadPictureFile(szFileName);
      break;
3:全图显示
  sx=SrcX;
  sy=SrcY;
  dx=GetSystemMetrics(SM_CXFULLSCREEN);
  dy=GetSystemMetrics(SM_CYFULLSCREEN);
  while((sx>dx)&&(sy>dy))
  {
    sx=sx>>1;
    sy=sy>>1;
  }
  dl=-4;
  dt=-4;
  if(dx>sx)
  {
    dl=(dx-sx-8)>>1;
    dx=  sx;
  }
  if(dy>sy)
  {
    dt=(dy-sy)>>1;
    dy=sy;
  }
  dx=dx+8;
  dy=dy+27;
  EndDeferWindowPos(DeferWindowPos(BeginDeferWindowPos(1),hWnd,HWND_TOP,dl,dt,dx,dy,SWP_NOZORDER));


gpPicture->Render(hDC, 0, 0, sx, sy, 0, hmHeight, hmWidth, -hmHeight, NULL);

4:窗口大小限制
    case WM_GETMINMAXINFO:
      ((LPMINMAXINFO)lParam)->ptMaxPosition.x=dl;
      ((LPMINMAXINFO)lParam)->ptMaxPosition.y=dt;
      ((LPMINMAXINFO)lParam)->ptMaxSize.x=dx;
      ((LPMINMAXINFO)lParam)->ptMaxSize.y=dy;
      ((LPMINMAXINFO)lParam)->ptMaxTrackSize.x=dx;
      ((LPMINMAXINFO)lParam)->ptMaxTrackSize.y=dy;
      break;

显示效果图:


编译后大小:2688字节
下载:LoadPic.zip

  • 标 题:答复
  • 作 者:xulay
  • 时 间:2011-08-04 09:38:20

继续增加:

5:命令行支持:
  p=GetCommandLine();
  if(*p=='"')
  {
    do
    {
      p++;
    }
    while(*p!='"');
  }
    do
  {
    p++;
  }
  while((*p!='\0')&&(*p!=' '));
  p--;
  do
  {
    p++;
  }
  while(*p==' ');
  if(*p=='"')
  {
    q=p;
    p++;
    *q=' ';
    do
    {
      q++;
    }
    while(*q!='"');
    *q='\0';
  }
  if(*p!='\0')
  {
    LoadPictureFile(p);
  }

6:添加图标
选用acdsee32经典图标

有了以上2步,可以文件关联了:
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\.JPG]
@="JPGFile"

[HKEY_CLASSES_ROOT\JPGFile]
@="JPG Image File"

[HKEY_CLASSES_ROOT\JPGFile\DefaultIcon]
@="LoadPic.exe,1"

[HKEY_CLASSES_ROOT\JPGFile\Shell]
@="Open"

[HKEY_CLASSES_ROOT\JPGFile\Shell\Open\Command]
@="LoadPic.exe %l"

替代系统的jpg文件浏览器,效果如何?

编译后大小:4.90k(5024字节)

源码下载:LoadPic.zip