如何在Win32 Console Application生成的窗口中添加按鈕

// Test.cpp : 定義控制檯應用程序的入口點。
//

#include "stdafx.h"
#include <tchar.h>
#include <iostream>
#include <Windows.h>
//#include <atlstr.h>
#include <string.h>
#include <stdio.h>


#define MAX_STR 100


//全局變量
HINSTANCE hInst;//當前實例
TCHAR szTitle[MAX_STR] = _TEXT("Console_Win Demo");   //標題欄文本
TCHAR szWindowClass[MAX_STR] = _TEXT("Console_Win Demo");//主窗口類名


//此代碼模塊中包含的函數的前向聲明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow);
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);


HWND hwndButton1;
HWND hwndButton2;
HWND hwndButton3;
HWND hwndButton4;
HWND hwndButton5;
HWND hWnd;
char str[255]="";//保存輸出的字符串

int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "Console_Win Demo" << std::endl << std::endl;
    std::cout << "================================" << std::endl << std::endl;


HINSTANCE hInstance = NULL;
    int       nCmdShow  = SW_SHOW;      // 該變量取值參見MSDN

    hInstance = GetModuleHandle(NULL);
    std::cout << "hInstance: " << hInstance << std::endl;
    std::cout << "hInstance->unused: " << hInstance->unused << std::endl << std::endl;
    std::cout << "================================" << std::endl << std::endl;

    MSG msg;

    MyRegisterClass(hInstance);


// 執行應用程序初始化:
    if (!InitInstance (hInstance, nCmdShow)) 
    {
        std::cout << "Error in InitInstance()!" << std::endl;
        return FALSE;
    }

    // 主消息循環:
    while (GetMessage(&msg, NULL, 0, 0)) 
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int) msg.wParam;
}

//
//  函數: MyRegisterClass()
//
//  目的: 註冊窗口類。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX); 

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
wcex.style         &=~CS_VREDRAW;/*新添加*/
    wcex.lpfnWndProc    = (WNDPROC)WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = NULL;
//wcex.hIcon          =LoadIcon(NULL,IDI_APPLICATION);
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
//wcex.hbrBackground  = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wcex.lpszMenuName   = NULL;
//wcex.lpszMenuName   = MAKEINTRESOURCE(IDR_MENU1);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = NULL;
//wcex.hIconSm        =LoadIcon(NULL,IDI_INFORMATION);

    return RegisterClassEx(&wcex);
}

//
//   函數: InitInstance(HANDLE, int)
//
//   目的: 保存實例句柄並創建主窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // 將實例句柄存儲在全局變量中

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
   //hWnd = CreateWindow(_TEXT("Button"),_TEXT("OK"), WS_OVERLAPPEDWINDOW,100, 100, 500, 500, NULL, NULL, hInst, NULL);/*新添加*/

   if (!hWnd)
   {
      return FALSE;
   }

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

   return TRUE;
}


//
//  函數: WndProc(HWND, unsigned, WORD, LONG)
//
//  目的: 處理主窗口的消息。
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;



    switch (message)
    {
/*case WM_CHAR是新添加*/
/*
case WM_CHAR://按鍵消息處理
hdc=GetDC(hWnd);//獲得設備上下文
strcpy(str,"        ");
TextOut(hdc,1,1,str,strlen(str));//擦除原有字符
sprintf(str,"%c",(char)wParam);//把字符轉換成字符串
        TextOut(hdc,1,1,str,strlen(str));//輸出字符
        ReleaseDC(hWnd,hdc);//釋放設備描述表

break;

                */

/*
case WM_LBUTTONDOWN:
        hdc=GetDC(hWnd);//獲得設備上下文
        strcpy(str,"            ");
        TextOut(hdc,1,1,str,strlen(str));//擦除原有字符
        sprintf(str,"Mouse");//把字符轉換成字符串
        TextOut(hdc,1,1,str,strlen(str));//輸出字符
        ReleaseDC(hWnd,hdc);//釋放設備描述表
        break;
*/


case WM_LBUTTONDOWN:
        hdc=GetDC(hwndButton1);//獲得設備上下文
        strcpy(str,"            ");
        //TextOut(hdc,1,1,str,strlen(str));//擦除原有字符
MessageBox(hWnd,L"鼠標左鍵按下",L"提示",1);


        sprintf(str,"Mouse");//把字符轉換成字符串
        //TextOut(hdc,1,1,str,strlen(str));//輸出字符
        ReleaseDC(hWnd,hdc);//釋放設備描述表
        break;


    case WM_CREATE://這裏創建一個按鈕,這裏沒有用到ID_BUTTON綁定
hwndButton1=CreateWindow(_TEXT("Button"),_TEXT("打開圖像"),WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON,100,0,100,100,hWnd,NULL,hInst,NULL);/*新添加*/
        ShowWindow(hwndButton1,SW_SHOWNORMAL);/*新添加*/


hwndButton2=CreateWindow(_TEXT("Button"),_TEXT("二值化處理"),WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON,100,100,100,100,hWnd,NULL,hInst,NULL);/*新添加*/
        ShowWindow(hwndButton2,SW_SHOWNORMAL);/*新添加*/


hwndButton3=CreateWindow(_TEXT("Button"),_TEXT("對數變換"),WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON,100,200,100,100,hWnd,NULL,hInst,NULL);/*新添加*/
        ShowWindow(hwndButton3,SW_SHOWNORMAL);/*新添加*/


hwndButton4=CreateWindow(_TEXT("Button"),_TEXT("伽馬變換"),WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON,100,300,100,100,hWnd,NULL,hInst,NULL);/*新添加*/
        ShowWindow(hwndButton4,SW_SHOWNORMAL);/*新添加*/


hwndButton5=CreateWindow(_TEXT("Button"),_TEXT("補色變換"),WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON,100,400,100,100,hWnd,NULL,hInst,NULL);/*新添加*/
        ShowWindow(hwndButton5,SW_SHOWNORMAL);/*新添加*/


        std::cout << "Hello! I'm a CONSOLE. The WINDOW is my baby." << std::endl;
        break;
    case WM_COMMAND:
        break;
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY://終止應用程序
        PostQuitMessage(0);
        std::cout << "Goodbye!" << std::endl << std::endl;
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章