ホームに戻る
 デバイスコンテキスト

#include <windows.h>
#include <stdio.h>

// ウィンドウプロシージャの宣言(ウィンドウの動作を規定)
LRESULT CALLBACK WndProc(
  HWND hwnd,    //ウィンドウハンドル(void型ポインタ)
  UINT message,    //メッセージ識別番号(unsigned int)
  WPARAM wParam,    //(32ビットlong)
  LPARAM lParam);    //(32ビットlong)

//メイン関数
int APIENTRY WinMain(
  HINSTANCE hInstance,  //インスタンスのハンドル
  HINSTANCE hPreInstance, //以前のインスタンスハンドル
  LPSTR cmdLine,  //コマンド行の文字列
  int cmdShow)    //ウィンドウの表示状態
{
  //ウィンドウクラスの宣言
  WNDCLASS wd;

  //ウィンドウクラス名
  char *wdName = "window00";

  //ウィンドウ登録のためのループ
  if (!hPreInstance) {
    //ウィンドウスタイル
    wd.style = CS_HREDRAW | CS_VREDRAW;  //幅、高さの変化に対し再描画する
    //ウィンドウプロシージャ
    wd.lpfnWndProc = (WNDPROC)WndProc;
    //クラスの予備のメモリ領域
    wd.cbClsExtra = 0;
    //ウィンドウの予備のメモリ領域
    wd.cbWndExtra = 0;
    //プログラムのインスタンスハンドル
    wd.hInstance = hInstance;
    //アイコンのハンドル
    wd.hIcon = NULL;
    //カーソルのハンドル
    wd.hCursor = LoadCursor(NULL, IDC_ARROW);
    //バックグラウンドのブラシのハンドル
    wd.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    //メニュー名
    wd.lpszMenuName = NULL;
    //クラス名
    wd.lpszClassName = wdName;

    //ウィンドウの登録(成功 非0、失敗 0)
    if (!RegisterClass(&wd))
      return FALSE;
  }

  //ウィンドウの生成(ハンドルを form00 とする)
  HWND form00 = CreateWindowEx(
    NULL,      //拡張スタイル(なし)
    wdName,      //ウィンドウクラス名
    "window",    //ウィンドウの名前
    WS_OVERLAPPEDWINDOW,  //ウィンドウスタイル(オーバーラップ型)
    CW_USEDEFAULT,    //ウィンドウのX座標(ディフォルト)
    CW_USEDEFAULT,    //ウィンドウのY座標(ディフォルト)
    200,      //ウィンドウの幅(80に設定)
    100,      //ウィンドウの高さ(50に設定)
    NULL,      //親ウィンドウ(この場合自身が親)
    NULL,      //メニュー(なし)
    hInstance,    //プログラムのハンドル
    NULL);      //予備パラメータ

  //ウィンドウの表示
  ShowWindow(form00, cmdShow);

  //ウィンドウの更新
  UpdateWindow(form00);

  //イベントのループ
  MSG msg;
  while (GetMessage(&msg, NULL, NULL, NULL))
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
  return msg.wParam;
}

// ウィンドウプロシージャ(ウィンドウの動作を規定)
LRESULT CALLBACK WndProc(
  HWND hwnd,
  UINT message,
  WPARAM wParam,
  LPARAM lParam)
  {
  static HDC hdc_display;
  static HDC hdc_window;
  int display_Width;
  int display_Height;
  RECT window_Rect;
  int window_Width;
  int window_Height;
  int xPos, yPos;
  int c_ref;
  int r, g, b;
  char s[256];
  char *zahyou = "r:%d g:%d b:%d";
  //インスタンスハンドルを得る
  HINSTANCE hInst;
  hInst = (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE);

  switch (message) {
  //ウィンドウを表示するとき処理
    case WM_CREATE:
      hdc_display = CreateDC("DISPLAY" , NULL , NULL , NULL);
      hdc_window = GetDC(hwnd);
      break;
    case WM_LBUTTONDOWN:
      xPos = LOWORD(lParam);
      yPos = HIWORD(lParam);
      if((c_ref = GetPixel(hdc_window, xPos, yPos)) != CLR_INVALID){
        r = c_ref & 0x000000FF;
        g = (c_ref >> 8) & 0x000000FF;
        b = (c_ref >> 16) & 0x000000FF;
        sprintf(s, zahyou, r, g, b);
        MessageBox(hwnd, 
          (LPCSTR)s, 
          (LPCSTR)"color",
          MB_OK);
      }
      break;
    case WM_SIZE:
      display_Width = GetDeviceCaps(hdc_display, HORZRES);
      display_Height = GetDeviceCaps(hdc_display, VERTRES);
      GetClientRect(hwnd, &window_Rect);
      window_Width = window_Rect.right;
      window_Height = window_Rect.bottom;
      //BitBlt(hdc_window, 0, 0, window_Width, window_Height, hdc_display, 0, 0, SRCCOPY);
      StretchBlt(hdc_window, 0, 0, window_Width, window_Height, hdc_display, 0, 0, display_Width, display_Height, SRCCOPY);
      break;
  //ウィンドウを破壊する時の処理
    case WM_DESTROY:
      ReleaseDC(hwnd, hdc_window);
      DeleteDC(hdc_display);
      //メイン関数のループを終了
      PostQuitMessage(0);
      break;

  //デフォルトの処理に対してデフォルトの処理を返す
    default:
      return DefWindowProc(hwnd, message, wParam, lParam);
  }
  return NULL;
}

inserted by FC2 system