Showing posts with label cpp. Show all posts
Showing posts with label cpp. Show all posts

Thursday, December 22, 2016

[HowTo] Make a Windows application full screen


MONITORINFO monitorInfo = { sizeof(monitorInfo) };
if (GetMonitorInfo(MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST), &monitorInfo))
{
    SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~(WS_CAPTION | WS_THICKFRAME));
    SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) & ~(WS_EX_DLGMODALFRAME | WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE));

    SetWindowPos(hwnd, NULL, monitorInfo.rcMonitor.left, monitorInfo.rcMonitor.top,
        monitorInfo.rcMonitor.right - monitorInfo.rcMonitor.left, monitorInfo.rcMonitor.bottom - monitorInfo.rcMonitor.top,
        SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
}

[HowTo] Remove system icon from the application title bar


DWORD flags = WTNCA_NODRAWICON | WTNCA_NOSYSMENU;
SetWindowThemeNonClientAttributes(hwnd, flags, flags);

[HowTo] Allow host app to be pinned to the taskbar


#include <shellapi.h>
#include <propsys.h>
#include <propkey.h>
#include <propvarutil.h>

HRESULT PropertyStoreSetStringValue(IPropertyStore* propertyStore, REFPROPERTYKEY pkey, PCWSTR value)
{
    PROPVARIANT propVariant;
    HRESULT hr = InitPropVariantFromString(value, &propVariant);
    if (SUCCEEDED(hr))
    {
        hr = propertyStore->SetValue(pkey, propVariant);
        PropVariantClear(&propVariant);
    }
    return hr;
}

HRESULT MakeWindowPinnable(HWND hwnd, PCWSTR userModelId, PCWSTR relaunchCommand, PCWSTR relaunchDisplayName, PCWSTR relaunchIcon = NULL)
{
    IPropertyStore* propertyStore;
    HRESULT hr = SHGetPropertyStoreForWindow(hwnd, IID_PPV_ARGS(&propertyStore));
    if (SUCCEEDED(hr))
    {
        PropertyStoreSetStringValue(propertyStore, PKEY_AppUserModel_ID, userModelId);
        PropertyStoreSetStringValue(propertyStore, PKEY_AppUserModel_RelaunchCommand, relaunchCommand);
        PropertyStoreSetStringValue(propertyStore, PKEY_AppUserModel_RelaunchDisplayNameResource, relaunchDisplayName);
        
        if (relaunchIcon != NULL)
        {
            PropertyStoreSetStringValue(propertyStore, PKEY_AppUserModel_RelaunchIconResource, relaunchIcon);
        }

        propertyStore->Release();
    }
    return hr;
}

char title[256];
GetWindowText(hwnd, title, sizeof(title));

MakeWindowPinnable(hwnd, title, GetCommandLine(), title);

[HowTo] Prevent app to be pinned to the taskbar



#include <shellapi.h>
#include <propsys.h>
#include <propkey.h>
#include <propvarutil.h>

HRESULT MakeWindowUnpinnable(HWND hwnd)
{
    IPropertyStore* propertyStore;
    HRESULT hr = SHGetPropertyStoreForWindow(hwnd, IID_PPV_ARGS(&propertyStore));
    if (SUCCEEDED(hr))
    {
        PROPVARIANT propVariant;
        hr = InitPropVariantFromBoolean(TRUE, &propVariant);
        if (SUCCEEDED(hr))
        {
            hr = propertyStore->SetValue(PKEY_AppUserModel_PreventPinning, propVariant);
            PropVariantClear(&propVariant);
        }
        propertyStore->Release();
    }
    return hr;
}

Friday, September 30, 2016

std::string to std::wstring conversion (and vice versa) using Windows API



#pragma once

#include <windows.h>
#include <string>

inline std::wstring multi2wide(const std::string& str, UINT codePage = CP_THREAD_ACP)
{
    if (str.empty())
    {
        return std::wstring();
    }

    int required = ::MultiByteToWideChar(codePage, 0, str.data(), (int)str.size(), NULL, 0);
    if (0 == required)
    {
        return std::wstring();
    }

    std::wstring str2;
    str2.resize(required);

    int converted = ::MultiByteToWideChar(codePage, 0, str.data(), (int)str.size(), &str2[0], str2.capacity());
    if (0 == converted)
    {
        return std::wstring();
    }

    return str2;
}

