windows 32 控制台自定义消息

#include<windows.h>
#include<stdio.h>
LRESULT CALLBACK WindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam);//过程函数声明
HWND bhwnd;
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{//设计、注册、显示窗口。。。
    WNDCLASS wndc;
    wndc.cbClsExtra=0;
    wndc.cbWndExtra=0;
    wndc.hbrBackground=(HBRUSH)GetStockObject(GRAY_BRUSH);
    wndc.hCursor=LoadCursor(NULL,IDC_ARROW);
    wndc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    wndc.hInstance=hInstance;
    wndc.lpfnWndProc=WindowProc;
    wndc.lpszClassName="MyWin";
    wndc.lpszMenuName=NULL;
    wndc.style=CS_HREDRAW|CS_VREDRAW;

    RegisterClass(&wndc);

    HWND hwnd;
    hwnd=CreateWindow("MyWin","自定义窗口",WS_OVERLAPPEDWINDOW,300,300,400,300,NULL,NULL,hInstance,NULL);
    ShowWindow(hwnd,SW_SHOWNORMAL);
    UpdateWindow(hwnd);
bhwnd=CreateWindow("Button","MyButton",WS_CHILDWINDOW,100,100,100,100,hwnd,NULL,hInstance,NULL);
ShowWindow(bhwnd,SW_SHOWNORMAL);
    MSG msg;
    while(GetMessage(&msg,NULL,0,0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
    switch(uMsg)
    {
case WM_COMMAND:
if(DWORD(lParam)==(int)bhwnd)
{
MessageBox(hwnd,"Hello!","",NULL);
}
break;
          case WM_CLOSE:
        DestroyWindow(hwnd);
        break;
          case WM_DESTROY:
        PostQuitMessage(0);
        break;
          default:
                 return DefWindowProc(hwnd,uMsg,wParam,lParam);    
    }
    return 0;

}

添加按钮button

#include <windows.h>
 
// Declare WndProcedure
LRESULT CALLBACK WndProcedure(HWND hWnd, UINT uMsg,
               WPARAM wParam, LPARAM lParam);
 
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
               LPSTR lpCmdLine, int nCmdShow)
{
    HWND       button;
    MSG        Msg;
    HWND       hWnd;
    HRESULT       hRet;
    WNDCLASSEX WndClsEx;
 
    // Populate the WNDCLASSEX structure
    WndClsEx.cbSize        = sizeof(WNDCLASSEX);
    WndClsEx.style         = CS_HREDRAW | CS_VREDRAW;
    WndClsEx.lpfnWndProc   = WndProcedure;
    WndClsEx.cbClsExtra    = 0;
    WndClsEx.cbWndExtra    = 0;
    WndClsEx.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    WndClsEx.hCursor       = LoadCursor(NULL, IDC_ARROW);
    WndClsEx.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    WndClsEx.lpszMenuName  = NULL;
    WndClsEx.lpszClassName = "GlowdotWin32TutorialPartI";
    WndClsEx.hInstance     = hInstance;
    WndClsEx.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
 
    // Register the class
    RegisterClassEx(&WndClsEx);
 
    // Create the window object
    hWnd = CreateWindow("GlowdotWin32TutorialPartI",
              "Glowdot Win32 Tutorial - Part I",
              WS_OVERLAPPEDWINDOW,
              CW_USEDEFAULT,
              CW_USEDEFAULT,
              CW_USEDEFAULT,
              CW_USEDEFAULT,
              NULL,
              NULL,
              hInstance,
              NULL);
 
    button = CreateWindow( 
    "BUTTON",                                    // predefined class
    "OK",                                        // button text
    WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,    // styles 
                                                 // Size and position values are given 
                                                 // explicitly, because the CW_USEDEFAULT 
                                                 // constant gives zero values for buttons. 
    100,                                          // starting x position
    100,                                          // starting y position 
    200,                                         // button width 
    200,                                         // button height 
    hWnd,                                        // parent window 
    NULL,                                        // No menu 
    hInstance,                                   // Our apps HINSTANCE 
    NULL                                         // pointer not needed 
);
 
    if ( !button)
       return 0;
    // Verify window creation
    if( !hWnd ) // If the window was not created,
        return 0; // stop the application
 
    // Show the window
    ShowWindow(hWnd, SW_SHOWNORMAL);
    ShowWindow(button, SW_SHOWNORMAL);
    UpdateWindow(hWnd);
    UpdateWindow(button);
    // our message pump
    while( (hRet = GetMessage( &Msg, NULL, 0, 0 )) != 0)
    { 
        if (hRet == -1)
        {
        // handle the error and possibly exit
        }
        else
        {
            TranslateMessage(&Msg); 
            DispatchMessage(&Msg); 
        }
    }
}
 
//
// WndProcedure //
//
 
LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg,
               WPARAM wParam, LPARAM lParam)
{
    switch(Msg)
    {
    case WM_DESTROY:
        // user wants to exit
        PostQuitMessage(WM_QUIT);
        break;
    default:
        // Hand off unprocessed messages to DefWindowProc
        return DefWindowProc(hWnd, Msg, wParam, lParam);
    }
 
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值