Windows API   ダイアログ

ホーム   C/C++チュートリアル


Cプログラミンの一環として始めた Windos API ですが、やってみたら面白かったので、もっと詳しく、かつ簡単に、ステップバイステップで学習するコーナーも作りました。
Windows API Primer
Primer は、入門書という意味です。

カラーダイアログ


colordialog.c


#include <windows.h>

LRESULT CALLBACK WndProc  (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK PanelProc(HWND, UINT, WPARAM, LPARAM);
void RegisterPanel(void);
COLORREF ShowColorDialog(HWND);
COLORREF gColor = RGB(255, 255, 255);

int WINAPI WinMain      ( HINSTANCE hInstance,
			  HINSTANCE hPrevInstance,
			  LPSTR lpCmdLine,
			  int nCmdShow)
{
    WNDCLASS wc;
    HWND     hwnd;
    MSG      msg;
    
    wc.style            = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc      = WndProc;
    wc.cbClsExtra       = 0;
    wc.cbWndExtra       = 0;
    wc.hInstance        = hInstance;
    wc.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor          = LoadCursor(NULL, IDC_ARROW);
    wc.lpszMenuName     = NULL;
    wc.lpszClassName    = TEXT("ColorDialog");

    RegisterClass       (&wc);

    hwnd = CreateWindow (TEXT("ColorDialog"),
			 TEXT("ColorDialog"),
			 WS_OVERLAPPEDWINDOW,
			 (GetSystemMetrics(SM_CXSCREEN) - 400) / 2,
			 (GetSystemMetrics(SM_CYSCREEN) - 250) / 2,
			 400,
			 250,
			 NULL,
			 NULL,
			 hInstance,
			 NULL);

    ShowWindow(hwnd, nCmdShow);

    while(GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
	DispatchMessage(&msg);
    }
    return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    static HFONT font;
    static HWND button, panel;
    RECT   rect;
    
    switch(msg)
    {
    case WM_CREATE:
        font   = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
        button = CreateWindow(TEXT("button"), TEXT("Color"),
			      WS_VISIBLE | WS_CHILD,
		              20, 20, 80, 25,
		              hwnd, (HMENU)1,
		              NULL, NULL);
	SendMessage(button, WM_SETFONT, (WPARAM)font, MAKELPARAM(FALSE, 0));
	RegisterPanel();
	panel = CreateWindow(TEXT("Panel"), NULL,
			     WS_VISIBLE | WS_CHILD,
			     120, 20, 100, 100,
			     hwnd, (HMENU)2,
			     NULL, NULL);
	break;
    case WM_SIZE:
        GetClientRect(hwnd, &rect);
	SetWindowPos(panel, HWND_TOP,
		     120, 20,
		     rect.right  - 140,
		     rect.bottom -  40,
		     SWP_NOMOVE);
	break;
    case WM_COMMAND:
        gColor = ShowColorDialog(hwnd);
	InvalidateRect(panel, NULL, TRUE);
	break;
    case WM_DESTROY:
        PostQuitMessage(0);
	break;
    }
    return DefWindowProc(hwnd, msg, wParam, lParam);
}

LRESULT CALLBACK PanelProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    HDC         hdc;
    PAINTSTRUCT ps;
    RECT        rect;

    switch(msg)
    {
    case WM_PAINT:
        GetClientRect(hwnd, &rect);
	hdc = BeginPaint(hwnd, &ps);
	SetBkColor(hdc, gColor);
	ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rect, "", 0, NULL);
	EndPaint(hwnd, &ps);
	break;
    }
    return DefWindowProc(hwnd, msg, wParam, lParam);
}

COLORREF ShowColorDialog(HWND hwnd)
{
    CHOOSECOLOR cc;
    static COLORREF crCustClr[16];
    
    ZeroMemory(&cc, sizeof(cc));
    cc.lStructSize = sizeof(cc);
    cc.hwndOwner = hwnd;
    cc.lpCustColors = (LPDWORD)crCustClr;
    cc.rgbResult    = RGB(0, 255, 0);
    cc.Flags        = CC_FULLOPEN | CC_RGBINIT;
    ChooseColor(&cc);
    
    return cc.rgbResult;
}
			  
void RegisterPanel(void)
{
    WNDCLASS wc = {0};
    wc.lpszClassName = TEXT("Panel");
    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.lpfnWndProc   = PanelProc;
    RegisterClass(&wc);
}
    


実行結果


ファイルダイアログ

filedialog.c


#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void    CreateMenubar   (HWND);
void    OpenDialog      (HWND);
void    LoadFile        (LPSTR);
HWND    gEdit;

