📄 button.c
字号:
textStyle &= ~DT_VCENTER;
textStyle |= DT_BOTTOM; // 底对齐
}
}
if( (style & BS_MULTILINE) )
textStyle &= ~DT_SINGLELINE; // 多行文本
// 画文本 now draw text
if( type == BS_GROUPBOX )
{
;
}
else
SetBkMode( hdc, TRANSPARENT ); // 背景用透明模式
if( bEnable )
{ // 启用模式 enable
SetTextColor( hdc, pAttrib->cl_Text );
}
else
{ // 停用模式 disable
//SetTextColor( hdc, GetSysColor(COLOR_BTNTEXT) );
SetTextColor( hdc, pAttrib->cl_Disable );
}
if( (style & BS_MULTILINE) && (style & BS_VCENTER) )
{ //
RECT rc = rectText;
int cyOrg, cyNew;
DrawText( hdc, buf, l, &rc, textStyle | DT_CALCRECT );
cyOrg = rectText.bottom - rectText.top;
cyNew = rc.bottom - rc.top;
if( cyNew < cyOrg )
rectText.top += (cyOrg - cyNew) / 2;
}
// 画文本
DrawText( hdc, buf, l, &rectText, textStyle );
}
if( buf && buf != strBuf )
free( buf ); //释放之前分配的缓冲
SelectObject( hdc, hBrush ); //恢复
}
/*
static void DrawButtonState( HDC hdc, HWND hWnd, DWORD style, DWORD state )
{
WORD type = GETTYPE( style );
RECT rectClient, rectText, rectIcon;
HBRUSH hBrush;
char buf[20];
int l, shift = 0;
WORD textStyle = 0;
GetClientRect( hWnd, &rectClient );
l = GetWindowText( hWnd, buf, sizeof( buf ) );
textStyle = DT_CENTER | DT_SINGLELINE | DT_VCENTER;
//hBrush = SelectObject( hdc, GetStockObject( NULL_BRUSH ) );
FillRect( hdc, &rectClient, GetStockObject( LTGRAY_BRUSH ) ); // clear background
hBrush = SelectObject( hdc, GetStockObject( NULL_BRUSH ) );
if( type == BS_RADIOBUTTON || type == BS_AUTORADIOBUTTON ||
type == BS_CHECKBOX || type == BS_AUTOCHECKBOX )
{
rectText = rectClient;
rectIcon = rectClient;
if( style & BS_LEFTTEXT )
{
rectIcon.left = rectClient.right - ICON_SIZE;
rectIcon.right = rectClient.right;
rectText.right = rectIcon.left - 2;
}
else
{
rectIcon.left = rectClient.left;
rectIcon.right = rectIcon.left + ICON_SIZE;
rectText.left = rectIcon.right + 2;
}
rectIcon.top = (rectClient.bottom + rectClient.top) / 2 - ICON_SIZE / 2;
rectIcon.bottom = rectIcon.top + ICON_SIZE;
if( type == BS_RADIOBUTTON || type == BS_AUTORADIOBUTTON )
DrawRadioBox( hdc, &rectIcon, state );
else
DrawCheckBox( hdc, &rectIcon, state );
}
else
{
if( type == BS_DEFPUSHBUTTON ||
type == BS_PUSHBUTTON ||
(style & BS_PUSHLIKE) )
{
if( type == BS_DEFPUSHBUTTON )
{
Rectangle( hdc, rectClient.left, rectClient.top, rectClient.right, rectClient.bottom );
InflateRect( &rectClient, -1, -1 );
}
if( state & BST_PUSHED ) // highlight state
{
DrawEdge( hdc, &rectClient, BDR_SUNKENOUTER, BF_RECT | BF_MIDDLE );
shift = 1;
}
else // normal state
DrawEdge( hdc, &rectClient, BDR_RAISEDOUTER, BF_RECT | BF_MIDDLE );
InflateRect( &rectClient, -1, -1 );
}
rectText = rectClient;
}
if( state & BST_FOCUS )
{ // has focus
DrawFocusRect( hdc, &rectText );
InflateRect( &rectText, -1, -1 );
}
if( shift ) // move down state
{
rectText.left += 1;
rectText.top += 1;
}
if( style & BS_LEFT )
{
textStyle &= ~DT_CENTER;
textStyle |= DT_LEFT;
}
else if( style & BS_RIGHT )
{
textStyle &= ~DT_CENTER;
textStyle |= DT_RIGHT;
}
if( style & BS_TOP )
{
textStyle &= ~DT_VCENTER;
textStyle |= DT_TOP;
}
else if( style & BS_BOTTOM )
{
textStyle &= ~DT_VCENTER;
textStyle |= DT_BOTTOM;
}
if( (style & BS_MULTILINE) )
textStyle &= ~DT_SINGLELINE;
// now draw text
//SetBkColor( hdc,GetSysColor(COLOR_3DFACE) );
SetBkMode( hdc, TRANSPARENT );
if( !IsWindowEnabled( hWnd ) )
{ // disable
SetTextColor( hdc, CL_DARKGRAY );
}
else
{ // enable
SetTextColor( hdc, CL_BLACK );
}
DrawText( hdc, buf, l, &rectText, textStyle );
SelectObject( hdc, hBrush );
}
*/
// **************************************************
// 声明:void DoOwnerDraw( HDC hdc, HWND hWnd, DWORD state, UINT uiAction )
// 参数:
// IN hdc - 显示设备句柄
// IN hWnd - 窗口句柄
// IN state - 按钮状态
// IN uiAction - 当前采取的行动,包含:
// ODA_DRAWENTIRE - 控件的整个区域需要被绘制.
// ODA_FOCUS - 控件失去或得到键盘输入焦点. 参数 state 标明了其是否失去或得到.
// ODA_SELECT - 选择状态改变. 参数 state 标明了新的状态
// 返回值:
// 无
// 功能描述:
// 用户自绘处理
// 引用:
//
// ************************************************
// if window's style is BS_OWNERDRAW, send message to owner window
static void DoOwnerDraw( HDC hdc, HWND hWnd, DWORD state, UINT uiAction )
{
DRAWITEMSTRUCT drawItem;
// 得到控件的客户区
GetClientRect( hWnd, &drawItem.rcItem );
// 初始化数据
drawItem.CtlType = ODT_BUTTON;
drawItem.CtlID = (WORD)GetWindowLong( hWnd, GWL_ID );
drawItem.itemID = 0;
drawItem.itemAction = uiAction;
drawItem.hwndItem = hWnd;
drawItem.hDC = hdc;
drawItem.itemData = 0;
drawItem.itemState = 0;
// 确定当前的状态
if( state & ( BST_CHECKED | BST_PUSHED ) )
drawItem.itemState |= ODS_SELECTED;
if( state & BST_FOCUS )
drawItem.itemState |= ODS_FOCUS;
if( !IsWindowEnabled( hWnd ) )
drawItem.itemState |= ODS_DISABLED;
// 发送消息给父窗口
SendMessage( GetParent(hWnd), WM_DRAWITEM, GetWindowLong( hWnd, GWL_ID ), (LPARAM)&drawItem );
}
// **************************************************
// 声明:static void RefreshButton( HWND hWnd, PBUTTON_ATTRIB pAttrib )
// 参数:
// IN hWnd - 窗口句柄
// IN pAttrib - 控件属性结构
// 返回值:
// 无
// 功能描述:
// 刷新控件的显示面
// 引用:
//
// ************************************************
static void RefreshButton( HWND hWnd, PBUTTON_ATTRIB pAttrib )
{
HDC hdc = GetDC( hWnd ); // 向系统申请显示DC
DWORD dwStyle = GetWindowLong( hWnd, GWL_STYLE ); // 得到窗口风格
if( pAttrib == NULL )
pAttrib = (PBUTTON_ATTRIB)GetWindowLong( hWnd, 0 ); // 从窗口私有数据区得到控件属性
switch( dwStyle & 0xf )
{
case BS_OWNERDRAW: // 自绘风格
DoOwnerDraw( hdc, hWnd, pAttrib->btState, ODA_DRAWENTIRE );
break;
default:
DrawButtonState( hdc, hWnd, dwStyle, pAttrib->btState );
}
ReleaseDC( hWnd, hdc ); // 释放DC
}
// **************************************************
// 声明:static LRESULT DoPAINT( HWND hWnd )
// 参数:
// IN hWnd - 窗口句柄
// 返回值:
// 返回0
// 功能描述:
// 处理WM_PAINT消息
// 引用:
//
// ************************************************
static LRESULT DoPAINT( HWND hWnd )
{
HDC hdc;
PAINTSTRUCT ps;
DWORD state;
DWORD dwStyle;
DWORD s;
PBUTTON_ATTRIB pAttrib = (PBUTTON_ATTRIB)GetWindowLong( hWnd, 0 );// 从窗口私有数据区得到控件属性
// get draw dc
hdc = BeginPaint( hWnd, &ps );// 向系统申请显示DC
// get myself state from extra window bytes, see RegisterMyButton
state = pAttrib->btState;//
// get window style
dwStyle = GetWindowLong( hWnd, GWL_STYLE ); // 窗口风格
s = dwStyle & 0xf;
switch( s )
{
case BS_OWNERDRAW: // 自绘风格
DoOwnerDraw( hdc, hWnd, state, ODA_DRAWENTIRE );
break;
default:
DrawButtonState( hdc, hWnd, dwStyle, state );
}
EndPaint( hWnd, &ps ); // 释放DC
return 0;
}
// **************************************************
// 声明:static LRESULT DoLBUTTONDOWN( HWND hWnd )
// 参数:
// IN hWnd - 窗口句柄
// 返回值:
// 返回0
// 功能描述:
// 处理 WM_LBUTTONDOWN 消息
// 引用:
//
// ************************************************
static LRESULT DoLBUTTONDOWN( HWND hWnd )
{
PBUTTON_ATTRIB pAttrib = (PBUTTON_ATTRIB)GetWindowLong( hWnd, 0 );// 从窗口私有数据区得到控件属性
//DWORD dwStyle;
if( (pAttrib->btFlag & BTF_DISABLEFOCUS) == 0 )
{
if( GetFocus() != hWnd )
SetFocus( hWnd ); // 设置本窗口为输入焦点
}
// highlight state if possible
//2004-05-20, not use remove lilin
//dwStyle = GetWindowLong( hWnd, GWL_STYLE );
//
// if possible, set button state
DoSETSTATE( hWnd, TRUE ); // 画高亮状态
// capture mouse
SetCapture(hWnd); // 抓住点输入
return 0;
}
// **************************************************
// 声明:static LRESULT DoMouseMove( HWND hWnd, short x, short y )
// 参数:
// IN hWnd - 窗口句柄
// IN x - 当前点输入设备在控件的客户区x坐标
// IN y - 当前点输入设备在控件的客户区y坐标
// 返回值:
// 返回0
// 功能描述:
// 处理 WM_MOUSEMOVE 消息
// 引用:
//
// ************************************************
static LRESULT DoMouseMove( HWND hWnd, short x, short y )
{
RECT rect;
DWORD state;
int type;
POINT pt;
PBUTTON_ATTRIB pAttrib = (PBUTTON_ATTRIB)GetWindowLong( hWnd, 0 );// 从窗口私有数据区得到控件属性
type = GETTYPE( GetWindowLong( hWnd, GWL_STYLE ) ); // 得到控件类型
pt.x = x;
pt.y = y;
if( GetCapture() == hWnd ) // 之前是否已经抓住点输入设备 ?
{ // 是
GetClientRect( hWnd, &rect ); // 得到控件客户区矩形
state = pAttrib->btState;
if( PtInRect( &rect, pt ) ) // 点输入设备在控件客户区吗 ?
{ //在控件客户区
if( (state & BST_PUSHED) == 0 ) // 当前控件状态为按下状态吗 ?
DoSETSTATE( hWnd, TRUE ); // 不是,改变状态
}
else
{ // 不在控件客户区
if( state & BST_PUSHED ) // // 当前控件状态为按下状态吗 ?
DoSETSTATE( hWnd, FALSE );// 不是,改变状态
}
}
return 0;
}
// **************************************************
// 声明:static LRESULT DoMouseMove( HWND hWnd, short x, short y )
// 参数:
// IN hWnd - 窗口句柄
// IN x - 当前点输入设备在控件的客户区x坐标
// IN y - 当前点输入设备在控件的客户区y坐标
// 返回值:
// 返回0
// 功能描述:
// 处理 WM_LBUTTONUP 消息
// 引用:
//
// ************************************************
//extern BOOL _Wnd_IsClass( HWND hWnd, LPCBYTE lpcName );
static LRESULT DoLBUTTONUP( HWND hWnd, short x, short y )
{
DWORD dwStyle;
DWORD s, dw;
RECT rect;
POINT pt;
PBUTTON_ATTRIB pAttrib = (PBUTTON_ATTRIB)GetWindowLong( hWnd, 0 );// 从窗口私有数据区得到控件属性
pt.x = x; pt.y = y;
if( GetCapture() != hWnd ) // 之前是否已经抓住点输入设备 ?
return 0; // 否,返回
// release mouse
ReleaseCapture(); // 释放点输入设备
s = pAttrib->btState;
DoSETSTATE( hWnd, FALSE ); // 设置按钮为释放状态
GetClientRect( hWnd, &rect ); //// 得到控件客户区矩形
if( PtInRect( &rect, pt ) )
{
dwStyle = GetWindowLong( hWnd, GWL_STYLE ); // 得到控件风格
if( GETTYPE(dwStyle) == BS_AUTOCHECKBOX ) // 自动复选按钮吗 ?
{ // 是,反转状态
DoSETCHECK( hWnd, (s & BST_CHECKED) ? BST_UNCHECKED : BST_CHECKED );
}
else if( GETTYPE(dwStyle) == BS_AUTORADIOBUTTON &&
(s & BST_CHECKED) == FALSE ) // 自动单选按钮吗 并且 当前状态为非选择状吗?
{ // 是
HWND hParent = GetParent( hWnd ); //得到按钮的父窗口句柄
if( hParent )
{ //
HWND hNext = GetWindow( hParent, GW_CHILD );//得到父窗口的第一个子窗口
// 遍历父窗口的所有为自动单选按钮风格的子窗口
// 如果其中的一个的状态为选择状态,则将其设为非选择状态
while( hNext )
{
TCHAR strClass[sizeof( strClassButton )];
strClass[0] = 0;
GetClassName( hNext, strClass, sizeof( strClassButton ) ); // 得到类名
if( stricmp( strClass, strClassButton ) == 0 ) // 是按钮类吗 ?
{ /// 是 yes , is button
dwStyle = GetWindowLong( hNext, GWL_STYLE ); // 得到按钮风格
if( GETTYPE(dwStyle) == BS_AUTORADIOBUTTON ) // 自动单选按钮吗 ?
{ // 是
PBUTTON_ATTRIB pa = (PBUTTON_ATTRIB)GetWindowLong( hNext, 0 ); // 从窗口私有数据区得到控件属性
s = pa->btState;
if( s & BST_CHECKED )
{ // 为选择状态
DoSETCHECK( hNext, BST_UNCHECKED );// 设为非选择状态
break;
}
}
}
hNext = GetWindow( hNext, GW_HWNDNEXT ); // 下一个窗口
}
}
DoSETCHECK( hWnd, BST_CHECKED );// 将当前按钮设为选择状态
}
dw = GetWindowLong( hWnd, GWL_ID );
SendNotify( GetParent(hWnd), dw, BN_CLICKED, hWnd ); // 想父发送通知消息
}
return 0;
}
// **************************************************
// 声明:static LRESULT DoSETFOCUS( HWND hWnd )
// 参数:
// IN hWnd - 窗口句柄
// 返回值:
// 返回0
// 功能描述:
// 处理 WM_SETFOCUS 消息
// 引用:
//
// ************************************************
static LRESULT DoSETFOCUS( HWND hWnd )
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -