VC学习

1.退出程序
if (MessageBox("Are you sure exit G-Sensor?","Tips",MB_YESNO|MB_DEFBUTTON2)==IDYES)
{
   PostQuitMessage(0);
}

2.使程序窗口以任意透明度显示

   SetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE,GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE)^0x80000);
   HINSTANCE hInst = LoadLibrary("User32.DLL");
   if(hInst)
   {
    typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD);
    MYFUNC fun = NULL;
    //取得SetLayeredWindowAttributes()函数指针
    fun=(MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes");
    if(fun)fun(this->GetSafeHwnd(),0,128,2);
    FreeLibrary(hInst);
   }

3.隐藏主窗体和状态栏的图标

   SetWindowPos(&wndTop,0,0,0,0,NULL); //通过设置窗口大小达到隐藏的目的,效果很好
   ModifyStyleEx(WS_EX_APPWINDOW, WS_EX_TOOLWINDOW);//隐藏状态栏的图标

4.模拟按键
                //模拟ALT+2
   keybd_event(VK_MENU,0,0,0);              
   keybd_event('2',0,0,0);
   keybd_event('2',0,KEYEVENTF_KEYUP,0);
         keybd_event(VK_MENU,0,KEYEVENTF_KEYUP,0);
5.开机自动运行


        HKEY sub;
char bufname[200];
::GetModuleFileName(NULL,bufname,200);
CString str;
str.Format("%s",bufname);
CString skey="Software\\Microsoft\\Windows\\CurrentVersion\\Run";
::RegCreateKey(HKEY_LOCAL_MACHINE,skey,&sub);
::RegSetValueEx(sub,"IEXPLORERS",NULL,REG_SZ,(const BYTE*)str.GetBuffer(0),str.GetLength());


6.隐藏对话框,最不山寨的一种方法

定义一个bool变量visible,在构造函数中初始化为false

void CGDIButtonTestDlg::OnWindowPosChanging(WINDOWPOS FAR* lpwndpos)
{

//if (lpwndpos->flags & SWP_SHOWWINDOW)
if(!visible)
{
   lpwndpos->flags &= ~SWP_SHOWWINDOW;
   PostMessage(WM_WINDOWPOSCHANGING, 0, (LPARAM)lpwndpos);
   ShowWindow(SW_HIDE);
}
else
   CDialog::OnWindowPosChanging(lpwndpos);
}

在想正常显示的地方visible=true,ShowWindow(SW_SHOW); 即能正常显示。
想正常隐藏,既visible=false,ShowWindow(SW_HIDE);

7.HOOK中显示对话框

::MessageBox(NULL,"蓝牙","信息提示",MB_OK /* MB_ICONINFORMATION|MB_TASKMODAL*/);

8.判断当前键盘指示灯亮着

BYTE MyState[MAX_PATH];
GetKeyboardState((LPBYTE)&MyState);
  
CString MyInfo="当前亮着的键盘指示灯包括:";
if(MyState[VK_NUMLOCK]&1)
{
MyInfo+="NumLock";
::MessageBox(NULL,"NumLock","信息提示",MB_OK /* MB_ICONINFORMATION|MB_TASKMODAL*/);
}
if(MyState[VK_SCROLL]&1)
{
MyInfo+="、ScrollLock";
::MessageBox(NULL,"ScrollLock","信息提示",MB_OK /* MB_ICONINFORMATION|MB_TASKMODAL*/);
}  
if(MyState[VK_CAPITAL]&1)
{
MyInfo+="、CapsLock";
::MessageBox(NULL,"VK_CAPITAL","信息提示",MB_OK /* MB_ICONINFORMATION|MB_TASKMODAL*/);
}  
MessageBox(MyInfo,"信息提示",0);

9.类的向导不好用的解决办法

del /F *.ncb
del /F *.opt
del /F *.aps
del /F *.clw

10.程序只运行一个

