当你使用了ClassWizard建立了控件和变量之间的联系后:当你修改了变量的值,而希望对话框控件更新显示,就应该在修改变量后调用UpdateData(FALSE);如果你希望知道用户在对话框中到底输入了什么,就应该在访问变量前调用UpdateData(TRUE)。
UpdateData(true);//将控件的值读到变量。
UpdateData(false);//将变量的值显示出来到控件
修改应用程序的外观,工具栏、状态栏编程
http://onlyone.hpsbhq.com/showitemend.asp?endid=50&leiclass=VC%B1%E0%B3%CC
1.为控件关联变量的方法:
先布置好对话框(就是在新建立的对话框上加上需要的按钮,文本框等),然后双击它,为它添加一个对话框类,然后ctrl+w 打开classwizard, 选择menmber variable。在class name中选择你的对话框对应的类.在下面会列出此对话框中的所有控件。双击需要的控件id,会弹出Add MemberVariable对话框,在其中Variable type中选择变量类型,然后输入你要关联的变量名称
VS2008中:在对话框处,对着控件右击,点增加变量 类向导则移动到properties属性了(类似闪电一样的图标 )
查看控件关联的变量:
在这里DoDataExchange()里查看:
DDX_Control就是将控件与控件变量相关联;IDC_TXT是控件ID,m_Static是关联的控件变量;
还有DDX_Text,是将Value型变量与控件关联。
2.为某个按钮添加事件响应函数:
双击该按钮,提示为它添加函数,在弹出的对话框给它取个名字,确定就跳到了响应函数编写代码的地方了。
3.设置默认按钮:
在类的构造函数处将第一个radio的值置为0(默认是-1)(注意,如果是多个radio,要把第一个的group属性勾选上)
3.设置对话框背景色:
a,在对话框调用DoModal()前调用SetDialogBkColor()
b,重载OnCtlColor 即对WM_CTLCOLOR消息的处理
if(nCtlColor==CTLCOLOR_DLG)
returnm_brushBlue;
returnhbr;
c,重载OnPaint()即对WM_PAINT消息的处理
CRect rect;
CPaintDCdc(this);
GetClientRect(rect);
dc.FillSolidRect(rect,RGB(0,255,0));
4.滚动条实现:(在建立单文档时,最后一步要选择CscrollView)
实现响应键盘的滚动条调节WM_KEYDOWN(添加OnKeyDown成员函数)
switch(nChar)
{
caseVK_HOME:
OnVScroll(SB_TOP,0,NULL);
OnHScroll(SB_LEFT,0,NULL);
break;
}
5,隐藏、显示任务栏:
CWnd *TaskBar;
TaskBar=FindWindow("Shell_TrayWnd",NULL);//获取任务栏窗口对象的指针
RECT TastBarRect;//保存任务栏的句柄
6.创建不规则窗口:(OnCreate, PreCreateWindow中设置)
CRgn m_irRgn;
// m_irRgn.CreateEllipticRgn(0,0,280,280);
// SetWindowRgn(m_irRgn,true);
7.改变窗口标题(CwinApp中有m_pMainWnd)
m_pMainWnd->SetWindowText("透明吧~");
去掉菜单项(CmainFrame类的PreCreateWindow函数)
cs.hMenu=NULL;
8.透明窗口:(CDC memDC;//程序中用到的内存上下文)
width=GetSystemMetrics(SM_CXSCREEN);
height=GetSystemMetrics(SM_CYSCREEN);
CWindowDCscreenDC(this);//获得屏幕窗口的上下文,用于初始化位图?
hbmp.CreateBitmap(width,height,screenDC.GetDeviceCaps(PLANES),screenDC.GetDeviceCaps(BITSPIXEL),NULL);
memDC.CreateCompatibleDC(NULL);
oldObject=memDC.SelectObject(&hbmp);//保存设备上下文原来的GdI对象
memDC.BitBlt(0,0,width,height,&screenDC,0,0,SRCCOPY);
响应WM_MOVING消息
void CMainFrame::OnMoving(UINT fwSide,LPRECT pRect)
{
CFrameWnd::OnMoving(fwSide,pRect);
//TODO: Add your message handler code here
Invalidate(NULL,TRUE);//使窗口重画;
}
9.总在最前面的设置
SetWindowPos(&wndTopMost,-1,-1,-1,-1,SWP_NOMOVE|SWP_NOSIZE);
10.窗口居中:
CenterWindow(GetDesktopWindow());
AfxGetMainWnd()->CenterWindow();
11.拖动非标题栏区来移动窗口:
voidCDragNonToolbarView::OnLButtonDown(UINT nFlags, CPoint point)
{
//TODO: Add your message handler code here and/or call default
CView::OnLButtonDown(nFlags,point);
GetParentFrame()->PostMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x,point.y)) ;
}
12.自动停靠窗口:
a.在mainFrame.h中添加(因为类向导中没有WM_WINDOWPOSCHANGING这个消息)
protected:
//{{AFX_MSG(CMainFrame)
afx_msgint OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msgvoid OnWindowPosChanging(WINDOWPOS* lpwndpos);
//NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks ofgenerated code!
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
b.在MainFrame.cpp中添加
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
//NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks ofgenerated code !
ON_WM_CREATE()
ON_WM_WINDOWPOSCHANGING()//(这里没有冒号)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
13.在窗口上写字
this->SetWindowText("XXX");
//////////////////////////////////////////
CWindowDC dc(this);
dc.TextOut(200,5,"AAAA");
14.设置桌面背景:
void CHideWindowDlg::OnSetWall()
{
//TODO: Add your control notification handler code here
CfileDialogOpenBmp(TRUE,NULL,NULL,OFN_HIDEREADONLY,"BitMapFiles(*.bmp)|*.bmp|",NULL);
OpenBmp.m_ofn.lpstrTitle="请选择一张位图作为桌面!";
if(OpenBmp.DoModal()==IDOK)
{
CStringFullName;
FullName=OpenBmp.GetPathName();
SystemParametersInfo(SPI_SETDESKWALLPAPER,0,(void*)(LPCTSTR)FullName,0);
}
}
15.闪烁标题栏:
void CMainFrame::OnTimer(UINT nIDEvent)
{
//TODO: Add your message handler code here and/or call default
FlashWindow(TRUE);
CFrameWnd::OnTimer(nIDEvent);
}
OnCreate函数上添加m_Timer=SetTimer(1,1000,NULL);
析构函数上添加KillTimer(m_Timer);
在InitInstance函数中添加m_pMainWnd->ShowWindow(SW_MINIMIZE);//因为只有在最小化的时候才会起作用
16.字体旋转:(很长的句子,第三个参数:int nEcapement表示字体倾斜的角度,900表示夹角是90度)
在View类中的OnDraw函数中
CFont RotateFont[4];
for(inti=0;i<4;i++)
{
RotateFont[i].CreateFont(27,0,i*900,0,400,FALSE,FALSE,0,0,
OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,
DEFAULT_PITCH|FF_ROMAN,"Arial");
CFont*pOldFont=(CFont*)pDC->SelectObject(&RotateFont[i]);
pDC->TextOut(180,180,"馊馒头社区");
pDC->SelectObject(pOldFont);
}
17.状态栏显示图标:
HICONhIcon;
NOTIFYICONDATAnid;
hIcon=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME));
nid.cbSize=sizeof(NOTIFYICONDATA);
nid.hWnd=GetSafeHwnd();
nid.uID=IDR_MAINFRAME;
nid.uFlags=NIF_ICON|NIF_TIP|NIF_MESSAGE;
nid.uCallbackMessage=255;//没用呀?
nid.hIcon=hIcon;
strcpy(nid.szTip,"任务栏图标");
Shell_NotifyIcon(NIM_DELETE,&nid);
18.对话框的动画弹出与消隐(响应WM_TIMER消息)
CRectdlgRect;
GetWindowRect(dlgRect);
CRectdesktopRect;
GetDesktopWindow()->GetWindowRect(desktopRect);
if(nIDEvent==1)
{
MoveWindow((desktopRect.Width()-dlgRect.Width()-dx)/2,
(desktopRect.Height()-dlgRect.Height()-dy)/2,
dlgRect.Width()+dx,dlgRect.Height()+dy);
if(dlgRect.Width()>=Width)dx=0;//这两句没有,窗口将无限扩大
if(dlgRect.Height()>=Height)dy=0;//防止越界
// if(dlgRect.Width()>=Width &&dlgRect.Height()>=Height)KillTimer(1);
}
if(nIDEvent==2)
{
MoveWindow((desktopRect.Width()-dlgRect.Width()-dx)/2,
(desktopRect.Height()-dlgRect.Height()-dy)/2,
dlgRect.Width()+dx1,dlgRect.Height()+dy1);
// if(dlgRect.Width()>=Width)dx1=0;
// if(dlgRect.Height()>=Height)dy1=0;//防止越界
// if(dlgRect.Width()<=0&& dlgRect.Height()<=0)
// {
// KillTimer(2);
// CDialog::OnOK();
// }
}
19播放声音:
#include "Mmsystem.h"
#pragma comment(lib,"Winmm.lib")//在PlaySound.cpp中添加Mmsystem.h文件包,并链接库Winmm.lib
20检测声卡
UINT result;
result=waveOutGetNumDevs();
if(result>0)
AfxMessageBox("声卡已安装");
else
AfxMessageBox("没有安装声卡!");
21:检测系统中有几个盘符:
DWORDm_DriveBitmask;
unsignedshort m_DriveNum=0,m_CDROMNum=0;
UINTIsLogicalCDROM;
CStringstr1,str2;
charDriveLabel;
m_DriveBitmask=GetLogicalDrives();
while(m_DriveBitmask)
{
m_DriveBitmask>>=1;
m_DriveNum++;
}
// str2.Format(总共有%d个盘符!\n",m_DriveNum);
str2.Format("%s","CD-ROMList:\r\n");
for(inti=3;i<=m_DriveNum;i++)
{
DriveLabel=(int)('A')+i-1;
str1.Format("%c:\\",DriveLabel);
IsLogicalCDROM=GetDriveType(str1);
if(IsLogicalCDROM==DRIVE_CDROM)
{
m_CDROMNum++;
str2=str2+str1+"\r\n";
}
}
str1.Format("CDROMNUMber:%d\r\n",m_CDROMNum);
str2=str2+str1;
SetDlgItemText(IDC_EDIT2,str2);//不小心设置成了富文本框,竟然不弹出窗口
22位图平铺
CBitmap bitmap;//在OnDraw函数中响应
CDCdcMemory;
bitmap.LoadBitmap(IDB_BITMAPPAVE);//位图Id可以导入,可以自建
dcMemory.CreateCompatibleDC(pDC);
CBitmap*pOldBitmap=dcMemory.SelectObject(&bitmap);
if(isPave==false)
pDC->BitBlt(0,0,280,160,&dcMemory,0,0,SRCCOPY);
else
for(inti=0;i<5;i++)//x=280 而分辨率为1024*768 如果调整分辨率会发析图标变少
for(intj=0;j<6;j++)//y=160
pDC->BitBlt(280*i,160*j,280,160,&dcMemory,0,0,SRCCOPY);
dcMemory.SelectObject(pOldBitmap);
23打开、关闭光驱
void CPopupMenuView::OnControlClose()
{
//TODO: Add your command handler code here
//mciSendString("SetcdAudio door closed wait",NULL,0,NULL);
if(dwReturn=mciSendCommand(DeviceID,MCI_SET,MCI_SET_DOOR_CLOSED,(DWORD)&mciSetParms))
MessageBox("没能关闭光驱!");
}
void CPopupMenuView::OnControlOpen()
{
//TODO: Add your command handler code here
//mciSendString("Set cdAudio door openwait",NULL,0,NULL);
MCI_OPEN_PARMSmciOpenParms;
mciOpenParms.lpstrDeviceType="cdaudio";
if(dwReturn=mciSendCommand(NULL,MCI_OPEN,MCI_OPEN_TYPE,(DWORD)&mciOpenParms))
MessageBox("无法打开设备!");
DeviceID=mciOpenParms.wDeviceID;
if(dwReturn=mciSendCommand(DeviceID,MCI_SET,MCI_SET_DOOR_OPEN,(DWORD)&mciSetParms))
MessageBox("没能弹出光驱!");
}
24屏保
PostMessage(WM_SYSCOMMAND,SC_SCREENSAVE,0);
ShellExecute(GetSafeHwnd(),"open","c:\\Ribbons.scr",NULL,NULL,SW_SHOWNORMAL);
25关机
void CAutoRunDlg::CloseWindow()
{
HANDLE hToken;
TOKEN_PRIVILEGEStkp;
if(!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken))
{
MessageBox("can'tshutdown the computer~");
return;
}
LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid);
tkp.PrivilegeCount=1;
tkp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken,FALSE,&tkp,0,NULL,0);
if(GetLastError()!=ERROR_SUCCESS)
MessageBox("can'tshutdown the computer~");
if(!ExitWindowsEx(EWX_POWEROFF,0))
MessageBox("can'tshutdown the computer~");
}
1 画图程序
CClientDCdc(this);
if(flag==1)
dc.SelectObject(&bluePen);
if(flag==2)
dc.SelectObject(&redPen);
endPoint=point;
if(begPoint.x>0)
{
dc.MoveTo(begPoint.x,endPoint.y);
dc.LineTo(endPoint.x,endPoint.y);
begPoint=endPoint;
}
任务栏添加图标:
NOTIFYICONDATA nid;
nid.cbSize=sizeof(NOTIFYICONDATA);
nid.hWnd=m_hWnd;
nid.hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
// nid.szTip="SystemTray";
strcpy(nid.szTip,"SystemTray");
nid.uCallbackMessage=WM_SYSTEMTRAY;
nid.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP;
nid.uID=ID_SYSTEMTRAY;
Shell_NotifyIcon(NIM_ADD,&nid);
组件学习:
POPUPMENU
Splash Screen//提示什么样的应用程序将马上出现
Windows Multimedia library//检测声卡函数用到
小程序:
显示,隐藏桌面/任务栏
用bmp图设置为桌面背景
标题栏闪烁用以提醒用户或引起用户注意
浙公网安备 33010602011771号