⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cpadimage.cpp

📁 Synaptics触摸板应用开发包(SDK)
💻 CPP
字号:
// cPadImage.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"

#include "commdlg.h"

// All programs using the Synaptics COM SDK include SynKit.h
#include "SynKit.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;                      // current instance
SynAPI g_synAPI;
SynDisplay g_synDisplay;
TCHAR g_szImageFile[MAX_PATH] = {0};
HBITMAP g_hImage = NULL;
BITMAP g_bmpBuf;
BOOL g_bDoStretch = FALSE;
BOOL g_bDoColor = FALSE;

TCHAR szTitle[MAX_LOADSTRING];        // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];  // The title bar text

// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK Preference(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
// Function of drawing a bitmap onto cPad
void DrawImage();

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
  if (SYN_OK != g_synAPI->Initialize())
  {
    MessageBox(NULL, "Cannot initialize SynCom.dll.", "Error", MB_ICONSTOP);
    return FALSE;
  }

  // Find a cPad device
  long lHandle = -1;
  if (SYN_OK != g_synAPI->FindDevice(SE_ConnectionAny, SE_DevicecPad, &lHandle) ||
    -1 == lHandle ||
    SYN_OK != g_synDisplay->Select(lHandle) ||
    SYN_OK != g_synDisplay->Acquire(SE_AcquireExclusive))
  {
    MessageBox(NULL, "Cannot acquire cPad display.", "Error", MB_ICONSTOP);
    return FALSE;
  }

  MSG msg;
  HACCEL hAccelTable;

  // Initialize global strings
  LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  LoadString(hInstance, IDC_CPADIMAGE, szWindowClass, MAX_LOADSTRING);
  MyRegisterClass(hInstance);

  // Perform application initialization:
  if (!InitInstance (hInstance, nCmdShow)) 
  {
    return FALSE;
  }

  hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_CPADIMAGE);

  // Main message loop:
  while (GetMessage(&msg, NULL, 0, 0)) 
  {
    if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
    {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
    }
  }

  return msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage is only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
  WNDCLASSEX wcex;

  wcex.cbSize = sizeof(WNDCLASSEX); 

  wcex.style          = CS_HREDRAW | CS_VREDRAW;
  wcex.lpfnWndProc    = (WNDPROC)WndProc;
  wcex.cbClsExtra     = 0;
  wcex.cbWndExtra     = 0;
  wcex.hInstance      = hInstance;
  wcex.hIcon          = LoadIcon(hInstance, (LPCTSTR)IDI_CPADIMAGE);
  wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
  wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
  wcex.lpszMenuName   = (LPCSTR)IDC_CPADIMAGE;
  wcex.lpszClassName  = szWindowClass;
  wcex.hIconSm        = NULL;

  return RegisterClassEx(&wcex);
}

//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
//     In this function, we save the instance handle in a global variable and
//     create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
  HWND hWnd;

  hInst = hInstance; // Store instance handle in our global variable

  hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, 0, 400, 400, NULL, NULL, hInstance, NULL);

  if (!hWnd)
  {
    return FALSE;
  }

  ShowWindow(hWnd, nCmdShow);
  UpdateWindow(hWnd);

  return TRUE;
}

//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE:  Processes messages for the main window.
//
// WM_COMMAND  - process the application menu
// WM_PAINT    - Paint the main window
// WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  int wmId, wmEvent;
  PAINTSTRUCT ps;
  HDC hdc;

  switch (message) 
  {
  case WM_COMMAND:
    wmId    = LOWORD(wParam); 
    wmEvent = HIWORD(wParam); 
    // Parse the menu selections:
    switch (wmId)
    {
      case IDM_ABOUT:
        DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
        break;
      case IDM_EXIT:
        if (g_hImage)
        {
          DeleteObject(g_hImage);
        }
        DestroyWindow(hWnd);
        break;
      case ID_FILE_LOADIMAGE:
        {
          OPENFILENAME ofn;       // common dialog box structure

          // Initialize OPENFILENAME
          ZeroMemory(&ofn, sizeof(OPENFILENAME));
          ofn.lStructSize = sizeof(OPENFILENAME);
          ofn.hwndOwner = hWnd;
          ofn.lpstrFile = g_szImageFile;
          ofn.nMaxFile = sizeof(g_szImageFile);
          ofn.lpstrFilter = "Bitmap (*.bmp)\0*.bmp\0";
          ofn.nFilterIndex = 1;
          ofn.lpstrFileTitle = NULL;
          ofn.nMaxFileTitle = 0;
          ofn.lpstrInitialDir = NULL;
          ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

          // Display the Open dialog box. 

          if (GetOpenFileName(&ofn))
          {
            DrawImage();
            InvalidateRect(hWnd, NULL, TRUE);
          }
        }
        break;
      case ID_FILE_PREFERENCE:
        if (IDOK == DialogBox(hInst, (LPCTSTR)IDD_PREFERENCE,
          hWnd, (DLGPROC)Preference))
        {
          DrawImage();
          InvalidateRect(hWnd, NULL, TRUE);
        }
        break;
      default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    break;
  case WM_PAINT:
    hdc = BeginPaint(hWnd, &ps);
    // TODO: Add any drawing code here...
    RECT rt;
    GetClientRect(hWnd, &rt);
    // empty the display first
    DrawText(hdc, " ", 1, &rt, DT_CENTER);
    if (g_hImage)
    {
      HDC hdcSrc;
      HBITMAP hbmOldSrc;
      hdcSrc = CreateCompatibleDC(hdc);
      hbmOldSrc = (HBITMAP) SelectObject(hdcSrc, g_hImage);
      int x = 0;
      int y = 0;
      if (g_bmpBuf.bmWidth < rt.right)
      {
        x = (rt.right - g_bmpBuf.bmWidth) / 2;
      }
      if (g_bmpBuf.bmHeight < rt.bottom)
      {
        y = (rt.bottom - g_bmpBuf.bmHeight) / 2;
      }
      BitBlt(hdc, x, y, g_bmpBuf.bmWidth, g_bmpBuf.bmHeight, hdcSrc, 0, 0, SRCCOPY);
      SelectObject(hdcSrc, hbmOldSrc);
      DeleteDC(hdcSrc);
    }
    EndPaint(hWnd, &ps);
    break;
  case WM_DESTROY:
    PostQuitMessage(0);
    break;
  default:
    return DefWindowProc(hWnd, message, wParam, lParam);
  }
  return 0;
}

