[MFC成长季]windows API程序逻辑

再过2个小时就要到上海比赛去了。闲暇时光还是和往常一样,一个人坐在办公室,不喜欢在寝室。恋爱季,难免会多花点钱,昨晚老妈严厉批评了我,今早老爸电话,就一句话,卡号给我。

自己看windows api ,mfc很久了,尚未入门。反思自己,学习观上,心态不正;方法论上,借用《一代宗师》一句话,老猿挂印回首望,关隘不在挂印而在回首望。所以我决定逼自己写博客。

文章多数汲取于csdn达达们的博客,如有疏漏,敬请谅解。


程序入口:int CALLBACK WinMain()


注册窗口:RegisterWndClass()


创建窗口图形:CreateWindow()


显示窗口:ShowWindow()
                    UpdateWindow(hWnd);



进入消息循环
  
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);

}


窗口处理:

LRESULT CALLBACK WindowProc()


一些逻辑细节:

程序如何知道WindowProc()是处理消息函数?

RegisterWndClass()函数中:wc.lpfnWndProc = (WNDPROC)WindowProc;


如何第一次进入WindowProc():

UpdateWindow(hWnd)向WindowProc()发送一个WM_PAINT消息,注意,这个消息系统只会发送一次,然后进入消息循环。


切记,window是事件驱动的。


// test_1.cpp : 定义应用程序的入口点。

//

#include "stdafx.h"
#include <windows.h>

ATOM  RegisterWndClass( HINSTANCE hInstance , LPCWSTR className);
LRESULT CALLBACK WindowProc(HWND ,UINT, WPARAM, LPARAM);

/* WinMain parameters:
 *  hInstance : A handle to the current instance of the application.
 *  hPrevInstance : A handle to the previous instance of the application. 
 *                  This parameter is always NULL. ...
 *  lpCmdLine : The command line for the application, excluding(不包括) the program name. 
 *             To retrieve(获取) the entire(全部) command line, use the GetCommandLine function.
 *  nCmdShow : Controls how the window is to be shown. eg. SW_HIDE, SW_MAXIMIZE
 *
 *  return value: If the function succeeds, terminating when it receives a WM_QUIT message, 
 *                it should return the exit value contained in that message's wParam parameter. 
 *                If the function terminates before entering the message loop, 
 *                it should return zero.
 */
int CALLBACK WinMain(
            __in  HINSTANCE hInstance,        
            __in  HINSTANCE hPrevInstance,     
            __in  LPSTR lpCmdLine,
            __in  int nCmdShow )
{
    // (1)注册窗口
    LPCWSTR className = TEXT("FirstWnd");
    if (!RegisterWndClass(hInstance, className)) {
        ::MessageBox(NULL, TEXT("Register class failed"), 
            TEXT("Error"), MB_OK | MB_ICONEXCLAMATION);
        return 0;
    }
    
    // (2)创建窗口
    HWND hWnd;  // 窗口句柄
    hWnd = CreateWindow(
        className,                    // 窗口类名称
        TEXT("A simple Win32 Application"), // 窗口标题
        WS_OVERLAPPEDWINDOW,          // 窗口风格
        100,                          // 窗口位置的 x 坐标 , CW_USEDEFAULT
        100,                          // 窗口位置的 y 坐标 , CW_USEDEFAULT
        400,                          // 窗口的宽度 , CW_USEDEFAULT
        300,                          // 窗口的高度 , CW_USEDEFAULT
        NULL,                         // 父窗口句柄
        NULL,                         // 菜单句柄
        hInstance,                    // 应用程序句柄
        NULL);                        // 窗口创建数据指针, WM_CREATE lParam
    if (!hWnd) {
        ::MessageBox(NULL, TEXT("Register class failed"), 
            TEXT("Error"), MB_OK | MB_ICONEXCLAMATION);
        return 0;
    }
    
    // (3)显示并更新窗口
    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    // (4)进入消息循环
    MSG  msg;
    // 如果 msg 消息为 WM_QUIT , GetMessage 返回为 0 ,退出循环
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    // 程序结束
    return msg.wParam;
}

