Skip to content

Instantly share code, notes, and snippets.

@hirosof
Created February 9, 2013 11:36
Show Gist options
  • Select an option

  • Save hirosof/4744962 to your computer and use it in GitHub Desktop.

Select an option

Save hirosof/4744962 to your computer and use it in GitHub Desktop.
/*----------------------------------------------------------------------------
プログラム名:ColorFullBox
バージョン:1.0.2.0
プログラム概要:
開発言語:Microsoft Visual C++ 2012
統合開発環境:Microsoft Visual Studio 2012 Professional Edition
開発開始日:2013/02/08
最終更新日:2013/02/09
-------------------------------------------------------------------------------
|更新日一覧|
--------------------------------------------------------------------------------
|2013/02/08|
|2013/02/09|
-------------------------------------------------------------------------------*/
/*-----------------------インクルード部------------------------*/
#include <windows.h>
#include <tchar.h>
#include "HSCOLOR2.h"
#include "myfuncs.h"
#include "resource.h"
#include "HSCommCtrl.h"
/*-------------------------------------------------------------*/
/*-------------------プロトタイプ宣言部------------------------*/
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
LRESULT CALLBACK WndProc_Box(HWND,UINT,WPARAM,LPARAM);
int CALLBACK WndProc_CreateColorBoxDlg(HWND hwnd,UINT msg,WPARAM wp,LPARAM lp);
DWORD CALLBACK ThreadProc_CCB(LPVOID);
ATOM SetApp(HINSTANCE);
ATOM RegistColorBox(HINSTANCE);
BOOL SetInstance(HINSTANCE,int);
//マニフェスト登録
#pragma comment(linker,"\"/manifestdependency:type='win32' " \
"name='Microsoft.Windows.Common-Controls' " \
"version='6.0.0.0' " \
"processorArchitecture='X86' " \
"publicKeyToken='6595b64144ccf1df' " \
"language='*'\"")
/*-------------------------------------------------------------*/
/*-------------------------マクロ定義部------------------------*/
#define PRONAME "ColorFullBox"
#define CLASSNAME_COLORBOX "ColorBox"
#define PROVER "1.0.2.0"
#define PRONAME_TXT TEXT(PRONAME)
#define PROVER_TXT TEXT(PROVER)
#define WindowClientSizeX 660
#define WindowClientSizeY 480
/*-------------------------------------------------------------*/
/*-------------------グローバル変数定義部----------------------*/
TCHAR szClassName[] = PRONAME_TXT; //ウィンドウクラス
enum MyMessage
{
WM_APP_SELECT_HLS_L = WM_APP,
WM_APP_GET_HLS_L,
};
struct MyCreateThreadData
{
HDC hDC_Base;
HWND hProgress;
};
enum MyMenuID
{
MMID_BRIGHT_TB = 100
};
#define ColorBoxLightNums 100
HDC hdc_colorbox[ColorBoxLightNums + 1];
#define ColorBoxSizeX 600
#define ColorBoxSizeY 200
//PBS_MARQUEE
/*-------------------------------------------------------------*/
/*----------------------------メイン関数定義部--------------------------------------------*/
int WINAPI WinMain(HINSTANCE hCurInst,HINSTANCE hPrevInst, LPSTR lpsCmdLine,int nCmdShow){
MSG msg;
BOOL bRet;
if (!SetApp(hCurInst)) return FALSE;
if (!SetInstance(hCurInst,nCmdShow)) return FALSE;
while ((bRet=GetMessage(&msg,NULL,0,0))){
if(bRet==-1){
MessageBox(NULL,TEXT("GetMessageエラー"),TEXT("Error"),MB_OK);
break;
}else{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int)msg.wParam;
}
/*---------------------------------------------------------------------------------------*/
/*------------------------ウィンドウクラスを登録する関数の定義部-------------------------*/
ATOM SetApp(HINSTANCE hInst){
WNDCLASSEX wc; //WNDCLASSEX構造体
wc.cbSize=sizeof(WNDCLASSEX);
wc.style=CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc=WndProc; //プロシージャ名
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hInst ; //インスタンス
wc.hIcon=(HICON)LoadImage(NULL,MAKEINTRESOURCE(IDI_APPLICATION),IMAGE_ICON,0,0,LR_DEFAULTSIZE | LR_SHARED);
wc.hCursor=(HCURSOR)LoadImage(NULL,MAKEINTRESOURCE(IDC_ARROW),IMAGE_CURSOR,0,0,LR_DEFAULTSIZE | LR_SHARED);
wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName=NULL; //メニュー名
wc.lpszClassName=szClassName;
wc.hIconSm=(HICON)LoadImage(NULL,MAKEINTRESOURCE(IDI_APPLICATION),IMAGE_ICON,0,0,LR_DEFAULTSIZE | LR_SHARED);
return (RegisterClassEx(&wc));
}
ATOM RegistColorBox(HINSTANCE hInst){
WNDCLASSEX wc; //WNDCLASSEX構造体
wc.cbSize=sizeof(WNDCLASSEX);
wc.style=CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc=WndProc_Box; //プロシージャ名
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hInst ; //インスタンス
wc.hIcon=(HICON)LoadImage(NULL,MAKEINTRESOURCE(IDI_APPLICATION),IMAGE_ICON,0,0,LR_DEFAULTSIZE | LR_SHARED);
wc.hCursor=(HCURSOR)LoadImage(NULL,MAKEINTRESOURCE(IDC_ARROW),IMAGE_CURSOR,0,0,LR_DEFAULTSIZE | LR_SHARED);
wc.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName=NULL; //メニュー名
wc.lpszClassName=TEXT(CLASSNAME_COLORBOX);
wc.hIconSm=(HICON)LoadImage(NULL,MAKEINTRESOURCE(IDI_APPLICATION),IMAGE_ICON,0,0,LR_DEFAULTSIZE | LR_SHARED);
return (RegisterClassEx(&wc));
}
/*---------------------------------------------------------------------------------------*/
/*-----------------------ウィンドウを作成する関数の定義部--------------------------------*/
BOOL SetInstance(HINSTANCE hInst,int nCmdShow){
HWND hwnd;
hwnd =
CreateWindowEx(NULL,
szClassName,
//タイトルバーにこの文字列が表示されます
PRONAME_TXT,
WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX,//ウィンドウの種類
CW_USEDEFAULT, //x座標
CW_USEDEFAULT,//y座標
CW_USEDEFAULT, //幅
CW_USEDEFAULT,//高さ
NULL,//親ウィンドウのハンドル、親を作るときはNULL
NULL,//メニューハンドル、クラスメニューを使うときはNULL
hInst,//インスタンスハンドル
NULL);
if(!hwnd)return FALSE;
//ウィンドウサイズの設定のためのブロック
{
RECT Window,Client;
POINT ptW,ptC;
GetWindowRect(hwnd,&Window);
GetClientRect(hwnd,&Client);
ptW.x = Window.right - Window.left;
ptW.y = Window.bottom - Window.top;
ptC.x = Client.right - Client.left;
ptC.y = Client.bottom - Client.top;
SetWindowPos(hwnd,NULL,0,0,WindowClientSizeX+(ptW.x - ptC.x),WindowClientSizeY+(ptW.y - ptC.y),SWP_NOZORDER | SWP_NOMOVE);
}
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
return TRUE;
}
/*-------------------------------------------------------------------------------------*/
/*------------------メインウィンドウのウィンドウプロシージャの定義部---------------------*/
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wp,LPARAM lp){
static HWND hColorChooseBox , hTrackBar_Bright;
RECT rt;
GetClientRect(hwnd , &rt);
switch(msg){
case WM_CREATE:
{
HDC hdc = GetDC(hwnd);
DialogBoxParam(((LPCREATESTRUCT)lp)->hInstance , MAKEINTRESOURCE(IDD_CREATINGCOLORBOX) , hwnd , WndProc_CreateColorBoxDlg , (LPARAM)hdc);
ReleaseDC(hwnd , hdc);
Sleep(500);
}
RegistColorBox(((LPCREATESTRUCT)lp)->hInstance);
hColorChooseBox = CreateWindowEx(NULL,TEXT(CLASSNAME_COLORBOX),TEXT("CFB_ColorChooseBox") ,
WS_VISIBLE | WS_CHILD , 5,5, ColorBoxSizeX , ColorBoxSizeY , hwnd , NULL ,
((LPCREATESTRUCT)lp)->hInstance,NULL);
hTrackBar_Bright = CreateTrackBar(hwnd , ColorBoxSizeX+5,5,40,200,TBS_VERT,MMID_BRIGHT_TB);
SendMessage(hTrackBar_Bright , TBM_SETRANGE , TRUE , MAKELONG (0,ColorBoxLightNums));
SendMessage(hTrackBar_Bright , TBM_SETPOS , TRUE ,
ColorBoxLightNums - SendMessage(hColorChooseBox , WM_APP_GET_HLS_L , 0 , 0));
break;
case WM_VSCROLL:
if((HWND)lp == hTrackBar_Bright){
int pos = SendMessage(hTrackBar_Bright , TBM_GETPOS , 0,0);
PostMessage(hColorChooseBox , WM_APP_SELECT_HLS_L , ColorBoxLightNums - pos , NULL);
}
break;
case WM_PAINT:
{
HDC hdc , hdc_mem;
HBITMAP hBit_mem;
PAINTSTRUCT ps;
int margin_bottom = 210 ; //オブジェクトサイズ + カラービュー
int margin_color = 50;
hdc = BeginPaint(hwnd , &ps);
hdc_mem = CreateCompatibleDC(hdc);
hBit_mem = CreateCompatibleBitmap(hdc , rt.right , rt.bottom - (margin_bottom));
SelectObject(hdc_mem , hBit_mem);
SelectObject(hdc_mem , GetStockObject(DC_PEN));
SelectObject(hdc_mem , GetStockObject(DC_BRUSH));
SetDCPenColor(hdc_mem , RGB(128,128,128));
PatBlt(hdc_mem , 0,0,rt.right , rt.bottom - margin_bottom , WHITENESS);
MoveToEx(hdc_mem , 0 , 0,NULL);
LineTo(hdc_mem , rt.right , 0);
for (int i = 1; i < 3; i++)
{
int px;
px = rt.right / 3 * i;
MoveToEx(hdc_mem , px , 0,NULL);
LineTo(hdc_mem , px , rt.bottom - (margin_color + margin_bottom));
}
MoveToEx(hdc_mem , 0 , rt.bottom - (margin_color + margin_bottom),NULL);
LineTo(hdc_mem , rt.right , rt.bottom - (margin_color + margin_bottom));
MoveToEx(hdc_mem , 0 , 0,NULL);
LineTo(hdc_mem , 0 , rt.bottom - (margin_color + margin_bottom));
MoveToEx(hdc_mem , rt.right-1 , 0,NULL);
LineTo(hdc_mem , rt.right - 1 , rt.bottom - (margin_color + margin_bottom));
BitBlt(hdc , 0,margin_bottom,rt.right , rt.bottom , hdc_mem , 0 , 0 ,SRCCOPY);
DeleteDC(hdc_mem);
DeleteObject(hBit_mem);
EndPaint(hwnd , &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return (DefWindowProc(hwnd,msg,wp,lp));
}
return 0;
}
int CALLBACK WndProc_CreateColorBoxDlg(HWND hwnd,UINT msg,WPARAM wp,LPARAM lp){
static HDC HDC_parent;
static MyCreateThreadData Data;
static HANDLE hThread;
switch(msg){
case WM_INITDIALOG:
Data.hDC_Base = (HDC)lp;
Data.hProgress = GetDlgItem(hwnd , IDC_MAQ_PROGR);
hThread = CreateThread(NULL , NULL , ThreadProc_CCB , &Data , NULL , NULL);
SetTimer(hwnd , 100 , 1 , NULL);
break;
case WM_TIMER:
if(wp == 100){
if(WaitForSingleObject(hThread , 0) == WAIT_OBJECT_0) EndDialog(hwnd , IDOK);
}
break;
default:
return FALSE;
}
return TRUE;
}
DWORD CALLBACK ThreadProc_CCB(LPVOID lpVData){
MyCreateThreadData *lpData = (MyCreateThreadData*)lpVData;
HDC hdc_main = lpData->hDC_Base;
double dHLS[3] , dRGB[3];
int color_rgb[3];
double plus_h , plus_s;
HSConvColorData convdata;
convdata.ModeFlag = 1;
convdata.Color.Pointer.lpInput = dHLS;
convdata.Color.Pointer.lpOutput = dRGB;
plus_h = 360.0 / (ColorBoxSizeX - 1);
plus_s = 1.0 / (ColorBoxSizeY - 1);
SetHSPB_Range(lpData->hProgress , 0 , ColorBoxLightNums);
for (int i = 0; i <= ColorBoxLightNums; i++)
{
dHLS[1] = (double)i / (double)ColorBoxLightNums;
hdc_colorbox[i] = CreateCompatibleDC(hdc_main);
SelectObject(hdc_colorbox[i] , CreateCompatibleBitmap(hdc_main , ColorBoxSizeX , ColorBoxSizeY));
for (int y = 0; y < ColorBoxSizeY; y++)
{
dHLS[2] = 1.0 - y * plus_s ;
for (int x = 0; x < ColorBoxSizeX; x++)
{
dHLS[0] = x * plus_h;
HSColor2_HLStoRGB(&convdata);
for (int j = 0; j < 3; j++) color_rgb[j] = (int)dRGB[j];
SetPixel(hdc_colorbox[i] , x , y , RGB(color_rgb[0],color_rgb[1],color_rgb[2]));
}
}
HSPB_SetPos(lpData->hProgress , i);
}
Sleep(1000);
ExitThread(0);
return 0;
}
LRESULT CALLBACK WndProc_Box(HWND hwnd,UINT msg,WPARAM wp,LPARAM lp){
static double hls_l = 0.5;
RECT rt;
GetClientRect(hwnd , &rt);
switch(msg){
case WM_PAINT:
{
HDC hdc;
PAINTSTRUCT ps;
int ArrayNum;
ArrayNum =(int)( hls_l * ColorBoxLightNums);
hdc = BeginPaint(hwnd , &ps);
BitBlt(hdc , 0,0,rt.right , rt.bottom , hdc_colorbox[ArrayNum] , 0 , 0 ,SRCCOPY);
EndPaint(hwnd , &ps);
}
break;
case WM_APP_GET_HLS_L:
return (int)(hls_l * ColorBoxLightNums);
case WM_APP_SELECT_HLS_L:
hls_l = ((double)wp) / ((double)ColorBoxLightNums);
InvalidateRect(hwnd , NULL , FALSE);
break;
case WM_DESTROY:
for (int i = 0; i <= ColorBoxLightNums; i++){
HBITMAP hBit;
hBit = (HBITMAP)GetCurrentObject(hdc_colorbox[i] ,OBJ_BITMAP);
DeleteDC(hdc_colorbox[i]);
DeleteObject(hBit);
}
default:
return (DefWindowProc(hwnd,msg,wp,lp));
}
return 0;
}
/*-------------------------------------------------------------------------------------*/
/*--------------------------------------[EOF]------------------------------------------*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment