封裝視窗物件。
(注意。有些命名我是直接複製win32api的,偷懶了)
本來是想直接用虛函式來處理訊息的,但是訊息很多,寫的會很吃力,所以採用類似mfc的訊息對映,現在記憶體是很富裕的,不用擔心虛函式表會占用大量記憶體,個人建議是採用虛函式的,我這裡偷偷懶。
該類主要作用:訊息處理函式呼叫該類的靜態函式wndproc,再呼叫例項物件的windowproc,進行訊息處理,如果是已經註冊的類,需要調換為我們的訊息處理函式。(wm_create和wm_nccreate並沒有呼叫)
pwnd.h
#pragma once
// windows 標頭檔案:
#include
// c 執行時標頭檔案
#include
#include
#include
#include
#include
#include
//簡單的訊息對映
#define declare_msgmap()\
public:\
virtual msginfo* getthismsgmap();\
static msginfo* getmsgmap();\
#define add_msg(msg, funproc)\
value_type(msg, (lresult(pwnd::*)(wparam wparam, lparam lparam))&funproc)
#define implement_begin_msgmap(thisclass, parentclass)\
msginfo* thisclass::getthismsgmap()\\\
msginfo* thisclass::getmsgmap()\
)\ };\
return &info;\
}class pwnd;
typedef
struct _tagmsginfo
msginfo;
class pwnd
;
pwnd.cpp
#include "pwnd.h"
#include
pwnd::pwnd() : _hwnd(null), _pfnoldwndproc(null)
pwnd::~pwnd()
}lresult callback pwnd::wndproc(hwnd hwnd, uint nmsg, wparam wparam, lparam lparam)
return ::defwindowproc(hwnd, nmsg, wparam, lparam);
} pwnd = (pwnd*)::getwindowlongptr(hwnd, gwl_userdata);
if (pwnd)
return ::defwindowproc(hwnd, nmsg, wparam, lparam);
}pwnd* pwnd::fromhandle(hwnd hwnd)
long pwnd::setwindowlongptr(int nindex, long ndata)
long pwnd::getwindowlongptr(int nindex)
bool pwnd::setsubclasswindow(hwnd hwnd)
bool pwnd::iswindow()
bool pwnd::showwindow(int nshow)
bool pwnd::updatewindow()
bool pwnd::destroywindow()
bool pwnd::createwindowex(dword dwexstyle, lpctstr lpclassname, lpctstr lpwindowname,
dword dwstyle, int x, int y,
int nwidth, int nheight, hwnd hwndparent,
hmenu hmenu, hinstance hinstance)
; cs.dwexstyle = dwexstyle;
cs.lpszclass = lpclassname;
cs.lpszname = lpwindowname;
cs.style = dwstyle;
cs.x = x;
cs.y = y;
cs.cx = nwidth;
cs.cy = nheight;
cs.hwndparent = hwndparent;
cs.hmenu = hmenu;
cs.hinstance = hinstance;
if (precreatewindow(&cs) == false)
return
false;
hwnd hwnd = ::createwindowex(cs.dwexstyle, cs.lpszclass, cs.lpszname,
cs.style, cs.x, cs.y,
cs.cx, cs.cy, cs.hwndparent,
cs.hmenu, cs.hinstance, cs.lpcreateparams);
//沒有呼叫我們的靜態函式
if (_hwnd == null)
_hwnd = hwnd;
} //物件例項繫結 以便 自定義的訊息處理函式能呼叫例項
setwindowlongptr(gwl_userdata, (long)this);
_pfnoldwndproc = (wndproc)getwindowlongptr(gwl_wndproc);
//不是我們所註冊的類
if (_pfnoldwndproc != pwnd::wndproc)
return
true;
}lresult pwnd::default(uint nmsg, wparam wparam, lparam lparam)
//不是我們註冊的函式,交予舊函式預設處理
return _pfnoldwndproc(_hwnd, nmsg, wparam, lparam);
}bool pwnd::precreatewindow(lpcreatestruct lpcs)
; wcex.cbsize = sizeof(wcex);
//判斷是否要自註冊類
if (::getclassinfoex(lpcs->hinstance, lpcs->lpszclass, &wcex) == false)
lpcs->lpcreateparams = this; //wndproc需要
return
true;
}lresult pwnd::windowproc(uint nmsg, wparam wparam, lparam lparam)
if (pinfo->_getparentmsg == null)
break;
pinfo = pinfo->_getparentmsg();
} return
default(nmsg, wparam, lparam);
}lresult pwnd::defaultproc(uint nmsg, wparam wparam, lparam lparam)
lresult pwnd::onnccreate(wparam wparam, lparam lparam)
lresult pwnd::oncreate(wparam wparam, lparam lparam)
lresult pwnd::onclose(wparam wparam, lparam lparam)
lresult pwnd::ondestroy(wparam wparam, lparam lparam)
lresult pwnd::onncdestroy(wparam wparam, lparam lparam)
msginfo* pwnd::getthismsgmap()
msginfo* pwnd::getmsgmap()
) };
return &info;
}
我們用的時候只需要繼承該類,新增更多的訊息處理即可。
#pragma once
#include "pwnd.h"
;
; while (getmessage(&msg, null, 0, 0))
return0;}
main函式示範:
#include "win.h"
hinstance g_hinst; // 當前例項
int apientry wwinmain(_in_ hinstance hinstance,
_in_opt_ hinstance hprevinstance,
_in_ lpwstr lpcmdline,
_in_ int ncmdshow)
pwnd示例
class
pmainwnd : public pwnd
lresult oncommand(wparam
wparam, lparam
lparam)
};implement_begin_msgmap
(pmainwnd, pwnd)
add_msg
(wm_paint, onpaint),
add_msg
(wm_command, oncommand)
implement_end_msgmap
()
win32視窗樣式
private const gwl style 16 視窗樣式 private sub command1 click orstyle end sub private sub orstyle dim lstyle as long lstyle getwindowlong me.hwnd,gwl sty...
WIN32視窗建立
win32視窗建立過程 1 定義視窗類 在msdn中找到函式原型 winmain 2 定義視窗處理函式 在msdn中找到原型 windowproc 3 設計視窗類 4 註冊視窗類 registerclass registerclas 5 建立視窗 createwindow createwindowe...
win32繪製視窗
static int cxclient,cyclient 宣告兩個變數存放滑鼠 x,y 座標.在下面,我們為這兩項賦值時,loword lparam 代表取 lparam 的低 16 位,hiword lparam 代表取lparam 的高 16 位,在 lparam 中存放的實際就是滑鼠的位置,低...