必威体育Betway必威体育官网
当前位置:首页 > IT技术

CreateWindow 最后一个参数作用

时间:2019-08-05 16:14:22来源:IT技术作者:seo实验室小编阅读:74次「手机版」
 

createwindow

by WJB date 2108/08/29

最近学了通过windows API 创建窗体 用到了createwindow函数

CreateWindow(

LPCTSTR lpClassName,     //这个名字一定要与 声明的WNDCLASS 对象中lpszClassName的值一致

LPCTSTR lpWindowName,   //窗体左上角显示的窗体名称

Dword dwStyle,

int x,           

int y,           

int nWidth,

int nHeight,

HWND hWndparent,

HMENU hMenu,

handle hlnstance,

LPVOID lpParam

重点记录一下最后一个参数 LPVOID lpParam

指向一个值的指针,该值传递给窗口WM_CREATE消息。该值通过在IParam参数中的CREATEstruct结构传递。如果应用程序调用CreateWindow创建一个MDI客户窗口,则lpParam必须指向一个CLIENTCREATESTRUCT结构。

CREATESTRUCT 结构如下所示:

typedef struct tagCREATESTRUCTW {

LPVOID      lpCreateparams;

Hinstance   hInstance;

HMENU       hMenu;

HWND        hwndParent;

int         cy;

int         cx;

int         y;

int         x;

LONG        style;

LPCWSTR     lpszName;

LPCWSTR     lpszClass;

dword       dwExStyle;

} CREATESTRUCT;

LPVOID lpParam 这个参数再HWDCLASS 中lpfnWndProc回调函数中传递

例如 回调函数如下 

LRESULT CALLBACK    MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)

{

   static D3DAPP* app = 0;

   switch (msg)

   {

   case WM_CREATE:

   {

       CREATESTRUCT* cs = (CREATESTRUCT*)lParam;

       return 0;

   }

   }

   // Don't start processing messages until after WM_CREATE.

   if (app)

       return app->msgProc(msg, wParam, lParam);

   else

       return DefWindowProc(hwnd, msg, wParam, lParam);

}

传递给窗口WM_CREATE时,即可取到该值;

开发中的应用,直接上代码

//D3Dapp.h

#include <windows.h>

class D3DApp

{

public:

   //HINSTANCE 是“句柄型”数据类型。

   D3DApp(HINSTANCE hInstance);

   virtual     ~D3DApp();

   HINSTANCE getAppInst();

   HWND      getMainWnd(); //获取主窗体

   int       run();

   virtual void     initApp();

   virtual LRESULT  msgProc(UINT msg, WPARAM wParam, LPARAM lParam);

protected:

   void initMainWindow();

protected:

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

   //windows 变量

   HINSTANCE mhAppInst;

   HWND      mhMainWnd;

   int mclientwidth;

   int mclientheight;

};

//D3Dapp.cpp

#include "D3DApp.h"

LRESULT CALLBACK MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)

{

   static D3DApp* app = 0;

   switch (msg)

   {

   case WM_CREATE:

   {

       // Get the 'this' pointer we passed to CreateWindow via the lpParam parameter.

       CREATESTRUCT* cs = (CREATESTRUCT*)lParam;

       app = (D3DApp*)cs->lpCreateParams;

       return 0;

   }

   }

   // Don't start processing messages until after WM_CREATE.

   if (app)

       return app->msgProc(msg, wParam, lParam);

   else

       return DefWindowProc(hwnd, msg, wParam, lParam);

}

D3DApp::D3DApp(HINSTANCE hInstance)

{

   mClientWidth = 800;

   mClientHeight = 600;

   mhAppInst = hInstance;

}

D3DApp::~D3DApp()

{

}

HINSTANCE D3DApp::getAppInst()

{

   return mhAppInst;

}

HWND D3DApp::getMainWnd()

{

   return mhMainWnd;

}

int D3DApp::run()

{

   MSG msg = { 0 };

   while (true)

   {

       if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))

       {

           if (msg.message == WM_QUIT)  //如果关闭消息就跳出

           {

               break;

           }

           TranslateMessage(&msg);

           DispatchMessage(&msg);         //派发消息

    }

   }

   return 0;

}

void D3DApp::initApp()

{

   initMainWindow();  //创建窗体

   initDirect3D();

}

LRESULT D3DApp::msgProc(UINT msg, WPARAM wParam, LPARAM lParam)

{

    return DefWindowProc(mhMainWnd, msg, wParam, lParam);;

}

void D3DApp::initMainWindow()

{

   WNDCLASS wc2;

   wc2.cbClsExtra = 0;

   wc2.cbWndExtra = 0;

   wc2.hbrBackground = NULL;

   wc2.hCursor = LoadCursor(NULL, IDC_ARROW);

   wc2.hIcon = LoadIcon(NULL, IDI_APPLICATION);

   wc2.hInstance = mhAppInst;

   wc2.lpfnWndProc = (WNDPROC)MainWndProc;

   wc2.lpszClassName = L"D3DWindow";

   wc2.lpszMenuName = NULL;

   wc2.style = CS_HREDRAW | CS_VREDRAW;   //样式

   if (!RegisterClass(&wc2))   //窗口注册

   {

       MessageBox(NULL, L"notice", L"ERROR", MB_OK);

   }

   mhMainWnd = CreateWindowEx(NULL,

       L"D3DWindow",

       L"D3DWnd",

       WS_overlapPEDWINDOW,

       CW_USEDEFAULT,

       CW_USEDEFAULT,

       mClientWidth,

       mClientHeight,

       0, 0,

       mhAppInst,

       this);

   if (mhMainWnd == 0)

   {

       MessageBox(0, L"CreateWindow failed", 0, 0);

       return;

   }

   ShowWindow(mhMainWnd, SW_SHOW);

   UpdateWindow(mhMainWnd);

}

//main.cpp

#include "D3DApp.h"

INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPreInstance, LPSTR IpCmdLine, int nShowCmd)

{

   D3DApp app(hinstance);

   app.initApp();

   return app.run();

}

相关阅读

CreateWindow失败的原因

Before returning, CreateWindow sends a WM_CREATE message to the window procedure. For overlapped, pop-up, and child wind

分享到:

栏目导航

推荐阅读

热门阅读