• Win32 API样例


    空窗口

    #ifndef UNICODE
    #define UNICODE
    #endif 
    
    #include <windows.h>
    
    LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
    
    int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
    {
       // Register the window class.
       const wchar_t CLASS_NAME[] = L"Sample Window Class";
    
       WNDCLASS wc = { };
    
       wc.lpfnWndProc = WindowProc;
       wc.hInstance = hInstance;
       wc.lpszClassName = CLASS_NAME;
    
       RegisterClass(&wc);
    
       // Create the window.
    
       HWND hwnd = CreateWindowEx(
          0,                              // Optional window styles.
          CLASS_NAME,                     // Window class
          L"Learn to Program Windows",    // Window text
          WS_OVERLAPPEDWINDOW,            // Window style
    
          // Size and position
          CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
    
          NULL,       // Parent window    
          NULL,       // Menu
          hInstance,  // Instance handle
          NULL        // Additional application data
       );
    
       if (hwnd == NULL)
       {
          return 0;
       }
    
       ShowWindow(hwnd, nCmdShow);
    
       // Run the message loop.
    
       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_DESTROY:
          PostQuitMessage(0);
          return 0;
    
       case WM_PAINT:
       {
          PAINTSTRUCT ps;
          HDC hdc = BeginPaint(hwnd, &ps);
    
    
    
          FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));
    
          EndPaint(hwnd, &ps);
       }
       return 0;
    
       }
       return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    

    按钮,子窗口

    #include <windows.h>
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    #define ID_BEEP 1
    #define ID_QUIT 2
    #define ID_MYTEST 3
    #define ID_MYTEST2 4
    int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
       PWSTR lpCmdLine, int nCmdShow) {
    
       MSG  msg;
       WNDCLASSW wc = { 0 };
       wc.lpszClassName = L"Buttons";
       wc.hInstance = hInstance;
       wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
       wc.lpfnWndProc = WndProc;
       wc.hCursor = LoadCursor(0, IDC_ARROW);
    
       RegisterClassW(&wc);
       CreateWindowW(wc.lpszClassName, L"Buttons",
          WS_OVERLAPPEDWINDOW | WS_VISIBLE,
          150, 150, 500, 500, 0, 0, hInstance, 0);
    
       while (GetMessage(&msg, NULL, 0, 0)) {
    
          TranslateMessage(&msg);
          DispatchMessage(&msg);
       }
    
       return (int)msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,
       WPARAM wParam, LPARAM lParam) {
    
       switch (msg) {
    
       case WM_CREATE:
    
          CreateWindowW(L"Button", L"Beep",
             WS_VISIBLE | WS_CHILD,
             20, 50, 80, 25, hwnd, (HMENU)ID_BEEP, NULL, NULL);
    
          CreateWindowW(L"Button", L"Quit",
             WS_VISIBLE | WS_CHILD,
             120, 50, 80, 25, hwnd, (HMENU)ID_QUIT, NULL, NULL);
    
          CreateWindowW(L"Button", L"MyTest",
             WS_VISIBLE | WS_CHILD,
             220, 50, 80, 25, hwnd, (HMENU)ID_MYTEST, NULL, NULL);
          break;
       case WM_COMMAND:
    
          if (LOWORD(wParam) == ID_BEEP) {
    
             MessageBeep(MB_OK);
          }
    
          if (LOWORD(wParam) == ID_QUIT) {
    
             PostQuitMessage(0);
          }
    
          if (LOWORD(wParam) == ID_MYTEST) {
    
             CreateWindowW(L"Buttons", L"子窗口",
                WS_OVERLAPPEDWINDOW | WS_CHILD | WS_VISIBLE | WS_POPUP,
                300, 50, 400, 200,
                NULL, NULL, NULL, NULL);
    
          }
    
          break;
    
       case WM_DESTROY:
    
          PostQuitMessage(0);
          break;
       }
    
       return DefWindowProcW(hwnd, msg, wParam, lParam);
    }
    
  • 相关阅读:
    IDEA连接 Oracle数据库
    什么是混合云备份
    什么是阿里云ACA认证
    什么是阿里云ACE认证
    什么是轻量应用服务器
    什么是时序时空数据库TSDB
    什么是数据管理DMS
    什么是分析型数据库PostgreSQL版
    阿里云多端小程序
    阿里云云计算ACP专业认证考试
  • 原文地址:https://www.cnblogs.com/chendeqiang/p/14610700.html
Copyright © 2020-2023  润新知