// Misc.cpp: implementation of the CWinMisc class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "WinMisc.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
int CWinMisc::ShowMsg(HWND hwnd,char *title,UINT uType,char *szFormat, ...)
{
char szBuffer [1024] ;
va_list pArgList ;
va_start (pArgList, szFormat) ;
vsprintf (szBuffer, szFormat, pArgList) ;
va_end (pArgList) ;
return MessageBox(hwnd,szBuffer,title,uType);
}
int CWinMisc::FolderSelect(HWND hp,char *dir) //��ʾ�ļ�ѡ��Ի���
{
BROWSEINFO sInfo;
char dispname[MAX_PATH];
dir[0]=0;
LPITEMIDLIST lpidlBrowse;
sInfo.hwndOwner = hp;
sInfo.pidlRoot = NULL;
sInfo.pszDisplayName = dispname;
sInfo.lpszTitle = "Select folder";
sInfo.ulFlags = BIF_RETURNONLYFSDIRS;
sInfo.lpfn = NULL;
sInfo.lParam = 0;
lpidlBrowse=SHBrowseForFolder(&sInfo);
if(lpidlBrowse != NULL)
{
SHGetPathFromIDList(lpidlBrowse,dispname); //ȡ���ļ�����
strcpy(dir,dispname);
}
if(lpidlBrowse != NULL)
{
CoTaskMemFree(lpidlBrowse);
}
return (lpidlBrowse != NULL);
}
int CWinMisc::GetAFileName(HWND hp,LPSTR fn)
{
OPENFILENAME ofn;
char szDirName[256]="";
char szFile[256];
char szFileTitle[256];
char szFilter[256]="All file(*.*)\0*.*\0";
szFile[0] = '\0';
memset(&ofn, 0, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hp;
ofn.lpstrFilter = szFilter;
ofn.nFilterIndex = 1;
ofn.lpstrFile= szFile;
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFileTitle = szFileTitle;
ofn.nMaxFileTitle = sizeof(szFileTitle);
ofn.lpstrInitialDir = szDirName;
ofn.Flags = NULL;
int ret = GetOpenFileName(&ofn);
strcpy(fn,szFile);
return ret;
}
int CWinMisc::ChooseColor(HWND hp,COLORREF *color)
{
static COLORREF cus[16]={0};
CHOOSECOLOR cc;
cc.lStructSize=sizeof(CHOOSECOLOR);
cc.hwndOwner=hp;
cc.hInstance=NULL;
cc.rgbResult=0;
cc.lpCustColors=cus;
cc.Flags=CC_ANYCOLOR|CC_FULLOPEN|CC_SOLIDCOLOR;
cc.lCustData=NULL;
cc.lpfnHook=NULL;
cc.lpTemplateName=NULL;
int ret = ::ChooseColor(&cc);
if(color)
*color = cc.rgbResult;
return ret;
}
int CWinMisc::ChooseFont(HWND hp,LOGFONT *lf)
{
int ret = ERROR;
if(lf == NULL)
return ERROR;
CHOOSEFONT cf;
cf.lStructSize = sizeof(CHOOSEFONT);
cf.hwndOwner = hp;
cf.hDC = NULL;
cf.lpLogFont = lf;
cf.iPointSize = 240;
cf.Flags = CF_BOTH|CF_TTONLY|CF_EFFECTS;
cf.rgbColors = 0;
cf.lCustData = NULL;
cf.lpfnHook = NULL;
cf.lpTemplateName = NULL;
cf.hInstance = ::GetModuleHandle(NULL);
ret = ::ChooseFont(&cf);
return ret;
}
HRESULT CWinMisc::CreateLink(LPCSTR lpszPathObj,LPSTR lpszPathLink,LPSTR WorkDir)
{ //lpszPathObj��Ŀ���ļ�����lpszPathLink�ǿ�ݷ�ʽ���ļ�����WorkDir����ʼ·��
HRESULT hres;
IShellLink* psl;
CoInitialize(NULL);
// Get a pointer to the IShellLink interface.
hres = CoCreateInstance(CLSID_ShellLink, NULL,CLSCTX_INPROC_SERVER,IID_IShellLink,(void **)&psl);
if (SUCCEEDED(hres))
{
IPersistFile* ppf;
// Set the path to the shortcut target and add the
// description.
psl->SetPath(lpszPathObj);
//psl->SetDescription(lpszDesc);
psl->SetWorkingDirectory(WorkDir);
// Query IShellLink for the IPersistFile interface for saving the
// shortcut in persistent storage.
hres = psl->QueryInterface(IID_IPersistFile, (void **)&ppf);
if (SUCCEEDED(hres))
{
WORD wsz[MAX_PATH]; // Ensure that the string is ANSI.
MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1,wsz, MAX_PATH);
// Save the link by calling IPersistFile::Save.
hres = ppf->Save(wsz, TRUE);
ppf->Release();
}
psl->Release();
}
return hres;
}
int CWinMisc::DeleteFolder( LPSTR pszFolder)
{
SHFILEOPSTRUCT fos ;
ZeroMemory( &fos, sizeof( fos)) ;
fos.hwnd = HWND_DESKTOP;
fos.wFunc = FO_DELETE ;
fos.fFlags = FOF_SILENT | FOF_NOCONFIRMATION ;
fos.pFrom = pszFolder ;
// ɾ���ļ��м�������
if( SHFileOperation( &fos)!=0)
return FALSE ;
return TRUE;
}
char * CWinMisc::GetRecourceAddr(DWORD id,char *type,long *size)
{
char *p;
HRSRC hrsrc=FindResource(GetModuleHandle(NULL),MAKEINTRESOURCE(id),type);
if(hrsrc==NULL)
{
MessageBox(0,"Load resource ERROR!","ERROR",MB_OK);
return ERROR;
}
p=(char *)LockResource(LoadResource(GetModuleHandle(NULL),hrsrc));
*size = SizeofResource(GetModuleHandle(NULL),hrsrc);
return p;
}
int CWinMisc::AddWindowStyle(HWND hwnd,UINT style)
{
UINT old=GetWindowLong(hwnd,GWL_STYLE);
old|=style;
SetWindowLong(hwnd,GWL_STYLE,old);
return OK;
}
int CWinMisc::AddWindowExtStyle(HWND hwnd,UINT ext_style)
{
UINT old=GetWindowLong(hwnd,GWL_EXSTYLE);
old|=ext_style;
SetWindowLong(hwnd,GWL_EXSTYLE,old);
return OK;
}
int CWinMisc::RemoveWindowStyle(HWND hwnd,UINT style)
{
UINT old=GetWindowLong(hwnd,GWL_STYLE);
old&=(!style);
SetWindowLong(hwnd,GWL_STYLE,old);
return OK;
}
int CWinMisc::RemoveWindowExtStyle(HWND hwnd,UINT ext_style)
{
UINT old=GetWindowLong(hwnd,GWL_EXSTYLE);
old&=(!ext_style);
SetWindowLong(hwnd,GWL_EXSTYLE,old);
return OK;
}
int CWinMisc::IsKeyDown(int nVirtKey)
{
return (GetKeyState(nVirtKey)&0xFF00)>>15;
}
int CWinMisc::SetTopMost(HWND hwnd,bool flag)
{
if(!flag)
SetWindowPos(hwnd,HWND_NOTOPMOST,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED) ;
else
SetWindowPos(hwnd,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE |SWP_FRAMECHANGED) ;
return OK;
}
int CWinMisc::PlaySound(void *buf)
{
::PlaySound((const char *)buf,NULL,SND_ASYNC|SND_MEMORY);
return OK;
}
int CWinMisc::MsgLoop()
{
MSG msg;
while (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return OK;
}