zoukankan      html  css  js  c++  java
  • Windows应用程序结构

    解决”char []"转换为"LPCWSTR":

    ①利用L" "或_T(" ")

    ②项目——属性——常规——字符集——使用多字节字符集

    #include<windows.h>
    #include<tchar.h>
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    
    int WINAPI WinMain(HINSTANCE hInsance, HINSTANCE PreInstance, LPSTR lpCmdLine, int nCmdShow)
    {
        HWND hwnd;
        HINSTANCE hInstance;
        MSG msg;
        char lpszClassName[] = "窗口";
        WNDCLASS wc;
        wc.style = 0;
        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.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
        wc.lpszMenuName = NULL;
        wc.lpszClassName = "lpszClassName";
    
        RegisterClass(&wc);
    
        hwnd = CreateWindow(lpszClassName, "Windows", WS_OVERLAPPEDWINDOW, 120, 50, 800, 600, NULL, NULL, hInstance, NULL);
    
        ShowWindow(hwnd, nCmdShow);
        UpdateWindow(hwnd);
    
        while (GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        return msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)
        {
        case WM_LBUTTONDOWN:
        {
            MessageBeep(0);
        }
        break;
    
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
    
        default:
            return DefWindowProc(hwnd, message, wParam, lParam);
        }
        return 0;
    }
    Windows程序:左键单击“叮”的一声
    #include<windows.h>
    #include<tchar.h>
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    int WINAPI WinMain(HINSTANCE hInsance, HINSTANCE PreInstance, LPSTR lpCmdLine, int nCmdShow)
    //{
    //    HWND hwnd;
    //    HINSTANCE hInstance;
    //    MSG msg;
    //    char lpszClassName[] = "窗口";
    //    WNDCLASS wc;
    //    wc.style = 0;
    //    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.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    //    wc.lpszMenuName = NULL;
    //    wc.lpszClassName = "lpszClassName";
    //
    //    RegisterClass(&wc);
    //
    //    hwnd = CreateWindow(lpszClassName, "Windows", WS_OVERLAPPEDWINDOW, 120, 50, 800, 600, NULL, NULL, hInstance, NULL);
    //
    //    ShowWindow(hwnd, nCmdShow);
    //    UpdateWindow(hwnd);
    //
    //    while (GetMessage(&msg, NULL, 0, 0))
    //    {
    //        TranslateMessage(&msg);
    //        DispatchMessage(&msg);
    //    }
    //    return msg.wParam;
    //}
    
    {
        HWND hwnd1;
        HWND hwnd2;
        HWND hwnd3;
    
        HINSTANCE hInstance;
    
        MSG msg;
    
        char lpszClassName1[] = "窗口1";
        WNDCLASS wc1;
        wc1.style = 0;
        wc1.style = 0;
            wc1.lpfnWndProc = WndProc;
            wc1.cbClsExtra = 0;
            wc1.cbWndExtra = 0;
            wc1.hInstance = hInstance;
            wc1.hIcon = LoadIcon(NULL, IDI_APPLICATION);
            wc1.hCursor = LoadCursor(NULL, IDC_ARROW);
            wc1.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
            wc1.lpszMenuName = NULL;
            wc1.lpszClassName = "lpszClassName";
    
            RegisterClass(&wc1);
    
            char lpszClassName2[] = "窗口2";
            WNDCLASS wc2;
            wc2.style = 0;
            wc2.style = 0;
            wc2.lpfnWndProc = WndProc;
            wc2.cbClsExtra = 0;
            wc2.cbWndExtra = 0;
            wc2.hInstance = hInstance;
            wc2.hIcon = LoadIcon(NULL, IDI_APPLICATION);
            wc2.hCursor = LoadCursor(NULL, IDC_ARROW);
            wc2.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
            wc2.lpszMenuName = NULL;
            wc2.lpszClassName = "lpszClassName";
    
            RegisterClass(&wc2);
    
            hwnd1 = CreateWindow(lpszClassName1, "Windows1", WS_OVERLAPPEDWINDOW, 120, 50, 800, 600, NULL, NULL, hInstance, NULL);
            hwnd2 = CreateWindow(lpszClassName2, "Windows2", WS_OVERLAPPEDWINDOW, 120, 50, 800, 600, NULL, NULL, hInstance, NULL);
            hwnd3 = CreateWindow(lpszClassName1, "Windows3", WS_OVERLAPPEDWINDOW, 120, 50, 800, 600, NULL, NULL, hInstance, NULL);
    
            ShowWindow(hwnd1, nCmdShow);
            UpdateWindow(hwnd1);
    
            ShowWindow(hwnd2, nCmdShow);
            UpdateWindow(hwnd2);
    
            ShowWindow(hwnd3, nCmdShow);
            UpdateWindow(hwnd3);
    
            while (GetMessage(&msg, NULL, 0, 0))
                    {
                        TranslateMessage(&msg);
                        DispatchMessage(&msg);
                    }
                    return msg.wParam;
    
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)
        {
        case WM_LBUTTONDOWN:
        {
            MessageBeep(0);
        }
        break;
    
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
    
        default:
            return DefWindowProc(hwnd, message, wParam, lParam);
        }
        return 0;
    }
    Windows程序:三个窗口

     Windows程序组成部分:

    头文件、源文件(程序员)、动态链接库、资源

      源文件:

      主函数:WinMain:创建应用程序窗口和建立消息循环(4个API函数用来创建和显示应用程序窗口,3个用来建立消息循环)

    int WINAPI WinMain
    (
    HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow
    );
     1 int WINAPI WinMain(HINSTANCE hInsance, HINSTANCE PreInstance, LPSTR lpCmdLine, int nCmdShow)
     2 {
     3     HWND hwnd1;
     4 
     5     HINSTANCE hInstance;
     6 
     7     MSG msg;
     8 
     9     char lpszClassName1[] = "窗口1";
    10     WNDCLASS wc1;
    11     wc1.style = 0;
    12     wc1.style = 0;
    13         wc1.lpfnWndProc = WndProc;
    14         wc1.cbClsExtra = 0;
    15         wc1.cbWndExtra = 0;
    16         wc1.hInstance = hInstance;
    17         wc1.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    18         wc1.hCursor = LoadCursor(NULL, IDC_ARROW);
    19         wc1.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    20         wc1.lpszMenuName = NULL;
    21         wc1.lpszClassName = "lpszClassName";
    22 
    23         RegisterClass(&wc1);
    24 
    25         hwnd1 = CreateWindow(lpszClassName1, "Windows1", WS_OVERLAPPEDWINDOW, 120, 50, 800, 600, NULL, NULL, hInstance, NULL);
    26 
    27         ShowWindow(hwnd1, nCmdShow);
    28         UpdateWindow(hwnd1);
    29 
    30         while (GetMessage(&msg, NULL, 0, 0))
    31                 {
    32                     TranslateMessage(&msg);
    33                     DispatchMessage(&msg);
    34                 }
    35                 return msg.wParam;
    36 
    37 }
    Windows程序主函数代码

       窗口函数:以参数的方式接受系统传递来的消息并对消息进行分类处理。

    LPESULT CALLBACK WndProc
    (
     HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam   
    )
    LRESULT CALLBACK WndProc
    (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)
        {
        case WM_LBUTTONDOWN:
        {
            MessageBeep(0);
        }
        break;
    
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
    
        default:
            return DefWindowProc(hwnd, message, wParam, lParam);
        }
        return 0;
    }
    简单的窗口函数代码

      Windows系统、主函数、窗口函数之间的关系:

    高内聚、低耦合:

    利用函数或类的封装性来实现功能上的高内聚;利用指针或类似指针的索引的隔离功能来实现信息流的低耦合。

  • 相关阅读:
    网页的摘要信息
    自我介绍
    DIV和SPAN的区别
    软件工程简介
    设置层的漂移
    构建之法现代软件工程
    手机验证码的相关知识
    Python2和Python3的区别
    我的第一个练习
    结对编程后传之做汉堡
  • 原文地址:https://www.cnblogs.com/hansichen/p/7278701.html
Copyright © 2011-2022 走看看