Skip to content

Instantly share code, notes, and snippets.

@hirosof
Created September 9, 2014 15:27
Show Gist options
  • Save hirosof/518eb2f9e72c58ffae7e to your computer and use it in GitHub Desktop.
Save hirosof/518eb2f9e72c58ffae7e to your computer and use it in GitHub Desktop.
#include "ControlFunc.h"
/*
コントロール作成
*/
HWND HSCreateStaticText(HWND hwnd, TCHAR *lpszCaption, int px, int py, int sx, int sy, HSStaticTextAlign TextAlign){
int TextAlignStyle;
switch (TextAlign) {
case HSStaticTextAlign_Left:
TextAlignStyle = SS_LEFT;
break;
case HSStaticTextAlign_Center:
TextAlignStyle = SS_CENTER;
break;
case HSStaticTextAlign_Right:
TextAlignStyle = SS_RIGHT;
break;
}
return CreateWindowEx(NULL,
WC_STATIC,
lpszCaption,
WS_CHILD | WS_VISIBLE | TextAlignStyle,
px,
py,
sx,
sy,
hwnd,
NULL,
NULL,
NULL);
}
HWND HSCreateButton(HWND hwnd, TCHAR *lpszCaption, int px, int py, int sx, int sy, HMENU hMenu) {
return CreateWindowEx(NULL,
WC_BUTTON,
lpszCaption,
WS_CHILD | WS_VISIBLE ,
px,
py,
sx,
sy,
hwnd,
hMenu,
NULL,
NULL);
}
HWND HSCreateGroupBox(HWND hwnd, TCHAR *lpszCaption, int px, int py, int sx, int sy) {
return CreateWindowEx(NULL,
WC_BUTTON,
lpszCaption,
WS_CHILD | WS_VISIBLE | BS_GROUPBOX |BS_CENTER,
px,
py,
sx,
sy,
hwnd,
NULL,
NULL,
NULL);
}
HWND HSCreateCheckBox(HWND hwnd, TCHAR *lpszCaption, int px, int py, int sx, int sy) {
return CreateWindowEx(NULL,
WC_BUTTON,
lpszCaption,
WS_CHILD | WS_VISIBLE | BS_CHECKBOX | BS_AUTOCHECKBOX,
px,
py,
sx,
sy,
hwnd,
NULL,
NULL,
NULL);
}
HWND HSCreateRadioButton(HWND hwnd, TCHAR *lpszCaption, int px, int py, int sx, int sy, BOOL bStartRadioButton) {
return CreateWindowEx(NULL,
WC_BUTTON,
lpszCaption,
WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON | ((bStartRadioButton) ? WS_GROUP : 0),
px,
py,
sx,
sy,
hwnd,
NULL,
NULL,
NULL);
}
HWND HSCreateTextBox(HWND hwnd, TCHAR *lpszCaption, int px, int py, int sx, int sy, BOOL bNumberOnly, BOOL bReadOnly){
return CreateWindowEx(WS_EX_CLIENTEDGE,
WC_EDIT,
lpszCaption,
WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ((bReadOnly) ? ES_READONLY : 0) | ((bNumberOnly) ? ES_NUMBER : 0),
px,
py,
sx,
sy,
hwnd,
NULL,
NULL,
NULL);
}
HWND HSCreateMultiLineTextBox(HWND hwnd, TCHAR *lpszCaption, int px, int py, int sx, int sy, BOOL bReadOnly) {
int ES_STYLE = ES_MULTILINE | ES_WANTRETURN | ES_AUTOVSCROLL | ES_AUTOHSCROLL;
ES_STYLE |= ((bReadOnly) ? ES_READONLY : 0);
return CreateWindowEx(WS_EX_CLIENTEDGE,
WC_EDIT,
lpszCaption,
WS_VISIBLE | WS_CHILD | WS_VSCROLL | WS_HSCROLL | ES_STYLE,
px,
py,
sx,
sy,
hwnd,
NULL,
NULL,
NULL);
}
//フォント
HFONT SetFontToControl(HWND hControl, HFONT hFont) {
LRESULT lRet = SendMessage(hControl, WM_GETFONT, 0, 0);
HFONT hOldFont = reinterpret_cast<HFONT>(lRet);
SendMessage(hControl, WM_SETFONT, reinterpret_cast<WPARAM>(hFont), TRUE);
return hOldFont;
}
void SetCheckButtonState(HWND hCheckBox, BOOL bStateChecked) {
if (hCheckBox == NULL)return;
SendMessage(hCheckBox, BM_SETCHECK, ((bStateChecked) ? BST_CHECKED : BST_UNCHECKED), 0);
}
BOOL GetCheckButtonState(HWND hCheckBox) {
if (hCheckBox == NULL)return FALSE;
LRESULT lResult = SendMessage(hCheckBox, BM_GETCHECK, 0, 0);
if (lResult == BST_CHECKED)return TRUE;
return FALSE;
}
void SetRadioButtonState(HWND hRadioButton, BOOL bStateChecked) {
if (hRadioButton == NULL)return;
SendMessage(hRadioButton, BM_SETCHECK, ((bStateChecked) ? BST_CHECKED : BST_UNCHECKED), 0);
}
BOOL GetRadioButtonState(HWND hRadioButton) {
if (hRadioButton == NULL)return FALSE;
LRESULT lResult = SendMessage(hRadioButton, BM_GETCHECK, 0, 0);
if (lResult == BST_CHECKED)return TRUE;
return FALSE;
}
HWND HSCreateBorderStaticControl(HWND hwnd, int px, int py, int size, HSBorderStaticStyle Style) {
switch (Style) {
case HSBorderStaticStyle_Horizontal:
return CreateWindowEx(NULL,
WC_STATIC,
NULL,
WS_CHILD | WS_VISIBLE | SS_ETCHEDHORZ,
px,
py,
size,
1,
hwnd,
NULL,
NULL,
NULL);
case HSBorderStaticStyle_Vertical:
return CreateWindowEx(NULL,
WC_STATIC,
NULL,
WS_CHILD | WS_VISIBLE | SS_ETCHEDVERT,
px,
py,
1,
size,
hwnd,
NULL,
NULL,
NULL);
}
return NULL;
}
HWND HSCreateBorderBoxStaticControl(HWND hwnd, int px, int py, int sx, int sy) {
return CreateWindowEx(NULL,
WC_STATIC,
NULL,
WS_CHILD | WS_VISIBLE | SS_ETCHEDFRAME,
px,
py,
sx,
sy,
hwnd,
NULL,
NULL,
NULL);
}
#pragma once
#include <Windows.h>
#include <CommCtrl.h>
/*
コントロール作成
*/
enum HSStaticTextAlign {
HSStaticTextAlign_Left = 0,
HSStaticTextAlign_Center,
HSStaticTextAlign_Right
};
HWND HSCreateStaticText(HWND hwnd, TCHAR *lpszCaption, int px, int py, int sx, int sy, HSStaticTextAlign TextAlign = HSStaticTextAlign_Left);
HWND HSCreateButton(HWND hwnd, TCHAR *lpszCaption, int px, int py, int sx, int sy , HMENU hMenu = NULL);
HWND HSCreateGroupBox(HWND hwnd, TCHAR *lpszCaption, int px, int py, int sx, int sy);
HWND HSCreateCheckBox(HWND hwnd, TCHAR *lpszCaption, int px, int py, int sx, int sy);
HWND HSCreateRadioButton(HWND hwnd, TCHAR *lpszCaption, int px, int py, int sx, int sy , BOOL bStartRadioButton = FALSE);
HWND HSCreateTextBox(HWND hwnd, TCHAR *lpszCaption ,int px, int py, int sx, int sy, BOOL bNumberOnly = FALSE , BOOL bReadOnly = FALSE);
HWND HSCreateMultiLineTextBox(HWND hwnd, TCHAR *lpszCaption, int px, int py, int sx, int sy, BOOL bReadOnly = FALSE);
enum HSBorderStaticStyle {
HSBorderStaticStyle_Horizontal = 0,
HSBorderStaticStyle_Vertical
};
HWND HSCreateBorderStaticControl(HWND hwnd, int px, int py, int size, HSBorderStaticStyle Style = HSBorderStaticStyle_Horizontal);
HWND HSCreateBorderBoxStaticControl(HWND hwnd, int px, int py, int sx, int sy);
HFONT SetFontToControl(HWND hControl, HFONT hFont);
void SetCheckButtonState(HWND hCheckBox, BOOL bStateChecked);
BOOL GetCheckButtonState(HWND hCheckBox);
void SetRadioButtonState(HWND hRadioButton, BOOL bStateChecked);
BOOL GetRadioButtonState(HWND hRadioButton);
#include "SupportFunc.h"
HFONT HSCreateFont(TCHAR *FontName, int PointSize, BOOL bBold, int Mode) {
HDC hdc = CreateCompatibleDC(NULL);
int PixelPerInch = GetDeviceCaps(hdc, LOGPIXELSY);
DeleteDC(hdc);
int LogicSize = -((PointSize * PixelPerInch) / 72);
int BoldSize = (bBold) ? FW_BOLD : FW_DONTCARE;
BOOL bItalic = FALSE, bUnderl = FALSE, bStrikout = FALSE;
if (Mode & 1)bItalic = TRUE;
if (Mode & 2)bUnderl = TRUE;
if (Mode & 4)bStrikout = TRUE;
return
CreateFont(LogicSize, 0, 0, 0,
BoldSize, bItalic, bUnderl, bStrikout,
SHIFTJIS_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH | FF_DONTCARE,
FontName);
}
SECURITY_ATTRIBUTES HSGetSecurityAttributes(BOOL bInherit) {
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = bInherit;
return sa;
}
BOOL ChooseExecuteFileDialog(HWND hParent, wchar_t *lpszFileName) {
if (hParent == NULL)return FALSE;
if (lpszFileName == nullptr)return FALSE;
IFileOpenDialog *pDialog;
HRESULT hr;
hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDialog));
if (SUCCEEDED(hr)) {
wchar_t Dir[MAX_PATH];
memset(Dir, 0, sizeof(Dir));
if (lstrlenW(lpszFileName)) {
lstrcpyW(Dir, lpszFileName);
PathRemoveFileSpecW(Dir);
if (lstrlenW(Dir) == 0) GetCurrentDirectoryW(MAX_PATH, Dir);
} else {
GetCurrentDirectoryW(MAX_PATH, Dir);
}
IShellItem *pItemDirectory;
hr = SHCreateItemFromParsingName(Dir, NULL, IID_PPV_ARGS(&pItemDirectory));
if (SUCCEEDED(hr)) {
pDialog->SetFolder(pItemDirectory);
pItemDirectory->Release();
}
COMDLG_FILTERSPEC Spec = { L"実行ファイル(*.exe)", L"*.exe" };
pDialog->SetFileTypes(1, &Spec);
pDialog->SetFileName(PathFindFileNameW(lpszFileName));
pDialog->SetTitle(L"実行ファイルを選択してください。");
hr = pDialog->Show(hParent);
if (SUCCEEDED(hr)) {
IShellItem *pResultFileName;
pDialog->GetResult(&pResultFileName);
wchar_t *pFileName;
pResultFileName->GetDisplayName(SIGDN_FILESYSPATH, &pFileName);
lstrcpyW(lpszFileName, pFileName);
CoTaskMemFree(pFileName);
pResultFileName->Release();
}
pDialog->Release();
}
return TRUE;
}
BOOL ChooseOpenTextFileDialog(HWND hParent, wchar_t *lpszFileName) {
if (hParent == NULL)return FALSE;
if (lpszFileName == nullptr)return FALSE;
IFileOpenDialog *pDialog;
HRESULT hr;
hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDialog));
if (SUCCEEDED(hr)) {
wchar_t Dir[MAX_PATH];
memset(Dir, 0, sizeof(Dir));
if (lstrlenW(lpszFileName)) {
lstrcpyW(Dir, lpszFileName);
PathRemoveFileSpecW(Dir);
if (lstrlenW(Dir) == 0) GetCurrentDirectoryW(MAX_PATH, Dir);
} else {
GetCurrentDirectoryW(MAX_PATH, Dir);
}
IShellItem *pItemDirectory;
hr = SHCreateItemFromParsingName(Dir, NULL, IID_PPV_ARGS(&pItemDirectory));
if (SUCCEEDED(hr)) {
pDialog->SetFolder(pItemDirectory);
pItemDirectory->Release();
}
COMDLG_FILTERSPEC Spec = { L"テキスト ファイル(*.txt)", L"*.txt" };
pDialog->SetFileTypes(1, &Spec);
pDialog->SetFileName(PathFindFileNameW(lpszFileName));
pDialog->SetTitle(L"テキストファイルを選択してください。");
hr = pDialog->Show(hParent);
if (SUCCEEDED(hr)) {
IShellItem *pResultFileName;
pDialog->GetResult(&pResultFileName);
wchar_t *pFileName;
pResultFileName->GetDisplayName(SIGDN_FILESYSPATH, &pFileName);
lstrcpyW(lpszFileName, pFileName);
CoTaskMemFree(pFileName);
pResultFileName->Release();
}
pDialog->Release();
}
return TRUE;
}
BOOL ChooseSaveTextFileDialog(HWND hParent, wchar_t *lpszFileName) {
if (hParent == NULL)return FALSE;
if (lpszFileName == nullptr)return FALSE;
IFileSaveDialog *pDialog;
HRESULT hr;
hr = CoCreateInstance(CLSID_FileSaveDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDialog));
if (SUCCEEDED(hr)) {
wchar_t Dir[MAX_PATH];
memset(Dir, 0, sizeof(Dir));
if (lstrlenW(lpszFileName)) {
lstrcpyW(Dir, lpszFileName);
PathRemoveFileSpecW(Dir);
if (lstrlenW(Dir) == 0) GetCurrentDirectoryW(MAX_PATH, Dir);
} else {
GetCurrentDirectoryW(MAX_PATH, Dir);
}
IShellItem *pItemDirectory;
hr = SHCreateItemFromParsingName(Dir, NULL, IID_PPV_ARGS(&pItemDirectory));
if (SUCCEEDED(hr)) {
pDialog->SetFolder(pItemDirectory);
pItemDirectory->Release();
}
COMDLG_FILTERSPEC Spec = { L"テキスト ファイル(*.txt)", L"*.txt" };
pDialog->SetFileTypes(1, &Spec);
pDialog->SetFileName(PathFindFileNameW(lpszFileName));
pDialog->SetOptions(FOS_OVERWRITEPROMPT);
pDialog->SetDefaultExtension(L"txt");
pDialog->SetTitle(L"保存先のテキストファイルを指定してください。");
hr = pDialog->Show(hParent);
if (SUCCEEDED(hr)) {
IShellItem *pResultFileName;
pDialog->GetResult(&pResultFileName);
wchar_t *pFileName;
pResultFileName->GetDisplayName(SIGDN_FILESYSPATH, &pFileName);
lstrcpyW(lpszFileName, pFileName);
CoTaskMemFree(pFileName);
pResultFileName->Release();
}
pDialog->Release();
}
return TRUE;
}
#pragma once
#include <Windows.h>
#include <Shlwapi.h>
#include <Shobjidl.h>
HFONT HSCreateFont(TCHAR *FontName, int PointSize, BOOL bBold = FALSE, int Mode = 0);
BOOL ChooseExecuteFileDialog(HWND hParent, wchar_t *lpszFileName);
BOOL ChooseOpenTextFileDialog(HWND hParent, wchar_t *lpszFileName);
BOOL ChooseSaveTextFileDialog(HWND hParent, wchar_t *lpszFileName);
SECURITY_ATTRIBUTES HSGetSecurityAttributes(BOOL bInherit = FALSE);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment