几个常见Win32 API函数
1.获取客户区矩形区域
RECT cliRect;
GetClientRect(hWnd, &cliRect);
2.获取窗口上下文句柄
HDC hdc = GetDC(hWnd);
//....
ReleaseDC(hWnd, hdc);
3.LPWSTR 与 char * 互转
int32_t WToChar(LPWSTR szWstr, char szCstr[], const int32_t chrLen) { int iLength = WideCharToMultiByte(CP_ACP, 0, szWstr, -1, NULL, 0, NULL, NULL); return WideCharToMultiByte(CP_ACP, 0, szWstr, -1, szCstr, iLength, NULL, NULL); } int32_t CharToW(const char* szCstr, WCHAR szWstr[], const int32_t wstrLen) { int iLength = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szCstr, -1, NULL, 0); return MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szCstr, -1, szWstr, iLength); }
4.获取带颜色的画刷
COLORREF colorObs = 0x9D9D9D;
HBRUSH hbObs = CreateSolidBrush(colorObs);
//....
DeleteObject(hbObs);
5.绘制矩形
Rectangle(hdc ,left,top,right,bottom);
6.填充矩形
FillRect(hdc, &rect, hbObs);
7.刷新整个窗口
InvalidateRect(hWnd, NULL, TRUE);
8.设置窗口标题
SetWindowText(hWnd, szText);
9.打开文件对话框
bool OpenFileDialog(HWND hWnd, char szFileName[MAX_PATH]) { WCHAR szFile[MAX_PATH] = { 0 }; OPENFILENAME ofn = { OPENFILENAME_SIZE_VERSION_400 }; ofn.hwndOwner = hWnd; // 过滤器,以\0相隔: 显示名称\0过滤器\0显示名称\0过滤器\0\0 ofn.lpstrFilter = TEXT("txt(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0"); ofn.lpstrFile = szFile; ofn.nMaxFile = sizeof(szFile); ofn.lpstrTitle = TEXT("选择文件"); ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST; TCHAR szCurDir[MAX_PATH]; GetCurrentDirectory(sizeof(szCurDir), szCurDir); ofn.lpstrInitialDir = szCurDir;//设置对话框显示的初始目录 BOOL bOk = GetOpenFileName(&ofn); if (!bOk) { return false; } //MessageBox(NULL, ofn.lpstrFile, TEXT("Tips_Yes"), MB_OK); memset(szFileName, 0, sizeof(szFileName)); WToChar(ofn.lpstrFile, szFileName, sizeof(szFileName)); return true; }
10.保存文件对话框
bool SaveFileDialog(HWND hWnd, char szFileName[MAX_PATH]) { WCHAR szFile[MAX_PATH] = { 0 }; OPENFILENAME ofn = { OPENFILENAME_SIZE_VERSION_400 }; ofn.hwndOwner = hWnd; // 过滤器,以\0相隔: 显示名称\0过滤器\0显示名称\0过滤器\0\0 ofn.lpstrFilter = TEXT("txt(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0"); ofn.lpstrFile = szFile; ofn.nMaxFile = sizeof(szFile); ofn.lpstrTitle = TEXT("选择文件"); ofn.Flags = OFN_OVERWRITEPROMPT; // 覆盖提示 TCHAR szCurDir[MAX_PATH]; GetCurrentDirectory(sizeof(szCurDir), szCurDir); ofn.lpstrInitialDir = szCurDir;//设置对话框显示的初始目录 BOOL bOk = GetSaveFileName(&ofn); if (!bOk) { return false; } //MessageBox(NULL, ofn.lpstrFile, TEXT("Tips_Yes"), MB_OK); memset(szFileName, 0, sizeof(szFileName)); WToChar(ofn.lpstrFile, szFileName, sizeof(szFileName)); return true; }
11.获取或设置控件文字
GetDlgItemText(hDlg, IDC_EDIT_ROW, szTRow, 64); SetDlgItemText(hDlg, IDC_EDIT_COL, szTCol);
12.获取光标位置
POINT pt;
GetCursorPos(&pt);
13.屏幕坐标与窗口坐标之间的转化
ScreenToClient(hWnd, &pt);
ClientToScreen(hWnd, &pt);
14.设置窗口位置和大小
SetWindowPos(hWnd, NULL, nX, nY, nWidth, nHeight, false);
15. 打开右键菜单
void UIManager::OnContextMenu(const HWND &hWnd, const RECT &cliRect, const POINT &clickPt) { POINT tmpt = clickPt; ScreenToClient(hWnd, &tmpt); if (PtInRect(&cliRect, tmpt) == FALSE){ return; } HMENU hroot = LoadMenu((HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), MAKEINTRESOURCE(IDR_CONTEXT)); if (!hroot){ return; } HMENU hpop = GetSubMenu(hroot, 0); if (!hpop){ return; } ClientToScreen(hWnd, &tmpt); //显示快捷菜单 TrackPopupMenu(hpop, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON, tmpt.x, tmpt.y, 0, hWnd, NULL); // 用完后要销毁菜单资源 DestroyMenu(hroot); }