(一)基于C++的widget界面框架搭建

main.cpp:

#include <iostream>
using namespace std;

#include"Engine.h"
 
//APIENTRY  :和C约定的入口
//HINSTANCE  :
//LPWSTR:命令行参数
//int nCmdShow  :窗口显示模式

//量类型 HINSTANCE HINSTANCE 是Windows里的一中数据类型
// ,其实就是一个无符号的长整形,是32位的,是用于标示(记录)一个程序的实例

int APIENTRY wWinMain(_In_  HINSTANCE hIntance, _In_opt_  HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow)
{
    Engine engine(hIntance, "window_class");
    engine.initialze("window title", 800, 600, nCmdShow);

    int time = 0;
    while (engine.ProcessMessage())
    {
        Sleep(10);
        if (time++ > 200)
        {
            //测试过200秒后就退出  close消息测试
           //engine.Close();
       }
    }
    return 0;
}

/*
int main()
{
    cout << "Hello, World!";
    return 0;
}
*/


/*++++++++++++++++++++++++++++++++++++++++++++++++++++

1.创建weindows的窗口

2.使用glew库 opengl的扩展库

3.创建HDC 设备上下文 ,HGLRC opengl 3d渲染上下文 
    在winds窗口:绘制属性,rc和dc 有一致的结构,rc与dc建立 联系 , opengl就能通过 rc对应的dc 发送到相应的显示设备上 
    所以,先获取dc  再创建对应的 rc,并把rc设为当前的上下文


一:

模糊图片 4K  8K 薇娅图片
2g  3g  4  g 10年
5g  10年 以上的繁荣 各种渠道 搜集资料学习

播放插件  第三方  这个播不了 那个播不了  很大部分原因  解码器问题

绘制带有颜色的三角形  
绘制带有颜色的四边形 美丽界面 绘图片添加贴图 
 解析3D漫游   场景漫游
帮助理解 opengl 
数据源 
各种像素格式的视频
也可以转换  
opengl:
  跨平台      相关资料越多越好 而选择学习
 Qt 跨平台     丰富功能  音频库  网络库 3d库
  Qt中的opengl有一些问题

  ffmpeg:
   
   通过视频 解码  转码  流程性强 可以有更好的收获

   二:绘制三角形
   字符集   unix   多字符 

   调试 看是否为空 看错误编号   然后 
   工具 渣找错误:

+======+================================================*/

engin.h:

#pragma once
#include<Windows.h>
#include<string>

class Engine
{

public:

    Engine(HINSTANCE hInstance, std::string window_class);
    ~Engine();
    bool initialze(std::string wind_title, int width, int height, int nCmdShow);

    //注册窗口类
    bool RegisterWindowsClass();

    //处理消息的方法
    bool ProcessMessage();


    void Close();

private:
    HWND handle;//窗口句柄
    HINSTANCE hInstance;//实例句柄

    std::string wind_title;//窗口标题
    std::wstring wind_title_wide;//宽字符标题  用于unicode

    std::string window_class;//窗口类 ??
    std::wstring window_class_wide;

    //窗口 宽高
    int width;
    int height;

private:
    //窗口句柄  消息类型  CALLBACK标准性约定
    static LRESULT CALLBACK HandleMessageSetup(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
    //重定向的方法
    static LRESULT HandleMessageRedict(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
    //重定向到这个方法
    LRESULT winProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);


    //创建 和销魂
    void OnStart();
    void OnClose();
};

engin.cpp:

#include "Engine.h"

Engine::Engine(HINSTANCE hInstance, std::string window_class)
{
    this->hInstance = hInstance;
    this->window_class = window_class;
    this->window_class_wide = std::wstring(window_class.begin(), window_class.end());

    RegisterWindowsClass();
}


bool Engine::initialze(std::string wind_title, int width, int height, int nCmdShow)
{
    
    this->wind_title = wind_title;
    this->wind_title_wide = std::wstring(wind_title.begin(), wind_title.end());
    this->width = width;
    this->height = height;
    
    int nWindMertricsX = GetSystemMetrics(SM_CXSCREEN);
    int nWindMertricsY = GetSystemMetrics(SM_CYSCREEN);
    RECT wr;
    wr.left = (nWindMertricsX - width) / 2;
    wr.top = (nWindMertricsY - height) / 2;
    wr.right = wr.left+width;
    wr.bottom= wr.top+height;//上下左右 有其他的可以
    //WS_OVERLAPPEDWINDOW 样式  False不包含菜单
    AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);

    handle=CreateWindowEx(0,//扩展风格

#ifdef UNICODE
        this->window_class_wide.c_str(),
        this->wind_title_wide.c_str(),
#else
        this->window_class.c_str(),
        this->wind_title.c_str(),
#endif // 
         
        
        WS_OVERLAPPEDWINDOW,//风格
        wr.left,
        wr.top,
        wr.right - wr.left,
        wr.bottom - wr.top,//屏幕上下左右
        NULL,
        NULL,
        hInstance,
        this);

