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;
}
- vc中控件的自绘制
- VC控件自绘制
- VC 自绘制控件
- VC控件自绘制三步曲
- 【MFC控件自绘】VC控件自绘制
- 【VC】 CStatic 控件绘制BITMAP的方法。。
- 绘制VC表格控件
- delphi中为自定控件绘制windows按钮
- VC 自绘制BMP图像
- VC控件自绘
- VC:使用VC自带的splash screen控件
- VC++ MFC中点线面的绘制
- vc中绘制有纹理的背景
- VC中自画控件的过程和方法(CButton, CEdit, etc)
- VC中自画控件的过程和方法(CButton, CEdit, etc)
- VC/MFC中如何替换自带滚动条控件的图片
- vc中 控件的移动
- Button的自绘制
- Unity 字体描边
- jquery使用技巧总结
- android开发之 Activity之间数据传递
- 科学的励志和励志的科学
- LCD 应用程序
- vc中控件的自绘制
- 全球首个公有云GIS平台ArcGIS Online全面解读(三)
- 工控机的发展趋势
- POJ 3668 Game of Lines
- java.lang.OutOfMemoryError: Java heap space 解决方法
- 浅析Linux下core文件
- 炒股七字经
- 有关mysql2008安装是 点开setup时候 闪一下的原因。
- Unity 调用外部的EXE和载入关卡