C++ Windows 窗体程序入门 - 3.初步封装

〇、前言

前一章阅读量好少啊~,难道是我鸽太久被限流啦?不至于bia

其中有六七次是我自己看的QAQ

OK,哭诉完毕,回归正题~


一、复习

上一章我们学习了:1、消息处理函数的基本实现

2、消息循环的处理

3、WinMain函数的返回值

4、改BUG(抱歉啦)

按照惯例,附上上一章的代码

#include <Windows.h>

LRESULT __stdcall MsgProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp);

int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{

    // ...

    // 消息循环:
    MSG msg;

    while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int)msg.wParam;
}



LRESULT __stdcall MsgProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
    switch (msg)
    {
    case WM_COMMAND:    // 命令消息
        // ...
        break;

    case WM_PAINT:        // 渲染消息
        // ...
        break;

    case WM_DESTROY:    // 销毁消息
        // ...
        break;

    default:
        return DefWindowProc(hWnd, msg, wp, lp);
    }
    return 0;
}

SEE,单独主窗口一个控件就要在源cpp里塞下这么多代码,以后加一个控件还要再来一遍,估计是个正常人都受不了,何况,作为一个精致的程序员,我的强迫症早就发作了

所以,今天我们来初步地封装一下这个程序


二、窗口类

既然是封装,我们先创建一个类,取个易懂的名字就叫Window吧,VS自带了生成类的功能,当然你也可以在源文件 创一个Window.cpp头文件 创一个Window.h:

当然你也可以这样

//.h
#pragma once
class Window
{
    // ...
};

//.cpp
#include "Window.h"

我们再看看这个窗口类:

Winc.cbSize               = sizeof(Winc);
Winc.cbClsExtra           = 0;
Winc.cbWndExtra           = 0;
Winc.lpszClassName        = "wind2067 is so handsome";

这些完全可以自动生成,而至于:

Winc.hIcon                = nullptr;
Winc.hCursor              = nullptr;
Winc.hbrBackground        = nullptr;
Winc.lpszMenuName         = nullptr;

这些我们暂时也用不到啊,所以……全部丢到函数里自动生成

<
  • 11
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
使用C++代码封装的win32操作类, 与MFC相似,对于学习SDK与C++是巨好的参考 Tutorials Menu of tutorials Tutorial 1: The Simplest Window Tutorial 2: Using Classes and Inheritance Tutorial 3: Using Messages to Create a Scribble Window Tutorial 4: Repainting the Window Tutorial 5: Wrapping a Frame around our Scribble Window Tutorial 6: Customising Window Creation Tutorial 7: Customising the Toolbar Tutorial 8: Loading and Saving Files Tutorial 9: Printing Tutorial 10: Finishing Touches Tutorial 1: The Simplest Window The following code uses Win32++ to create a window. This is all the code you need (in combination with Win32++) to create and display a simple window. Note that in order to add the Win32++ code to our program, we use an #include statement as shown below. #include "../Win32++/Wincore.h" INT WINAPI WinMain(HINSTANCE, HINSTANCE, LPTSTR, int) { //Start Win32++ CWinApp MyApp; //Create a CWnd object CWnd MyWindow; //Create (and display) the window MyWindow.Create(); //Run the application return MyApp.Run(); } This program has four key steps: Start Win32++. We do this here by creating a CWinApp object called MyApp. Create a CWnd object called MyWindow. Create a default window by calling the Create function. Start the message loop, by calling the Run function. If you compile and run this program, you'll find that the application doesn't end when the window is closed. This is behaviour is normal. An illustration of how to use messages to control the windows behaviour (including closing the application) will be left until tutorial 3.
1,cstatic_filespec.zip静态文本中用省略号显示长文件名(3KB)2,cstatic_filespec_demo.zip静态文本中用省略号显示长文件名的演示程序(14KB)3,newlabel_app.zipCNewLabel--高级的CStatic派生(39KB)4,jumpytext_demo.zip跳动的文本类演示程序(138KB)<END)5,jumpytext_src.zip跳动的文本类(5KB)<END)6,alexf_histogram_src.zip由CStatic派生的柱状图控制(3KB)7,alexf_histogram.zip由CStatic派生的柱状图控制演示程序(20KB)8,history_edit.zip滚动显示历史信息的编辑控制 (2KB)9,history_edit_demo.zip滚动显示历史信息的编辑控制演示程序 (14KB)10,bcmenu23.zip用工具条的位图资源制作自绘菜单(59KB)11,owner_drawn_menu4.zip自动使用工具条的位图资源制作自绘菜单(109KB)12,contentmenu.zip一个非常COOL的导航菜单(3KB)13,contentmenu_demo.zip一个非常COOL的导航菜单演示程序(37KB)14,freemenu.zip随意设定颜色和字的自绘菜单(35KB)15,menutip.zip实现菜单的工具提示演示程序(33KB)16,dockmenubar_src.zipDevStudio样式的泊位菜单条(不用MSIE)(27KB)17,dockmenubar_demo.zipDevStudio样式的泊位菜单条演示程序(不用MSIE)(58KB)18,label_edit.zip可编辑的标号控制(5KB)19,flat_scrollbar.zip扁平滚动条控制(11KB)20,avidemo.zip动画控制的例子(145KB)21,balloontooltip_src.zip像气球一样的工具提示(7KB)22,balloontooltip_demo.zip像气球一样的工具提示演示程序(49KB)23,check_frame.zip一个有用的CheckFrame控制(31KB)24,extended_tooltip.zip扩展的ToolTipCtrl类(21KB)25,progress_in_status2_src.zip在状态栏里显示进度条(4KB)26,progress_in_status2_demo.zip在状态栏里显示进度条演示程序(39KB)27,vcmenu.zipVisual Studio/Office 97 式样的扁平工具条和可泊位菜单条(350KB)28,flatbar_source.zip另一个扁平工具条(不需要MSIE)(11KB)29,flatbar_sample_project.zip另一个扁平工具条(不需要MSIE)工程程序(185KB)30,enh_flatbar_source.zip另一个扁平工具条(不需要MSIE)增强版(25KB)31,enh_flatbar_sample.zip另一个扁平工具条(不需要MSIE)演示增强版(56KB)32,customizable_tb.zip可定制的工具条(25KB)33,treemenudemo.zip一个类似IE4的收藏夹/历史/频道视图的控制(72KB)34,cdirtreectrl_demo.zipDirTreeCtrl--显示文件夹和文件演示程序(56KB)35,cdirtreectrl_src.zipDirTreeCtrl--显示文件夹和文件(6KB)36,a

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值