ラベル win32 の投稿を表示しています。 すべての投稿を表示
ラベル win32 の投稿を表示しています。 すべての投稿を表示

2013年6月22日土曜日

最初のプログラム:win32

win32コンソールアプリケーションを空で作る。
#include <stdio.h>//printf とかで必要
#include<windows.h>//MessageBox とかで必要

void main(void)
{
 printf("コンソール表示開始\n");
 MessageBox(0,0,TEXT("OK"),0);
}


2013年6月17日月曜日

MCI ウィンドウの音量の設定


  hMCI = MCIWndCreate(hWnd, ((LPCREATESTRUCT)lParam)->hInstance, 0, "G:\\sampleMovie\\AVI-352x240.AVI");
  MCIWndSetVolume(hMCI,0);//MCI ウィンドウの音量を設定0-1000
  MCIWndPlay(hMCI);//再生開始

MCI ウィンドウ-再生位置の指定


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

#include <vfw.h>
#pragma comment (lib, "vfw32.lib")
// Global variables

// The main window class name.
static TCHAR szWindowClass[] = _T("win32app");

// The string that appears in the application's title bar.
static TCHAR szTitle[] = _T("Win32 Guided Tour Application");

HINSTANCE hInst;

// Forward declarations of functions included in this code module:
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

    if (!RegisterClassEx(&wcex))
    {
        MessageBox(NULL,
            _T("Call to RegisterClassEx failed!"),
            _T("Win32 Guided Tour"),
            NULL);

        return 1;
    }

    hInst = hInstance; // Store instance handle in our global variable

    HWND hWnd = CreateWindow(
        szWindowClass,
        szTitle,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,
        500, 100,
        NULL,
        NULL,
        hInstance,
        NULL
    );

    if (!hWnd)
    {
        MessageBox(NULL,
            _T("Call to CreateWindow failed!"),
            _T("Win32 Guided Tour"),
            NULL);

        return 1;
    }

    ShowWindow(hWnd,
        nCmdShow);
    UpdateWindow(hWnd);

    // Main message loop:
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int) msg.wParam;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    TCHAR greeting[] = _T("MCI コマンド");

 //MCI関係
 static HWND hMCI;
 switch (message)
 {
 case WM_CREATE:
  hMCI = MCIWndCreate(hWnd, ((LPCREATESTRUCT)lParam)->hInstance, 0, "G:\\sampleMovie\\AVI-352x240.avi");
  MCIWndSeek(hMCI,145);//フレームで指定
  MCIWndPlay(hMCI);//再生開始

  break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
        break;
    }

    return 0;
}

ミリ秒で指定する場合
  hMCI = MCIWndCreate(hWnd, ((LPCREATESTRUCT)lParam)->hInstance, 0, "G:\\sampleMovie\\AVI-352x240.avi");
  MCIWndSetTimeFormat(hMCI,"ms");//MCIウインドウのタイムフォーマットをミリ秒に設定
  MCIWndSeek(hMCI,5000);//ミリ秒で指定
  MCIWndPlay(hMCI);//再生開始


MCI ウィンドウ-再生速度


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

#include <vfw.h>
#pragma comment (lib, "vfw32.lib")
// Global variables

// The main window class name.
static TCHAR szWindowClass[] = _T("win32app");

// The string that appears in the application's title bar.
static TCHAR szTitle[] = _T("Win32 Guided Tour Application");

HINSTANCE hInst;

// Forward declarations of functions included in this code module:
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

    if (!RegisterClassEx(&wcex))
    {
        MessageBox(NULL,
            _T("Call to RegisterClassEx failed!"),
            _T("Win32 Guided Tour"),
            NULL);

        return 1;
    }

    hInst = hInstance; // Store instance handle in our global variable

    HWND hWnd = CreateWindow(
        szWindowClass,
        szTitle,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,
        500, 100,
        NULL,
        NULL,
        hInstance,
        NULL
    );

    if (!hWnd)
    {
        MessageBox(NULL,
            _T("Call to CreateWindow failed!"),
            _T("Win32 Guided Tour"),
            NULL);

        return 1;
    }

    ShowWindow(hWnd,
        nCmdShow);
    UpdateWindow(hWnd);

    // Main message loop:
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int) msg.wParam;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    TCHAR greeting[] = _T("MCI コマンド");

 //MCI関係
 static HWND hMCI;
 switch (message)
 {
 case WM_CREATE:
  hMCI = MCIWndCreate(hWnd, ((LPCREATESTRUCT)lParam)->hInstance, 0, "G:\\sampleMovie\\avi_divx5_mp3.avi");
  MCIWndSetSpeed(hMCI,2000);//2倍速
  MCIWndPlay(hMCI);//再生開始

  break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
        break;
    }

    return 0;
}



MCI コマンド-MCI_SEEK_PARMS(前進)


// GT_HelloWorldWin32.cpp
// compile with: /D_UNICODE /DUNICODE /DWIN32 /D_WINDOWS /c

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

#pragma comment(lib,"winmm")
// Global variables

// The main window class name.
static TCHAR szWindowClass[] = _T("win32app");

// The string that appears in the application's title bar.
static TCHAR szTitle[] = _T("Win32 Guided Tour Application");

HINSTANCE hInst;

// Forward declarations of functions included in this code module:
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

    if (!RegisterClassEx(&wcex))
    {
        MessageBox(NULL,
            _T("Call to RegisterClassEx failed!"),
            _T("Win32 Guided Tour"),
            NULL);

        return 1;
    }

    hInst = hInstance; // Store instance handle in our global variable

    // The parameters to CreateWindow explained:
    // szWindowClass: the name of the application
    // szTitle: the text that appears in the title bar
    // WS_OVERLAPPEDWINDOW: the type of window to create
    // CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
    // 500, 100: initial size (width, length)
    // NULL: the parent of this window
    // NULL: this application does not have a menu bar
    // hInstance: the first parameter from WinMain
    // NULL: not used in this application
    HWND hWnd = CreateWindow(
        szWindowClass,
        szTitle,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,
        500, 100,
        NULL,
        NULL,
        hInstance,
        NULL
    );

    if (!hWnd)
    {
        MessageBox(NULL,
            _T("Call to CreateWindow failed!"),
            _T("Win32 Guided Tour"),
            NULL);

        return 1;
    }

    // The parameters to ShowWindow explained:
    // hWnd: the value returned from CreateWindow
    // nCmdShow: the fourth parameter from WinMain
    ShowWindow(hWnd,
        nCmdShow);
    UpdateWindow(hWnd);

    // Main message loop:
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int) msg.wParam;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;
    TCHAR greeting[] = _T("MCI コマンド");

 //MCI関係
 static MCI_OPEN_PARMS mop;
 static MCI_SEEK_PARMS msp;
 static DWORD ms=0;

 int iErr;
 TCHAR strErr[1024];

 switch (message)
 {
 case WM_CREATE:
  mop.dwCallback = (DWORD)hWnd;
  mop.lpstrDeviceType = (LPCSTR)MCI_DEVTYPE_WAVEFORM_AUDIO;
  mop.lpstrElementName = "G:\\sampleMovie\\sample.wav";
  iErr = mciSendCommand(0, 
   MCI_OPEN, 
   MCI_OPEN_TYPE|MCI_OPEN_TYPE_ID | MCI_OPEN_ELEMENT
   , (DWORD)&mop);
  if (iErr) {
   mciGetErrorString(iErr , strErr , 1024);
   MessageBox(hWnd , strErr , NULL , MB_OK);
  }

  //前進
  msp.dwCallback=(DWORD)hWnd;
  msp.dwTo=(ms+=10000);    //ミリ秒単位
  iErr=mciSendCommand(mop.wDeviceID,
   MCI_SEEK,MCI_NOTIFY | MCI_TO,
   (DWORD)&msp);
  if(iErr){
   mciGetErrorString(iErr,strErr,1024);
   MessageBox(hWnd,strErr,"前進",MB_OK);
  }

  mciSendCommand(mop.wDeviceID , MCI_PLAY , 0 , 0);

  break;
 case MM_MCINOTIFY:


  break;
 case WM_PAINT:
  hdc = BeginPaint(hWnd, &ps);

        // Here your application is laid out.
        // For this introduction, we just print out "Hello, World!"
        // in the top left corner.
        TextOut(hdc,
            5, 5,
            greeting, _tcslen(greeting));
        // End application-specific layout section.

        EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
        break;
    }

    return 0;
}



MCI コマンド-繰り返し再生


// GT_HelloWorldWin32.cpp
// compile with: /D_UNICODE /DUNICODE /DWIN32 /D_WINDOWS /c

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

#pragma comment(lib,"winmm")
// Global variables

// The main window class name.
static TCHAR szWindowClass[] = _T("win32app");

// The string that appears in the application's title bar.
static TCHAR szTitle[] = _T("Win32 Guided Tour Application");

HINSTANCE hInst;

// Forward declarations of functions included in this code module:
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

    if (!RegisterClassEx(&wcex))
    {
        MessageBox(NULL,
            _T("Call to RegisterClassEx failed!"),
            _T("Win32 Guided Tour"),
            NULL);

        return 1;
    }

    hInst = hInstance; // Store instance handle in our global variable

    // The parameters to CreateWindow explained:
    // szWindowClass: the name of the application
    // szTitle: the text that appears in the title bar
    // WS_OVERLAPPEDWINDOW: the type of window to create
    // CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
    // 500, 100: initial size (width, length)
    // NULL: the parent of this window
    // NULL: this application does not have a menu bar
    // hInstance: the first parameter from WinMain
    // NULL: not used in this application
    HWND hWnd = CreateWindow(
        szWindowClass,
        szTitle,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,
        500, 100,
        NULL,
        NULL,
        hInstance,
        NULL
    );

    if (!hWnd)
    {
        MessageBox(NULL,
            _T("Call to CreateWindow failed!"),
            _T("Win32 Guided Tour"),
            NULL);

        return 1;
    }

    // The parameters to ShowWindow explained:
    // hWnd: the value returned from CreateWindow
    // nCmdShow: the fourth parameter from WinMain
    ShowWindow(hWnd,
        nCmdShow);
    UpdateWindow(hWnd);

    // Main message loop:
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int) msg.wParam;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;
    TCHAR greeting[] = _T("MCI コマンド");

 //MCI関係
 static MCI_OPEN_PARMS mop;
 static MCI_PLAY_PARMS mpp;

    switch (message)
    {
 case WM_CREATE:
   mop.dwCallback=(DWORD)hWnd;
   mop.lpstrDeviceType = (LPCSTR)MCI_DEVTYPE_WAVEFORM_AUDIO;
   mop.lpstrElementName="G:\\sampleMovie\\sample.wav";

   mciSendCommand(0,
    MCI_OPEN,
    MCI_OPEN_TYPE |MCI_OPEN_TYPE_ID| MCI_OPEN_ELEMENT | MCI_NOTIFY
    ,(DWORD)&mop);
   //MCI_PLAY_PARMS構造体の設定
   mpp.dwCallback=(DWORD)hWnd;
   break;
 case MM_MCINOTIFY:    //MCI_NOTIFY 再生終了通知を受け取って再生
  if(wParam!=MCI_NOTIFY_SUCCESSFUL){
   MessageBox(hWnd,"異常終了しました",NULL,MB_OK);
   return 0;
  }else 
  {
   mciSendCommand((MCIDEVICEID)lParam,MCI_SEEK,MCI_SEEK_TO_START,0);
   mciSendCommand((MCIDEVICEID)lParam,MCI_PLAY,MCI_NOTIFY,(DWORD)&mpp);
  }

  break;
 case WM_PAINT:
  hdc = BeginPaint(hWnd, &ps);

        // Here your application is laid out.
        // For this introduction, we just print out "Hello, World!"
        // in the top left corner.
        TextOut(hdc,
            5, 5,
            greeting, _tcslen(greeting));
        // End application-specific layout section.

        EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
        break;
    }

    return 0;
}


MCI コマンド

WAVE ファイルの再生

ライブラリをリンクする
#pragma comment(lib,"winmm")

ウインドウの WndProc関数部分の一部

//MCI関係
 static MCI_OPEN_PARMS mop;
 int iErr;
 TCHAR strErr[1024];


