收录查询

[原创]SDKDEMO FIRST

//HelloShanzy.h

#ifndef  HELLOSHANZY_H
#define  HELLOSHANZY_H

#include <windows.h>

//in this header file, I will encapsalution 3 parts
//(1)WinProc()
//(2)class CWinClass
//(3)class CreateWnd

/************************************************************************/
/* designed by shanzy  2005-05-08                                                                             */
/************************************************************************/

char yourProcess[] = "HelloShanzy";

//the CALLBACK windowproc------the first part
LRESULT CALLBACK mywindowproc
(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); 

//the register-----------------the second part
class CWinClass
{
public:
 CWinClass(WNDPROC wndProc, char const *yourProcess, HINSTANCE hInst);
 
 bool Register()
 {
  
  if(!::RegisterClass(&wc))
  {
   ::MessageBox(hwnd, TEXT("Mybe you need windows 2000"), yourProcess, MB_ICONERROR);
   return 0;
  }
 }
 
private:
 WNDCLASS  wc;
 HWND   hwnd;
};

//////////////////////////////////////////////////////////////////////////
//the third part-------create + show + update
class CreateWnd
{
public:
 CreateWnd() : hwnd()
 {
  //nothing, just call initialization list to create hwnd
 }
 CreateWnd(const char * yourApplication, char const * yourProcess, HINSTANCE hInst);
 
 void ShowAndUpdate(int nCmdShow)
 {
  ::ShowWindow(hwnd, nCmdShow);
  ::UpdateWindow(hwnd);
 }
private:
 //the window handle of your application's
 HWND hwnd; 
};
#endif

********************************************************************************
//HelloShanzy.cpp

#include "HelloShanzy.h"

//////////////////////////////////////////////////////////////////////////

//the main entry of most kinds of the windows applications
int WINAPI WinMain
(HINSTANCE hInst, HINSTANCE hPrevInst, char * cmdParam, int nCmdShow)
{


 //register
 CWinClass myRegister(mywindowproc, yourProcess, hInst);
 myRegister.Register();
 

 //create window, and then----show and update window
 //this will create a process
 CreateWnd myWindow("这是shanzy的窗口", yourProcess, hInst);
 myWindow.ShowAndUpdate(nCmdShow);

 MSG msg;
 int status;
 
 //then into the windows-message loop
 while(0 != (status = ::GetMessage(&msg, 0, 0, 0)))
 {
  if(-1 == status)
   return -1;

  ::TranslateMessage(&msg);
  ::DispatchMessage(&msg);
 }

 return msg.wParam;
}

//////////////////////////////////////////////////////////////////////////

CWinClass::CWinClass(WNDPROC wndProc, char const *yourProcess, HINSTANCE hInst)
{
 wc.lpfnWndProc  = mywindowproc; //invoke the mywindowproc
 wc.cbClsExtra  = 0;
 wc.cbWndExtra  = 0;
 wc.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH);
 wc.hCursor  = ::LoadCursor(NULL, IDC_ARROW);
 wc.hIcon  = ::LoadIcon(NULL, IDI_APPLICATION);
 wc.hInstance  = hInst;
 wc.lpszClassName = yourProcess;
 wc.lpszMenuName  = NULL;
 wc.style  = CS_HREDRAW | CS_VREDRAW;
 
}

//////////////////////////////////////////////////////////////////////////
CreateWnd::CreateWnd
(const char * yourApplication, char const * yourProcess, HINSTANCE hInst)
{
 hwnd = ::CreateWindow(
         yourProcess,
         yourApplication,
         WS_OVERLAPPEDWINDOW,
         CW_USEDEFAULT,
         CW_USEDEFAULT,
         CW_USEDEFAULT,
         CW_USEDEFAULT,
         0,
         0,
         hInst,
         0
         );

}

//////////////////////////////////////////////////////////////////////////

LRESULT CALLBACK mywindowproc
(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
 
 HDC   hdc;
 PAINTSTRUCT  ps;
 RECT   rect;


 switch(message)
 {
 case WM_CREATE:
  return 0;
 case WM_PAINT:
  hdc = ::BeginPaint(hwnd, &ps);

  ::GetClientRect(hwnd, &rect);

  ::DrawText(
      hdc,
      TEXT(" HelloShanzy, this OS is Windows2000 Professional"),
      -1,
      &rect,
      DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  ::EndPaint(hwnd, &ps);
 return 0;

 case WM_DESTROY:
  PostQuitMessage(0);
 return 0;
 }

 return DefWindowProc(hwnd, message, wParam, lParam);
}

posted @ 2005-05-13 14:02  ->  阅读(478)  评论(2)    收藏  举报