c_cpp utilities.win.cc

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp utilities.win.cc相关的知识,希望对你有一定的参考价值。

#include "utilities.win.h"


// UTF8 Implementation
std::string UTF8( const wchar_t* utf16, size_t count ){
  std::string utf8_buffer("");
  if(utf16) {
    const int utf8_length = WideCharToMultiByte(
      CP_UTF8,                                                                                                                                                                                                                                                                                                                           // convert to UTF-8
      0,                                                                                                                                                                                                                                                                                                                           // default flags
      utf16,                                                                                                                                                                                                                                                                                                                           // source UTF-16 string
      count,                                                                                                                                                                                                                                                                                                                           // source string length, in wchar_t's,
      NULL,                                                                                                                                                                                                                                                                                                                           // unused - no conversion required in this step
      0,                                                                                                                                                                                                                                                                                                                           // request buffer size
      NULL, NULL                                                                                                                                                                                                                                                                                                                           // unused
      );
    assert(utf8_length != 0);

    utf8_buffer.resize(utf8_length);
    int result = WideCharToMultiByte(CP_UTF8,// convert to UTF-8
      0,// default flags
      utf16,// source UTF-16 string
      count,// source string length, in wchar_t's,
      &utf8_buffer[0],// destination buffer
      utf8_buffer.length(),// destination buffer size, in chars
      NULL, NULL// unused
      );

    assert(result != NULL);
  }
  return utf8_buffer;
}

std::wstring UTF16( const char* utf8, size_t count ){
  // destination buffer for UTF-16 string
  std::wstring utf16_buffer(L"");
  if(utf8) {
    const int utf16_length = MultiByteToWideChar(
      CP_UTF8,// convert from UTF-8
      0,// default flags
      utf8,// source UTF-8 string
      count,// length (in chars) of source UTF-8 string
      NULL,// unused - no conversion done in this step
      0// request size of destination buffer, in wchar_t's
      );
    assert(utf16_length != 0);
    utf16_buffer.resize(utf16_length);
    int result = ::MultiByteToWideChar(CP_UTF8,// convert from UTF-8
      0,// default flags
      utf8,// source UTF-8 string
      count,// length (in chars) of source UTF-8 string
      &utf16_buffer[0],// destination buffer
      utf16_buffer.length()// size of destination buffer, in wchar_t's
      );
    assert(result != 0);
  }
  return utf16_buffer;
}