case IDM_MCI_CMD://MCIコマンド実行
   mop.dwCallback = (DWORD)hWnd;
   mop.lpstrDeviceType = (LPCSTR)MCI_DEVTYPE_WAVEFORM_AUDIO;
   mop.lpstrElementName = "G:\\sampleMovie\\sample.wav";
   iErr = mciSendCommand(0, 
    MCI_OPEN, 
    MCI_OPEN_TYPE|MCI_OPEN_TYPE_ID | MCI_OPEN_ELEMENT
    , (DWORD)&mop);
   if (iErr) {
    mciGetErrorString(iErr , strErr , 1024);
    MessageBox(hWnd , strErr , NULL , MB_OK);
   }
   mciSendCommand(mop.wDeviceID , MCI_PLAY , 0 , 0);
   
   break;

終了時
mciSendCommand(mop.wDeviceID , MCI_CLOSE , 0 , 0);

動画の再生

 //MCI関係
 static MCI_ANIM_OPEN_PARMS maop;
 static MCI_ANIM_PLAY_PARMS mapp;
 int iErr;
 TCHAR strErr[1024];

  case IDM_MCI_CMD://MCIコマンド実行
   maop.dwCallback = (DWORD)hWnd;
   maop.lpstrDeviceType = NULL;
   maop.lpstrElementName = "G:\\sampleMovie\\avi_divx5_mp3.avi";
   maop.dwStyle = WS_CHILD | WS_VISIBLE;
   maop.hWndParent = hWnd;

   iErr = mciSendCommand(0 , MCI_OPEN , MCI_OPEN_ELEMENT |
    MCI_ANIM_OPEN_PARENT | MCI_ANIM_OPEN_WS , (DWORD)&maop);
   if (iErr) {
    mciGetErrorString(iErr , strErr , 1024);
    MessageBox(hWnd , strErr , NULL , MB_OK);
   }

   mapp.dwCallback = (DWORD)hWnd;
   mciSendCommand(maop.wDeviceID , MCI_PLAY , 0 , (DWORD)&mapp);
   
   break;


2013年6月16日日曜日

メニューの操作

メニューアイテムを無効化し選択出来なくする。

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
 int wmId, wmEvent;

 static HMENU hmenu;
 static MENUITEMINFO menuInfo;

 switch (message)
 {
 case WM_CREATE:
  hmenu = GetMenu(hWnd);
  menuInfo.cbSize = sizeof (MENUITEMINFO);
  menuInfo.fMask = MIIM_STATE;
  menuInfo.fState = MFS_GRAYED;
  SetMenuItemInfo(hmenu , IDM_PLAY_PAUSE , FALSE , &menuInfo);
  break;
........

メニューにチェックを付ける


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
 int wmId, wmEvent;

 static HMENU hmenu;
 static MENUITEMINFO menuInfo;

 switch (message)
 {
 case WM_CREATE:
  hmenu = GetMenu(hWnd);
  menuInfo.cbSize = sizeof (MENUITEMINFO);
  menuInfo.fState = MFS_UNCHECKED;
  break;
 case WM_COMMAND:
  wmId    = LOWORD(wParam);
  wmEvent = HIWORD(wParam);
  
  // 選択されたメニューの解析:
  switch (wmId)
  {
  case IDM_ABOUT:
   DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
   break;
  case IDM_EXIT:
   DestroyWindow(hWnd);
   break;
  case IDM_PLAY_PAUSE:
   menuInfo.fMask = MIIM_STATE;
   if (menuInfo.fState == MFS_CHECKED)
    menuInfo.fState = MFS_UNCHECKED;
   else menuInfo.fState = MFS_CHECKED;
   SetMenuItemInfo(hmenu , IDM_PLAY_PAUSE , FALSE , &menuInfo);
   break;
  default:
   return DefWindowProc(hWnd, message, wParam, lParam);
  }
  break;
 case WM_DESTROY:
  PostQuitMessage(0);
  break;
 default:
  return DefWindowProc(hWnd, message, wParam, lParam);
 }
 return 0;
}


MCI ウィンドウ

MCIWndCreate

とりあえず使ってみる

ウインドウの作成:win32 の記事で作ったウインドウに、
MCI ウィンドウ をつけてみた。


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

#include <vfw.h>
#pragma comment (lib, "vfw32.lib")
  
// Global variables  
  
// The main window class name.  
static TCHAR szWindowClass[] = _T("win32app");  
  
// The string that appears in the application's title bar.  
static TCHAR szTitle[] = _T("Win32 Guided Tour Application");  
  
HINSTANCE hInst;  
  
// Forward declarations of functions included in this code module:  
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);  
  
int WINAPI WinMain(HINSTANCE hInstance,  
                   HINSTANCE hPrevInstance,  
                   LPSTR lpCmdLine,  
                   int nCmdShow)  
{  
    WNDCLASSEX wcex;  
  
    wcex.cbSize = sizeof(WNDCLASSEX);  
    wcex.style          = CS_HREDRAW | CS_VREDRAW;  
    wcex.lpfnWndProc    = WndProc;  
    wcex.cbClsExtra     = 0;  
    wcex.cbWndExtra     = 0;  
    wcex.hInstance      = hInstance;  
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));  
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);  
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);  
    wcex.lpszMenuName   = NULL;  
    wcex.lpszClassName  = szWindowClass;  
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));  
  
    if (!RegisterClassEx(&wcex))  
    {  
        MessageBox(NULL,  
            _T("Call to RegisterClassEx failed!"),  
            _T("Win32 Guided Tour"),  
            NULL);  
  
        return 1;  
    }  
  
    hInst = hInstance; // Store instance handle in our global variable  
  
    // The parameters to CreateWindow explained:  
    // szWindowClass: the name of the application  
    // szTitle: the text that appears in the title bar  
    // WS_OVERLAPPEDWINDOW: the type of window to create  
    // CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)  
    // 500, 100: initial size (width, length)  
    // NULL: the parent of this window  
    // NULL: this application does not have a menu bar  
    // hInstance: the first parameter from WinMain  
    // NULL: not used in this application  
    HWND hWnd = CreateWindow(  
        szWindowClass,  
        szTitle,  
        WS_OVERLAPPEDWINDOW,  
        CW_USEDEFAULT, CW_USEDEFAULT,  
        500, 300,  
        NULL,  
        NULL,  
        hInstance,  
        NULL  
    );  
  
    if (!hWnd)  
    {  
        MessageBox(NULL,  
            _T("Call to CreateWindow failed!"),  
            _T("Win32 Guided Tour"),  
            NULL);  
  
        return 1;  
    }  
  
    // The parameters to ShowWindow explained:  
    // hWnd: the value returned from CreateWindow  
    // nCmdShow: the fourth parameter from WinMain  
    ShowWindow(hWnd,  
        nCmdShow);  
    UpdateWindow(hWnd);  
  
    // Main message loop:  
    MSG msg;  
    while (GetMessage(&msg, NULL, 0, 0))  
    {  
        TranslateMessage(&msg);  
        DispatchMessage(&msg);  
    }  
  
    return (int) msg.wParam;  
}  
  
