Visual C++ 2011526

一.GetLastActivePopup

参考:http://blog.163.com/zhyang@yeah/blog/static/13014384420102296245297/

二.获取main函数参数

使用__argc和__targv可以在其他函数中也可以获取main函数参数

int argc=__argc;
if(argc>0)
 char *pApp=__targv[0];

三.控制音量

用COM接口(Vista+)控制音量

void VolumeUpDown(BOOL bIsUp)
{
    HRESULT hr;
    IMMDeviceEnumerator *pDeviceEnumerator = NULL;
    IMMDevice *pDefaultDevice = NULL;
    IAudioEndpointVolume *pEndpointVolume = NULL;

    //
    // Create a enumerator to get default device in the system
    //
    CoInitialize(NULL);
    hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator),
        (LPVOID *)&pDeviceEnumerator);

    if ( hr != S_OK)
        goto exit;

    //
    // Get default device in the system
    //
    hr = pDeviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pDefaultDevice);

    if (hr != S_OK)
        goto exit;

    hr = pDefaultDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&pEndpointVolume);

    if (hr != S_OK || pEndpointVolume == NULL)
        goto exit;
    if(bIsUp)
    {
        pEndpointVolume->VolumeStepUp(&GUID_NULL);
    }
    else
    {
        pEndpointVolume->VolumeStepDown(&GUID_NULL);
    }


exit:
    if (pDeviceEnumerator != NULL)
    {
        pDeviceEnumerator->Release();
        pDeviceEnumerator = NULL;
    }

    if (pDefaultDevice != NULL)
    {
        pDefaultDevice->Release();
        pDefaultDevice = NULL;
    }

    if (pEndpointVolume != NULL)
    {
        pEndpointVolume->Release();
        pEndpointVolume = NULL;
    }

    CoUninitialize();

}
void MuteInVista()
{
    BOOL bMute = FALSE;

    if (m_pEndpoint == NULL)
    {
        return;
    }

    m_pEndpoint->GetMute(&bMute);

    if (bMute)
    {
        DrawIconOn();
        m_pEndpoint->SetMute(FALSE,NULL);
    }
    else
    {
        DrawIconOff();
        m_pEndpoint->SetMute(TRUE,NULL);
    }
}

XP下很麻烦,com接口极大简化了编程步骤

四.CString的传参

void Fun1(CString a)
{
    ::GetCurrentDirectory(200,(LPWSTR)(LPCTSTR)a);
}

CString a=TEXT("");
Fun1(a);

函数内部将CString转成了指针,这样也可以?

http://msdn.microsoft.com/zh-cn/library/cc468200(VS.71).aspx

五.GetWindowLong和GetClassLong

这两个函数困扰我很长时间…其实很简单

创建窗体用CreateWindow函数,其中要输入很多参数

HWND CreateWindow(      
                LPCTSTR 
            lpClassName,
    LPCTSTR 
            lpWindowName,
    DWORD 
            dwStyle,
    int 
            x,
    int 
            y,
    int 
            nWidth,
    int 
            nHeight,
    HWND 
            hWndParent,
    HMENU 
            hMenu,
    HINSTANCE 
            hInstance,
    LPVOID 
            lpParam
);

那么在创建窗体之后就可以用GetWindowLong来获取窗体结构信息,如窗体样式,父窗体,窗体实例句柄(程序句柄),同时可以用SetWindowLong来动态修改窗体样式

SetWindowLong (hwnd, 0, 1 ^ GetWindowLong (hwnd, 0)) ;

那么同理GetClassLong也是如此.

六.窗体子类化

使用SetWindowLong函数给指定子控件指定一个窗体过程,这个貌似很有用

OldScroll[i] = (WNDPROC)SetWindowLong (hwndScroll[i], GWL_WNDPROC, (LONG) ScrollProc) ;
LRESULT CALLBACK ScrollProc (HWND hwnd, UINT message, 
                             WPARAM wParam, LPARAM lParam)
{
     int id = GetWindowLong (hwnd, GWL_ID) ;
          
     switch (message)
     {
     case WM_KEYDOWN :
                   break ;
               
     case WM_SETFOCUS :
                  break ;
     }
     return CallWindowProc (OldScroll[id], hwnd, message, wParam, lParam) ;
}
原文地址:https://www.cnblogs.com/Clingingboy/p/2059242.html