Windows普通視窗程序

來源:互聯網
上載者:User

標籤:

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普通視窗程序

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.