//  
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)  
//  
//  PURPOSE:  Processes messages for the main window.  
//  
//  WM_PAINT    - Paint the main window  
//  WM_DESTROY  - post a quit message and return  
//  
//  
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)  
{  
    PAINTSTRUCT ps;  
    HDC hdc;  
    TCHAR greeting[] = _T("Hello, World!");  
  
    switch (message)  
    {
 case WM_CREATE:
  MCIWndCreate(hWnd, ((LPCREATESTRUCT)lParam)->hInstance, 0, L"E:\\sampleMovie\\sample.avi");

  break;  

 case WM_PAINT:  
        hdc = BeginPaint(hWnd, &ps);  
  
        // Here your application is laid out.  
        // For this introduction, we just print out "Hello, World!"  
        // in the top left corner.  
        TextOut(hdc,  
            5, 5,  
            greeting, _tcslen(greeting));  
        // End application-specific layout section.  
  
        EndPaint(hWnd, &ps);  
        break;  
    case WM_DESTROY:  
        PostQuitMessage(0);  
        break;  
    default:  
        return DefWindowProc(hWnd, message, wParam, lParam);  
        break;  
    }  
  
    return 0;  
}  

AVIファイルでも再生できるファイルと再生できないファイルがあった。

子ウィンドウの既定のウィンドウスタイルは WS_CHILD、WS_BORDER および WS_VISIBLE です。




WS_MAXIMIZE | WS_VISIBLE | WS_CHILD

2013年6月15日土曜日

作成したウインドウ上で動画再生

ウインドウの作成:win32 の記事で作ったウインドウに
msdnのサンプルコードを実行してみる で作ったソースを追加

IVideoWindow インターフェイス

ウインドウ作成時に動画を再生するようにする。


// GT_HelloWorldWin32.cpp
// compile with: /D_UNICODE /DUNICODE /DWIN32 /D_WINDOWS /c

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

#include <dshow.h>

// Global variables

// The main window class name.
static TCHAR szWindowClass[] = _T("win32app");

// The string that appears in the application's title bar.
static TCHAR szTitle[] = _T("Win32 Guided Tour Application");

HINSTANCE hInst;

// Forward declarations of functions included in this code module:
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,
 HINSTANCE hPrevInstance,
 LPSTR lpCmdLine,
 int nCmdShow)
{
 WNDCLASSEX wcex;

 wcex.cbSize = sizeof(WNDCLASSEX);
 wcex.style          = CS_HREDRAW | CS_VREDRAW;
 wcex.lpfnWndProc    = WndProc;
 wcex.cbClsExtra     = 0;
 wcex.cbWndExtra     = 0;
 wcex.hInstance      = hInstance;
 wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
 wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
 wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
 wcex.lpszMenuName   = NULL;
 wcex.lpszClassName  = szWindowClass;
 wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

 if (!RegisterClassEx(&wcex))
 {
  MessageBox(NULL,
   _T("Call to RegisterClassEx failed!"),
   _T("Win32 Guided Tour"),
   NULL);

  return 1;
 }

 hInst = hInstance; // Store instance handle in our global variable

 // The parameters to CreateWindow explained:
 // szWindowClass: the name of the application
 // szTitle: the text that appears in the title bar
 // WS_OVERLAPPEDWINDOW: the type of window to create
 // CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
 // 500, 100: initial size (width, length)
 // NULL: the parent of this window
 // NULL: this application does not have a menu bar
 // hInstance: the first parameter from WinMain
 // NULL: not used in this application
 HWND hWnd = CreateWindow(
  szWindowClass,
  szTitle,
  WS_OVERLAPPEDWINDOW,
  CW_USEDEFAULT, CW_USEDEFAULT,
  500, 400,
  NULL,
  NULL,
  hInstance,
  NULL
  );

 if (!hWnd)
 {
  MessageBox(NULL,
   _T("Call to CreateWindow failed!"),
   _T("Win32 Guided Tour"),
   NULL);

  return 1;
 }

 // The parameters to ShowWindow explained:
 // hWnd: the value returned from CreateWindow
 // nCmdShow: the fourth parameter from WinMain
 ShowWindow(hWnd,
  nCmdShow);
 UpdateWindow(hWnd);

 // Main message loop:
 MSG msg;
 while (GetMessage(&msg, NULL, 0, 0))
 {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
 }

 return (int) msg.wParam;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
 PAINTSTRUCT ps;
 HDC hdc;
 TCHAR greeting[] = _T("Hello, World!");

 IGraphBuilder *pGraph = NULL;
 IMediaControl *pControl = NULL;
 IMediaEvent   *pEvent = NULL;
 IVideoWindow  *pVideoWindow = NULL;

 HRESULT hr = CoInitialize(NULL);// COM ライブラリを初期化する。

 switch (message)
 {
 case WM_CREATE://ウインドウ作成時の処理

  // フィルタ グラフ マネージャを作成し、インターフェイスを問い合わせる。
  hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, 
   IID_IGraphBuilder, (void **)&pGraph);
  if (FAILED(hr))
  {
   printf("ERROR - Could not create the Filter Graph Manager.");
   
  }

  hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
  hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);

  hr = pGraph->QueryInterface(IID_IVideoWindow, (LPVOID*)&pVideoWindow);
  
  // グラフを作成する。 重要 : この文字列をシステム上のファイルに置き換える。
  hr = pGraph->RenderFile(L"E:\\sampleMovie\\avi_divx5_mp3.avi", NULL);

  // 描画先ウィンドウの設定
  hr = pVideoWindow->put_Owner((OAHWND)hWnd);
  hr = pVideoWindow->put_WindowStyle(WS_CHILD|WS_CLIPSIBLINGS);
  hr = pVideoWindow->put_Visible(OATRUE);

  // グラフを実行する。
  hr = pControl->Run();

  // 描画先ウィンドウ内の描画位置の設定
  pVideoWindow->SetWindowPosition(0, 0, 320, 240);

  pVideoWindow->Release();
  pControl->Release();
  pEvent->Release();
  pGraph->Release();
  CoUninitialize();

  break;
 case WM_PAINT:
  hdc = BeginPaint(hWnd, &ps);

  // Here your application is laid out.
  // For this introduction, we just print out "Hello, World!"
  // in the top left corner.
  //TextOut(hdc,5, 5,greeting, _tcslen(greeting));
  // End application-specific layout section.

  EndPaint(hWnd, &ps);
  break;
 case WM_DESTROY:
  PostQuitMessage(0);
  break;
 default:
  return DefWindowProc(hWnd, message, wParam, lParam);
  break;
 }

 return 0;
}