int WINAPI WinMain      (HINSTANCE hInstance,
			 HINSTANCE hPrevInstance,
			 LPSTR lpCmdLine,
			 int nCmdShow)
{
    WNDCLASS wc;
    HWND     hwnd;
    MSG      msg;
    
    wc.style            = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc      = WndProc;
    wc.cbClsExtra       = 0;
    wc.cbWndExtra       = 0;
    wc.hInstance        = hInstance;
    wc.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor          = LoadCursor(NULL, IDC_ARROW);
    wc.lpszMenuName     = NULL;
    wc.lpszClassName    = TEXT("FileDialog");

    RegisterClass       (&wc);

    hwnd = CreateWindow (TEXT("FileDialog"),
			 TEXT("FileDialog"),
			 WS_OVERLAPPEDWINDOW,
			 (GetSystemMetrics(SM_CXSCREEN) - 400) / 2,
			 (GetSystemMetrics(SM_CYSCREEN) - 250) / 2,
			 400,
			 250,
			 NULL,
			 NULL,
			 hInstance,
			 NULL);

    ShowWindow(hwnd, nCmdShow);

    while(GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
	DispatchMessage (&msg);
    }
    return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    RECT  rect;
    HFONT font;
    
    switch(msg)
    {
    case WM_CREATE:
        /* テキストエディタを等幅フォントにするためにフォントを作成します。           */
        /* CreateFontの引数は14個ありますが、0を指定するとデフォルト値が使われます。*/
        /* 13番目の引数にFIXED_PITCH定数を指定すると、等幅フォントになります。     */
        font  = CreateFont    (0,
			       0,
			       0,
			       0,
			       0,
			       0,
			       0,
			       0,
			       0,
			       0,
			       0,
			       0,
			       FIXED_PITCH,
			       0);
        gEdit = CreateWindowEx(WS_EX_RIGHTSCROLLBAR,
			       TEXT("edit"),
			       NULL,
			       WS_VISIBLE | WS_CHILD | WS_HSCROLL | WS_VSCROLL |
			       ES_MULTILINE,
			       0, 0, 260, 180,
			       hwnd, (HMENU)1, NULL, NULL);
        /* 先ほど作成した等幅フォントをテキストエディタに設定します */
        SendMessage(gEdit, WM_SETFONT, (WPARAM)font, MAKELPARAM(FALSE, 0));
        CreateMenubar(hwnd);
        break;
    case WM_SIZE:
        GetClientRect(hwnd, &rect);
	SetWindowPos(gEdit, HWND_TOP,
		     0, 0, rect.right, rect.bottom, SWP_NOMOVE);
	break;
    case WM_COMMAND:
        if (wParam == 1)
	{
	    OpenDialog(hwnd);
	}
	break;
    case WM_DESTROY:
        PostQuitMessage(0);
	break;
    }
    return DefWindowProc(hwnd, msg, wParam, lParam);
}

void CreateMenubar(HWND hwnd)
{
    HMENU menubar;
    HMENU menu;
    menubar = CreateMenu();
    menu    = CreateMenu();
    AppendMenu(menubar, MF_POPUP,  (UINT_PTR)menu, TEXT("&File"));
    AppendMenu(menu,    MF_STRING, 1,              TEXT("&Open"));
    SetMenu(hwnd, menubar);
}

void OpenDialog(HWND hwnd)
{
    OPENFILENAME filename;
    TCHAR        filesize[MAX_PATH];

    ZeroMemory(&filename,      sizeof(filename));
    filename.lStructSize     = sizeof(filename );
    filename.lpstrFile       = filesize;
    filename.lpstrFile[0]    = '\0';
    filename.hwndOwner       = hwnd;
    filename.nMaxFile        = sizeof(filesize);
    filename.lpstrFilter     = TEXT("All files(*.*)\0*.*\0");
    filename.nFilterIndex    = 1;
    filename.lpstrInitialDir = NULL;
    filename.lpstrFileTitle  = NULL;
    filename.Flags           = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

    if(GetOpenFileName(&filename))
    {
        LoadFile(filename.lpstrFile);
    }
}

void LoadFile(LPSTR file)
{
    HANDLE filehandle;
    DWORD  dwordsize ;
    DWORD  dword     ;

    LPBYTE buffer = NULL;

    filehandle = CreateFile(file, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0,NULL);
    dwordsize  = GetFileSize(filehandle, NULL);
    buffer     = (LPBYTE)HeapAlloc(GetProcessHeap(),
				   HEAP_GENERATE_EXCEPTIONS, dwordsize + 1);
    ReadFile(filehandle, (LPWSTR)buffer, dwordsize, &dword, NULL);
    CloseHandle(filehandle);
    buffer[dwordsize] = 0;
    SetWindowText(gEdit, (LPSTR)buffer);
    HeapFree(GetProcessHeap(), 0, buffer);
}
    


実行結果




2469 visits
Posted: Mar. 27, 2020
Update: May. 28, 2020

ホーム   C/C++チュートリアル   目次