码迷,mamicode.com
首页 > Windows程序 > 详细

Windows普通窗口程序

时间:2015-10-09 13:47:15      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

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);
            return 0;
        }
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            BeginPaint(m_hWnd, &ps);
            OnDraw(ps.hdc);
            EndPaint(m_hWnd, &ps);
            return 0;
        }
    case WM_DESTROY:
        {
            PostQuitMessage(0);
            return 0;
        }
    }
    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

LRESULT CALLBACK KWindow::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    KWindow* pWindow;
    DWORD dwErr  = 0;
    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, 0, 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, 0, sizeof(wc));

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = 0;
    wc.lpfnWndProc = WindowProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    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;
    while(GetMessage(&msg, NULL, 0, 0))
    {
        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) / 72,
        0, 0, 0, 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(0,0,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, 0, 0);
    }

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

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


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

    return win.MessageLoop();
}
/*/

 

Windows普通窗口程序

标签:

原文地址:http://www.cnblogs.com/felove2013/p/4863656.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!