vc中控件的自绘制

来源:互联网 发布:php手机音乐源码 编辑:程序博客网 时间:2024/06/06 05:19

控件的自绘制

条件:控件具有OWNERDRAW样式

        例如:BUTTON  LISTBOX     COMBOBOX

 消息处理(2个):WM_DRAWITEM和WM_MEASUREITEN

 

在WM_DRAWITEM消息中实现:绘制

(UINT)wParam------空间ID

(LPDRAWITEMSTRUCT)lParam-----控件绘制的信息

在WM_MEASUREITEN消息中实现:计算空间的大小奋斗

 

例子:

// WinPaint.cpp : Defines the entry point for the application.
//

#include "stdafx.h"

HINSTANCE   g_hInst = NULL;

void OnCreate(HWND hWnd,WPARAM wParam, LPARAM lParam)
{
 CreateWindow( "BUTTON", "myWindow", WS_CHILD|WS_VISIBLE|BS_OWNERDRAW,//=================1.创建自绘制风格的button
  50, 50, 200, 50, hWnd, (HMENU)1001, g_hInst, NULL );
}

//========OnDrawItem实现
void OnDrawItem( HWND hWnd,WPARAM wParam, LPARAM lParam)
{
 LPDRAWITEMSTRUCT pDis = (LPDRAWITEMSTRUCT)lParam;
 if( ODT_BUTTON == pDis->CtlType)
 {
  //================绘制按钮外形
  if( pDis->itemState & ODS_SELECTED )
  { //=============被选择时按钮状态
   HBRUSH hBrush = CreateSolidBrush(
    RGB(200, 200, 255 ) );
   HBRUSH hOldBrush = (HBRUSH)SelectObject( pDis->hDC, hBrush);
   RoundRect(pDis->hDC,pDis->rcItem.left,
    pDis->rcItem.top, pDis->rcItem.right,
    pDis->rcItem.bottom ,15,15);
   SelectObject( pDis->hDC, hBrush);
   DeleteObject( hOldBrush );
  }
  else
  {
   HBRUSH hBrush = CreateSolidBrush(
    RGB(100, 100, 255 ) );
   HBRUSH hOldBrush = (HBRUSH)SelectObject( pDis->hDC, hBrush);
   RoundRect(pDis->hDC,pDis->rcItem.left,
    pDis->rcItem.top, pDis->rcItem.right,
    pDis->rcItem.bottom ,15,15);
   SelectObject( pDis->hDC, hBrush);
   DeleteObject( hOldBrush );//================矩形按钮
  }
  //===============绘制按钮文字
 
  CHAR szName[260] = { 0 };
  GetWindowText( pDis->hwndItem, szName, 260);
  int nOldMode = SetBkMode( pDis->hDC, TRANSPARENT );//===================设置文字背景透明
  DrawText( pDis->hDC, szName, strlen(szName) ,
   &pDis->rcItem , DT_CENTER|DT_VCENTER|DT_SINGLELINE );
  SetBkMode( pDis->hDC, nOldMode );
 }
}

LRESULT  CALLBACK WndProc(HWND hWnd,UINT nMsg, WPARAM wParam, LPARAM lParam )
{
 switch( nMsg )
 {
 case WM_DRAWITEM://==================2.WM_DRAWITEM消息处理
  OnDrawItem( hWnd, wParam,lParam);
  return 0;
 case WM_CREATE:
  OnCreate( hWnd, wParam, lParam );
  break;
 case WM_DESTROY:
  PostQuitMessage( 0 );
  return 0;

 }
 return DefWindowProc( hWnd, nMsg, wParam, lParam );
}

BOOL RegisterWnd( LPSTR  pszClassName)
{
 WNDCLASSEX wce = { 0 };
 wce.cbSize = sizeof( wce );
 wce.cbClsExtra = 0;
 wce.cbWndExtra = 0;
 wce.hbrBackground = HBRUSH ( COLOR_BTNFACE);
 wce.hCursor = NULL;
 wce.hIcon = NULL;
 wce.hIconSm  = NULL;
 wce.hInstance = g_hInst;
 wce.lpfnWndProc    = WndProc;
 wce.lpszClassName  = pszClassName;
 wce.style      = CS_HREDRAW|CS_VREDRAW;

 ATOM nAtom  = RegisterClassEx( &wce );
 if( 0 == nAtom )
 {
  return FALSE;
 }
 return TRUE;

}

HWND CreateWnd( LPSTR pszClassName )
{
 HWND hWnd = CreateWindowEx( 0,
  pszClassName, "Mywindow",
  WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
  CW_USEDEFAULT, CW_USEDEFAULT,
  NULL, NULL, g_hInst, NULL );
 return hWnd;
}

void DisplayWnd( HWND  hWnd )
{
 ShowWindow( hWnd, SW_SHOW );
 UpdateWindow( hWnd );
}

void Message( )
{
 MSG msg = { 0 };
 while( GetMessage( &msg, NULL, 0, 0 ) )
 {
  TranslateMessage( &msg );
  DispatchMessage( &msg );
 }
}

 

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
  // TODO: Place code here.
 g_hInst = hInstance ;
 RegisterWnd( "MYWND" );
 HWND hWnd = CreateWnd( "MYWND" );
 DisplayWnd( hWnd );
 Message( );
 return 0;
}

 

 

 

 

 

 

原创粉丝点击