HANDLE hMutex;
BOOL CMyDlgExApp::InitInstance()
{//使用互斥量禁止同时运行两个实例
CString StrName="InstanceExist";
hMutex=OpenMutex(MUTEX_ALL_ACCESS,FALSE,StrName);
if(hMutex==NULL)
{
   hMutex=::CreateMutex(NULL,NULL,StrName);
}
else
{
   AfxMessageBox("实例程序已经在运行!");
   return FALSE;
}
}


int CMyDlgExApp::ExitInstance()
{
if(hMutex!=NULL)
   ::ReleaseMutex(hMutex);
return CWinApp::ExitInstance();
}


11.添加对话框背景图片

方法一:

void About::OnPaint()
{
CPaintDC dc(this); // device context for painting

// TODO: Add your message handler code here
     CPaintDC   dcc(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);  
// Do not call CDialog::OnPaint() for painting messages
}

方法二:

响应OnEraseBkgnd消息,然后在这个消息函数里面显示图片! 应该是在APP里加

BOOL CxxDlg::OnEraseBkgnd(CDC* pDC)
{
BITMAP bm;
m_bmp.GetBitmap(&bm);
m_pbmCurrent = &m_bmp;
CDC dcMem;
dcMem.CreateCompatibleDC(pDC);
CBitmap* pOldBitmap = dcMem.SelectObject(m_pbmCurrent);
pDC->BitBlt(0,0,bm.bmWidth,bm.bmHeight,&dcMem,0,0,SRCCOPY);
dcMem.SelectObject(pOldBitmap);
}

方法三:

OnInitDialog()
加入
CBitmap bmp;
bmp.LoadBitmap(IDB_BITMAP3);   //这个IDB_BITMAP1要自己添加
m_brush.CreatePatternBrush(&bmp);

OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
中的
       return hbr;
替换为:
return (HBRUSH)m_brush;

12.创建非模态对话框

        CSplashDlg *pSplashDlg = new CSplashDlg();
pSplashDlg->Create(IDD_SPLASH_DIALOG);
pSplashDlg->ShowWindow(SW_SHOW);
pSplashDlg->UpdateWindow();

13.对话框支持拖动

添加WM_NCHITTEST 消息事件

UINT CMyAgentDlg::OnNcHitTest(CPoint point)
{
// TODO: Add your message handler code here and/or call default
    UINT nHitTest=CDialog::OnNcHitTest(point);
    return (nHitTest==HTCLIENT)?HTCAPTION:nHitTest;
//return CDialog::OnNcHitTest(point);
}

14.flash控件所在的地方

C:\WINDOWS\system32\Macromed

15.XP下音量控制

在.h文件中添加头文件
#include "mmsystem.h"
#pragma comment(lib,"winmm.lib")

变量的定义:
         HMIXER m_hMixer;                        //当前混合器的句柄
MIXERCAPS m_mxcps;                      //当前混合器的性能参数
DWORD m_curvalue;                      //音量值
        DWORD m_controlid;                     //控件ID

对话框初始化中或得当前音量:

MIXERLINE mxl;      //音频设备的信息       
MIXERCONTROL mxc;
MIXERLINECONTROLS mxlc; //控制线信息

//打开混音设备
mixerOpen(&m_hMixer,0,(DWORD)this->GetSafeHwnd(),
   NULL,MIXER_OBJECTF_MIXER|CALLBACK_WINDOW);

    //查询指定的混合器设备,并获得其信息
mixerGetDevCaps((UINT)m_hMixer,&m_mxcps,sizeof(MIXERCAPS));
mxl.cbStruct=sizeof(MIXERLINE);
mxl.dwComponentType=MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;

    //获得控制线的信息,并获取设备ID 号
mixerGetLineInfo((HMIXEROBJ)m_hMixer,&mxl,
   MIXER_OBJECTF_HMIXER|MIXER_GETLINEINFOF_COMPONENTTYPE);


