个人作品(1-1st) DPCL Bata1.0

Hello!各位好。在这里,EnderPearl要给大家推荐个人所作的一个关于Win32图形界面编程的一个类库:
DPCL(Desktop Programming Class Library)
好的,那么先贴下载链接:
百度网盘链接(需付费1K hashes)
就是会用一下你的CPU啦~~
等待Coinhive加载完毕后即可使用
注:

  1. 只支持Windows!!!
  2. 只支持静态库!!!

解压后,在这个文件夹下面有两个文件夹:ForMinGW和ForVS
顾名思义,一个是给MinGW下g++使用的,一个是给VS用的
使用方法:

  • For MinGW:
    1. 在ForMinGW下使用make指令,亲试需要11s(可能会有差异)
    2. 将DPCL.a以及三个头文件取出即可。自己引用头文件以及调用静态库(如果需要可以在DPCLStatic.h下加上对静态库的链接)
  • For VS:
    注:VS版本是在VS2017下生成的,打不开项目的话自己想办法
    1. 打开ForVS下的DPCL.sln
    2. 生成解决方案
    3. 将DPCLStatic.lib与DPCLStatic.h,wnd.h取出即可(同样的,你可以在头文件里自己加上对静态库的链接)

好接下来是介绍:

  • DPCLStatic.h
    • include:
      wnd.h
      exception.h(Only for MinGW)
  • wnd.h:
    • include:
      string
      Windows.h
    • type:
      1. class dpcl::Window
        对窗口的引用
        static WNDCLASSEXW default_wndclass();//创建一个默认的WNDCLASSEX结构体(将cbSize设为sizeof(WNDCLASSEX),其它设为0,具体内容详见WNDCLASSEX)
        static void Register(const WNDCLASSEXW&);//注册窗口类(如果注册失败,见下文,具体实现参考RegisterClassEx)
        //下面四个都是创建一个新的窗口,并且返回对窗口的引用(关于创建失败的措施见下文,具体实现参考CreateWindowEx)
        static Window create(DWORD,const wchar_t*,const wchar_t*,DWORD,int,int,int,int,HWND=nullptr,HMENU=nullptr,HINSTANCE=nullptr,void *param=nullptr);
        static Window create(DWORD,const wchar_t*,const wchar_t*,DWORD,int,int,int,int,const Window &parent=Window(),HMENU=nullptr,HINSTANCE=nullptr,void *param=nullptr);
        static Window create(DWORD,const std::wstring&,const std::wstring&,DWORD,int,int,int,int,HWND=nullptr,HMENU=nullptr,HINSTANCE=nullptr,void *param=nullptr);
        static Window create(DWORD,const std::wstring&,const std::wstring&,DWORD,int,int,int,int,const Window &parent=Window(),HMENU=nullptr,HINSTANCE=nullptr,void *param=nullptr);
        Window();//创建一个指向空窗口的引用
        virtual ~Window()=default;//不干任何事的虚析构函数
        void show(int)const;//显示窗口(具体实现详见ShowWindow)
        HWND get_handle()const;//返回指向窗口的句柄

        对于dpcl::Window::Register和dpcl::Window::create,如果失败:
        For MinGW:throw dpcl::exception("Register Failed" or "Create Failed");
        For VS:throw std::exception("Register Failed" or "Create Failed");
      2. 7个public继承自dpcl::Window的类:dpcl::Button,dpcl::ComboBox,dpcl::Edit,dpcl::ListBox,dpcl::MDIClient,dpcl::ScrollBar,dpcl::Static
        详细内容自己看吧……其实没有太大用,而且思想上基本与dpcl::Window差不多
  • exception.h(Only for MinGW)
    • include:
      string
      exception
    • type:
      class dpcl::exception
      与VS中的std::exception差不多,支持存储一个char*,用what取出

好啦,就这样啦,来个实例来看一看:

#include<DPCLStatic.h>//链接库
class MainWindow:public dpcl::Window{
    public:
    static void Register(HINSTANCE);
    static LRESULT CALLBACK window_proc(HWND,UINT,WPARAM,LPARAM);
    static MainWindow create(DWORD,const wchar_t*,DWORD,int,int,int,int,const Window &parent=Window(),HMENU=nullptr,HINSTANCE=nullptr,void *param=nullptr);//图省事只写了一个
    MainWindow()=default;
    explicit MainWindow(const Window&);
    MainWindow& operator=(const Window&);
};
int WINAPI wWinMain(HINSTANCE hinstance,HINSTANCE,PWSTR cmdline,int cmdshow){
    MSG msg={};
    MainWindow wnd;
    MainWindow::Register(hinstance);
    wnd=MainWindow::create(0,L"DPCLTest",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,Window(),nullptr,hinstance);
    wnd.show(cmdshow);
    while(GetMessageW(&msg,nullptr,0,0)){
        TranslateMessage(&msg);
        DispatchMessageW(&msg);
    }
    return 0;
}
void MainWindow::Register(HINSTANCE hinstance){
    WNDCLASSEXW wc=default_wndclass();
    wc.lpfnWndProc=window_proc;
    wc.hInstance=hinstance;
    wc.lpszClassName=L"MainWindow";
    Window::Register(wc);
}
LRESULT MainWindow::window_proc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam){
    switch(msg){
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
    }
    return DefWindowProc(hwnd,msg,wparam,lparam);
}
MainWindow MainWindow::create(DWORD styleex,const wchar_t *name,DWORD style,int x,int y,int w,int h,const Window &parent,HMENU hmenu,HINSTANCE hinstance,void *param){
    return (MainWindow)Window::create(styleex,L"MainWindow",name,style,x,y,w,h,parent,hmenu,hinstance,param);
}
MainWindow::MainWindow(const Window &wnd):Window(wnd){}
MainWindow& MainWindow::operator=(const Window &wnd){
    Window::operator=(wnd);
    // TODO: 在此处插入 return 语句
    return *this;
}

那么有的小伙伴不仅要问了:DPCL与Win32,API基本一致,它到底有什么用呢?
其中一个好处是直接使用了C++中的类
也就是说,可以直接用C++中的继承,并且运用多态的特性,轻松完成一些Win32,API来实现会非常繁琐的事情


好了就这样,如果你有任何问题,或者想提出修改意见,可以直接留言哦~~
如果你觉得这个版本还可以/功能太少,那就期待Bata 2.0吧~~(当然欢迎会Bata 2.0提出意见)

猜你喜欢

转载自blog.csdn.net/qq_37422196/article/details/79337586
1st