软件开发中,为了让用户更喜欢使用我们的软件,除了具有良好的性能外,还需要美观的界面。
本文简单地介绍下对话框背景贴图和按钮贴图,以及Static控件自定义成各种Static效果。
首先贴个图,让大家看看效果
1.新建对话框应用程序
2.添加图片资源
3.背景贴图
重写OnPaint函数
void CDlgBmpTestDemoDlg::OnPaint()
{
CPaintDC dc(this); // 用于绘制的设备上下文
//绘制背景
CBitmap bitmap;
bitmap.LoadBitmap(MAKEINTRESOURCE(IDB_BITMAP_PIC));
BITMAP bmpInfo;
bitmap.GetBitmap(&bmpInfo);
CDC bitmapDC;
bitmapDC.CreateCompatibleDC(&dc);
CBitmap* pOldBitmap = bitmapDC.SelectObject(&bitmap);
dc.BitBlt(0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, &bitmapDC,0, 0, SRCCOPY);
bitmapDC.SelectObject(pOldBitmap);
bitmap.DeleteObject();
}
设置位置和大小
<span style="white-space:pre"> </span>//对话框背景图片宽和高
const int nBkBmpWidth = 480;
const int nBkBmpHeight = 300;
int xPos = 0;
int yPos = 0;
//获得电脑显示器的像素宽度和像素高度
int ax = GetDC()->GetDeviceCaps(HORZRES) - nBkBmpWidth;
int ay = GetDC()->GetDeviceCaps(VERTRES) - nBkBmpHeight;
int nWidth = 0;
int nHeight = 0;
if(ax <= 0)
{ ax = 0; }
else
{ ax = ax/2; }
if(ay <= 0)
{ ay = 0; }
else
{ ay = ay/2; }
RECT clientRect;
RECT rt;
clientRect.left = ax;
clientRect.top = ay;
clientRect.right = clientRect.left + nBkBmpWidth;
clientRect.bottom = clientRect.top + nBkBmpHeight;
MoveWindow(&clientRect);
效果
4.按钮贴图
OwnerDraw的属性设置为True
定义变量
CBitmapButton m_btnOne;
加载图片资源
m_btnOne.LoadBitmaps(IDB_BITMAP_BTN);
设置按钮位置和大小
const int nBottomBtnWidth = 95;
const int nBottomBtnHeight = 30;
const int nPadding = 8;//按钮的水平间距
xPos = 0;
yPos = 0;
yPos = clientRect.top + 250;
//xPos = clientRect.left + nPadding;
xPos = clientRect.left + nPadding*4 + nBottomBtnWidth*3;
m_btnOne.MoveWindow(xPos,yPos,nBottomBtnWidth,nBottomBtnHeight);
效果
5.Static标题自定义
DlgTitleStatic.h
#pragma once
// CDlgTitleStatic
class CDlgTitleStatic : public CStatic
{
DECLARE_DYNAMIC(CDlgTitleStatic)
public:
CDlgTitleStatic();
virtual ~CDlgTitleStatic();
protected:
DECLARE_MESSAGE_MAP()
private:
CString m_sTitleText;
LOGFONT m_LogFontTitle;
UINT m_nFormat;
DWORD m_dwColor;
public:
afx_msg void OnPaint();
void SetTitleText(CString sTitleText);
};
DlgTitleStatic.cpp
// DlgTitleStatic.cpp : 实现文件
//
#include "stdafx.h"
#include "DlgBmpTestDemo.h"
#include "DlgTitleStatic.h"
// CDlgTitleStatic
IMPLEMENT_DYNAMIC(CDlgTitleStatic, CStatic)
CDlgTitleStatic::CDlgTitleStatic()
{
m_LogFontTitle.lfHeight = 32;
m_LogFontTitle.lfWeight = 600;
m_LogFontTitle.lfOrientation= 0;
m_LogFontTitle.lfEscapement = 0;
m_LogFontTitle.lfWidth = 14;
m_LogFontTitle.lfItalic = false;
m_LogFontTitle.lfUnderline = false;
m_LogFontTitle.lfStrikeOut = false;
m_LogFontTitle.lfCharSet = GB2312_CHARSET;
m_LogFontTitle.lfOutPrecision = OUT_DEFAULT_PRECIS;
m_LogFontTitle.lfClipPrecision = CLIP_DEFAULT_PRECIS;
m_LogFontTitle.lfQuality = DRAFT_QUALITY;
m_LogFontTitle.lfPitchAndFamily = DEFAULT_PITCH;
wcscpy_s(m_LogFontTitle.lfFaceName,_T("黑体"));
m_nFormat = DT_SINGLELINE|DT_CENTER |DT_VCENTER|DT_END_ELLIPSIS;
//m_nFormat = DT_LEFT |DT_TOP |DT_END_ELLIPSIS;
m_dwColor = RGB(255,0,0);
m_sTitleText = _T("对话框测试");
}
CDlgTitleStatic::~CDlgTitleStatic()
{
}
BEGIN_MESSAGE_MAP(CDlgTitleStatic, CStatic)
ON_WM_PAINT()
END_MESSAGE_MAP()
// CDlgTitleStatic 消息处理程序
void CDlgTitleStatic::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: 在此处添加消息处理程序代码
// 不为绘图消息调用 CStatic::OnPaint()