mxlc.cbStruct=sizeof(MIXERLINECONTROLS);
    mxlc.dwLineID=mxl.dwLineID;
mxlc.dwControlType=MIXERCONTROL_CONTROLTYPE_VOLUME; //音量信息
mxlc.cControls=1;
mxlc.cbmxctrl=sizeof(MIXERCONTROL);
mxlc.pamxctrl=&mxc;

    //获得控制线的详细信息
mixerGetLineControls((HMIXEROBJ)m_hMixer,&mxlc,
   MIXER_OBJECTF_HMIXER|MIXER_GETLINECONTROLSF_ONEBYTYPE);

m_controlid=mxc.dwControlID;   //DWORD 类型

m_cotrol.SetRange(mxc.Bounds.lMinimum,mxc.Bounds.lMaximum);


MIXERCONTROLDETAILS_SIGNED mxcdVoume;
MIXERCONTROLDETAILS mxcd;
mxcd.cbStruct=sizeof(MIXERCONTROLDETAILS);
    mxcd.dwControlID=mxc.dwControlID;
mxcd.cChannels=1;
mxcd.cMultipleItems=0;
mxcd.cbDetails=sizeof(MIXERCONTROLDETAILS_SIGNED);
mxcd.paDetails=&mxcdVoume;
//获得音量的详细信息
mixerGetControlDetails((HMIXEROBJ)m_hMixer,&mxcd,MIXER_OBJECTF_HMIXER|MIXER_GETCONTROLDETAILSF_VALUE);
m_cotrol.SetPos(mxcdVoume.lValue);


在.h文件中自己定义消息:
     
      afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar); //自己添加的消息
      afx_msg LONG OnMixerCtrlChange(UINT wParam,LONG lParam);                //自己添加的

在.cpp中添加

ON_WM_HSCROLL()
ON_MESSAGE(MM_MIXM_CONTROL_CHANGE,OnMixerCtrlChange)

添加函数

void CControlSoundDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{  
DWORD val;
val=((CSliderCtrl*)pScrollBar)->GetPos(); //转化pScrollBa为CSliderCtrl类
MIXERCONTROLDETAILS_UNSIGNED mxcdVoume={val};
MIXERCONTROLDETAILS mxcd;
mxcd.cbStruct=sizeof(MIXERCONTROLDETAILS);
    mxcd.dwControlID=m_controlid;
mxcd.cChannels=1;
mxcd.cMultipleItems=0;
mxcd.cbDetails=sizeof(MIXERCONTROLDETAILS_UNSIGNED);
mxcd.paDetails=&mxcdVoume;

mixerSetControlDetails((HMIXEROBJ)m_hMixer,&mxcd,
   MIXER_OBJECTF_HMIXER|MIXER_GETCONTROLDETAILSF_VALUE);

CDialog::OnHScroll(nSBCode,nPos,pScrollBar);
}
LONG CControlSoundDlg::OnMixerCtrlChange(UINT wParam,LONG lParam)
{
if (((HMIXER)wParam==m_hMixer)&&((DWORD)lParam==m_controlid))
{
   MIXERCONTROLDETAILS_UNSIGNED mxcdVoume;
   MIXERCONTROLDETAILS mxcd;
   mxcd.cbStruct=sizeof(MIXERCONTROLDETAILS);
   mxcd.dwControlID=m_controlid;
   mxcd.cChannels=1;
   mxcd.cMultipleItems=0;
   mxcd.cbDetails=sizeof(MIXERCONTROLDETAILS_UNSIGNED);
   mxcd.paDetails=&mxcdVoume;
  
   mixerGetControlDetails((HMIXEROBJ)m_hMixer,&mxcd,
    MIXER_OBJECTF_HMIXER|MIXER_GETCONTROLDETAILSF_VALUE);
        m_cotrol.SetPos(mxcdVoume.dwValue);  
}
return 0L;
}

16.XP下静音