2013年6月7日金曜日

エディットボックスに追記する



ボタンをクリックしたらエディットボックスに文字を追記する。

// GT_HelloWorldWin32.cpp
// compile with: /D_UNICODE /DUNICODE /DWIN32 /D_WINDOWS /c

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

// Global variables

// The main window class name.
static TCHAR szWindowClass[] = _T("win32app");

// The string that appears in the application's title bar.
static TCHAR szTitle[] = _T("Win32 Guided Tour Application");

HINSTANCE hInst;

// Forward declarations of functions included in this code module:
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

#define ID_BUTTON1 100 
#define ID_EDIT 200 

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

    if (!RegisterClassEx(&wcex))
    {
        MessageBox(NULL,
            _T("Call to RegisterClassEx failed!"),
            _T("Win32 Guided Tour"),
            NULL);

        return 1;
    }

    hInst = hInstance; // Store instance handle in our global variable

    // The parameters to CreateWindow explained:
    // szWindowClass: the name of the application
    // szTitle: the text that appears in the title bar
    // WS_OVERLAPPEDWINDOW: the type of window to create
    // CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
    // 500, 100: initial size (width, length)
    // NULL: the parent of this window
    // NULL: this application does not have a menu bar
    // hInstance: the first parameter from WinMain
    // NULL: not used in this application
    HWND hWnd = CreateWindow(
        szWindowClass,
        szTitle,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,
        500, 100,
        NULL,
        NULL,
        hInstance,
        NULL
    );

    if (!hWnd)
    {
        MessageBox(NULL,
            _T("Call to CreateWindow failed!"),
            _T("Win32 Guided Tour"),
            NULL);

        return 1;
    }

    // The parameters to ShowWindow explained:
    // hWnd: the value returned from CreateWindow
    // nCmdShow: the fourth parameter from WinMain
    ShowWindow(hWnd,
        nCmdShow);
    UpdateWindow(hWnd);

    // Main message loop:
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int) msg.wParam;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
 int    wmId, wmEvent, len;
    HDC hdc;
    TCHAR greeting[] = _T("Hello, World!");
 static HWND  hButton,hEdit;
 TCHAR *tchstr2 = TEXT("ボタンで追記。\r\n");

    switch (message)
    {
 case WM_CREATE://ウインドウ作成時の処理
  //ボタンの作成
  hButton = CreateWindowEx(0, TEXT("BUTTON"), TEXT("Button1"),
   WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
   100, 0, 100, 30,
   hWnd,
   (HMENU)ID_BUTTON1, 
   (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
   NULL);
  //エディットボックスの作成
  hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, // 拡張ウィンドウスタイル
   TEXT("EDIT"),
   TEXT("ここに入力\r\n"),
   WS_CHILD | WS_VISIBLE | WS_HSCROLL |
   WS_VSCROLL | ES_LEFT | ES_MULTILINE |
   ES_AUTOHSCROLL | ES_AUTOVSCROLL ,
   0, 0,
   LOWORD(lParam), HIWORD(lParam),
   hWnd,
   (HMENU)ID_EDIT,
   hInst,
   NULL);

  SetFocus(hEdit);

  break;
 case WM_SIZE://ウインドウのサイズが変わったとき(ウインドウ初回生成時も)に実行
  MoveWindow(hEdit, 0, 30, LOWORD(lParam), HIWORD(lParam), TRUE);
  break;
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);

        // Here your application is laid out.
        // For this introduction, we just print out "Hello, World!"
        // in the top left corner.
        TextOut(hdc,
            5, 5,
            greeting, _tcslen(greeting));
        // End application-specific layout section.

        EndPaint(hWnd, &ps);
        break;
 case WM_COMMAND:
  wmId = LOWORD(wParam);
  wmEvent = HIWORD(wParam);
  switch (wmId)
  {
  case ID_BUTTON1://ボタンが押された時の処理
   len = GetWindowTextLength(GetDlgItem(hWnd, ID_EDIT));
   SendMessage(GetDlgItem(hWnd, ID_EDIT), EM_SETSEL, len, len);
   SendMessage(GetDlgItem(hWnd, ID_EDIT), EM_REPLACESEL, 0, (LPARAM)tchstr2);
   break;
  }
  
  break;
    
 case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
        break;
    }

    return 0;
}


ウインドウにエディットボックスを付ける。



// GT_HelloWorldWin32.cpp
// compile with: /D_UNICODE /DUNICODE /DWIN32 /D_WINDOWS /c

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

// Global variables

// The main window class name.
static TCHAR szWindowClass[] = _T("win32app");

// The string that appears in the application's title bar.
static TCHAR szTitle[] = _T("Win32 Guided Tour Application");

HINSTANCE hInst;

// Forward declarations of functions included in this code module:
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

