• vc++知识点记录


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

    2:

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

    定义一个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);

    3:

    判断当前键盘指示灯亮着

    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);

    判断当前键盘指示灯亮着

    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);

    4:类的向导不好用的解决办法

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

    del /F *.clw

    5:添加对话框背景图片

    方法一:

    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;

    6:创建非模态对话框

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

    7:对话框支持拖动

    添加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);
    }


    8:获取屏幕大小

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

    int heigh= GetSystemMetrics(SM_CYFULLSCREEN);

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


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

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

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

    9:一个类访问控制另一个类中的变量控件

    如果要在类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。

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

    10: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);
    }

    11:全局函数访问对话框中的控件

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

    12:

    .格盘代码


    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);  

    13:系统下关机代码:

        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);

    14:对话框加载工具栏

       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);

    15:VC如何作出有动画效果的托盘图标

    //主程序对话框类构造函数
    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);
    动画才能出来。

  • 相关阅读:
    最近项目中遇到的一个算法挑战
    编程艺术之美,代码也玩穿越
    算法设计之部门消息通知机制
    解决系统迁移到windows 2003 64位服务器后引发的几个小问题
    我的IT成长历程
    a标签各种状态下的样式
    页面底部按钮被顶起问题
    ios点击input输入框,键盘弹出,此时滑动页面,光标位移
    解决移动端弹窗滚动事件触发主页面滚动事件
    浏览器记住密码之后,input背景变黄
  • 原文地址:https://www.cnblogs.com/lzjsky/p/1795165.html
Copyright © 2020-2023  润新知