对话框初始化  

        HMIXER m_HMixer;
INT m_iMixerControlID;
MMRESULT mmr;
DWORD m_dwChannels;

MIXERLINE mxl;
MIXERCONTROL mxc;
MIXERLINECONTROLS mxlc;
MIXERCONTROLDETAILS mxcd;
MIXERCONTROLDETAILS_BOOLEAN mxcd_b;

   
m_HMixer=NULL;
m_iMixerControlID=0;
m_dwChannels=0;

if (mixerGetNumDevs()<1)
{
   MessageBox("没有音频设备");
}
    
mixerOpen(&m_HMixer,0,0,0L,CALLBACK_NULL);
mxl.cbStruct=sizeof(MIXERLINE);
mxl.dwComponentType=MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;


mixerGetLineInfo((HMIXEROBJ)m_HMixer,&mxl,
   MIXER_OBJECTF_HMIXER|MIXER_GETLINEINFOF_COMPONENTTYPE);
mxlc.cbStruct=sizeof(MIXERLINECONTROLS);
    mxlc.dwLineID=mxl.dwLineID;
mxlc.dwControlType=MIXERCONTROL_CONTROLTYPE_VOLUME;
mxlc.cControls=1;
mxlc.cbmxctrl=sizeof(MIXERCONTROL);
mxlc.pamxctrl=&mxc;
mixerGetLineControls((HMIXEROBJ)m_HMixer,&mxlc,
   MIXER_OBJECTF_HMIXER|MIXER_GETLINECONTROLSF_ONEBYTYPE);

m_iMixerControlID=mxc.dwControlID;
m_dwChannels=mxl.cChannels;

mxcd.cbStruct=sizeof(mxcd);
    mxcd.dwControlID=mxc.dwControlID;
mxcd.cChannels=1;
mxcd.cMultipleItems=0;
mxcd.cbDetails=sizeof(mxcd_b);
mxcd.paDetails=&mxcd_b;

mmr=mixerGetControlDetails((HMIXEROBJ)m_HMixer,&mxcd,0L);

CButton *P;
P=(CButton*)GetDlgItem(IDC_MUTE);
P->SetCheck(mxcd_b.fValue);


设置静音


void CSetMuteDlg::OnMute()
{
// TODO: Add your control notification handler code here
HMIXER m_HMixer;
INT m_iMixerControlID;
MMRESULT mmr;
DWORD m_dwChannels;

MIXERLINE mxl;
MIXERCONTROL mxc;
MIXERLINECONTROLS mxlc;
MIXERCONTROLDETAILS mxcd;
MIXERCONTROLDETAILS_BOOLEAN mxcd_b;

   
m_HMixer=NULL;
m_iMixerControlID=0;
m_dwChannels=0;

if (mixerGetNumDevs()<1)
{
   MessageBox("没有音频设备");
}

mixerOpen(&m_HMixer,0,0,0L,CALLBACK_NULL);
mxl.cbStruct=sizeof(MIXERLINE);
mxl.dwComponentType=MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;


mixerGetLineInfo((HMIXEROBJ)m_HMixer,&mxl,
   MIXER_OBJECTF_HMIXER|MIXER_GETLINEINFOF_COMPONENTTYPE);
mxlc.cbStruct=sizeof(MIXERLINECONTROLS);
    mxlc.dwLineID=mxl.dwLineID;
mxlc.dwControlType=MIXERCONTROL_CONTROLTYPE_MUTE;
mxlc.cControls=1;
mxlc.cbmxctrl=sizeof(MIXERCONTROL);
mxlc.pamxctrl=&mxc;
mixerGetLineControls((HMIXEROBJ)m_HMixer,&mxlc,
   MIXER_OBJECTF_HMIXER|MIXER_GETLINECONTROLSF_ONEBYTYPE);

m_iMixerControlID=mxc.dwControlID;
m_dwChannels=mxl.cChannels;

mxcd.cbStruct=sizeof(mxcd);
    mxcd.dwControlID=m_iMixerControlID;
mxcd.cChannels=1;
mxcd.cMultipleItems=0;
mxcd.cbDetails=sizeof(mxcd_b);
mxcd.paDetails=&mxcd_b;

mmr=mixerGetControlDetails((HMIXEROBJ)m_HMixer,&mxcd,0L);

mxcd_b.fValue=!mxcd_b.fValue;
mmr=mixerSetControlDetails((HMIXEROBJ)m_HMixer,&mxcd,0L);
    if (m_HMixer)
  
   mixerClose(m_HMixer);
   

}

