如何在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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章