按钮字体及颜色
篇一:vc,windows,窗口文本颜色
CButton::DrawItem
virtual void DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct );
Parameters
lpDrawItemStruct
A long pointer to a DRAWITEMSTRUCT structure. The structure contains information about the item to be drawn and the type of drawing required.
Remarks
Called by the framework when a visual aspect of an owner-drawn button has changed. An owner-drawn button has the BS_OWNERDRAW style set. Override this member function to implement drawing for an owner-drawn CButton object. The application should restore all graphics device interface (GDI) objects selected for the display context supplied in lpDrawItemStruct before the member function terminates.
Also see the BS_ style values.
Example
// NOTE: CMyButton is a class derived from CButton. The CMyButton
// object was created as follows:
//
// CMyButton myButton;
// myButton.Create(_T("My button"),
// WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|BS_OWNERDRAW,
// CRect(10,10,100,30), pParentWnd, 1);
//
// This example implements the DrawItem method for a CButton-derived
// class that draws the button's text using the color red.
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
UINT uStyle = DFCS_BUTTONPUSH;
// This code only works with buttons.
ASSERT(lpDrawItemStruct->CtlType == ODT_BUTTON);
// If drawing selected, add the pushed style to DrawFrameControl.
if (lpDrawItemStruct->itemState & ODS_SELECTED)
uStyle |= DFCS_PUSHED;
// Draw the button frame.
::DrawFrameControl(lpDrawItemStruct->hDC, &lpDrawItemStruct->rcItem,
DFC_BUTTON, uStyle);
// Get the button's text.
CString strText;
GetWindowText(strText);
// Draw the button text using the text color red.
COLORREF crOldColor = ::SetTextColor(lpDrawItemStruct->hDC, RGB(255,0,0));
::DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(),
&lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
::SetTextColor(lpDrawItemStruct->hDC, crOldColor);
}
在Windows中Edit,StaticBox的背景色都可以通过处理WM_CTLCOLOR消息来改变,但Push Button却不行。
唯一的方法是使用OwnerDraw风格的按钮。本文讲述的方法是使用CButton的派生类。
class CCButton : public CButton
{
DECLARE_DYNAMIC(CCButton)
public:
CCButton();
virtual ~CCButton();
BOOL CCButton::Attach(const UINT nID, CWnd* pParent)
protected:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDIS);//必需重载的函数
public:
COLORREF m_fg, m_bg, m_disabled_fg, m_disabled_bg;//四种颜色分别为文字,背景,失效时文字,失效时背景 };
实现DrawItem
void CCButton::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
CDC* pDC = CDC::FromHandle(lpDIS->hDC);//???????DC
UINT state = lpDIS->itemState; //得到状态
CRect focusRect, btnRect;//两个矩形,表示得当输入焦点时的虚线矩形和按钮矩形
focusRect.CopyRect(&lpDIS->rcItem);
btnRect.CopyRect(&lpDIS->rcItem);vc,windows,窗口文本颜色。
//
//调整虚线矩形
//
focusRect.left += 4;
focusRect.right -= 4;
focusRect.top += 4;
focusRect.bottom -= 4;
//
// 得当Button上文字
//
const int bufSize = 512;
TCHAR buffer[bufSize];
GetWindowText(buffer, bufSize);
// 使用m_fg, m_bg颜色利用 Draw3dRect(...)绘制按钮边框
// FillRect(...)填充按钮内部
// DrawText(...)绘制文字
//根据当前状态调整显示
//
if (state & ODS_FOCUS) {
.........//得到输入焦点,通过focusRect画虚线
if (state & ODS_SELECTED){
.....// 被按下,绘制下陷边框
}
}
else if (state & ODS_DISABLED) {
//失效,通过m_disabled_fg, m_disabled_bg 重绘按钮内部
}
}
CCButton是CButton派生类,具有CButton的全部成员函数,但在创建时需要使用BS_OWNERDRAW风格。
如果按钮不是动态生成,使用Attach函数使CCButton代替原来按钮的窗口过程。
BOOL CCButton::Attach(const UINT nID, CWnd* pParent)
{
GetDlgItem(nID)->ModifyStyle(0,BS_OWNERDRAW,0);
if (!SubclassDlgItem(nID, pParent))
return FALSE;
return TRUE;
}
如在一对话框的InitDialog(...)中加入下面几行
{// 假定 m_cbBtn为成员变量 IDC_BUTTON1为一按钮ID值
m_cbBtn.Attach(IDC_BUTTON1,this);
}
设置vc中MFC的Button颜色和字体,CButton继承类的三色和字体设置
2008-07-23 13:11
// ButtonColor.cpp : implementation file
#include "stdafx.h"
#include "MyButtonFont.h"
#include "ButtonColor.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
///////////////////////////////////////////////////////////////////////////// // CButtonColor
CButtonColor::CButtonColor()
{
m_Style = 0; //按钮形状风格
b_InRect = false; //鼠标进入标志
m_strText = _T(""); //按钮文字(使用默认文字)
m_ForeColor = RGB(0,0,0); //文字颜色(黑色)
m_BackColor = RGB(243,243,243); //背景色(灰白色)
m_LockForeColor = GetSysColor(COLOR_GRAYTEXT); //锁定按钮的文字颜色 p_Font = NULL;
}
CButtonColor::~CButtonColor()
{
if ( p_Font ) delete p_Font; //删除字体
}
BEGIN_MESSAGE_MAP(CButtonColor, CButton)
//{{AFX_MSG_MAP(CButtonColor)
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
///////////////////////////////////////////////////////////////////////////// // CButtonColor message handlers
void CButtonColor::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class
ModifyStyle(0,BS_OWNERDRAW);
CButton::PreSubclassWindow();
}
void CButtonColor::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item
UINT uStyle = DFCS_BUTTONPUSH;
// This code only works with buttons.
ASSERT(lpDrawItemStruct->CtlType == ODT_BUTTON);
// If drawing selected, add the pushed style to DrawFrameControl.
if (lpDrawItemStruct->itemState & ODS_SELECTED)
uStyle |= DFCS_PUSHED;
// Draw the button frame.
::DrawFrameControl(lpDrawItemStruct->hDC, &lpDrawItemStruct->rcItem, DFC_BUTTON, uStyle);
// Get the button's text.
CString strText;
GetWindowText(strText);
// Draw the button text using the text color red.
COLORREF crOldColor = ::SetTextColor(lpDrawItemStruct->hDC, m_ForeColor); ::DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(),
&lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
vc怎样设置控制台窗口界面
篇二:vc,windows,窗口文本颜色
VC怎样设置控制台窗口界面
一、概述
所谓控制台应用程序,就是指那些需要与传统DOS操作系统保持某种程序的兼容,同时又不需要为用户提供完善界面的程序。简单地讲,就是指在Windows环境下运行的DOS程序。一旦C++控制台应用程序在Windows 9x/NT/2000操作系统中运行后,就会弹出一个窗口。例如下列过程:
单击Visual C++标准工具栏上的“New Text File”按钮,打开一个新的文档窗口。
选择File | Save菜单或按快捷键Ctrl+S或单击标准工具栏的Save按钮,弹出“保存为”文件对话框。将文件名为“Hello.cpp” (注意扩展名.cpp不能省略)。
在文档窗口中输入下列代码:
#include<iostream.h>
{
cout<<"Hello, Console!"<<endl;
}
单击小型编译工具栏中的“Build”按钮或按F7键,系统出现一个对话框,询问是否将此项目的工作文件夹设定源文件所在的文件夹,单击[是]按钮,系统开始编译。
单击小型编译工具栏中的“Execute Program”按钮或按Ctrl+F5键,运行刚才的程序。
程序运行后,弹出下图的窗口。vc,windows,窗口文本颜色。
这就是控制台窗口,与传统的DOS屏幕窗口相比最主要的区别有:
(1) 默认的控制台窗口有系统菜单和标题,它是一个内存缓冲区窗口,缓冲区大小取决于Windows操作系统的分配;而DOS屏幕是一种物理窗口,不具有Windows窗口特性,其大小取决于ROM BIOS分配的内存空间。
(2) 控制台窗口的文本操作是调用低层的Win32 APIs,而DOS屏幕的文本操作是通过调用BIOS的16(10h)中断而实现的。
(3) 默认的控制台窗口可以接收键盘和鼠标的输入信息,设备驱动由Windows管理,而DOS屏幕窗口接收鼠标时需要调用33h中断,且鼠标设备驱动程序由自己安装。
二、控制台文本窗口的一般控制步骤
在Visual C++ 6.0中,控制台窗口界面的一般编程控制步骤如下:
调用GetStdHandle获取当前的标准输入(STDIN)和标准输出(STDOUT)设备句柄。函数原型为:
HANDLE GetStdHandle( DWORD nStdHandle );
其中,nStdHandle可以是STD_INPUT_HANDLE(标准输入设备句柄)、STD_OUTPUT_HANDLE(标准输出设备句柄)和
STD_ERROR_HANDLE(标准错误句柄)。需要说明的是,“句柄”是
Windows最常用的概念。它通常用来标识Windows资源(如菜单、图标、窗口等)和设备等对象。虽然可以把句柄理解为是一个指针变量类型,但它不是对象所在的地址指针,而是作为Windows系统内部表的索引值来使用的。
调用相关文本界面控制的API函数。这些函数可分为三类。一是用于控制台窗口操作的函数(包括窗口的缓冲区大小、窗口前景字符和背景颜色、窗口标题、大小和位置等);二是用于控制台输入输出的函数(包括字符属性操作函数);其他的函数并为最后一类。
调用CloseHandle()来关闭输入输出句柄。
注意,在程序中还必须包含头文件windows.h。下面看一个程序: #include <stdio.h>
#include <windows.h>
#include <conio.h>
void main()
{
HANDLE hOut;
hout = GetStdHandle(STD_OUTPUT_HANDLE);
// 获取标准输出设备句柄
CONSOLE_SCREEN_BUFFER_INFO bInfo; // 窗口信息
GetConsoleScreenBufferInfo(hOut, &bInfo ); // 获取窗口信息 printf("\n\nThe soul selects her own society,\n");
printf("Then shuts the door;\n");
printf("On her devine majority\n");
printf("Obtrude no more.\n\n");
_getch();
COORD pos = {0, 0};
FillConsoleOutputCharacter(hOut, ' ', bInfo.dwSize.X * bInfo.dwSize.Y, pos, NULL);vc,windows,窗口文本颜色。
// 向窗口中填充字符以获得清屏的效果
CloseHandle(hOut); // 关闭标准输出设备句柄
}
程序中,COORD和CONSOLE_SCREEN_BUFFER_ INFO是wincon.h定义的控制台结构体类型,其原型如下:
// 坐标结构体
typedef struct _COORD {
SHORT X;
SHORT Y;
} COORD;
// 控制台窗口信息结构体
typedef struct _CONSOLE_SCREEN_BUFFER_INFO {
COORD dwSize; // 缓冲区大小
COORD dwCursorPosition; // 当前光标位置
WORD wAttributes; // 字符属性
SMALL_RECT srWindow; // 当前窗口显示的大小和位置
COORD dwMaximumWindowSize; // 最大的窗口缓冲区大小
} CONSOLE_SCREEN_BUFFER_INFO ;
还需要说明的是,虽然在C++中,iostream.h定义了cin和cout的标准输入和输出流对象。但它们只能实现基本的输入输出操作,对于控制台窗口界面的控制却无能为力,而且不能与stdio.h和conio.h友好相处,因为iostream.h和它们是C++两套不同的输入输出操作方式,使用时要特别注意。
三、控制台窗口操作
用于控制台窗口操作的API函数如下:
GetConsoleScreenBufferInfo 获取控制台窗口信息
GetConsoleTitle 获取控制台窗口标题
ScrollConsoleScreenBuffer 在缓冲区中移动数据块
SetConsoleScreenBufferSize 更改指定缓冲区大小
SetConsoleTitle 设置控制台窗口标题
SetConsoleWindowInfo 设置控制台窗口信息
http://m.myl5520.com/mingrenmingyan/100829.html