001

#include <windows.h>

#pragma comment(lib,"gdiplus.lib")
#include <gdiplus.h>
using namespace Gdiplus;


/*

//画一条直线
VOID OnPaint(HDC hdc)
{
   Graphics graphics(hdc);
   Pen      pen(Color(255, 0, 0, 255));
   graphics.DrawLine(&pen, 45, 67, 200, 100);
}
*/


/*

//画字符串
VOID OnPaint(HDC hdc)
{
   Graphics    graphics(hdc);
   SolidBrush  brush(Color(255, 0, 0, 255));
   FontFamily  fontFamily(L"Times New Roman");
   Font        font(&fontFamily, 24, FontStyleRegular, UnitPixel);
   PointF      pointF(10.0f, 20.0f);
  
   graphics.DrawString(L"Hello World!", -1, &font, pointF, &brush);
}
*/

/*

//画矩形
VOID OnPaint(HDC hdc)
{
   Graphics graphics(hdc);
   Pen blackPen(Color(255, 0, 0, 0), 5);
   graphics.DrawRectangle(&blackPen, 10, 10, 100, 50);

}
*/

/*

//加载并显示图像
VOID OnPaint(HDC hdc)
{
   Graphics graphics(hdc);
Image image(L"D:\\13lhc-600.jpg");
graphics.DrawImage(&image, 20, 100);
}
*/

/*

//缩放并剪切图像
VOID OnPaint(HDC hdc)
{
Graphics graphics(hdc);
Image image(L"D:\\13lhc-600.jpg");
UINT width = image.GetWidth();
UINT height = image.GetHeight();
// Make the destination rectangle 30 percent wider and
// 30 percent taller than the original image.
// Put the upper-left corner of the destination
// rectangle at (150, 20).
Rect destinationRect(150, 20, 1.3 * width, 1.3 * height);
// Draw the image unaltered with its upper-left corner at (0, 0).
graphics.DrawImage(&image, 0, 0);
// Draw a portion of the image. Scale that portion of the image
// so that it fills the destination rectangle.
graphics.DrawImage(
&image,
destinationRect,
0, 0,              // upper-left corner of source rectangle
0.75 * width,      // width of source rectangle
0.75 * height,     // height of source rectangle
UnitPixel);
}
*/


//缩放时使用插值模式控制图像质量
VOID OnPaint(HDC hdc)
{
Graphics graphics(hdc);
Image image(L"D:\\13lhc-600.jpg");
UINT width = image.GetWidth();
UINT height = image.GetHeight();
// Draw the image with no shrinking or stretching.
graphics.DrawImage(
&image,
Rect(10, 10, width, height),  // destination rectangle 
0, 0,        // upper-left corner of source rectangle
width,       // width of source rectangle
height,      // height of source rectangle
UnitPixel);
// Shrink the image using low-quality interpolation.
graphics.SetInterpolationMode(InterpolationModeNearestNeighbor);
graphics.DrawImage(
&image,
Rect(10, 350, 0.6*width, 0.6*height),  // destination rectangle
0, 0,        // upper-left corner of source rectangle
width,       // width of source rectangle
height,      // height of source rectangle
UnitPixel);
// Shrink the image using medium-quality interpolation.
graphics.SetInterpolationMode(InterpolationModeHighQualityBilinear);
graphics.DrawImage(
&image,
Rect(250, 350, 0.6 * width, 0.6 * height),  // destination rectangle
0, 0,        // upper-left corner of source rectangle
width,       // width of source rectangle
height,      // height of source rectangle
UnitPixel);
// Shrink the image using high-quality interpolation.
graphics.SetInterpolationMode(InterpolationModeHighQualityBicubic);
graphics.DrawImage(
&image,
Rect(490, 350, 0.6 * width, 0.6 * height),  // destination rectangle
0, 0,        // upper-left corner of source rectangle
width,       // width of source rectangle
height,      // height of source rectangle
UnitPixel);


}



LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR, INT iCmdShow)
{
   HWND                hWnd;
   MSG                 msg;
   WNDCLASS            wndClass;
   GdiplusStartupInput gdiplusStartupInput;
   ULONG_PTR           gdiplusToken;
  
   // Initialize GDI+
   GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
  
   wndClass.style          = CS_HREDRAW | CS_VREDRAW;
   wndClass.lpfnWndProc    = WndProc;
   wndClass.cbClsExtra     = 0;
   wndClass.cbWndExtra     = 0;
   wndClass.hInstance      = hInstance;
   wndClass.hIcon          = LoadIcon(NULL, IDI_APPLICATION);
   wndClass.hCursor        = LoadCursor(NULL, IDC_ARROW);
   wndClass.hbrBackground  = (HBRUSH)GetStockObject(WHITE_BRUSH);
   wndClass.lpszMenuName   = NULL;
   wndClass.lpszClassName  = TEXT("GettingStarted");
  
   RegisterClass(&wndClass);

   hWnd = CreateWindow(
      TEXT("GettingStarted"),   // window class name
      TEXT("Getting Started"),  // window caption
      WS_OVERLAPPEDWINDOW,      // window style
      CW_USEDEFAULT,            // initial x position
      CW_USEDEFAULT,            // initial y position
      CW_USEDEFAULT,            // initial x size
      CW_USEDEFAULT,            // initial y size
      NULL,                     // parent window handle
      NULL,                     // window menu handle
      hInstance,                // program instance handle
      NULL);                    // creation parameters
 
   ShowWindow(hWnd, iCmdShow);
   UpdateWindow(hWnd);
  
   while(GetMessage(&msg, NULL, 0, 0))
   {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
   }
  
   GdiplusShutdown(gdiplusToken);
   return msg.wParam;
}  // WinMain




LRESULT CALLBACK WndProc(HWND hWnd, UINT message,
   WPARAM wParam, LPARAM lParam)
{
   HDC          hdc;
   PAINTSTRUCT  ps;
  
   switch(message)
   {
   case WM_PAINT:
      hdc = BeginPaint(hWnd, &ps);
      OnPaint(hdc);
      EndPaint(hWnd, &ps);
      return 0;
   case WM_DESTROY:
      PostQuitMessage(0);
      return 0;
   default:
      return DefWindowProc(hWnd, message, wParam, lParam);
   }
} // WndProc

你可能感兴趣的:(01)