#define ID_BUTTON1 100 
#define ID_EDIT 200 

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

    if (!RegisterClassEx(&wcex))
    {
        MessageBox(NULL,
            _T("Call to RegisterClassEx failed!"),
            _T("Win32 Guided Tour"),
            NULL);

        return 1;
    }

    hInst = hInstance; // Store instance handle in our global variable

    // The parameters to CreateWindow explained:
    // szWindowClass: the name of the application
    // szTitle: the text that appears in the title bar
    // WS_OVERLAPPEDWINDOW: the type of window to create
    // CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
    // 500, 100: initial size (width, length)
    // NULL: the parent of this window
    // NULL: this application does not have a menu bar
    // hInstance: the first parameter from WinMain
    // NULL: not used in this application
    HWND hWnd = CreateWindow(
        szWindowClass,
        szTitle,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,
        500, 100,
        NULL,
        NULL,
        hInstance,
        NULL
    );

    if (!hWnd)
    {
        MessageBox(NULL,
            _T("Call to CreateWindow failed!"),
            _T("Win32 Guided Tour"),
            NULL);

        return 1;
    }

    // The parameters to ShowWindow explained:
    // hWnd: the value returned from CreateWindow
    // nCmdShow: the fourth parameter from WinMain
    ShowWindow(hWnd,
        nCmdShow);
    UpdateWindow(hWnd);

    // Main message loop:
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int) msg.wParam;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
 int    wmId, wmEvent;
    HDC hdc;
    TCHAR greeting[] = _T("Hello, World!");
 static HWND  hButton,hEdit;

    switch (message)
    {
 case WM_CREATE://ウインドウ作成時の処理
  //ボタンの作成
  hButton = CreateWindowEx(0, TEXT("BUTTON"), TEXT("Button1"),
   WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
   100, 0, 100, 30,
   hWnd,
   (HMENU)ID_BUTTON1, 
   (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
   NULL);
  //エディットボックスの作成
  hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, // 拡張ウィンドウスタイル
   TEXT("EDIT"),
   TEXT("ここに入力\r\n"),
   WS_CHILD | WS_VISIBLE | WS_HSCROLL |
   WS_VSCROLL | ES_LEFT | ES_MULTILINE |
   ES_AUTOHSCROLL | ES_AUTOVSCROLL ,
   0, 0,
   LOWORD(lParam), HIWORD(lParam),
   hWnd,
   (HMENU)ID_EDIT,
   hInst,
   NULL);

  SetFocus(hEdit);

  break;
 case WM_SIZE://ウインドウのサイズが変わったとき(ウインドウ初回生成時も)に実行
  MoveWindow(hEdit, 0, 30, LOWORD(lParam), HIWORD(lParam), TRUE);
  break;
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);

        // Here your application is laid out.
        // For this introduction, we just print out "Hello, World!"
        // in the top left corner.
        TextOut(hdc,
            5, 5,
            greeting, _tcslen(greeting));
        // End application-specific layout section.

        EndPaint(hWnd, &ps);
        break;
 case WM_COMMAND:
  wmId = LOWORD(wParam);
  wmEvent = HIWORD(wParam);
  switch (wmId)
  {
  case ID_BUTTON1://ボタンが押された時の処理

   MessageBox(0,0,0,0);
   break;
  }
  
  break;
    
 case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
        break;
    }

    return 0;
}

ウインドウにボタンを付ける



// GT_HelloWorldWin32.cpp
// compile with: /D_UNICODE /DUNICODE /DWIN32 /D_WINDOWS /c

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

// Global variables

// The main window class name.
static TCHAR szWindowClass[] = _T("win32app");

// The string that appears in the application's title bar.
static TCHAR szTitle[] = _T("Win32 Guided Tour Application");

HINSTANCE hInst;

// Forward declarations of functions included in this code module:
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

#define ID_BUTTON1 (0) 

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

    if (!RegisterClassEx(&wcex))
    {
        MessageBox(NULL,
            _T("Call to RegisterClassEx failed!"),
            _T("Win32 Guided Tour"),
            NULL);

        return 1;
    }

    hInst = hInstance; // Store instance handle in our global variable

    // The parameters to CreateWindow explained:
    // szWindowClass: the name of the application
    // szTitle: the text that appears in the title bar
    // WS_OVERLAPPEDWINDOW: the type of window to create
    // CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
    // 500, 100: initial size (width, length)
    // NULL: the parent of this window
    // NULL: this application does not have a menu bar
    // hInstance: the first parameter from WinMain
    // NULL: not used in this application
    HWND hWnd = CreateWindow(
        szWindowClass,
        szTitle,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,
        500, 100,
        NULL,
        NULL,
        hInstance,
        NULL
    );

    if (!hWnd)
    {
        MessageBox(NULL,
            _T("Call to CreateWindow failed!"),
            _T("Win32 Guided Tour"),
            NULL);

        return 1;
    }

    // The parameters to ShowWindow explained:
    // hWnd: the value returned from CreateWindow
    // nCmdShow: the fourth parameter from WinMain
    ShowWindow(hWnd,
        nCmdShow);
    UpdateWindow(hWnd);

    // Main message loop:
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int) msg.wParam;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
 int    wmId, wmEvent;
    HDC hdc;
    TCHAR greeting[] = _T("Hello, World!");
 static HWND  hButton;

    switch (message)
    {
 case WM_CREATE://ウインドウ作成時の処理
  hButton = CreateWindowEx(0, TEXT("BUTTON"), TEXT("Button1"),
   WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
   100, 0, 100, 30,
   hWnd,
   (HMENU)ID_BUTTON1, 
   (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
   NULL);

  break;
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);

        // Here your application is laid out.
        // For this introduction, we just print out "Hello, World!"
        // in the top left corner.
        TextOut(hdc,
            5, 5,
            greeting, _tcslen(greeting));
        // End application-specific layout section.

        EndPaint(hWnd, &ps);
        break;
 case WM_COMMAND:
  wmId = LOWORD(wParam);
  wmEvent = HIWORD(wParam);
  switch (wmId)
  {
  case ID_BUTTON1://ボタンが押された時の処理

   MessageBox(0,0,0,0);
   break;
  }
  
  break;
    
 case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
        break;
    }

    return 0;
}

2013年6月5日水曜日

関数を作る

MyFunc.cpp
#include "stdafx.h"

void f(){
 MessageBox(NULL, TEXT("ボタンがクリックされた"),
  TEXT("タイトル"),
  NULL);
}

//他の関数........


MyFunc.h を作って関数を登録
void f();

//他の関数........


stdafx.h に登録
#pragma once

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

#include "Resource.h"

#include "MyFunc.h"


include 部分をまとめる

ウインドウにボタンを張り付ける」を修正する。

stdafx.h ファイルを作ってinclude 部分をまとめる。


