码迷,mamicode.com
首页 > 编程语言 > 详细

仿写MFC,自己写的封装sdk API 应用

时间:2014-11-01 21:46:51      阅读:397      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   color   os   ar   sp   

仿写MFC,自己写的封装sdk API 应用

 1 #include "QWinApp.h"
 2 #include "QGlobal.h"
 3 int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
 4 {
 5     QWinApp* pWinApp = GlbGetApp();
 6     //new QWinApp;
 7     //pWinApp = GlbGetApp();
 8     assert(pWinApp);
 9     pWinApp->hInst = hInstance;
10     pWinApp->InitInstance();
11     pWinApp->Run();
12     pWinApp->ExitInstance();
13 
14     return TRUE;
15 }
 1 #pragma once
 2 #include <windows.h>
 3 class QWnd
 4 {
 5 public:
 6     QWnd(void);
 7     ~QWnd(void);
 8 public:
 9     HWND m_hWnd;
10     virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
11 
12     virtual BOOL CreateEx(DWORD dwExStyle,LPCTSTR lpszClassName,LPCTSTR lpszWindowName,    DWORD dwStyle,
13         int x,int y,int nWidth,    int nHeight,HWND hWndParent,HMENU nIDorHMenu,LPVOID lpParam = NULL);
14 public:
15     WNDPROC m_lpfnOldWndProc;
16     BOOL ShowWindow(int nCmdShow);
17     BOOL UpdateWindow();
18     BOOL DestroyWindow();
19 public:
20     virtual LRESULT OnClose(WPARAM wParam,LPARAM lParam);
21     virtual LRESULT OnDestroy(WPARAM wParam,LPARAM lParam);
22     virtual LRESULT OnCreate(WPARAM wParam,LPARAM lParam);
23 public:
24     static LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
25     virtual LRESULT  WindowProc(UINT uMsg, WPARAM wParam, LPARAM lParam);
26     virtual LRESULT  Default(UINT uMsg, WPARAM wParam, LPARAM lParam);
27     BOOL SubClassWindows(HWND hWnd);
28     static QWnd* FromHandle(HWND hWnd);
29 };
  1 #include "QWnd.h"
  2 LRESULT CALLBACK QWnd::WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  3 {
  4     if (uMsg == WM_CREATE || uMsg == WM_NCCREATE)
  5     {
  6         CREATESTRUCT* pCs = (CREATESTRUCT*)lParam;
  7         if (pCs)
  8         {
  9             QWnd* pWnd = (QWnd*)pCs->lpCreateParams;
 10             if (pWnd)
 11             {
 12                 pWnd->m_hWnd = hwnd;
 13                 pWnd->m_lpfnOldWndProc = (WNDPROC)GetWindowLong(hwnd,GWL_WNDPROC);
 14                 return pWnd->WindowProc(uMsg,wParam,lParam);
 15             }
 16         }
 17     }
 18     QWnd* pWnd = (QWnd*)GetWindowLong(hwnd,GWL_USERDATA);
 19     if (pWnd)
 20     {
 21         return pWnd->WindowProc(uMsg,wParam,lParam);
 22     }
 23 
 24     return DefWindowProc(hwnd, uMsg, wParam, lParam);
 25 }
 26 LRESULT  QWnd::WindowProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
 27 {
 28     switch (uMsg)
 29     {
 30     case WM_CLOSE:
 31         {
 32             return OnClose(wParam,lParam);
 33         }
 34         break;
 35     case WM_DESTROY:
 36         {
 37             return OnDestroy(wParam,lParam);
 38         }
 39     case WM_CREATE:
 40         {
 41             return OnCreate(wParam,lParam);
 42         }
 43         break;
 44     }
 45     return Default(uMsg, wParam, lParam);
 46 }
 47 LRESULT  QWnd::Default(UINT uMsg, WPARAM wParam, LPARAM lParam)
 48 {
 49     if (m_lpfnOldWndProc == QWnd::WndProc)
 50     {
 51         return ::DefWindowProc(m_hWnd,uMsg, wParam, lParam);
 52     }
 53     return m_lpfnOldWndProc(m_hWnd,uMsg, wParam, lParam);
 54 }
 55 LRESULT QWnd::OnClose(WPARAM wParam,LPARAM lParam)
 56 {
 57     return Default(WM_CLOSE,wParam,lParam);
 58 }
 59 LRESULT QWnd::OnCreate(WPARAM wParam,LPARAM lParam)
 60 {
 61     return Default(WM_CREATE,wParam,lParam);
 62 }
 63 
 64 LRESULT QWnd::OnDestroy(WPARAM wParam,LPARAM lParam)
 65 {
 66     return Default(WM_DESTROY,wParam,lParam);
 67 }
 68 QWnd::QWnd(void)
 69 {
 70     m_hWnd =NULL;
 71     m_lpfnOldWndProc = NULL;
 72 }
 73 
 74 QWnd::~QWnd(void)
 75 {
 76 }
 77 BOOL QWnd::CreateEx(    DWORD dwExStyle,    LPCTSTR lpszClassName,    LPCTSTR lpszWindowName,    DWORD dwStyle,
 78                     int x,    int y,    int nWidth,    int nHeight,    HWND hWndParent,    HMENU nIDorHMenu,    LPVOID lpParam    )
 79 {
 80     CREATESTRUCT cs;
 81     cs.lpCreateParams = lpParam;
 82     cs.hInstance = (HINSTANCE)GetModuleHandle(NULL);
 83     cs.hMenu = nIDorHMenu;
 84     cs.hwndParent = hWndParent;
 85     cs.cy = nWidth;
 86     cs.cx = nHeight;
 87     cs.y = y;
 88     cs.x = x;
 89     cs.style = dwStyle;
 90     cs.lpszName = lpszWindowName;
 91     cs.lpszClass = lpszClassName;
 92     cs.dwExStyle = dwExStyle;
 93     BOOL bRet = PreCreateWindow(cs);
 94     if (!bRet)
 95     {
 96         return FALSE;
 97     }
 98     HWND hWnd = CreateWindowEx(cs.dwExStyle,cs.lpszClass,cs.lpszName,
 99         cs.style,cs.x,cs.y,cs.cx,cs.cy,cs.hwndParent,cs.hMenu,
100         cs.hInstance,this);
101     if (NULL == hWnd)
102     {
103         return FALSE;
104     }
105     m_hWnd =hWnd;
106     SetWindowLong(m_hWnd,GWL_USERDATA,(LONG) this);
107     m_lpfnOldWndProc = (WNDPROC) GetWindowLong(m_hWnd,GWL_WNDPROC);
108     if (m_lpfnOldWndProc == QWnd::WndProc)
109     {
110         //子类化
111         SetWindowLong(m_hWnd,GWL_WNDPROC,(LONG)QWnd::WndProc);
112         WindowProc(WM_CREATE,0,0);
113         WindowProc(WM_NCCREATE,0,0);
114     }
115     return TRUE;
116 }
117 QWnd* FromHandle(HWND hWnd)
118 {
119     QWnd* pWnd =(QWnd*)GetWindowLong(hWnd,GWL_USERDATA);
120     return pWnd;
121 }
122 BOOL QWnd::PreCreateWindow(
123     CREATESTRUCT& cs
124     )
125 {
126     //HINSTANCE hInst = (HINSTANCE)GetModuleHandle(NULL);
127     //assert(hInst);
128     WNDCLASSEX wc = {0};
129     BOOL bRet = GetClassInfoEx(cs.hInstance,cs.lpszClass,&wc);
130     if (bRet)
131     {
132         return TRUE;
133     }
134     //WNDCLASSEX wc = {0};
135     wc.cbClsExtra = 0;
136     wc.cbSize = sizeof(wc);
137     wc.cbWndExtra = 0;
138     wc.hbrBackground = (HBRUSH)GetStockObject(COLOR_HIGHLIGHTTEXT);
139     wc.hCursor = LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW));
140     wc.hIcon = LoadIcon(NULL, MAKEINTRESOURCE(IDI_APPLICATION));
141     wc.hIconSm = NULL;
142     wc.hInstance = cs.hInstance;
143     wc.lpfnWndProc = WndProc;
144     wc.lpszClassName = cs.lpszClass;
145     wc.lpszMenuName = NULL;
146     wc.style = CS_HREDRAW | CS_VREDRAW;
147     BOOL bRetClass = RegisterClassEx(&wc);
148     if(!bRetClass)
149     {
150         //MessageBox(NULL, L"Register Class Failed!", NULL, MB_OK);
151         return FALSE;
152     }
153     return TRUE;
154 }
155 
156 BOOL  QWnd::ShowWindow(int nCmdShow)
157 {
158     return ::ShowWindow(m_hWnd,nCmdShow);
159 }
160 BOOL  QWnd::UpdateWindow()
161 
162 {
163     return ::UpdateWindow(m_hWnd);
164 }
165 
166 BOOL  QWnd::DestroyWindow()
167 
168 {
169     return ::DestroyWindow(m_hWnd);
170 }
171 
172 BOOL QWnd::SubClassWindows(HWND hWnd)
173 {
174     if (m_hWnd == hWnd) return TRUE;
175     m_lpfnOldWndProc = (WNDPROC)GetWindowLong(hWnd,GWL_WNDPROC);
176     if(m_lpfnOldWndProc!=QWnd::WndProc)
177     {
178         m_hWnd =hWnd;
179         SetWindowLong(m_hWnd,GWL_USERDATA,(LONG)this);
180         SetWindowLong(m_hWnd,GWL_WNDPROC,(LONG)QWnd::WndProc);
181         return TRUE;
182     }
183     return FALSE;
184 }

 

http://pan.baidu.com/s/1eQjCWH8

 

仿写MFC,自己写的封装sdk API 应用

标签:des   style   blog   http   io   color   os   ar   sp   

原文地址:http://www.cnblogs.com/whypro/p/4067725.html

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