// Mesage handler for Preference.
LRESULT CALLBACK Preference(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
  switch (message)
  {
  case WM_INITDIALOG:
    SendDlgItemMessage(hDlg, g_bDoStretch? IDC_SIZE1 : IDC_SIZE2,
      BM_SETCHECK, BST_CHECKED, 0);
    SendDlgItemMessage(hDlg, g_bDoColor? IDC_COLOR1 : IDC_COLOR2,
      BM_SETCHECK, BST_CHECKED, 0);
    return TRUE;

  case WM_COMMAND:
    if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
    {
      if (LOWORD(wParam) == IDOK)
      {
        g_bDoStretch = SendDlgItemMessage(hDlg, IDC_SIZE1, BM_GETSTATE, 0, 0); 
        g_bDoColor = SendDlgItemMessage(hDlg, IDC_COLOR1, BM_GETSTATE, 0, 0); 
      }
      EndDialog(hDlg, LOWORD(wParam));
      return TRUE;
    }
    break;
  }
  return FALSE;
}

// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
  switch (message)
  {
  case WM_INITDIALOG:
    return TRUE;

  case WM_COMMAND:
    if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
    {
      EndDialog(hDlg, LOWORD(wParam));
      return TRUE;
    }
    break;
  }
  return FALSE;
}

// See this function how to draw a bitmap file onto cPad display
void DrawImage()
{
  // load bitmap from an image file
  DeleteObject(g_hImage);
  g_hImage = (HBITMAP) ::LoadImage(NULL, g_szImageFile, IMAGE_BITMAP,
    0, 0, LR_LOADFROMFILE | (g_bDoColor? 0 : LR_MONOCHROME));

  // get cPad display properties
  HDC hdc;
  g_synDisplay->GetDC(&hdc);
  long w = g_synDisplay.GetLongProperty(SP_DisplayColumns);
  long h = g_synDisplay.GetLongProperty(SP_DisplayRows);
  // clear display area
  RECT rc = { 0, 0, w + 1, h + 1 };
  FillRect(hdc, &rc, (HBRUSH)GetStockObject(WHITE_BRUSH));

  // draw image to the cPad device context
  if (g_hImage)
  {
    GetObject(g_hImage, sizeof(g_bmpBuf), &g_bmpBuf);
    HDC hMemDC = CreateCompatibleDC(hdc);
    HBITMAP hOldBmp = (HBITMAP) SelectObject(hMemDC, g_hImage);
    if (g_bDoStretch)
    {
      StretchBlt(hdc, 0, 0, w, h, hMemDC, 0, 0,
        g_bmpBuf.bmWidth, g_bmpBuf.bmHeight, SRCCOPY);
    }
    else
    {
      int x = 0;
      int y = 0;
      if (g_bmpBuf.bmWidth < w)
      {
        x = (w - g_bmpBuf.bmWidth) / 2;
      }
      if (g_bmpBuf.bmHeight < h)
      {
        y = (h - g_bmpBuf.bmHeight) / 2;
      }
      BitBlt(hdc, x, y, w, h, hMemDC, 0, 0, SRCCOPY);
    }
    SelectObject(hMemDC, hOldBmp);
    DeleteDC(hMemDC);

    // turn on backlight for 3 seconds
    g_synDisplay->SetProperty(SP_BackLightOnOffOnce, 3000);
  }

  // flush the display synchronously
  g_synDisplay->FlushDC(SE_FlushSynchronous);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -