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. LeetCode Fizz Buzz

    原题链接在这里:https://leetcode.com/problems/fizz-buzz/ 题目: Write a program that outputs the string represe ...

  2. LintCode Longest Common Subsequence

    原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...

  3. CS193P - 2016年秋 第一讲 课程简介

    Stanford 的 CS193P 课程可能是最好的 ios 入门开发视频了.iOS 更新很快,这个课程的最新内容也通常是一年以内发布的. 最新的课程发布于2016年春季.目前可以通过 iTunes ...

  4. 导入excel2007文件问题

    基于oledb方式导入excel2007文件时,使用如下链接字符串: string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Sour ...

  5. Spring操作指南-AOP基本示例(基于XML)

  6. Windows Server 2008 R2域控组策略设置禁用USB

    问题: Windows Server 2008 R2域控服务器如何禁用客户端使用USB移动存储(客户端操作系统需要 Windows Vista以上的操作系统,XP以下的操作系统不能禁用USB移动存储) ...

  7. Git过滤文件和文夹

    今天是2016年10月份的最后一天,感觉时间过得真的是飞快呢,此刻天下着小雨,天气灰蒙如天每个人的心情一样.在此把这个月的项目上传到git上,就需要过滤一些项目自动生成的不必要的文件,如:bin,ob ...

  8. MySQL数据库中tinyint类型字段读取数据为true和false

    今天遇到这么一个问题,公司最近在做一个活动,然后数据库需要建表,其中有个字段是关于奖励发放的状态的字段,结果读取出来的值为true 一.解决读取数据为true/false的问题 场景: 字段:stat ...

  9. C++笔记(一)

    一.动态数组 一般我们定义数组都是用下面的方式: int str[100]; 但对于一些需要手动输入数组大小的情况,这种定义方式就行不通了.因为上面这种方式要求事先知道数组大小,并且给数组中括号中必须 ...

  10. gulp 安装 使用 和删除

    1.安装 全局安装: npm intstall gulp -g      (首先你得有node.js ,这个可以去node 官网下载个iso的镜像安装包,傻瓜式安装.自带npm) 安装在项目中: 首先 ...