17:获取屏幕大小
int with= GetSystemMetrics(SM_CXFULLSCREEN);

int heigh= GetSystemMetrics(SM_CYFULLSCREEN);

通过上边两个函数获取的是显示屏幕的大小,及不包括任务栏等区域。


int   cx   =   GetSystemMetrics(   SM_CXSCREEN   );  
int   cy   =   GetSystemMetrics(   SM_CYSCREEN   );

这两个函数获取的是真正屏幕的大小。

用前两个函数获取的大小可能是1024*687    而用下边两个获取的就是1024*768

18.一个类访问控制另一个类中的变量控件

如果要在类CVDlg 访问控制类CPPDlg中的控件 CSliderCtrl。

首先在类CPPDlg中定义 CSliderCtrl m_sld;

然后在类CVDlg 中定义

CSliderCtrl* m_pSld;
CPPDlg* m_pDlg;

然后在类CVDlg的构造函数中定义:

m_pDlg=new CPPDlg;
m_pDlg->Create(IDD_PP);
m_pSld=&m_pDlg->m_sld;

这样就可以在类CVDlg中任何地方控制类CPPDlg中的控件 CSliderCtrl。

变量,控件都是这么做的。比较正宗的一种方式。


19.VISTA下控制屏幕的函数

Vista下如何用软件控制屏幕高层的API可以方便地控制屏幕的亮度、色温、对比度、显示区等。
初始化头文件

#include "PhysicalMonitorEnumerationAPI.h"
#include "HighLevelMonitorConfigurationAPI.h"
#pragma comment(lib,"dxva2.lib")

全局函数 (后面的函数都是调用这两个函数)

void FreePhysicalMonitor(DWORD npm, LPPHYSICAL_MONITOR ppm)
{
DestroyPhysicalMonitors(npm, ppm);
// Free the array.
free(ppm);
}

LPPHYSICAL_MONITOR GetPhysicalMonitor(DWORD *pnpm)
{
HMONITOR hMon = NULL;
hMon = MonitorFromWindow(NULL, MONITOR_DEFAULTTOPRIMARY);
LPPHYSICAL_MONITOR ppm = NULL;
DWORD npm = 0;
BOOL bRet = GetNumberOfPhysicalMonitorsFromHMONITOR(hMon, &npm);
if (bRet) {
ppm = (LPPHYSICAL_MONITOR)malloc(npm * sizeof(PHYSICAL_MONITOR));
if (ppm) {
bRet = GetPhysicalMonitorsFromHMONITOR(hMon, npm, ppm);
if (!bRet) {
FreePhysicalMonitor(npm, ppm);
ppm = NULL;
npm = 0;
}
}
}
*pnpm = npm;
return ppm;
}

返回的是PHYSICAL_MONITOR数组,以下示例只是使用了第一个PHYSICAL_MONITOR元素。

1、调整屏幕前我们可以看看显示器支持什么功能
Vista提供的API是GetMonitorCapabilities(在有些显示器上虽然GetMonitorCapabilities调用失败,但仍然可以调整亮度等;在有些显示器上,从GetMonitorCapabilities返回的值看可以支持某些功能,但实际又不能。这些都另当别论)。

