添加背景图片方法1
a。
CBrush cbrsh;
// Generated message map functions
//{{AFX_MSG(CTupianDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
//}}AFX_MSG
afx_msg HBRUSH OnCtlColor(
CDC* pDC,
CWnd* pWnd,
UINT nCtlColor );
DECLARE_MESSAGE_MAP()
b。
BEGIN_MESSAGE_MAP(CTupianDlg, CDialog)
//{{AFX_MSG_MAP(CTupianDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CTLCOLOR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
c。
BOOL CTupianDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
CBitmap cbit;
cbit.LoadBitmap(IDB_BITMAP1);
cbrsh.CreatePatternBrush(&cbit);
cbit.DeleteObject();
return TRUE; // return TRUE unless you set the focus to a control
}
d。
HBRUSH CTupianDlg::OnCtlColor(
CDC* pDC,
CWnd* pWnd,
UINT nCtlColor )
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if (pWnd == this)
{
return cbrsh;
}
return hbr;
}
添加背景图片方法2
定位到 void CXXXDlg::OnPaint(),在if()...else()中的else()下添加如下代码:
//CDialog::OnPaint();//要禁止这个调用 ??
CPaintDC dc(this);
CRect rect;
GetClientRect(&rect);
CDC dcMem;
dcMem.CreateCompatibleDC(&dc);
CBitmap bmpBackground;
bmpBackground.LoadBitmap(IDB_BITMAP); //IDB_BITMAP是你自己的图对应的ID
BITMAP bitmap;
bmpBackground.GetBitmap(&bitmap);
CBitmap *pbmpOld=dcMem.SelectObject(&bmpBackground);
dc.StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem,
0,0, //选图片中的某部分
bitmap.bmWidth,bitmap.bmHeight,SRCCOPY); //这个函数是会把图片拉伸到符合客户区大小的
添加背景图片方法3
用BitBlt函数,不会拉伸图片
CPaintDC dc(this);
CRect rect;
GetClientRect(&rect);
CDC dcMem;
dcMem.CreateCompatibleDC(&dc);
CBitmap bmpBackground;
bmpBackground.LoadBitmap(IDB_BITMAP1); //IDB_BITMAP是你自己的图对应的ID
//BITMAP bitmap;
//bmpBackground.GetBitmap(&bitmap);
CBitmap *pbmpOld=dcMem.SelectObject(&bmpBackground);
//dc.StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem,0,0, bitmap.bmWidth,bitmap.bmHeight,SRCCOPY);
dc.BitBlt(0, 0, //图片起始点位置
rect.Width(), //客户区宽度
rect.Height(), //客户区高度
&dcMem,
5, //只选取图片的部分显示
5,
SRCCOPY);
添加背景色方法一
void CFlipCardsDlg::OnPaint()
{
if (IsIconic())
{
//保持不变
}
else
{
CRect rc;
GetClientRect( &rc );// 获取客户区
CPaintDC dc(this);
dc.FillSolidRect(&rc, RGB(0,160,0)); // 填充客户区颜色
CDialog::OnPaint();
}
}
添加背景色方法二
注意 这里绘制的颜色是针对程序中所有的对话框(如果是单文档的话,好像不行)
SetDialogBkColor(RGB(0,0,255),RGB(255,0,0));
// 前一个RGB是背景色,后一RGB是文本颜色
该函数放在工程的APP文件的初始化函数中。
CTupianApp::CTupianApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
SetDialogBkColor(RGB(0,0,255),RGB(255,0,0));
}
添加背景色方法三
a。
在要着色的对话框中申明一个变量,CBRUSH m_hbrush; 并在类初始化函数里初始化
protected:
HICON m_hIcon;
CBrush m_bsh;
// Generated message map functions
//{{AFX_MSG(CTupianDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
//}}AFX_MSG
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
DECLARE_MESSAGE_MAP()
CTupianDlg::CTupianDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTupianDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTupianDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_bsh.CreateSolidBrush(RGB(255, 0, 0));
}
b。 在类中 声明函数OnCtlColor。。。。把OnCtlColor 加入消息映射
ON_WM_CTLCOLOR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
c。实现函数OnCtlColor
HBRUSH CTupianDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if (pWnd == this)
{
return m_bsh;
}
return hbr;
}
windows背景音乐
MFC 中设置背景音乐问题
#include <mmsystem.h>
#pragma comment( lib, "Winmm.lib" )
PlaySou nd函数的声明为:
BOOL PlaySound(LPCSTR pszSound, HMODULE hmod,DWORD fdwSound);
第一种方法是直接播出声音文件,相应的代码为:
PlaySound("c://win95//media//The Microsoft Sound.wav", NULL, SND_FILENAME | SND_ASYNC);
注意参数中的路径使用两个连续的反斜杠转义代表一个反斜杠。
第二种方法是把声音文件加入到资源中,然后从资源中播放声音。、、、、、、、、、、、、、vc6不行
Visual C++支持WAVE型资源,用户在资源视图中单击鼠标右键并选择Import命令,
然后在文件选择对话框中选择The Microsoft Sound.wav文件,则该文件就会被加入到WAVE资源中。
假定声音资源的ID为IDR_STARTWIN,则下面的调用同样会输出启动声音:
PlaySound((LPCTSTR)IDR_STARTWIN, AfxGetInstanceHandle(), SND_RESOURCE | SND_ASYNC);
第三种方法是用PlaySound播放系统声音,Windows启动的声音是由SystemStart定义的系统声音,因此可以用下面的方法播放启动声音:
PlaySound("SystemStart",NULL,SND_ALIAS|SND_ASYNC);
函数sndPlaySound的功能与PlaySound类似,但少了一个参数。函数的声明为:
BOOL sndPlaySound(LPCSTR lpszSound, UINT fuSound);
除了不能指定资源名字外,参数lpszSound与PlaySound的是一样的。参数fuSound是如何播放声音的标志,可以是SND_ASYNC、SND_LOOP、SND_MEMORY、SND_NODEFAULT、SND_NOSTOP和SND_SYNC的组合,这些标志的含义与PlaySound的一样。
第四种方法: 、、、//注意: 该函数是非阻塞函数,所以在播放期间要保证该进程不结束
sndPlaySound不能直接播放声音资源。要用该函数播放WAVE文件,可按下面的方式调用:
sndPlaySound(“MYSOUND.WAV”,SND_ASYNC);
自己把资源添在Debug文件夹里。
为了方便,部分引用自: http://blog.renren.com/share/269483805/8480405773