2015-10-09 12:55:38

KWindow.h

#pragma once
#include <windows.h>

class KWindow
{
    virtual void OnDraw(HDC hdc)
    {

    }

    virtual void OnKeyDown(WPARAM wParam, LPARAM lParam)
    {

    }

    virtual LRESULT WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

    static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

    virtual void GetWndClassEx(WNDCLASSEX& wc);

public:
    HWND    m_hWnd;

    KWindow()
    {
        m_hWnd = NULL;
    }

    virtual ~KWindow()
    {

    }

    virtual bool CreateEx(DWORD dwExStyle, LPCTSTR lpszClass, LPCTSTR lpszName,
                        DWORD dwStyle, int x, int y, int nWidth, int nHeight,
                        HWND hParent, HMENU hMenu, HINSTANCE hInst);

    bool RegisterClass(LPCTSTR lpszClass, HINSTANCE hInst);

    virtual WPARAM MessageLoop();

    BOOL ShowWindow(int nCmdShow) const
    {
        return ::ShowWindow(m_hWnd, nCmdShow);
    }

    BOOL UpdateWindow() const
    {
        return ::UpdateWindow(m_hWnd);
    }

    void CenterText(HDC hdc, int x, int y, LPCTSTR szFace, LPCTSTR szMessage, int point);

};

KWindow.cpp

#define STRICT
#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <tchar.h>
#include <assert.h>
#include "KWindow.h"

LRESULT KWindow::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
    case WM_KEYDOWN:
        {
            OnKeyDown(wParam, lParam);
            ;
        }
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            BeginPaint(m_hWnd, &ps);
            OnDraw(ps.hdc);
            EndPaint(m_hWnd, &ps);
            ;
        }
    case WM_DESTROY:
        {
            PostQuitMessage();
            ;
        }
    }
    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

LRESULT CALLBACK KWindow::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    KWindow* pWindow;
    DWORD dwErr  = ;
    if(uMsg == WM_NCCREATE)
    {
        assert( ! IsBadReadPtr((void*)lParam, sizeof(CREATESTRUCT)));
        MDICREATESTRUCT* pMDIC = (MDICREATESTRUCT*)((LPCREATESTRUCT)lParam)->lpCreateParams;
        pWindow = (KWindow*)(pMDIC->lParam);
        assert(!IsBadReadPtr(pWindow, sizeof(KWindow)));
        SetWindowLong(hWnd, GWL_USERDATA, (LONG)pWindow);

    }
    else
    {
        pWindow = (KWindow*)GetWindowLong(hWnd, GWL_USERDATA);
    }

    if(pWindow)
    {
        return pWindow->WndProc(hWnd, uMsg, wParam, lParam);
    }
    else
    {
        return DefWindowProc(hWnd, uMsg, wParam, lParam);
    }
}

bool KWindow::RegisterClass(LPCTSTR lpszClass, HINSTANCE hInst)
{
    WNDCLASSEX wc;

        GetWndClassEx(wc);

        wc.hInstance = hInst;
        wc.lpszClassName = lpszClass;
        if(!RegisterClassEx(&wc))
            return false;

    return true;
}

bool KWindow::CreateEx(DWORD dwExStyle, LPCTSTR lpszClass, LPCTSTR lpszName,
                       DWORD dwStyle, int x, int y, int nWidth, int nHeight,
                       HWND hParent, HMENU hMenu, HINSTANCE hInst)
{
    if(!RegisterClass(lpszClass,hInst))
        return false;

    MDICREATESTRUCT mdic;
    memset(&mdic, , sizeof(mdic));
    mdic.lParam = (LPARAM)this;
    m_hWnd = CreateWindowEx(dwExStyle, lpszClass, lpszName,
        dwStyle, x, y, nWidth, nHeight, hParent, hMenu, hInst, &mdic);
    return m_hWnd != NULL;
}

void KWindow::GetWndClassEx(WNDCLASSEX& wc)
{
    memset(&wc, , sizeof(wc));

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = ;
    wc.lpfnWndProc = WindowProc;
    wc.cbClsExtra = ;
    wc.cbWndExtra = ;
    wc.hInstance = NULL;
    wc.hIcon = NULL;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = NULL;
    wc.hIconSm = NULL;
}

WPARAM KWindow::MessageLoop()
{
    MSG msg;
    , ))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}

void KWindow::CenterText(HDC hdc, int x, int y, LPCTSTR szFace, LPCTSTR szMessage, int point)
{
    HFONT hFont = CreateFont(-point * GetDeviceCaps(hdc, LOGPIXELSY) / ,
        , , , FW_BOLD, TRUE, FALSE, FALSE, ANSI_CHARSET,
        OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, VARIABLE_PITCH, szFace);
    assert(hFont);
    HGDIOBJ  hold = SelectObject(hdc, hFont);
    SetTextAlign(hdc, TA_CENTER | TA_BASELINE);
    SetBkMode(hdc, TRANSPARENT);
    SetTextColor(hdc, RGB(,,0xFF));
    TextOut(hdc, x, y, szMessage, _tcslen(szMessage));
    SelectObject(hdc, hold);
    DeleteObject(hFont);
}

KHelloWindow.cpp

#define STRICT
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <tchar.h>
#include <assert.h>
#include <conio.h>
#include <iostream>

#include "KWindow.h"
#pragma warning( disable:4996 )

const TCHAR szMessage[] = _T("Hello,World!");
const TCHAR szFace[] = _T("Times New Roman");
const TCHAR szHint[] = _T("Press ESC to quit.");
const TCHAR szProgram[] = _T("HelloWorld3");