まとめた後、



ウインドウにボタンを張り付ける

ウインドウにメニューをつける  の記事のプロジェクト・ソースを変更する



resource.h ファイルの最後に次の行を加える
#define ID_BUTTON 200

// GT_HelloWorldWin32.cpp
// compile with: /D_UNICODE /DUNICODE /DWIN32 /D_WINDOWS /c

#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include "resource.h"

// Global variables

// The main window class name.
static TCHAR szWindowClass[] = _T("win32app");

// The string that appears in the application's title bar.
static TCHAR szTitle[] = _T("Win32 Guided Tour Application");

HINSTANCE hInst;

// Forward declarations of functions included in this code module:
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,
 HINSTANCE hPrevInstance,
 LPSTR lpCmdLine,
 int nCmdShow)
{
 WNDCLASSEX wcex;

 wcex.cbSize = sizeof(WNDCLASSEX);
 wcex.style          = CS_HREDRAW | CS_VREDRAW;
 wcex.lpfnWndProc    = WndProc;
 wcex.cbClsExtra     = 0;
 wcex.cbWndExtra     = 0;
 wcex.hInstance      = hInstance;
 wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
 wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
 wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
 wcex.lpszMenuName   = MAKEINTRESOURCE(IDC_WIN32SANPLE);
 wcex.lpszClassName  = szWindowClass;
 wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

 if (!RegisterClassEx(&wcex))
 {
  MessageBox(NULL,
   _T("Call to RegisterClassEx failed!"),
   _T("Win32 Guided Tour"),
   NULL);

  return 1;
 }

 hInst = hInstance; // Store instance handle in our global variable

 // The parameters to CreateWindow explained:
 // szWindowClass: the name of the application
 // szTitle: the text that appears in the title bar
 // WS_OVERLAPPEDWINDOW: the type of window to create
 // CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
 // 500, 100: initial size (width, length)
 // NULL: the parent of this window
 // NULL: this application does not have a menu bar
 // hInstance: the first parameter from WinMain
 // NULL: not used in this application
 HWND hWnd = CreateWindow(
  szWindowClass,
  szTitle,
  WS_OVERLAPPEDWINDOW,
  CW_USEDEFAULT, CW_USEDEFAULT,
  500, 100,
  NULL,
  NULL,
  hInstance,
  NULL
  );

 if (!hWnd)
 {
  MessageBox(NULL,
   _T("Call to CreateWindow failed!"),
   _T("Win32 Guided Tour"),
   NULL);

  return 1;
 }

 // The parameters to ShowWindow explained:
 // hWnd: the value returned from CreateWindow
 // nCmdShow: the fourth parameter from WinMain
 ShowWindow(hWnd,
  nCmdShow);
 UpdateWindow(hWnd);

 // Main message loop:
 MSG msg;
 while (GetMessage(&msg, NULL, 0, 0))
 {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
 }

 return (int) msg.wParam;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
 PAINTSTRUCT ps;
 HDC hdc;
 TCHAR greeting[] = _T("Hello, World!");
 static HMENU hMenu;
 int wmId, wmEvent;
 static HWND hButton;

 switch (message)
 {
 case WM_CREATE://ウインドウ作成時の処理
  hButton = CreateWindowEx(0, TEXT("BUTTON"), TEXT("Button1"),
   WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
   100, 0, 100, 30,
   hWnd, (HMENU)ID_BUTTON, 
   (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), NULL);
  break;

 case WM_COMMAND:
  wmId = LOWORD(wParam);
  wmEvent = HIWORD(wParam);
  switch (wmId)
  {
  case ID_BUTTON://ボタンが押された時の処理
   MessageBox(NULL, TEXT("ボタンがクリックされた"),
    TEXT("タイトル"),
    NULL);
   break;
  case IDM_MAKE:
   MessageBox(NULL,TEXT("新規作成が選択された"),
    TEXT("タイトル"),
    NULL);
   break;
  case IDM_OPEN:
   MessageBox(NULL,TEXT("開くが選択された"),
    TEXT("タイトル"),
    NULL);
   break;
  case IDM_EXIT://プログラムの終了
   DestroyWindow(hWnd);
   break;
  default:
   return DefWindowProc(hWnd, message, wParam, lParam);
  }
  break;

 case WM_PAINT:
  hdc = BeginPaint(hWnd, &ps);

  // Here your application is laid out.
  // For this introduction, we just print out "Hello, World!"
  // in the top left corner.
  TextOut(hdc,
   5, 5,
   greeting, _tcslen(greeting));
  // End application-specific layout section.

  EndPaint(hWnd, &ps);
  break;
 case WM_DESTROY:
  PostQuitMessage(0);
  break;
 default:
  return DefWindowProc(hWnd, message, wParam, lParam);
  break;
 }

 return 0;
}






ウインドウにメニューをつける

ウインドウの作成:win32 の記事のプロジェクト・ソースを変更する


ヘッダファイルを作成する resource.h
#define IDM_MAKE 100
#define IDM_OPEN 101
#define IDM_ABOUT 104
#define IDM_EXIT 105
#define IDC_WIN32SANPLE 109
  
最後の行は改行しないとエラーが出る。

Visual C++ 2010 Express ではリソースファイルを作れないので、
コピーして持ってくるかテキストエディッターで作って読み込む。

読み込んだファイルを開いてソースコードを書き込んで保存する。 test.rc
#include "resource.h"

/////////////////////////////////////////////////////////////////////////////
//
// メニュー
//

IDC_WIN32SANPLE MENU
BEGIN
    POPUP "ファイル(&F)"
    BEGIN
    MENUITEM "新規作成(&N)" , IDM_MAKE
    MENUITEM "開く(&O)" , IDM_OPEN
    MENUITEM "アプリケーションの終了(&X)", IDM_EXIT
    END
    POPUP "ヘルプ(&H)"
    BEGIN
    MENUITEM "バージョン情報(&A)...", IDM_ABOUT
    END
END

// GT_HelloWorldWin32.cpp
// compile with: /D_UNICODE /DUNICODE /DWIN32 /D_WINDOWS /c

#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include "resource.h"

// Global variables

// The main window class name.
static TCHAR szWindowClass[] = _T("win32app");

// The string that appears in the application's title bar.
static TCHAR szTitle[] = _T("Win32 Guided Tour Application");