// Preprocessor hack used in  MessageString( UINT message ) implementation
#define WM_MESSAGE_LIST(X) X(WM_ACTIVATE) \
  X(WM_ACTIVATEAPP) \
  X(WM_AFXFIRST) \
  X(WM_AFXLAST) \
  X(WM_APP) \
  X(WM_APPCOMMAND) \
  X(WM_ASKCBFORMATNAME) \
  X(WM_CANCELJOURNAL) \
  X(WM_CANCELMODE) \
  X(WM_CAPTURECHANGED) \
  X(WM_CHANGECBCHAIN) \
  X(WM_CHANGEUISTATE) \
  X(WM_CHAR) \
  X(WM_CHARTOITEM) \
  X(WM_CHILDACTIVATE) \
  X(WM_CLEAR) \
  X(WM_CLIPBOARDUPDATE) \
  X(WM_CLOSE) \
  X(WM_COMMAND) \
  X(WM_COMMNOTIFY) \
  X(WM_COMPACTING) \
  X(WM_COMPAREITEM) \
  X(WM_CONTEXTMENU) \
  X(WM_COPY) \
  X(WM_COPYDATA) \
  X(WM_CREATE) \
  X(WM_CTLCOLORBTN) \
  X(WM_CTLCOLORDLG) \
  X(WM_CTLCOLOREDIT) \
  X(WM_CTLCOLORLISTBOX) \
  X(WM_CTLCOLORMSGBOX) \
  X(WM_CTLCOLORSCROLLBAR) \
  X(WM_CTLCOLORSTATIC) \
  X(WM_CUT) \
  X(WM_DEADCHAR) \
  X(WM_DELETEITEM) \
  X(WM_DESTROY) \
  X(WM_DESTROYCLIPBOARD) \
  X(WM_DEVICECHANGE) \
  X(WM_DEVMODECHANGE) \
  X(WM_DISPLAYCHANGE) \
  X(WM_DRAWCLIPBOARD) \
  X(WM_DRAWITEM) \
  X(WM_DROPFILES) \
  X(WM_DWMCOLORIZATIONCOLORCHANGED) \
  X(WM_DWMCOMPOSITIONCHANGED) \
  X(WM_DWMNCRENDERINGCHANGED) \
  X(WM_DWMSENDICONICLIVEPREVIEWBITMAP) \
  X(WM_DWMSENDICONICTHUMBNAIL) \
  X(WM_DWMWINDOWMAXIMIZEDCHANGE) \
  X(WM_ENABLE) \
  X(WM_ENDSESSION) \
  X(WM_ENTERIDLE) \
  X(WM_ENTERMENULOOP) \
  X(WM_ENTERSIZEMOVE) \
  X(WM_ERASEBKGND) \
  X(WM_EXITMENULOOP) \
  X(WM_EXITSIZEMOVE) \
  X(WM_FONTCHANGE) \
  X(WM_GESTURE) \
  X(WM_GESTURENOTIFY) \
  X(WM_GETDLGCODE) \
  X(WM_GETFONT) \
  X(WM_GETHOTKEY) \
  X(WM_GETICON) \
  X(WM_GETMINMAXINFO) \
  X(WM_GETOBJECT) \
  X(WM_GETTEXT) \
  X(WM_GETTEXTLENGTH) \
  X(WM_GETTITLEBARINFOEX) \
  X(WM_HANDHELDFIRST) \
  X(WM_HANDHELDLAST) \
  X(WM_HELP) \
  X(WM_HOTKEY) \
  X(WM_HSCROLL) \
  X(WM_HSCROLLCLIPBOARD) \
  X(WM_ICONERASEBKGND) \
  X(WM_IME_CHAR) \
  X(WM_IME_COMPOSITION) \
  X(WM_IME_COMPOSITIONFULL) \
  X(WM_IME_CONTROL) \
  X(WM_IME_ENDCOMPOSITION) \
  X(WM_IME_KEYDOWN) \
  X(WM_IME_KEYUP) \
  X(WM_IME_NOTIFY) \
  X(WM_IME_REQUEST) \
  X(WM_IME_SELECT) \
  X(WM_IME_SETCONTEXT) \
  X(WM_IME_STARTCOMPOSITION) \
  X(WM_INITDIALOG) \
  X(WM_INITMENU) \
  X(WM_INITMENUPOPUP) \
  X(WM_INPUT) \
  X(WM_INPUTLANGCHANGE) \
  X(WM_INPUTLANGCHANGEREQUEST) \
  X(WM_INPUT_DEVICE_CHANGE) \
  X(WM_KEYDOWN) \
  X(WM_KEYUP) \
  X(WM_KILLFOCUS) \
  X(WM_LBUTTONDBLCLK) \
  X(WM_LBUTTONDOWN) \
  X(WM_LBUTTONUP) \
  X(WM_MBUTTONDBLCLK) \
  X(WM_MBUTTONDOWN) \
  X(WM_MBUTTONUP) \
  X(WM_MDIACTIVATE) \
  X(WM_MDICASCADE) \
  X(WM_MDICREATE) \
  X(WM_MDIDESTROY) \
  X(WM_MDIGETACTIVE) \
  X(WM_MDIICONARRANGE) \
  X(WM_MDIMAXIMIZE) \
  X(WM_MDINEXT) \
  X(WM_MDIREFRESHMENU) \
  X(WM_MDIRESTORE) \
  X(WM_MDISETMENU) \
  X(WM_MDITILE) \
  X(WM_MEASUREITEM) \
  X(WM_MENUCHAR) \
  X(WM_MENUCOMMAND) \
  X(WM_MENUDRAG) \
  X(WM_MENUGETOBJECT) \
  X(WM_MENURBUTTONUP) \
  X(WM_MENUSELECT) \
  X(WM_MOUSEACTIVATE) \
  X(WM_MOUSEHOVER) \
  X(WM_MOUSELAST) \
  X(WM_MOUSELEAVE) \
  X(WM_MOUSEMOVE) \
  X(WM_MOUSEWHEEL) \
  X(WM_MOVE) \
  X(WM_MOVING) \
  X(WM_NCACTIVATE) \
  X(WM_NCCALCSIZE) \
  X(WM_NCCREATE) \
  X(WM_NCDESTROY) \
  X(WM_NCHITTEST) \
  X(WM_NCLBUTTONDBLCLK) \
  X(WM_NCLBUTTONDOWN) \
  X(WM_NCLBUTTONUP) \
  X(WM_NCMBUTTONDBLCLK) \
  X(WM_NCMBUTTONDOWN) \
  X(WM_NCMBUTTONUP) \
  X(WM_NCMOUSEHOVER) \
  X(WM_NCMOUSELEAVE) \
  X(WM_NCMOUSEMOVE) \
  X(WM_NCPAINT) \
  X(WM_NCRBUTTONDBLCLK) \
  X(WM_NCRBUTTONDOWN) \
  X(WM_NCRBUTTONUP) \
  X(WM_NCXBUTTONDBLCLK) \
  X(WM_NCXBUTTONDOWN) \
  X(WM_NCXBUTTONUP) \
  X(WM_NEXTDLGCTL) \
  X(WM_NEXTMENU) \
  X(WM_NOTIFY) \
  X(WM_NOTIFYFORMAT) \
  X(WM_NULL) \
  X(WM_PAINT) \
  X(WM_PAINTCLIPBOARD) \
  X(WM_PAINTICON) \
  X(WM_PALETTECHANGED) \
  X(WM_PALETTEISCHANGING) \
  X(WM_PARENTNOTIFY) \
  X(WM_PASTE) \
  X(WM_PENWINFIRST) \
  X(WM_PENWINLAST) \
  X(WM_POWER) \
  X(WM_POWERBROADCAST) \
  X(WM_PRINT) \
  X(WM_PRINTCLIENT) \
  X(WM_QUERYDRAGICON) \
  X(WM_QUERYENDSESSION) \
  X(WM_QUERYNEWPALETTE) \
  X(WM_QUERYOPEN) \
  X(WM_QUERYUISTATE) \
  X(WM_QUEUESYNC) \
  X(WM_QUIT) \
  X(WM_RBUTTONDBLCLK) \
  X(WM_RBUTTONDOWN) \
  X(WM_RBUTTONUP) \
  X(WM_RENDERALLFORMATS) \
  X(WM_RENDERFORMAT) \
  X(WM_SETCURSOR) \
  X(WM_SETFOCUS) \
  X(WM_SETFONT) \
  X(WM_SETHOTKEY) \
  X(WM_SETICON) \
  X(WM_SETREDRAW) \
  X(WM_SETTEXT) \
  X(WM_SHOWWINDOW) \
  X(WM_SIZE) \
  X(WM_SIZECLIPBOARD) \
  X(WM_SIZING) \
  X(WM_SPOOLERSTATUS) \
  X(WM_STYLECHANGED) \
  X(WM_STYLECHANGING) \
  X(WM_SYNCPAINT) \
  X(WM_SYSCHAR) \
  X(WM_SYSCOLORCHANGE) \
  X(WM_SYSCOMMAND) \
  X(WM_SYSDEADCHAR) \
  X(WM_SYSKEYDOWN) \
  X(WM_SYSKEYUP) \
  X(WM_TABLET_FIRST) \
  X(WM_TABLET_LAST) \
  X(WM_TCARD) \
  X(WM_THEMECHANGED) \
  X(WM_TIMECHANGE) \
  X(WM_TIMER) \
  X(WM_TOUCH) \
  X(WM_UNDO) \
  X(WM_UNICHAR) \
  X(WM_UNINITMENUPOPUP) \
  X(WM_UPDATEUISTATE) \
  X(WM_USER) \
  X(WM_USERCHANGED) \
  X(WM_VKEYTOITEM) \
  X(WM_VSCROLL) \
  X(WM_VSCROLLCLIPBOARD) \
  X(WM_WINDOWPOSCHANGED) \
  X(WM_WINDOWPOSCHANGING) \
  X(WM_WININICHANGE) \
  X(WM_WTSSESSION_CHANGE) \
  X(WM_XBUTTONDBLCLK) \
  X(WM_XBUTTONDOWN) \
  X(WM_XBUTTONUP)


// WM_SETTINGCHANGE   == WM_INICHANGE
// WM_KEYDOWN         == WM_KEYFIRST
// WM_UNICHAR         == WM_KEYLAST
// WM_IME_COMPOSITION == WM_IME_KEYLAST
// WM_MOUSEFIRST      == WM_MOUSEMOVE
// WM_MOUSELAST       == WM_MOUSEHWHEEL on vista
#define CASES(MESSAGE) case MESSAGE: return # MESSAGE;

const char* MessageString(UINT message){
  static char unknown_message[32] = {0};

  switch (message) {
    WM_MESSAGE_LIST(CASES)

  default:
    sprintf_s(unknown_message, "Unknown Message: 0x8X", message);
    return unknown_message;
  }
}

#undef CASES
#undef WM_MESSAGE_LIST

以上是关于c_cpp utilities.win.cc的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 200.岛屿数量

c_cpp 127.单词阶梯

c_cpp MOFSET

c_cpp MOFSET

c_cpp 31.下一个排列

c_cpp string→char *