為了寫一個gui,要協(xié)調(diào)好控件和事件監(jiān)聽的關(guān)系以及應(yīng)用程序不同窗口的關(guān)系。

為了要寫一個良好支持可擴展的控件庫,需要寫一個泛化組件庫,要寫一個泛化組件庫,需要先實現(xiàn)它的邏輯基礎(chǔ),也就是可能利用到的幾何對象(arith::geo),

為了能夠讓對組件和窗體的事件監(jiān)聽變得透明,需要封裝需要被利用的api和注冊窗口類。

現(xiàn)在正在兩頭開始,這樣能夠不斷測試自己的代碼,當(dāng)上下兩部分越來越充實,就自然的結(jié)合了.

以下是 os::frame 子庫半成品,目的是封裝windows  application.

代碼結(jié)構(gòu)如下:

namespace appinfo;  封裝應(yīng)用程序信息.

namespace frame; 應(yīng)用框架.

frame::api::def;  常量信息集
frame::api::type;  api的基本類型集
frame::api::handle;  封裝的句柄集
frame::api::paint;   封裝的繪制類,實例化后方便的處理所有跟繪制有關(guān)的api.
frame::api::mouse;  封裝的鼠標(biāo)信息集
frame::api::message; message處理集
frame::api::proc;   form回調(diào)函數(shù)類,實例化后專門為form池集中處理不同的事件
frame::api::wndclass; 注冊窗口類以及窗口相關(guān)創(chuàng)建
frame::form;  所有對話框和文檔都統(tǒng)一用form類實例化.

由于是兩頭寫,組件庫和關(guān)鍵的事件監(jiān)聽框架還沒有完全寫完,等寫到一定階段再把不同form的事件分開獨立處理,


/*  frame.h :os::frame */

#ifndef APPFRAME__

#define APPFRAME__

#include 
<windows.h>
#include 
<tchar.h>
#include 
<stdio.h>

#include 
"axiom.h"
#include 
"string.h"
#include 
"geo.h"


namespace appinfo
{
    
static const int max_form=500;
    
static int iCmdShow,regNum,listIndex;
    
static HINSTANCE hInstance;
    
static HINSTANCE hPrevInstance;
    
static PSTR szCmdLine;
    
static MSG  msg;
    
/* register windowclass-name */
    
static TCHAR regList[max_form][4];
    
static void decform()
    
{
        
--regNum;
    }
    
    
static void incform()
    
{
        
++regNum;
    }

    
static void setListIndex(int & reg_index,LPCWSTR & wc_name)
    
{
        reg_index
=listIndex;
        regList[listIndex][
0]=1;
        regList[listIndex][
1]=reg_index;
        regList[listIndex][
2]=0;
        wc_name
=regList[listIndex];
        
++listIndex;
        
++regNum;
    }


}

namespace frame
{


    
using adt::ustring;




    
class api
    
{
    
public:
        
class def
        
{
        
public:
            
static const int nil =0;
            
static const int sm_x = 0;
            
static const int sm_y = 1;
            
static const int wm_create=WM_CREATE;
            
static const int wm_paint=WM_PAINT;
            
static const int wm_destroy=WM_DESTROY;
            
static const int wm_mousemove=WM_MOUSEMOVE;
            
static const unsigned int cs_default= CS_HREDRAW | CS_VREDRAW ;
        }
;
        
class type
        
{
        
public:
            typedef HDC__
* hdc;
            typedef HWND hwnd;
            typedef ustring 
string;
            typedef UINT 
uint;
            typedef RECT rect;
            typedef LPRECT lprc;
            typedef WPARAM wp;
            typedef LPCWSTR lpcwstr;
            typedef LPARAM lp;
            typedef PAINTSTRUCT ps;
            typedef LPPAINTSTRUCT lpps;
            typedef BYTE 
byte;
            typedef LONG_PTR result;
            typedef WNDCLASS wndclass;
            typedef HICON hicon;
            typedef HCURSOR hcursor;
            typedef HBRUSH__
* hbrush;
            typedef HINSTANCE hinstance;
            typedef PSTR  pstr;
            typedef MSG msg;
            typedef TCHAR tchar;
            typedef BOOL state;
            typedef POINT point;
        }
;
        
class handle
        
{public:
        
static type::hicon icon(int i=0)
        
{
            
return  LoadIcon (NULL, IDI_APPLICATION);
        }

        
static type::hcursor cursor(int i=0)
        
{
            
return  LoadCursor (NULL, IDC_ARROW);
        }

        
static type::hbrush brush(int i=WHITE_BRUSH)
        
{
            
return (type::hbrush) GetStockObject (i);
        }

        }
;
        
class paint
        
{
        
public:
            type::hdc hdc;
            type::rect rect;
            type::hwnd hwnd;
            type::ps ps;
            
bool ready;
            paint():ready(
false){}
            
~paint()
            
{
                
if(ready!=false)
                    end();
                ready
=false;
            }

            type::hdc begin(type::hwnd 
& hwnd)
            
{                
                ready
=true;
                hdc 
= BeginPaint(hwnd,&ps);
                GetClientRect (hwnd, 
&rect);
                
return (hdc );
            }

            
int end()
            
{
                ready
=false;
                
return EndPaint(hwnd,&ps);
            }

            
int text(type::string & tx)
            
{
                
if(ready)
                    
return DrawTextW (hdc, tx(), -1&rect,
                    DT_SINGLELINE 
| DT_CENTER | DT_VCENTER) ;
                
else return 0;
            }

            type::state rectangle(
int x,int y,int w,int h)
            
{
                
int sx=sm::screenx();
                
int sy=sm::screeny();
                
return Rectangle (hdc,x,y,x+w, y+h) ;
            }

            type::state polygon(type::point 
*apt,int i)
            
{
                
return Polygon (hdc,apt,i) ;
            }



        }
;
        
class mouse
        
{
        
public:
            
static int x(type::lp & lp)
            
{
                
return LOWORD(lp) ;
            }

            
static int y(type::lp & lp)
            
{
                
return HIWORD(lp) ;

            }

        }
;
        
class sm
        
{
        
public:
            
static int screenx()
            
{
                
return GetSystemMetrics(def::sm_x); 
            }

            
static int screeny()
            
{
                
return GetSystemMetrics(def::sm_y); 
            }

        }
;
        
class message
        
{
        
public:
        }
;
        
class proc
        
{
        
public:
            type::hwnd hwnd;
            type::
uint msg;
            type::wp wp;
            type::lp lp;

            proc()
{}
            proc(type::hwnd 
&hwnd_t,type::uint &msg_t,type::wp &wp_t,type::lp &lp_t)
            
{
                update(hwnd_t,msg_t,wp_t,lp_t);
            }

            
void update(type::hwnd &hwnd_t,type::uint &msg_t,type::wp &wp_t,type::lp &lp_t)
            
{
                hwnd
=hwnd_t;
                msg
=msg_t;
                wp
=wp_t;
                lp
=lp_t;
            }

            type::result defproc()
            
{
                
return DefWindowProcW (hwnd, msg, wp, lp); 
            }


            
void postquit(int exitCode_t=0)
            
{
                
if(appinfo::regNum==1)
                
{
                    
//WSHOW__(TEXT("啊!是你."))
                        --appinfo::regNum;
                    PostQuitMessage(exitCode_t);
                }
else
                
{
                    
--appinfo::regNum;
                }

            }



        }
;

        
class wndclass
        
{
        
public:
            type::hwnd   hwnd ;
            type::hwnd   parenthwnd;
            type::
string  title;
            type::wndclass wc;
/* wndclass info */
            
int regIndex;/* self-wndclass index in regList */



            
static type::result __stdcall wndproc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
            
{

                
static proc proc;
                
static paint pt;
                
static type::string  m(TEXT("啊!是你."));

                
using geo::polygon;

                
static polygon plg;

                
static int angle=0;
                
if(angle==0)
                
{
                    plg.push(
100,100);
                    plg.push(
200,100);
                    plg.push(
300,200);
                    plg.push(
300,300);
                    plg.push(
200,250);
                    plg.push(
100,400);
                    angle
=1;
                }

                
                
static wchar_t buf[20];

                proc.update(hwnd,message,wParam,lParam);

                
switch (message)
                
{
                
case api::def::wm_create:
                    
return 0 ;
                
case api::def::wm_mousemove:
                    pt.begin(hwnd);
                     m.
set(0);
                     wsprintf(buf,L
"(x:%d ,y:%d)\0",mouse::x(lParam),mouse::y(lParam));
                     m.
set(buf);

                     pt.end();
                     InvalidateRect(hwnd,NULL,TRUE);

                     
return 0;
                
case api::def::wm_paint:
                    pt.begin(hwnd);
                
                    
                    
{
                    
                    POINT apt[
6];
                    plg.rotate(angle);
                    
for(int i=0;i!=6;i++)
                    
{
                        apt[i].x
=plg(i,0);
                        apt[i].y
=plg(i,1);
                    }

                    pt.polygon(apt,
6);
                    }

                    pt.text(m);


                    pt.end();


                    
return 0;

                
case api::def::wm_destroy:
                    proc.postquit();
                    
return 0 ;

                }

                
return proc.defproc();
            }

            
void init()
            
{
                wc.style         
= def::cs_default;
                wc.lpfnWndProc   
= wndproc ;
                wc.cbClsExtra    
= def::nil ;
                wc.cbWndExtra    
= def::nil ;
                wc.hInstance     
= appinfo::hInstance ;
                wc.hIcon         
= handle::icon() ;
                wc.hCursor       
= handle::cursor();
                wc.hbrBackground 
= handle::brush();
                wc.lpszMenuName  
= def::nil;

                appinfo::setListIndex(regIndex,wc.lpszClassName);

                
if (!RegisterClassW (&wc))
                    
return ;
            }

            wndclass()
            
{

                init();
            }

            
~wndclass()
            
{
                appinfo::decform();
            }

            
void setStyle(UINT style)
            
{
                wc.style 
= style;
            }

            
void setIcon(HICON hIcon)
            
{
                wc.hIcon 
= hIcon;
            }

            
void setCursor(HCURSOR hCursor)
            
{
                wc.hCursor 
= hCursor;
            }

            
void setBackground(HBRUSH hbr)
            
{
                wc.hbrBackground 
= hbr;
            }

            
int renew()
            
{
                hwnd 
= CreateWindowW(appinfo::regList[regIndex],      // window class name
                    title(),   // window caption
                    WS_OVERLAPPEDWINDOW,  // window style
                    CW_USEDEFAULT,// initial x position
                    CW_USEDEFAULT,// initial y position
                    CW_USEDEFAULT,// initial x size
                    CW_USEDEFAULT,// initial y size
                    NULL,                 // parent window handle
                    NULL,            // window menu handle
                    appinfo::hInstance,   // program instance handle
                    NULL) ;      // creation parameters
                return 1;

            }

            
void setTitle(wchar_t  _title[])
            
{
                title.
set(_title);
            }

        }
;
    }
;



    
class form
    
{
        api::wndclass tf;
    
public:
        form()
        
{


            tf.setTitle(TEXT(
"ving"));
            tf.init();
            tf.renew();
        }

        
~form()
        
{
        }

        form(wchar_t 
*title)
        
{
            tf.setTitle(title);
            tf.init();
            tf.renew();
        }


        
int show()
        
{
            tf.renew();
            
int scx=api::sm::screenx(); 
            
int scy=api::sm::screeny(); 
            SetWindowPos(tf.hwnd,NULL,scx
/3 ,scx/3 ,scx/3,scy/3,SWP_HIDEWINDOW);
            ShowWindow (tf.hwnd, appinfo::iCmdShow) ;
            UpdateWindow (tf.hwnd) ;
            
return 1;
        }
    
    }
;
}

int cpp();
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow)
{

    appinfo::regNum
=0;
    appinfo::listIndex
=0;
    appinfo::hInstance
=hInstance;
    appinfo::hPrevInstance
=hPrevInstance;
    appinfo::szCmdLine
=szCmdLine;
    appinfo::iCmdShow
=iCmdShow;

    cpp();

    
while (GetMessageW(&appinfo::msg, NULL, 00))
    
{
        TranslateMessage (
&appinfo::msg) ;
        DispatchMessage (
&appinfo::msg) ;
    }

    
return (int)appinfo::msg.wParam ;
}





#endif




這里是它的入口點界面,在該框架下,入口點是cpp(),當(dāng)有需要,可以加入隱含變量處理入口點獲取的路徑和執(zhí)行參數(shù):


/* init.h  */

#include 
"appframe.h"


int cpp()
{
    
using frame::form;

    form f;
    f.show();

    
return 0;

}



當(dāng)寫到一定時機成熟時,會把appframe里的proc form管理池的接口搬到init.h的入口點函數(shù).

由于之前基于自己寫的arith::axiom庫實現(xiàn)過關(guān)于不規(guī)則內(nèi)陷多邊形的點區(qū)域檢測demo,所以接下來關(guān)于組件庫和監(jiān)聽框架的實現(xiàn)應(yīng)該會快了.



該代碼的demo是一個多邊形圍繞一個點旋轉(zhuǎn),中間不停更新鼠標(biāo)在Client區(qū)的相對坐標(biāo).