// 注册窗口
ATOM  RegisterWndClass( HINSTANCE hInstance , LPCWSTR className)
{
    WNDCLASS wc;
    wc.style       = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS; // 
    wc.lpfnWndProc = (WNDPROC)WindowProc;     // A pointer to the window procedure.
    wc.cbClsExtra  = 0;                       // 
    wc.cbWndExtra  = 0;                       // 
    wc.hInstance   = hInstance;               // 
    wc.hIcon       = LoadIcon( hInstance, IDI_APPLICATION ); // A handle to the class icon.
    wc.hCursor     = LoadCursor( NULL, IDC_ARROW );          // A handle to the class cursor.
    wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);          // A handle to the class background brush.
    wc.lpszMenuName  = NULL;                  // The resource name of the class menu,
    wc.lpszClassName = className;            // A pointer to a null-terminated string or is an atom.
    return RegisterClass( &wc );
}

// 窗口过程
LRESULT CALLBACK WindowProc(
              __in  HWND hWnd,     // A handle to the window. 
              __in  UINT uMsg,     // The message.
              __in  WPARAM wParam, // Additional message information
              __in  LPARAM lParam  // Additional message information
            )
{
    HDC hDC;
    PAINTSTRUCT ps;

    switch (uMsg) 
    {  
    case WM_PAINT:  
        hDC = BeginPaint(hWnd, &ps);
        RECT rc;
        rc.bottom = 100;
        rc.left = 100;
        rc.right = 300;
        rc.top = 400;
        ::SetTextColor(hDC, RGB(255, 0, 0));
        SetBkMode(hDC, TRANSPARENT);
        ::TextOut(hDC, 30, 30, TEXT("This is my first Win32 App !"), 
            strlen("This is my first Win32 App !"));
        EndPaint(hWnd, &ps);
        break; 
   
    case WM_LBUTTONDOWN:
        MessageBox(NULL, TEXT("left button down"), TEXT("Message"), MB_OK);
        break;
		case WM_RBUTTONUP:
		MessageBox(NULL,TEXT("right button up"),TEXT("message"),MB_OK);

    case WM_DESTROY:  
        MessageBox(NULL, TEXT("Window will be closed"), TEXT("Message"), MB_OK);
        PostQuitMessage(0);  
        break;

    default:  
        break;
    }  
    return DefWindowProc(hWnd, uMsg, wParam, lParam);  
}

内容概要:本文围绕基于三电平ANPC构网型逆变器的虚拟同步控制策略展开研究,重点探讨了其在Simulink环境下的仿真实现方法。研究聚焦于虚拟同步发电机(VSG)控制、双闭环控制及中点电位平衡控制等核心技术,旨在提升高渗透率新能源背景下逆变器的惯量支撑能力和电能质量。通过构建详细的系统模型,提出并优化控制策略,有效解决了三电平逆变器在动态响应、稳定性及中点电压波动等方面的挑战,增强了系统对复杂电网工况的适应能力。研究进一步结合VSG的虚拟惯量与阻尼特性,实现对电网频率波动的有效抑制,并通过双闭环结构提升电流跟踪精度与功率调节性能,同时引入中点电位平衡控制策略,确保多电平拓扑输出电压对称性与可靠性。; 适合人群:具备电力电子、自动控制或新能源发电相关背景,从事科研或工程开发的研发人员,尤其是关注构网型逆变器、虚拟同步技术及多电平拓扑控制的研究生与工程师。; 使用场景及目标:①应用于新能源并网系统中构网型逆变器的设计与仿真;②为提升电力系统稳定性提供虚拟同步控制方案;③实现三电平ANPC逆变器中点电位的有效平衡与动态性能优化; 阅读建议:建议结合Simulink仿真模型进行实践操作,重点关注控制策略的实现细节与参数整定过程,同时可参考文中提到的双闭环结构与VSG控制逻辑进行扩展研究。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值