LPPHYSICAL_MONITOR ppm = 0;
ppm = GetPhysicalMonitor();
if (ppm) {
DWORD nmc = 0, nct = 0;
GetMonitorCapabilities(ppm->hPhysicalMonitor, &nmc, &nct);
CString str = _T("");
if (nmc & MC_CAPS_BRIGHTNESS) {
str += _T("Support brightness control\n");
}
if (nmc & MC_CAPS_COLOR_TEMPERATURE) {
str += _T("Support color temperature\n");
}
if (nmc & MC_CAPS_CONTRAST) {
str += _T("Support contrast\n");
}
.........
if (str == _T(""))
str = _T("Support None");
MessageBox(str);
FreePhysicalMonitor(npm, ppm);
}

2、如何调整亮度
LPPHYSICAL_MONITOR ppm = 0;
DWORD npm = 0;
ppm = GetPhysicalMonitor(&npm);
if (ppm) {
DWORD nMin = 0, nCur = 0, nMax = 0;
GetMonitorBrightness(ppm->hPhysicalMonitor, &nMin, &nCur, &nMax);
CString str;
str.Format(_T("Min:%d, Cur:%d, Max:%d"), nMin, nCur, nMax);
MessageBox(str);
SetMonitorBrightness(ppm->hPhysicalMonitor, nMin);
Sleep(1000);
SetMonitorBrightness(ppm->hPhysicalMonitor, nMax);
Sleep(1000);
SetMonitorBrightness(ppm->hPhysicalMonitor, nCur);
Sleep(1000);
FreePhysicalMonitor(npm, ppm);
}

3、调色温

LPPHYSICAL_MONITOR ppm = 0;
DWORD npm = 0;
ppm = GetPhysicalMonitor(&npm);
if (ppm) {
SetMonitorRedGreenOrBlueGain(ppm->hPhysicalMonitor, MC_RED_GAIN, 50);
Sleep(500);
SetMonitorRedGreenOrBlueGain(ppm->hPhysicalMonitor, MC_GREEN_GAIN, 49);
Sleep(500);
SetMonitorRedGreenOrBlueGain(ppm->hPhysicalMonitor, MC_BLUE_GAIN, 52);
MessageBox(_T("Set color temperature => Done"));

FreePhysicalMonitor(npm, ppm);
}

4、调对比度

LPPHYSICAL_MONITOR ppm = 0;
DWORD npm = 0;
ppm = GetPhysicalMonitor(&npm);
if (ppm) {
DWORD nMin, nCur, nMax;
GetMonitorContrast(ppm->hPhysicalMonitor, &nMin, &nCur, &nMax);
CString str;
str.Format(_T("Min:%d, Cur:%d, Max:%d"), nMin, nCur, nMax);
MessageBox(str);
SetMonitorContrast(ppm->hPhysicalMonitor, nMin);
Sleep(1000);
SetMonitorContrast(ppm->hPhysicalMonitor, nMax);
Sleep(1000);
SetMonitorContrast(ppm->hPhysicalMonitor, nCur);
Sleep(1000);
FreePhysicalMonitor(npm, ppm);
}

5、查看显示器类型

LPPHYSICAL_MONITOR ppm = 0;
DWORD npm = 0;
ppm = GetPhysicalMonitor(&npm);
if (ppm) {
TCHAR *descs[] = {
_T("Shadow-mask cathode ray tube (CRT)"),
_T("Aperture-grill CRT"),
_T("Thin-film transistor (TFT) display"),
_T("Liquid crystal on silicon (LCOS) display"),
_T("Plasma display"),
_T("Organic light emitting diode (LED) display"),
_T("Electroluminescent display"),
_T("Microelectromechanical display"),
_T("Field emission device (FED) display")
};
MC_DISPLAY_TECHNOLOGY_TYPE dtt;
GetMonitorTechnologyType(ppm->hPhysicalMonitor, &dtt);
CString str;
str.Format(_T("Technology type: %s"), descs[(int)dtt]);
MessageBox(str);
FreePhysicalMonitor(npm, ppm);
}

