如何使用面向对象技术在win32和c ++中创建编辑控件

时间:2017-11-05 23:00:56

标签: c++ windows

我正在尝试使用oop技术创建编辑控件,但我真的不明白我做错了什么。如果有人可以指出我应该如何去做这将非常有帮助。我试过谷歌搜索但是没有很多,因为人们不使用Microsoft API在win32中编码我会尝试mfc但我坚持使用win32 api。

#include <windows.h>

class winMaker
{
   public:
   winMaker(char * className,HINSTANCE hInstance);
   HWND create(const char * title);
   protected:
    HINSTANCE   _hInst;          
    char const *_className;      
    DWORD       _style;          
    DWORD       _exStyle;        
    char const *_title;          
    int         _x;              
    int         _y;              
    int         _width;          
    int         _height;         
    HWND        _hWndParent;     
    HMENU       _hMenu;           
    void *      _data;           
 };

 winMaker::winMaker(char * className,HINSTANCE hInstance) : 
  _style (WS_OVERLAPPEDWINDOW),
 _exStyle (0),
 _className (className),
_x (CW_USEDEFAULT),  
_y (0),              
_width (CW_USEDEFAULT),  
_height (0),         
_hWndParent (0),     
_hMenu (0),          
_data (0),           
_hInst (hInstance)
{
}

HWND winMaker::create(const char * title)  
{
 HWND hwnd = CreateWindowEx(
    _exStyle,
    _className,
    title,
    _style,
    _x,
    _y,
    _width,
    _height,
    _hWndParent,
    _hMenu,
    _hInst,
    NULL  
    );

    return hwnd;
  }

  class childMaker : public winMaker
  {
  public:
    childMaker(char * className,winApp appInstance, int childId);
    HWND create(const char * text = "");
 };

 childMaker::childMaker(char * className, winApp appInstance, int childId): 
 winMaker(className,appInstance.getInstance())
 {
    _style = WS_CHILD;
    _hWndParent = appInstance;
   _hMenu = reinterpret_cast<HMENU> (childId);
 }

 HWND childMaker::create(const char * text)
 {
    HWND hwnd = CreateWindowEx(
    _exStyle,
    _className,
    text,
    _style,
    _x,
    _y,
    _width,
    _height,
    _hWndParent,
    _hMenu,
    _hInst,
    0 //controller parameter goes here 
    );
   return hwnd;
 }

下面的代码是我进入编辑控件类

的窗口入口点
#include <windows.h>
#include "winApp.h"
#include "winMaker.h"


LRESULT CALLBACK WndProcedure(HWND hWnd, UINT uMsg,
              WPARAM wParam, LPARAM lParam);

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
           LPSTR lpCmdLine, int nCmdShow)
{
MSG        Msg;
HWND       hWnd;
HWND       hnd;

winApp myWindow(hInstance,"window1","simp");
char * ClsName = myWindow.RegisterWindowStructure();

winMaker myWindowMaker(ClsName,hInstance);
hWnd = myWindowMaker.create("Simple Window");

myWindow.saveHwnd(hWnd);

childMaker editControl("EDIT",myWindow,0);
hnd = editControl.create();

// Find out if the window was created
if( !hWnd ) // If the window was not created,
    return 0; // stop the application

// Display the window to the user
ShowWindow(hWnd, SW_SHOWNORMAL);
UpdateWindow(hWnd);

// Decode and treat the messages
// as long as the application is running
while( GetMessage(&Msg, NULL, 0, 0) )
{
         TranslateMessage(&Msg);
         DispatchMessage(&Msg);
}

return Msg.wParam;
}

下面的代码是winMan类,其中包含创建窗口类结构的方法

#include <windows.h>
#include "windows.h"

class winApp
{
public:
    friend LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg,
                         WPARAM wParam, LPARAM lParam);

    winApp(HINSTANCE hInst,char nameOfStructWindow[],char MenuName[]);
    char * RegisterWindowStructure();
    HINSTANCE getInstance() const { return _hInstance; }
    operator HWND () const { return _h; }
    void saveHwnd(HWND h){ _h = h; }
    HWND returnHandle() { return _h; }
private:
    char * ClsName;
    char * menuName;
    HINSTANCE _hInstance;
    WNDCLASSEX _WndClsEx;
    HWND _h;
};

LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg,
           WPARAM wParam, LPARAM lParam)
{
    switch(Msg)
    {
     // If the user wants to close the application
    case WM_DESTROY:
    // then close it
    PostQuitMessage(WM_QUIT);
    break;
   default:
    // Process the left-over messages
    return DefWindowProc(hWnd, Msg, wParam, lParam);
  }
  // If something was not done, let it go
  return 0;
}


winApp::winApp(HINSTANCE hInst,char nameOfStructWindow[],char MenuName[])
: ClsName(nameOfStructWindow),menuName(MenuName),_hInstance(hInst)
{
_WndClsEx.cbSize        = sizeof(WNDCLASSEX);
    _WndClsEx.style         = CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;
    _WndClsEx.lpfnWndProc   = WndProcedure;
    _WndClsEx.cbClsExtra    = 0;
    _WndClsEx.cbWndExtra    = 0;
    _WndClsEx.hInstance     = hInst;
    _WndClsEx.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    _WndClsEx.hCursor       = LoadCursor(NULL, IDC_ARROW);
    _WndClsEx.hbrBackground = static_cast<HBRUSH>
                              (GetStockObject(GRAY_BRUSH));
    _WndClsEx.lpszMenuName  = MenuName;
    _WndClsEx.lpszClassName = ClsName;
    _WndClsEx.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
}

char * winApp::RegisterWindowStructure()
{
    RegisterClassEx(&_WndClsEx);
    return  ClsName;
}

1 个答案:

答案 0 :(得分:0)

使用默认样式创建编辑框,在这种情况下为0,因为它未从指定默认样式的资源加载。

因此,应手动指定大小以使编辑框可见。

::SetWindowPos (hnd, HWND_TOP,0,0,100,100, SWP_SHOWWINDOW);

我用100表示​​目的。

您还可以使用其他可用于窗口管理的Win32库。重塑不是一个更好的选择。