Color dialog box

本文介绍了一个使用Windows API创建调色板对话框的简单示例程序。该程序包含一个按钮,点击后弹出颜色选择对话框,允许用户选择颜色,并在一个面板中实时显示所选颜色。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <windows.h>


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


void RegisterPanel(void);
COLORREF ShowColorDialog(HWND);  // COLORREF 就是个DWORD // typedef DWORD COLORREF;


COLORREF gColor = RGB(255, 255, 255);


int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    LPSTR lpCmdLine, int nCmdShow )
{
  MSG  msg ;    
  WNDCLASS wc = {0};
  wc.lpszClassName = TEXT( "Color dialog box" );
  wc.hInstance     = hInstance ;
  wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
  wc.lpfnWndProc   = WndProc ;
  
  RegisterClass(&wc);
  CreateWindow( wc.lpszClassName, TEXT("Color dialog box"),
                WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                150, 150, 250, 200, 0, 0, hInstance, 0);  


  while( GetMessage(&msg, NULL, 0, 0)) {
    DispatchMessage(&msg);
  }
  return (int) msg.wParam;
}


LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{


  static HWND hwndPanel;


    
  switch(msg)  
  {
    	case WM_CREATE:
  	{
          CreateWindow(TEXT("button"), TEXT("Color"),    
	       WS_VISIBLE | WS_CHILD ,
	       20, 30, 80, 25,        
	       hwnd, (HMENU) 1, NULL, NULL);// hwnd 父窗口    


          RegisterPanel();
          hwndPanel = CreateWindow(TEXT("Panel"), NULL, 
		           WS_CHILD | WS_VISIBLE,
		           130, 30, 80, 80,
		           hwnd, (HMENU) 2, NULL, NULL); // hwnd 父窗口    
          break;
	}


	case WM_COMMAND: // 什么情形下产生这种消息?
	{
           gColor = ShowColorDialog(hwnd);  // 调用调色板对话框
           InvalidateRect(hwndPanel, NULL, TRUE);    
           break;
	}


      case WM_DESTROY:
      {
          PostQuitMessage(0);
          break;
      }
  }


  return DefWindowProc(hwnd, msg, wParam, lParam);
}	


LRESULT CALLBACK PanelProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
  HDC hdc;
  PAINTSTRUCT ps; 
  RECT rect;


  switch(msg)  
  {
    case WM_PAINT:
    {
        GetClientRect(hwnd, &rect);
        hdc = BeginPaint(hwnd, &ps);
        SetBkColor(hdc, gColor);
        ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rect, TEXT(""), 0, NULL);
        EndPaint(hwnd, &ps);
        break;
    }
  }
  return DefWindowProc(hwnd, msg, wParam, lParam);
}




COLORREF ShowColorDialog(HWND hwnd) {


  CHOOSECOLOR cc;                 
  static COLORREF crCustClr[16];     


  ZeroMemory(&cc, sizeof(cc));
  cc.lStructSize = sizeof(cc);
  cc.hwndOwner = hwnd;
  cc.lpCustColors = (LPDWORD) crCustClr;
  cc.rgbResult = RGB(0, 255, 0);
  cc.Flags = CC_FULLOPEN | CC_RGBINIT;
  ChooseColor(&cc);


  return cc.rgbResult;
}


void RegisterPanel(void) {


  WNDCLASS rwc = {0};
  rwc.lpszClassName = TEXT( "Panel" );
  rwc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
  rwc.lpfnWndProc   = PanelProc;
  RegisterClass(&rwc);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值