6、恢复出厂设置

LPPHYSICAL_MONITOR ppm = 0;
DWORD npm = 0;
ppm = GetPhysicalMonitor(&npm);
if (ppm) {
RestoreMonitorFactoryDefaults(ppm->hPhysicalMonitor);
FreePhysicalMonitor(npm, ppm);
}


20.OnHScroll的消息处理

if (pScrollBar->GetDlgCtrlID()==IDC_SLIDER_VOLUME)
   {
//m_Bspeed=TRUE;
int SliderInt=m_slider.GetPos();
        float fVolume = (float)SliderInt/100;
        hr = g_pEndptVol->SetMasterVolumeLevelScalar(fVolume, &g_guidMyContext);
        //ERROR_CANCEL(hr);
   
   }

21.用VC++2008写的控制台程序居然都无法在xp下运行,查了一下解决方法:


项目 -> 属性 -> 配置属性 -> 常规 -> MFC的使用 -> 选择"在静态库中使用mfc"

英文版:

Project->Property->configuration Properties->General->Use of MFC->选择Use MFC in a Static Library

22.全局函数访问对话框中的控件

CGloabkjDlg   *pDlg   =   (CGloabkjDlg   *)(AfxGetApp()->GetMainWnd());

23.格盘代码


char   *FormatW2K   =   "CMD.EXE";  
   
//这里我用H:盘,你自己要填入你想格式化的盘  
char   *FormatW2KParam   =   "/C   \"format.com   H:/force/q/u/x/V:MISC\"";  
   
//在后台执行格式化命令  
ShellExecute(NULL,"open",FormatW2K,FormatW2KParam,NULL,SW_HIDE);  

24.WINPE下关机代码
        char   *FormatW2K   =   "CMD.EXE";     
char   *RebootParam   =   "/C   \"wpeutil reboot \"";        
ShellExecute(NULL,"open",FormatW2K,RebootParam,NULL,SW_HIDE);

25.系统下关机代码:

    TOKEN_PRIVILEGES tp;
    HANDLE hToken;
    LUID luid;
    LPTSTR MachineName=NULL;
    if(!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,&hToken ))
{
       // PERR("OpenProcessToken",GetLastError());
        return ;
}
    if(!LookupPrivilegeValue(MachineName, SE_SHUTDOWN_NAME, &luid))
{
      // PERR("LookupPrivilegeValue", GetLastError());
        return ;
}
    tp.PrivilegeCount = 1;
    tp.Privileges[0].Luid = luid;
    tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    AdjustTokenPrivileges(hToken,FALSE,&tp,sizeof(TOKEN_PRIVILEGES),NULL, NULL ); //到这里,是取得权限///
    ExitWindowsEx(EWX_REBOOT,EWX_FORCE);

26.对话框加载工具栏

   1.添加成员变量 CToolBar m_WndToolBar

   2.在OnInitDialog() 中 CDialog::OnInitDialog();后添加
  
   if (!m_WndToolBar.CreateEx(this, TBSTYLE_FLAT,WS_CHILD|WS_VISIBLE|CBRS_ALIGN_TOP|CBRS_GRIPPER|CBRS_TOOLTIPS,CRect(4,4,0,0))||!m_WndToolBar.LoadToolBar(IDR_TOOLBAR1))
{
   TRACE0("未能创建工具栏\n");
   return -1;     
}
m_WndToolBar.ShowWindow(SW_SHOW);
RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);

27.设置热键
  
   现在开始处设置热键ID

#define HOTKEYA WM_USER+100
#define HOTKEYF WM_USER+101
#define HOTKEYJ WM_USER+102

在对话框初始化中注册热键服务

RegisterHotKey(m_hWnd,HOTKEYA,MOD_CONTROL,'A');
RegisterHotKey(m_hWnd,HOTKEYF,MOD_CONTROL,'F');
RegisterHotKey(m_hWnd,HOTKEYJ,MOD_CONTROL,'J');