class KHelloWindow : public KWindow
{
    void OnKeyDown(WPARAM wParam, LPARAM lParam)
    {
        if(wParam == VK_ESCAPE)
            PostMessage(m_hWnd, WM_CLOSE, , );
    }

    void OnDraw(HDC hdc)
    {
        TextOut(hdc, , , szHint, lstrlen(szHint));
        CenterText(hdc, GetDeviceCaps(hdc, HORZRES) / ,
            GetDeviceCaps(hdc, VERTRES) / ,
            szFace, szMessage, );
    }
};

//*
int WINAPI WinMain( _In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd )
{
    KHelloWindow win;
    , szProgram, szProgram, WS_POPUP, , ,
        GetSystemMetrics(SM_CXSCREEN),
        GetSystemMetrics(SM_CYSCREEN),
        NULL, NULL, hInstance);

    win.ShowWindow(nShowCmd);
    win.UpdateWindow();

    return win.MessageLoop();
}
/*/

Windows普通窗口程序的更多相关文章

  1. Cocos2dx集成于windows桌面窗口程序的步骤

    2D游戏需要做编辑器,而编辑器总是希望可以复用游戏中的逻辑来运行场景试看效果. 对于cocos2dx开发的程序,这个需求可以描述为: 实现一种方法,在桌面窗口程序中的某个控件上显示cocos2dx的场 ...

  2. VC菜菜鸟:建立第一个基于Visual C++的Windows窗口程序

    建立第一个基于VisualC++的Windows窗口程序: 发表于:http://blog.csdn.net/it1988888/article/details/10306585 a)执行命令:新建 ...

  3. Windows程序设计笔记(二) 关于编写简单窗口程序中的几点疑惑

    在编写窗口程序时主要是5个步骤,创建窗口类.注册窗口类.创建窗口.显示窗口.消息环的编写.对于这5个步骤为何要这样写,当初我不是太理解,学习到现在有些问题我基本上已经找到了答案,同时对于Windows ...

  4. windows cmd窗口提示“telnet”命令不能内部或外部命令,也不是可运行的程序

    windows cmd窗口提示“telnet”命令不能内部或外部命令,也不是可运行的程序 原因:C:\Windows\System32目录下没有telnet.exe,path系统变量的值包含了C:\W ...

  5. (Delphi)第一个Windows 32 API的窗口程序

    program Project1; uses Winapi.Windows, Winapi.messages; {$R *.res} const className = 'MyDelphiWindow ...

  6. Windows窗口程序从创建到关闭产生的消息

    Windows是消息驱动的,理解消息机制及消息循环是特别重要.知道在什么情况下产生什么消息会让我们对程序有更好的控制.Windows给应用程序发消息,有些会加入应用程序的消息队列,也是就是队列消息.有 ...

  7. 我的第一个 Windows 窗口程序(1)

    一般来说,构建一个 Windows 程序可以分为如下几个步骤: 定义窗口类(WNDCLASS) 注册窗口类(RegisterClass) 创建窗口(CreateWindow) 更新显示窗口(Updat ...

  8. [MFC]_在vs2019中使用MFC快速构建简单windows窗口程序

    微软基础类库(英语: Classes,简称MFC)是微软公司提供的一个类库(class libraries),以C++类的形式封装了Windows API,并且包含一个应用程序框架,以减少应用程序开发 ...

  9. Windows编程入门程序详解

    引用:http://blog.csdn.net/jarvischu/article/details/8115390 1.     程序 /******************************* ...

随机推荐

  1. input range样式更改,模拟滑块

    input range 样式更改,js模拟滑块实时更新数据. 效果图: html 代码: <div> <span class="slider"></s ...

  2. jquery easyui tree的全选与反选

    //全选反选 //参数:selected:传入this,表示当前点击的组件 //treeMenu:要操作的tree的id:如:id="userTree" function tree ...

  3. php try catch throw 用法

    1.try catch 捕捉不到fatal error致命错误 2.只有抛出异常才能被截获,如果异常抛出了却没有被捕捉到,就会产生一个fatal error. 3.父类可以捕获抛出的子类异常,Exce ...

  4. Java 泛型约束

    类型约束: import java.util.List; import java.util.ArrayList; import java.util.LinkedList; /** * Created ...

  5. ndt histogram_direction

    histogram_direction N_FLAT_BINS=40; dlong = pi*(3-sqrt(5.0)); % ~2.39996323 dz = 2.0/N_FLAT_BINS; lo ...

  6. ASP.NET corrupt assembly “Could not load file or assembly App_Web_*

    以下是从overFlow 复制过来的问题 I've read through many of the other questions posted on the same issue, but I s ...

  7. System.Web.AspNetHostingPermission 类型的权限已失败

    System.Security.SecurityException: 请求“System.Web.AspNetHostingPermission, System, Version=2.0.0.0, C ...

  8. 分析App应用市场, App应用有哪些类型

    随着移动互联网的流行,APP应用也是异常火爆,App应用市场就如破冰的泉水在我们的生活中到处渗透,对于App开发的的状况来分析,企业在寻找技术人员开发一款专业的App软件的时候,必须先了解用户的需求与 ...

  9. python中的GIL(全局解释锁)多线程能够提升效率

    预启动的时候,应用程序仍然会调用 OnLaunched 方法的,在 OnLaunched 方法调用之后,会马上发生 Suspending 事件,随后应用就会暂停. 我先基于develop主分支拉出一个 ...

  10. JavaSE基础第四篇

    1.参数传递   2,方法的重载 方法的参数的个数.类型.顺序 跟修饰符.返回值无关   3.构造方法: return 表示当前方法执行结束,后面不能写任何语句   4工程导入 单个.java文件粘贴 ...