    if (!handle)//窗口句柄
    {
        throw;
        return false;
    }

    OnStart();

    ShowWindow(handle, nCmdShow);//最大化最小化 nCmdshow 显示模式
    UpdateWindow(handle);//更新窗口
    
    SetForegroundWindow(handle);//把窗口激活到前列线程,能接受键盘 鼠标事件
    SetFocus(handle);
    
     
    return true;
}


bool Engine::RegisterWindowsClass()
{
    WNDCLASSEX wcex;
    //置零
    ZeroMemory(&wcex, sizeof(WNDCLASSEX));
    //WNDCLASSEX大小 
    wcex.cbSize = sizeof(WNDCLASSEX);
    //窗口风格
    wcex.style = CS_HREDRAW|CS_VREDRAW|CS_OWNDC;
    
    //创口处理函数
     //wcex.lpfnWndProc = DefWindowProc;
    wcex.lpfnWndProc = HandleMessageSetup;


    //wcex.style = WS_OVERLAPPEDWINDOW;
    //wcex.lpfnWndProc = NULL;
    wcex.cbClsExtra = 0;//??
    wcex.cbWndExtra = 0;

    wcex.hInstance = hInstance;
    //默认图标
    wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    //默认光标
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
    //画布的背景颜色
    wcex.hbrBackground = (HBRUSH)GetStockObject(DKGRAY_BRUSH);
    //设置菜单
    wcex.lpszMenuName = NULL;
    //小图标
    wcex.hIconSm = NULL;

    #ifdef UNICODE

    wcex.lpszClassName = this->window_class_wide.c_str();
    #else
        wcex.lpszClassName = this->window_class.c_str();
    #endif // !UNICODE

    if (!RegisterClassEx(&wcex))
    {
        int error = GetLastError();
        throw;//注意:实际开发中 不能这么做
        return false;
    }
    return true;
}

bool Engine::ProcessMessage()
{
    //定义msg 并且置0
    MSG msg;
    ZeroMemory(&msg, sizeof(MSG));

    while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    {
        //转移这份消息
        TranslateMessage(&msg);
        //分发消息
        DispatchMessage(&msg);
    }
    if (msg.message == WM_NULL)
    {
        if (!IsWindow(handle))
            return false;
    }
    if (!handle)
    {
        return false;
    }
    return true;
}

LRESULT Engine::HandleMessageSetup(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{

    switch (uMsg)
    {
    case WM_NCCREATE://NCCREATE的消息
    {
        const CREATESTRUCT* pCreate = reinterpret_cast<CREATESTRUCT*>(lParam);

        Engine* engine = reinterpret_cast<Engine*>(pCreate->lpCreateParams);

        if (!engine)
        {
            throw;
        }

        //设置用户数据
        SetWindowLongPtr(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(engine));
        //窗口处理函数
        SetWindowLongPtr(hwnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(HandleMessageRedict));

        return DefWindowProcW(hwnd, uMsg, wParam, lParam);
    }

    default:
        return DefWindowProcW(hwnd, uMsg, wParam, lParam);
    }
    return LRESULT();
}

LRESULT Engine::HandleMessageRedict(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    Engine* engine = reinterpret_cast<Engine*>(GetWindowLongPtr(hwnd, GWLP_USERDATA));
    //return LRESULT();
    return engine->winProc(hwnd, uMsg, wParam, lParam);
}
//窗口处理函数
LRESULT Engine::winProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{

    //窗口的生命周期做好
    switch (uMsg)
    {
    case WM_CLOSE:
         
        DestroyWindow(hwnd);
        this->handle = NULL;
        OnClose();
        return 0;
    case WM_DESTROY:
        //发出退出消息
        PostQuitMessage(0);
        return 0;
    default:
        return DefWindowProcW(hwnd, uMsg, wParam, lParam);
        break;
    }
    //return LRESULT();
    return DefWindowProcW(hwnd, uMsg, wParam, lParam);
}

void Engine::OnStart()
{
}

void Engine::OnClose()
{
    //SendMessage(handle, WM_CLOSE, 0, 0);
}

Engine::~Engine()
{

    if (!UnregisterClass(

#ifdef UNICODE
        this->window_class_wide.c_str(),
#else
        this->window_class.c_str(),
#endif // 
         hInstance))
    {
        throw;
    }
}

void Engine::Close()
{
    SendMessage(handle, WM_CLOSE, 0, 0);
}

通过句柄形式 学习 窗口程序 ,然后 和之前的qt窗口做对比;消耗内存减少一半多 此处消耗内存16M,而原来的视频的老师演示 达到2M ,可能因为电脑环境的原因,也可能代码哪里有点不足,冗余太多。而单纯的用qt开发的界面 达到35M: 

从而得到 两者 是有哪里有优势的 ,感觉一个简单便捷开发,一个是运行比较高效

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值