得到系统时间日期(使用GetLocalTime)
1:
2: 分离字串
3: 得到当前目录 (GetCurrentDirectory)
4: 从字符串中提取数字
5: 创建无模对话框
6: 得到窗口绝对坐标
7: 复制文件夹
8: 捕获 Ctrl+鼠标左键组合
9: 开机时即启动某一程序
10: 窗口前端显示与取消窗口前端显示
11: 下面是常见的Afx全局函数:
12: CString 与char []之间的转换
13: 关闭程序
14: 在关闭窗口时,当要对文件进行保存时
15: 如何修改窗体的标题:
16: 得到窗体的标题:
17: 在多文档/视图中子窗口的最大化:
18: 在多文档/视图中屏蔽子对话框:在APP类里把这两句话屏蔽掉
19: 在多文档/视图中关闭子窗口:
20: 装进自定义的光标
21: 怎样禁止改变窗口的大小和不能移动的窗口:
22: 使窗口始终在最前方:
23: 如何得到计算机当前用户名:
24: 如何得到‘我的文档’文件夹路径:
25:向文本文件中在原有的内容上加入新内容的方法:
26.一个函数可以让窗口在拖动时显示窗口内容
27.把0-36的数随机排列出来
28.实现标题条的显示与隐藏
1: 得到系统时间日期(使用GetLocalTime)
CString sTime,sYear,sMonth,sDay;
SYSTEMTIME CurTime;
GetLocalTime(&CurTime);
sYear.Format("%d年",CurTime.wYear);
sMonth.Format("%d月",CurTime.wMonth);
sDay.Format("%d日",CurTime.wDay);
sTime = sYear+ sMonth + sDay;
// CurTime.wHour
// CurTime.wMinute
// CurTime.wSecond IBM的
// </r/n>表示换行
AfxMessageBox(sTime);
2: 分离字串
CString str = "4d3f0a2278";
unsigned char a[12];
long x;
for(int i = 0;i< (str.GetLength()/2);i++)
{
sscanf(str.Mid(2*i,2),"%x",&x);
a[i] = x;
}
3: 得到当前目录 (GetCurrentDirectory)
char CurPath[MAX_PATH];
DWORD size=MAX_PATH;
GetCurrentDirectory(size,CurPath);
AfxMessageBox(CurPath);
//
CString number;
int len = LineLength(LineIndex(0));
LPTSTR p=number.GetBuffer(len);
this->GetLine(0,p,len);
AfxMessageBox(number);
得到系统目录 (GetSystemDirectory)
4: 从字符串中提取数字
CString strNum;
CString str("测试125各国87kk");
strNum = GetStr(str);
AfxMessageBox(strNum);
5: 创建无模对话框
CDlg_Test *aa = new CDlg_Test;
aa->Create(IDD_DIALOG1,NULL);
aa->ShowWindow(SW_SHOW);
6: 得到窗口绝对坐标
CString strNum,strNum1;
CRect rect;
GetClientRect(&rect);
ClientToScreen(&rect);
strNum.Format("X: %d",rect.top);
strNum1.Format(" Y: %d",rect.left);
strNum = strNum + strNum1;
AfxMessageBox(strNum);
7: 复制文件夹
SHFILEOPSTRUCT Op;
char FromBuf[]="E://temp/0";
char ToBuf[]="////SINTEKSERVER//个人文档//陈 伟/0";;
Op.hwnd = NULL;
Op.wFunc = FO_COPY;
Op.pFrom = FromBuf;
Op.pTo = ToBuf;
Op.fFlags = FOF_NOCONFIRMATION | FOF_RENAMEONCOLLISION ;
Op.fAnyOperationsAborted = FALSE;
Op.hNameMappings = NULL;
Op.lpszProgressTitle = NULL;
if(SHFileOperation(&Op) == 0)
MessageBox("复制完毕","提示",MB_OK|MB_ICONINFORMATION);
8: 捕获 Ctrl+鼠标左键 组合
case WM_LBUTTONDOWN://鼠标消息wParam ==
if (wParam & MK_CONTROL)
MessageBox(hwnd,"aaa","bbb",MB_OK);
break;
或
case WM_LBUTTONDOWN:
if(GetKeyState(VK_CONTROL)<0)
MessageBox(hwnd,"aaa","bbb",MB_OK);
break;
9:开机时即启动某一程序
方法一:
利用WIN.INI文件实现相关程序的自动启动。
通过WritePrivateProfileString函数来实现。
代码如下:
void CAutoRunDlg::OnApply() //假设本问题的对应工程为AutoRun
{
//更新数据
UpdateData(TRUE);
//写入ini文件
LPCTSTR filename;
filename=m_strFileName; //保存了需要被自动执行程序的完整路径
WritePrivateProfileString(_T("windows"),_T("load"),filename,_T("c:windows//win.ini"));
}
方法二:
写入到HOTKEY_LOCAL_MACHINE/SOFTWARE/MICROSOFT/WINDOWS/CURRENTVERSION/RUN
下面是一个函数:
void COAMinderDlg::AutoReg()
{
CRegKey m_Key;
if(m_Key.Open(HKEY_LOCAL_MACHINE,"SOFTWARE//Microsoft//Windows//CurrentVersion//Run")!=ERROR_SUCCESS)
{
if(m_Key.Create(HKEY_LOCAL_MACHINE,"SOFTWARE//Microsoft//Windows//CurrentVersion//Run")==ERROR_SUCCESS)
{
//AfxMessageBox(".",MB_ICONINFORMATION);
}
}
char strFullPath[255];
GetModuleFileName(AfxGetInstanceHandle(),strFullPath,255);
m_Key.SetValue(strFullPath,"DigitalTitan");
m_Key.Close();
}
10: 窗口前端显示与取消窗口前端显示。
::SetWindowPos(GetSafeHwnd(),HWND_TOPMOST,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE);
::SetWindowPos(GetSafeHwnd(),HWND_NOTOPMOST,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE);
11:下面是常见的Afx全局函数:
AfxFormatString1:类似printf一般地将字符串格式化
AfxFormatString2:类似printf一般地将字符串格式化
AfxMessageBox:类似Windows API 函数 MessageBox
AfxOuputDebugString:将字符串输往除错装置
AfxGetApp:获得application object (CwinApp派生对象)的指针
AfxGetMainWnd:获得程序主窗口的指针
AfxGetInstance:获得程序的instance handle
12: CString 与char []之间的转换.
在VC中,恐怕这两个是经常要进行转换的吧
char str[10] = ”str”;
CString sstr = “sstr”;
sstr.Format(“%s”,str);
strcpy(str,(LPCTSTR)sstr);
13: 关闭程序:
PostQuitMessage(WM_CLOSE); 或者PostQuitMessage(WM_DESTROY);
更绝的是关闭所有的程序:::ExitWindows ();
14:在关闭窗口时,当要对文件进行保存时,可在这里添加函数:
1.)在CMainFrame里的OnClose()里,用MessageBox("内容","标题",组合形式);组合形式可以查看MSDN的MESSAGEBOX( ) Function
2.)在CXXXDoc::SaveModified() 里,只能用AfxMessageBox("");
不能用MessageBox()函数
15:如何修改窗体的标题:
1.)修改主窗口的标题:m_pMainWnd->SetWindowText("你的标题");
2.)如果在你的document类中进行改,则直接调用SetTitle("..."),如果在你的view类中改,则GetDocument()->SetTitle("...")
3.)如果想使窗口的标题全部替换,则用:AfxGetMainWnd()->SetWindowText("你的标题");
16: 得到窗体的标题:
1.)AfxGetMainWnd()->GetWindowText();
2.)先FindWindow()找到窗口的HWND,在GetWindowText();
17:在多文档/视图中,子窗口的最大化:
void CChildFrame::ActivateFrame(int nCmdShow)
{
// TODO: Add your specialized code here and/or call the base class
nCmdShow=SW_MAXIMIZE;
CMDIChildWnd::ActivateFrame(nCmdShow);
}
18: 在多文档/视图中屏蔽子对话框:在APP类里把这两句话屏蔽掉
if (!ProcessShellCommand(cmdInfo))
return FALSE;
19:在多文档/视图中关闭子窗口:
::SendMessage(::AfxGetMainWnd()->m_hWnd, WM_COMMAND,ID_FILE_CLOSE,0);
20:装进自定义的光标
在装进自定义的光标后,在移动的过程中,鼠标的形状总是在自定义和默认
的光标之间晃动,可以这样解决,在视中的PreCreateWindow()中加入如
下几句:
BOOL CXXXXView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.lpszClass =AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW,0,
(HBRUSH)::GetStockObject (WHITE_BRUSH),0);
return CView::PreCreateWindow(cs);
}
21: 怎样禁止改变窗口的大小和不能移动的窗口:
再 CMainFrame的OnCreate函数中加入:
CMenu *pTopMenu=GetSystemMenu(false);
pTopMenu->RemoveMenu(4,MF_BYPOSITION);//最大化窗口不可用
pTopMenu->RemoveMenu(2,MF_BYPOSITION);//size
pTopMenu->RemoveMenu(1,MF_BYPOSITION);//使不可移动
22:使窗口始终在最前方:
只要在App类中的InitInstance()函数中加入以下代码就可以了:
BOOL CwindowOnTopApp:: InitInstance()
{
//此处略去了VC自动生成的代码
m_pMainWnd->showWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
m_pMainWnd->SetWindowPos(&CWnd::WndTopMost,0,0,0,0,
SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
Return true;
}
23: 如何得到计算机当前用户名;
char CurrentHostName[100];
ULONG nSize = sizeof(CurrentHostName);
GetUserName(CurrentHostName, &nSize);
24: 如何得到‘我的文档’文件夹路径;
TCHAR szPath[MAX_PATH];
SHGetFolderPath(NULL, CSIDL_PERSONAL , NULL, 0, szPath);
25:向文本文件中在原有的内容上加入新内容的方法
CString str = pTemp.sName+"/"+pTemp.sMphone+"/"+pTemp.sOphone+"/"+pTemp.sOphone+"/r/n";
char* pStr = str.GetBuffer(str.GetLength());
FILE* pFile = fopen(m_sUserPath[3],"a+");
if (pFile)
{
fputs(pStr,pFile);
}
str.ReleaseBuffer();
26.一个函数可以让窗口在拖动时显示窗口内容
实现窗口在拖动时显示窗口内容与隐藏窗口内容相互切换方法:
void CTDlg::OnButton1()
{
// TODO: Add your control notification handler code here
bool isFullWindows =FALSE;
SystemParametersInfo(SPI_GETDRAGFULLWINDOWS,NULL,&isFullWindows,0);
if (isFullWindows) // Drag Full Windows
{
SystemParametersInfo(SPI_SETDRAGFULLWINDOWS,FALSE,NULL,0);
isFullWindows =FALSE;
}
else
{// Drag Only the outline border
SystemParametersInfo(SPI_SETDRAGFULLWINDOWS,TRUE,NULL,0);
isFullWindows =TRUE;
}
}
27.把0-36的数随机排列出来
#include <iostream>
#include <ctime>
#include <algorithm>
using namespace std;
#define NUM 36
int a[NUM];
void main()
{
srand( static_cast<unsigned>( time(NULL) ) ); //随机种子还是要提供的
for(int i=0;i<NUM;++i)
{
a[i] = i;
}
copy( a,a+NUM,ostream_iterator<int>( cout," " )); //输出
cout<<endl;
random_shuffle( a,a+NUM ); //随机重排第一次
copy( a,a+NUM,ostream_iterator<int>( cout," " ));
cout<<endl;
}
28.实现标题条的显示与隐藏
// 使用API实现
m_bViewTitleBar = !m_bViewTitleBar;
LONG lStyle = ::GetWindowLong(this->m_hWnd, GWL_STYLE);
if (m_bViewTitleBar == FALSE) { // 隐藏TitleBar
::SetWindowLong(this->m_hWnd, GWL_STYLE, lStyle & ~WS_CAPTION);
::SetWindowPos(this->m_hWnd, NULL, 0, 0, 0, 0,
SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
}
else { // 显示TitleBar
::SetWindowLong(this->m_hWnd, GWL_STYLE, lStyle | WS_CAPTION);
::SetWindowPos(this->m_hWnd, NULL, 0, 0, 0, 0,
SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
}
/*
// 使用CWnd成员函数ModifyStyle实现
m_bViewTitleBar = !m_bViewTitleBar;
if (m_bViewTitleBar == FALSE) { // 隐藏TitleBar
ModifyStyle(WS_CAPTION, 0, SWP_FRAMECHANGED);
}
else { // 显示TitleBar
ModifyStyle(0, WS_CAPTION, SWP_FRAMECHANGED);
}*/