[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);  
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值