inline std::string wide2multi(const std::wstring& str, UINT codePage = CP_THREAD_ACP)
{
    if (str.empty())
    {
        return std::string();
    }

    int required = ::WideCharToMultiByte(codePage, 0, str.data(), (int)str.size(), NULL, 0, NULL, NULL);
    if (0 == required)
    {
        return std::string();
    }

    std::string str2;
    str2.resize(required);

    int converted = ::WideCharToMultiByte(codePage, 0, str.data(), (int)str.size(), &str2[0], str2.capacity(), NULL, NULL);
    if (0 == converted)
    {
        return std::string();
    }

    return str2;
}

Tuesday, July 12, 2016

Escape JSON string

std::string EscapeJson(const char* text)
{
    std::string string;
    
    int length = strlen(text);
    for (int i = 0; i < length; i++)
    {
        switch (text[i])
        {
            case '\\':
                string.append("\\\\");
                break;
            case '"':
                string.append("\\\"");
                break;
            case '/':
                string.append("\\/");
                break;
            case '\b':
                string.append("\\b");
                break;
            case '\f':
                string.append("\\f");
                break;
            case '\n':
                string.append("\\n");
                break;
            case '\r':
                string.append("\\r");
                break;
            case '\t':
                string.append("\\t");
                break;
            default:
                char c = text[i];
                string.append(1, c < 32 ? '?' : c);
                break;
        }
    }

    return string;
}

Monday, June 27, 2016

Trim function for std::string

std::string stdtrim(std::string& str)
{
    size_t first = str.find_first_not_of(' ');
    size_t last = str.find_last_not_of(' ');
    return (std::string::npos == first) || (std::string::npos == last) ? "" : str.substr(first, (last - first + 1));
}

Sunday, November 23, 2014

Encode binary array as a hex string

std::string hexEncode(unsigned char* buffer, int bufferSize)
{
    std::string hex;
    hex.reserve(bufferSize * 2 + 1);

    const char chars[] = "0123456789ABCDEF";

    for (int i = 0; i < bufferSize; i++)
    {
        unsigned char b = buffer[i];
        hex += chars[(b >> 4) & 0x0F];
        hex += chars[b & 0x0F];
    }

    return hex;
}

Wednesday, October 1, 2014

A simple way to get wchar_t from std::string

int outputDebugString(const char *text)
{
    std::string textA = text;
    std::wstring textW = std::wstring(textA.begin(), textA.end());
    ::OutputDebugStringW(textW.c_str());
}

Thursday, September 25, 2014

printf-like function that returns std::string


std::string text = stdprintf("Function %s failed with error %i", functionName, ::GetlastError());

#include <windows.h>
#include <string>
#include <memory>

std::string stdprintf(const char* format, va_list args)
{
    std::unique_ptr output;
    int size = ((int)strlen(format)) * 2;

    for (;;)
    {
        output.reset(new char[size]);
        strcpy_s(&output[0], size, format);

        int result = vsnprintf_s(&output[0], size, _TRUNCATE, format, args);

        if ((result >= 0) && (result < size))
        {
            break;
        }

        size += abs(result - size + 1);
    }

    return std::string(output.get());
}

std::string stdprintf(const char* format, ...)
{
    va_list args;
    va_start(args, format);
    std::string result = stdprintf(format, args);
    va_end(args);

    return result;
}

Friday, January 20, 2012

Dereferencing a NULL pointer

The following code does not cause a run-time error, even it is dereferencing a NULL pointer:

#include <stdio.h>

class C
{
public:
    int one()
    {
        printf("this=%08X\n", this);
        return 1;
    }
};

int main(int argc, char *argv[])
{
    printf("== Start\n\n");

    C* c = 0;

    printf("one=%d\n\n", c->one());

    printf("one=%d\n\n", (*c).one());

    printf("== End\n");
    return 0;
}

Output is:

== Start

this=00000000
one=1

this=00000000
one=1

== End
Press <RETURN> to close this window...