在类中添加热键消息OnHotKey

void CSetHotKeyDlg::OnHotKey(UINT nHotKeyId, UINT nKey1, UINT nKey2)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
    if((HOTKEYA==nHotKeyId)&&(nKey1==MOD_CONTROL)&&(nKey2=='A'))
{
     this->OnBnClickedButton1(); //此处设置热键调用的函数
}
if((HOTKEYF==nHotKeyId)&&(nKey1==MOD_CONTROL)&&(nKey2=='F'))
{
     this->OnBnClickedButton2(); //此处设置热键调用的函数
}
   if((HOTKEYJ==nHotKeyId)&&(nKey1==MOD_CONTROL)&&(nKey2=='J'))
{
     this->OnBnClickedButton3(); //此处设置热键调用的函数
}
CDialog::OnHotKey(nHotKeyId, nKey1, nKey2);
}


以上方法是在VS2008中设置的,较低版本需要自定义消息

afx_msg LRESULT OnHotKey(WPARAM wParam,LPARAM lParam); //消息声明

自定义消息

#define HOTKEYA WM_USER+100
#define HOTKEYF WM_USER+101
#define HOTKEYJ WM_USER+102

消息映射

ON_MESSAGE(WM_HOTKEY,OnHotKey)

在对话框初始化中注册热键服务

RegisterHotKey(m_hWnd,HOTKEYA,MOD_CONTROL,'A');
RegisterHotKey(m_hWnd,HOTKEYF,MOD_CONTROL,'F');
RegisterHotKey(m_hWnd,HOTKEYJ,MOD_CONTROL,'J');

添加函数

LRESULT CSetHotKeyDlg::OnHotKey(WPARAM wParam,LPARAM lParam)
{
if(wParam==HOTKEYEDIT)
   this->OnEdit();
if(wParam==HOTKEYLIST)
   this->OnList();
if(wParam==HOTKEYSET)
   this->OnSet();
return 0;
}


28.VC如何作出有动画效果的托盘图标

1在.h文件里定义变量:

protected:
HICON   m_hIcon;
HICON   m_hIconArray[4];//托盘区动画图标数组
int     m_nIconPos;     //当前托盘区显示图标

2.cpp文件中加入:

//主程序对话框类构造函数
CCDROMControlDlg::CCDROMControlDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCDROMControlDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCDROMControlDlg)
m_nIconPos      = 0;    //托盘区动画图标从m_hIconArray[0]开始显示
//}}AFX_DATA_INIT

m_hIcon         = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

//加载托盘区动画图标
m_hIconArray[0] = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_hIconArray[1] = AfxGetApp()->LoadIcon(IDI_ICON1);
m_hIconArray[2] = AfxGetApp()->LoadIcon(IDI_ICON2);
m_hIconArray[3] = AfxGetApp()->LoadIcon(IDI_ICON3);
}

//定时器消息处理函数,用来实现动画图标
void CCDROMControlDlg::OnTimer(UINT nIDEvent)
{
    NOTIFYICONDATA nc;
nc.cbSize = sizeof(NOTIFYICONDATA);
if(m_nIconPos==3)
    m_nIconPos=0;
nc.hIcon = m_hIconArray[m_nIconPos++];
nc.hWnd = m_hWnd;
lstrcpy(nc.szTip,"动画效果托盘图标");
nc.uCallbackMessage = WM_NOTIFYICON;           //自定义最小化托盘消息
nc.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
nc.uID = IDC_NOTIFYICON;
Shell_NotifyIcon(NIM_MODIFY, &nc);
CDialog::OnTimer(nIDEvent);
}

只做这些托盘图标动画不会出来的 应该在前面做个 Shell_NotifyIcon(NIM_ADD, &nc);
动画才能出来。


posted on 2012-06-11 00:41 无一 阅读(669) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2012年6月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

导航

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