利用class类窗口过程实现

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


class CMyWindow
{
public:
BOOL CreateEx (DWORD dwExStyle,      // extended window style
 LPCTSTR lpClassName,  // registered class name
 LPCTSTR lpWindowName, // window name
 DWORD dwStyle,        // window style
 int x,                // horizontal position of window
 int y,                // vertical position of window
 int nWidth,           // window width
 int nHeight,          // window height
 HWND hWndParent,      // handle to parent or owner window
 HMENU hMenu,          // menu handle or child identifier
 HINSTANCE hInstance,  // handle to application instance
 LPVOID lpParam        // window-creation data
);
BOOL ShowWindow(int CmdShow);
BOOL UpdateWindow();
public:
HWND m_hwnd;
};


BOOL CMyWindow::CreateEx(DWORD dwExStyle,      // extended window style
 LPCTSTR lpClassName,  // registered class name
 LPCTSTR lpWindowName, // window name
 DWORD dwStyle,        // window style
 int x,                // horizontal position of window
 int y,                // vertical position of window
 int nWidth,           // window width
 int nHeight,          // window height
 HWND hWndParent,      // handle to parent or owner window
 HMENU hMenu,          // menu handle or child identifier
 HINSTANCE hInstance,  // handle to application instance
 LPVOID lpParam        // window-creation data
 )
{
m_hwnd=::CreateWindowEx(dwExStyle, lpClassName,lpWindowName,dwStyle,
x,y,nWidth,nHeight,hWndParent, hMenu,hInstance,lpParam);
if(m_hwnd!=NULL)
return TRUE;
else
return FALSE;
}


BOOL CMyWindow::ShowWindow(int CmdShow)
{
return ::ShowWindow(m_hwnd,CmdShow);


}


BOOL CMyWindow::UpdateWindow()
{
return ::UpdateWindow(m_hwnd);
}


LRESULT CALLBACK Function(  HWND hwnd,      // handle to window
 UINT uMsg,      // message identifier
 WPARAM wParam,  // first message parameter
 LPARAM lParam   // second message parameter
 );
int WINAPI WinMain(
 HINSTANCE hInstance,      // handle to current instance
 HINSTANCE hPrevInstance,  // handle to previous instance
 LPSTR lpCmdLine,          // command line
 int nCmdShow              // show state
)
{
WNDCLASS ws;
ws.cbClsExtra=0;
ws.cbWndExtra=0;
ws.hbrBackground=(HBRUSH)GetStockObject(LTGRAY_BRUSH);
ws.hCursor=LoadCursor(NULL,IDC_IBEAM);
ws.hIcon=LoadIcon(NULL,IDI_QUESTION);
ws.hInstance=hInstance;
ws.lpfnWndProc=Function;
ws.lpszClassName="Kitus";
ws.lpszMenuName=NULL;
ws.style=CS_BYTEALIGNWINDOW;


RegisterClass(&ws);


CMyWindow window;
window.CreateEx(WS_EX_OVERLAPPEDWINDOW,ws.lpszClassName,"Kitus' window",WS_OVERLAPPEDWINDOW  | WS_HSCROLL | WS_VSCROLL,CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 
NULL,NULL,hInstance,NULL);
window.ShowWindow(SW_SHOWNORMAL);
window.UpdateWindow();


MSG msg;
while(GetMessage(&msg,window.m_hwnd,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
};


LRESULT CALLBACK Function(  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
  )
{
PAINTSTRUCT ps;
HDC hdc;
switch(uMsg)
{
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
TextOut(hdc,0,0,"I love my family!",strlen("I love my family!"));
EndPaint(hwnd,&ps);
break;


case WM_LBUTTONDOWN:
MessageBox(hwnd,"You pressed the left button!","Notice",MB_OKCANCEL);
hdc=GetDC(hwnd);
TextOut(hdc,20,20,"Good Good Study!",strlen("Good Good Study!"));
ReleaseDC(hwnd,hdc);
break;
case WM_CLOSE:
if(IDOK==MessageBox(hwnd,"Do you rellay want to close the window?","Warning!",MB_OKCANCEL))
CloseWindow(hwnd);
PostQuitMessage(0);
break;
case WM_DESTROY:
DestroyWindow(hwnd);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值