
Дракон->Спать();
 
Профиль
Группа: Участник
Сообщений: 687
Регистрация: 4.1.2006
Репутация: 1 Всего: 10
|
Ты когда нибудь писал компоненты на WinAPI? Суть такова: Для ООП Единственная большая сложность, это функция обратного вызова обработчика сообшений. Его надо делать static. Чтобы различать какой все таки экземпляр класса соответствует данному окну, нужно в поле cbWndExtra сохранять this экземпляра. Код очень сырой. Код шаблон для виндовс компонент. При желании его можно доработать. BaseCompon.hКод | #ifndef BASEKOMPONENT_H #define BASEKOMPONENT_H
#include <windows.h>
class BaseCompon { public: BaseCompon(HINSTANCE hInst, char *Name, HWND Parent, RECT rect); ~BaseCompon(void); bool RunWindow(DWORD st, int nCmdShow);
void GetName(char *ch, int len); void SetName(char *ch); void GetSize(POINT *point); void SetSize(POINT point); void GetPlace(POINT *point); void SetPlace(POINT *point); void SetBGColor(COLORREF col); HCURSOR GetCursorP(void); HCURSOR SetCursorP(HCURSOR Cursor); HWND GetHWND(void);
protected: virtual LRESULT WProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam);
virtual void OnDestroy(void); virtual bool OnCreate(CREATESTRUCT FAR* lpCreateStruct); virtual void OnPaint(HDC dc); virtual bool OnEraseBkgnd(HDC hdc); virtual void OnSize(int fwSizeType, int nWidth, int nHeight); virtual void OnMove(int xPos, int yPos); //virtual void SetDefaultPlacement(void); static char NameClass[]; HWND hWindow; HINSTANCE hIns; HWND hParent; HCURSOR hCursor; RECT WRect; DWORD Style; static BaseCompon *TempB;
HWND statwnd;
private: static LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam); static BaseCompon *GetData(const HWND hwnd); void SetData(const HWND hwnd); ATOM RegClass(void); void Destroy(void); bool Create(HWND hwnd, CREATESTRUCT FAR* lpCreateStruct); void Paint(void); void WMSize(int fwSizeType, int nWidth, int nHeight); void WMMove(int xPos, int yPos);
COLORREF RGBBkGnd; HBRUSH brBkGnd; char Name[255]; static ATOM atom; };
#endif //BASEKOMPONENT_H
| BaseCompon.cppКод | #include "BaseKomponent.h"
// ------ BaseCompon ------------------------------------------------ char BaseCompon::NameClass[]="BaseClass"; ATOM BaseCompon::atom=0; BaseCompon *BaseCompon::TempB=NULL;
BaseCompon *BaseCompon::GetData(const HWND hwnd) { return (BaseCompon *) GetWindowLong(hwnd,GWL_USERDATA); }
void BaseCompon::SetData(const HWND hwnd) { SetWindowLong(hwnd, GWL_USERDATA, (LONG) this); }
LRESULT CALLBACK BaseCompon::WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) { BaseCompon *temp=GetData(hwnd); if (temp !=NULL) return temp->WProc(hwnd,Message,wParam,lParam); else if (TempB !=NULL) return TempB->WProc(hwnd,Message,wParam,lParam); return 0; }
ATOM BaseCompon::RegClass(void) { if (atom == 0) { WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = (WNDPROC) WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = sizeof(BaseCompon *); wc.hInstance = hIns; wc.hIcon = 0; wc.hCursor = hCursor; wc.hbrBackground = 0; wc.lpszMenuName = NULL; wc.lpszClassName = NameClass; atom= RegisterClass(&wc); } return atom; }
LRESULT BaseCompon::WProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) { RECT r; int wNotifyCode; int wID; HWND hwndCtl;
switch (Message) { case WM_DESTROY: OnDestroy(); break; case WM_CREATE: return Create(hwnd, (LPCREATESTRUCT) lParam); break; case WM_PAINT: Paint(); break; case WM_ERASEBKGND: return OnEraseBkgnd((HDC) wParam); break; case WM_SIZE: WMSize(wParam, (int) LOWORD(lParam), (int) HIWORD(lParam)); break; case WM_MOVE: WMMove((int) LOWORD(lParam), (int) HIWORD(lParam)); break; case WM_COMMAND: wNotifyCode = HIWORD(wParam); // notification code wID = LOWORD(wParam); // item, control, or accelerator identifier hwndCtl = (HWND) lParam; // handle of control if ((statwnd == hwndCtl) && (wNotifyCode == BN_CLICKED)) SendMessage(hWindow, WM_CLOSE,0,0);//PostQuitMessage(0);
break; case WM_CLOSE: DestroyWindow(hWindow); return 1; break; case WM_CTLCOLORBTN: SetRectEmpty(&r); GetClientRect((HWND)lParam, &r); FillRect((HDC) wParam ,&r,brBkGnd); SetBkColor((HDC) wParam,RGBBkGnd); SelectObject((HDC) wParam,brBkGnd); return (LONG) brBkGnd;//(LONG) SelectObject((HDC) wParam,brBkGnd); break;
default: return DefWindowProc(hwnd, Message, wParam, lParam); } return 0; }
void BaseCompon::Destroy(void) { if (brBkGnd != NULL) DeleteObject(brBkGnd); OnDestroy(); hWindow=NULL; if (hParent == NULL) PostQuitMessage(0); }
bool BaseCompon::Create(HWND hwnd, CREATESTRUCT FAR* lpCreateStruct) { SetData(hwnd); hWindow=hwnd; bool res=OnCreate(lpCreateStruct); if (!res) { brBkGnd=CreateSolidBrush(RGBBkGnd); } return res; }
void BaseCompon::OnDestroy(void) { MessageBox(hWindow,"Mey","Prob", MB_YESNO ); }
bool BaseCompon::OnCreate(CREATESTRUCT FAR* lpCreateStruct) { statwnd=CreateWindow("BUTTON", "My static", WS_CHILD | WS_VISIBLE , 30,20,300,16,hWindow, 0, hIns, NULL); //SetClassLong(statwnd,GCL_HBRBACKGROUND, (LONG) brBkGnd); return false; }
void BaseCompon::Paint(void) { PAINTSTRUCT PaintStruct;
HDC dc = BeginPaint(hWindow,&PaintStruct); OnPaint(dc); EndPaint(hWindow, &PaintStruct); }
void BaseCompon::OnPaint(HDC dc) { }
bool BaseCompon::OnEraseBkgnd(HDC hdc) { RECT r; SetRectEmpty(&r); GetClientRect(hWindow, &r); FillRect(hdc,&r,brBkGnd); return true; }
void BaseCompon::WMSize(int fwSizeType, int nWidth, int nHeight) { WRect.right=nWidth; WRect.bottom=nHeight; OnSize(fwSizeType, nWidth, nHeight); }
void BaseCompon::OnSize(int fwSizeType, int nWidth, int nHeight) { }
void BaseCompon::WMMove(int xPos, int yPos) { WRect.left=xPos; WRect.top=yPos; OnMove(xPos, yPos); }
void BaseCompon::OnMove(int xPos, int yPos) { }
void BaseCompon::GetName(char *ch, int len) { if (ch != NULL) { int i=0; while ((i<(len-1)) && (Name[i] !=0)) ch[i++]=Name[i]; ch[i]=0; } }
void BaseCompon::SetName(char *ch) { if (ch != NULL) { int i=0; while ((i<253) && (ch[i] !=0)) Name[i++]=ch[i]; Name[i]=0; } }
void BaseCompon::GetSize(POINT *point) { if (point != NULL) { point->x=WRect.right; point->y=WRect.bottom; } }
void BaseCompon::SetSize(POINT point) { WRect.right=point.x; WRect.bottom=point.y; SetWindowPos(hWindow,NULL,0,0,WRect.right,WRect.bottom, SWP_NOMOVE | SWP_NOZORDER);
}
void BaseCompon::GetPlace(POINT *point) { if (point != NULL) { point->x=WRect.left; point->y=WRect.top; } }
void BaseCompon::SetPlace(POINT *point) { WRect.left=point->x; WRect.top=point->y; SetWindowPos(hWindow,NULL,WRect.left,WRect.top,0,0, SWP_NOSIZE | SWP_NOZORDER); }
HWND BaseCompon::GetHWND(void) { return hWindow; }
HCURSOR BaseCompon::GetCursorP(void) { return hCursor; }
HCURSOR BaseCompon::SetCursorP(HCURSOR Cursor) { HCURSOR res=hCursor; hCursor=Cursor; SetClassLong(hWindow, GCL_HCURSOR, (LONG) hCursor); return res; }
BaseCompon::BaseCompon(HINSTANCE hInst, char *Name, HWND Parent, RECT rect) { hCursor=LoadCursor(NULL, IDC_ARROW); hParent=Parent; SetName(Name); hIns=hInst; WRect.bottom=rect.bottom; WRect.left=rect.left; WRect.right=rect.right; WRect.top=rect.top; RegClass(); brBkGnd=NULL; SetBGColor(RGB(192,192,254)); hWindow =NULL; Style=0;
}
BaseCompon::~BaseCompon(void) { if (hWindow !=NULL) SendMessage(hWindow,WM_CLOSE, 0, 0); }
bool BaseCompon::RunWindow(DWORD St, int nCmdShow) {
if (hWindow !=NULL) SendMessage(hWindow,WM_CLOSE, 0, 0); Style=St; TempB=this; hWindow=CreateWindow(NameClass, Name, Style, WRect.left, WRect.top, WRect.right, WRect.bottom, hParent, NULL,hIns, NULL); TempB=NULL; int err=GetLastError(); if (hWindow != NULL) { ShowWindow(hWindow, nCmdShow); UpdateWindow(hWindow); }
return (hWindow !=NULL); }
void BaseCompon::SetBGColor(COLORREF col) { RGBBkGnd=col; if (brBkGnd != NULL) DeleteObject(brBkGnd); brBkGnd=CreateSolidBrush(RGBBkGnd); UpdateWindow(hWindow);
}
|
--------------------
Пролетал мимо.
|