HINSTANCE hInst;

// Forward declarations of functions included in this code module:
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,
 HINSTANCE hPrevInstance,
 LPSTR lpCmdLine,
 int nCmdShow)
{
 WNDCLASSEX wcex;

 wcex.cbSize = sizeof(WNDCLASSEX);
 wcex.style          = CS_HREDRAW | CS_VREDRAW;
 wcex.lpfnWndProc    = WndProc;
 wcex.cbClsExtra     = 0;
 wcex.cbWndExtra     = 0;
 wcex.hInstance      = hInstance;
 wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
 wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
 wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
 wcex.lpszMenuName   = MAKEINTRESOURCE(IDC_WIN32SANPLE);
 wcex.lpszClassName  = szWindowClass;
 wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

 if (!RegisterClassEx(&wcex))
 {
  MessageBox(NULL,
   _T("Call to RegisterClassEx failed!"),
   _T("Win32 Guided Tour"),
   NULL);

  return 1;
 }

 hInst = hInstance; // Store instance handle in our global variable

 // The parameters to CreateWindow explained:
 // szWindowClass: the name of the application
 // szTitle: the text that appears in the title bar
 // WS_OVERLAPPEDWINDOW: the type of window to create
 // CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
 // 500, 100: initial size (width, length)
 // NULL: the parent of this window
 // NULL: this application does not have a menu bar
 // hInstance: the first parameter from WinMain
 // NULL: not used in this application
 HWND hWnd = CreateWindow(
  szWindowClass,
  szTitle,
  WS_OVERLAPPEDWINDOW,
  CW_USEDEFAULT, CW_USEDEFAULT,
  500, 100,
  NULL,
  NULL,
  hInstance,
  NULL
  );

 if (!hWnd)
 {
  MessageBox(NULL,
   _T("Call to CreateWindow failed!"),
   _T("Win32 Guided Tour"),
   NULL);

  return 1;
 }

 // The parameters to ShowWindow explained:
 // hWnd: the value returned from CreateWindow
 // nCmdShow: the fourth parameter from WinMain
 ShowWindow(hWnd,
  nCmdShow);
 UpdateWindow(hWnd);

 // Main message loop:
 MSG msg;
 while (GetMessage(&msg, NULL, 0, 0))
 {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
 }

 return (int) msg.wParam;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
 PAINTSTRUCT ps;
 HDC hdc;
 TCHAR greeting[] = _T("Hello, World!");
 static HMENU hMenu;
 int wmId, wmEvent;
 

 switch (message)
 {
 case WM_COMMAND:
  wmId = LOWORD(wParam);
  wmEvent = HIWORD(wParam);
  // 選択したメニューの処理
  switch (wmId)
  {
  case IDM_MAKE:
   MessageBox(NULL,TEXT("新規作成が選択された"),
    TEXT("タイトル"),
    NULL);
   break;
  case IDM_OPEN:
   MessageBox(NULL,TEXT("開くが選択された"),
    TEXT("タイトル"),
    NULL);
   break;
  case IDM_EXIT://プログラムの終了
   DestroyWindow(hWnd);
   break;
  default:
   return DefWindowProc(hWnd, message, wParam, lParam);
  }
  break;

 case WM_PAINT:
  hdc = BeginPaint(hWnd, &ps);

  // Here your application is laid out.
  // For this introduction, we just print out "Hello, World!"
  // in the top left corner.
  TextOut(hdc,
   5, 5,
   greeting, _tcslen(greeting));
  // End application-specific layout section.

  EndPaint(hWnd, &ps);
  break;
 case WM_DESTROY:
  PostQuitMessage(0);
  break;
 default:
  return DefWindowProc(hWnd, message, wParam, lParam);
  break;
 }

 return 0;
}

ウインドウの作成:win32

msdnのCreating Win32 Applications (C++) のコードを実行してみる。


// GT_HelloWorldWin32.cpp
// compile with: /D_UNICODE /DUNICODE /DWIN32 /D_WINDOWS /c

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

// Global variables

// The main window class name.
static TCHAR szWindowClass[] = _T("win32app");

// The string that appears in the application's title bar.
static TCHAR szTitle[] = _T("Win32 Guided Tour Application");

HINSTANCE hInst;

// Forward declarations of functions included in this code module:
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

    if (!RegisterClassEx(&wcex))
    {
        MessageBox(NULL,
            _T("Call to RegisterClassEx failed!"),
            _T("Win32 Guided Tour"),
            NULL);

        return 1;
    }

    hInst = hInstance; // Store instance handle in our global variable

    // The parameters to CreateWindow explained:
    // szWindowClass: the name of the application
    // szTitle: the text that appears in the title bar
    // WS_OVERLAPPEDWINDOW: the type of window to create
    // CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
    // 500, 100: initial size (width, length)
    // NULL: the parent of this window
    // NULL: this application does not have a menu bar
    // hInstance: the first parameter from WinMain
    // NULL: not used in this application
    HWND hWnd = CreateWindow(
        szWindowClass,
        szTitle,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,
        500, 100,
        NULL,
        NULL,
        hInstance,
        NULL
    );

    if (!hWnd)
    {
        MessageBox(NULL,
            _T("Call to CreateWindow failed!"),
            _T("Win32 Guided Tour"),
            NULL);

        return 1;
    }

    // The parameters to ShowWindow explained:
    // hWnd: the value returned from CreateWindow
    // nCmdShow: the fourth parameter from WinMain
    ShowWindow(hWnd,
        nCmdShow);
    UpdateWindow(hWnd);

    // Main message loop:
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int) msg.wParam;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;
    TCHAR greeting[] = _T("Hello, World!");

    switch (message)
    {
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);

        // Here your application is laid out.
        // For this introduction, we just print out "Hello, World!"
        // in the top left corner.
        TextOut(hdc,
            5, 5,
            greeting, _tcslen(greeting));
        // End application-specific layout section.

        EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
        break;
    }

    return 0;
}

最初のプログラム:win32


#include <windows.h>

int WINAPI WinMain(
 HINSTANCE hInstance,
 HINSTANCE hPrevInstance,
 LPSTR lpCmdLine,
 int nCmdShow)
{
 MessageBox(0 , 0 ,0, 0);

 return 0;
}