C++ 中如何使用API函数 生成一个窗体

C++ 中如何使用API函数 生成一个窗体,第1张

生产窗体可以使用CreateWindowEx函数

函数功能:该函数创建一个具有扩展风格的层叠式窗口、d出式窗口或子窗口,其他与CreateWindow函数相同。

函数原型:

CreateWindowEx函数创建一个层叠的,自动d出的(pop-up)或是一个子窗口通过扩展格式。另外这个函数的作用与CreateWindow函数的作用相同。要获得更多的关于创建窗口的信息和关于CreateWindowEx函数参数的详细描述。参见CreateWindow

HWND CreateWindowEx(

DWOR DdwExStyle,        //窗口的扩展风格

LPCTSTR lpClassName,    //指向注册类名的指针

LPCTSTR lpWindowName,   //指向窗口名称的指针

DWORD dwStyle,          //窗口风格

int x,                  //窗口的水平位置

int y,                  //窗口的垂直位置

int nWidth,             //窗口的宽度

int nHeight,            //窗口的高度

HWND hWndParent,        //父窗口的句柄

HMENU hMenu,            //菜单的句柄或是子窗口的标识符

HINSTANCE hInstance,    //应用程序实例的句柄

LPVOID lpParam          //指向窗口的创建数据

);

例程:

include<windowsh>

#include<stdioh>

LRESULT CALLBACK WinDouProc(

    HWND hwnd,      // handle to window

    UINT uMsg,      // message identifier

    WPARAM wParam,  // first message parameter

    LPARAM lParam   // second message parameter

);

class CWnd

{

public:

     CWnd()

     {

          m_hWnd = NULL;

     }

     BOOL CreateEx(

          DWORD dwExStyle,      // extended window style

          LPCTSTR lpClassName,  // pointer to registered class name

          LPCTSTR lpWindowName, // pointer to 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,          // handle to menu or child-window identifier

         HANDLE hInstance,     // handle to application instance

         LPVOID lpParam        // pointer to window-creation data

     );

     BOOL ShowWindow( int nCmdShow );

     BOOL UpdateWindow();

public:

     HWND m_hWnd;

};

BOOL CWnd::CreateEx(

      DWORD dwExStyle,      // extended window style

      LPCTSTR lpClassName,  // pointer to registered class name

      LPCTSTR lpWindowName, // pointer to 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,          // handle to menu or child-window identifier

      HANDLE hInstance,     // handle to application instance

      LPVOID lpParam        // pointer to window-creation data

)

{

     m_hWnd = ::CreateWindowEx  (dwExStyle,lpClassName,lpWindowName,dwStyle,x,y,nWidth,nHeight,hWndParent,hMenu,(HINSTANCE)hInstance,lpParam);

     if(m_hWnd != NULL)

          return TRUE;

     else

         return FALSE;

}

BOOL CWnd::ShowWindow(int nCmdShow)

{

     return ::ShowWindow(m_hWnd,nCmdShow);

}

BOOL CWnd::UpdateWindow()

{

     return ::UpdateWindow(m_hWnd);

}

int WINAPI WinMain(

  HINSTANCE hInstance,  // handle to current instance

  HINSTANCE hPrevInstance,  // handle to previous instance

  LPSTR lpCmdLine,      // pointer to command line

  int nCmdShow          // show state of window

)

{

    WNDCLASS wndclass;     //先设计窗口类

    wndclasscbClsExtra = 0;

    wndclasscbWndExtra = 0;

    wndclasshbrBackground = (HBRUSH)GetStockObject(DKGRAY_BRUSH);

    wndclasshCursor = LoadCursor(NULL,IDC_HELP);

    wndclasshIcon = LoadIcon(NULL,IDI_WARNING);

    wndclasshInstance = hInstance;

    wndclasslpfnWndProc = WinDouProc;

    wndclasslpszClassName = "Magic_Maggie";

    wndclasslpszMenuName = 0;

    wndclassstyle = CS_VREDRAW | CS_HREDRAW;

     //某一个变量原油几个变量去掉一个特征,可以用取反(~)后再进行与(&)

     //例如:style上去掉CS_NOCLOSE,可以style&~CS_NOCLOSE;

    RegisterClass(&wndclass);     ///注意先建立再注册昂

    CWnd wnd;

     wndCreateEx(NULL,"Magic_Maggie","DouDou",WS_OVERLAPPEDWINDOW,0,0,800,600,NULL,NULL,hInstance,NULL);

     wndShowWindow(SW_SHOWNORMAL);

     wndUpdateWindow();

     MSG msg;     //消息循环

     while(GetMessage(&msg,NULL,0,0))

    {

           TranslateMessage(&msg);

           DispatchMessage(&msg);   //触发WinDouProc

     }

    return 0;

}

 

 

LRESULT CALLBACK WinDouProc(

    HWND hwnd,      // handle to window

     UINT uMsg,      // message identifier

    WPARAM wParam,  // first message parameter

    LPARAM lParam   // second message parameter

)

{

 switch(uMsg)

 {

 case WM_LBUTTONDOWN:

      MessageBox(hwnd,"您按下了鼠标左键昂","豆豆的程序",MB_OK);

      HDC hdc;

      hdc = GetDC(hwnd);    

     //The GetDC function retrieves a handle to a display device context for the client area of a specified window or for the entire screen You can use the returned handle in subsequent GDI functions to draw in the device context

      TextOut(hdc,0,0,"感谢您对豆豆程序的支持昂",strlen("感谢您对豆豆程序的支持昂"));

      ReleaseDC(hwnd,hdc);

      break;

 

case WM_CHAR:

      char szChar[20];

      sprintf(szChar,"Char is %d",wParam);

      MessageBox(hwnd,szChar,"豆豆的程序",MB_OK);

      break;

 

case WM_PAINT:

      PAINTSTRUCT ps;

      HDC hDc;

      hDc = BeginPaint(hwnd,&ps);

      TextOut(hDc,0,0,"这个是重绘滴哦",strlen("这个是重绘滴哦"));

      EndPaint(hwnd,&ps);

      break;

 

case WM_CLOSE:   //这个case与下边的destroy这个case不要弄错了,否则窗口不出现,但任务管理器中运行

      if(IDYES == MessageBox(hwnd,"您真的要退出么","豆豆的程序",MB_YESNO))

      {

           DestroyWindow(hwnd);

      }

      break;

 

case WM_DESTROY:

      PostQuitMessage(0);

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

      break;

 

default:

     return DefWindowProc(hwnd,uMsg,wParam,lParam);  // 别忘记了return

 

  }

 return 0;

}

如何用纯Win32 API写模态窗口?

所谓模态窗口(modal window),又叫做模式窗口,一般是指应用程序中那些任务比较紧要的窗口。只要它们存在,它们便会阻止用户访问其他窗口(或者是阻止用户访问其祖先窗口)。在windows中,使用DialogBoxParam显示的对话框就是模态的。虽然模态对话框在windows中很普遍,但是并没有SDK级别的API,可以将一个窗口变为模态显示。不过在同为微软提供的MFC/WTL框架中有这样的API——Domodal(),使用代码基本上是这个样子的:

CXXXDialog dlg(someWindow);

int nRet = dlgDomodal();

if (nRet == IDOK)

{

//从dlg中获取想要的数据

}

else

{

//用户取消了窗口

}

这方法挺好用的,但是如果只是使用win32 api 怎么办?或者要让一个窗口(而不是对话框)模态显示又要怎么办?这个时候就需要我们自己实现模态机制了。

我们知道windows系统是依靠windows消息分发与响应来驱动UI交互的。在单线程UI中,如果某个消息的处理函数占用了大量时间,其他消息就不能得到及时的处理(因为是单线程)。如果我们的Domodal()不做特殊处理,结果就会导致整个UI线程卡住(因为代码的执行会停在Domodal()内部,阻塞线程的消息分发),这显然是不满足我们要求的。既要代码停留在Domodal(),又要不阻塞Windows消息分发过程,有什么方法可以解决呢?答案就是接管消息循环,即在DoModal()内部,将线程的消息循环接管过来,自己维护,直到外部通知结束模态循环。

wndclasslpszClassName=(LPCWSTR)szClassName; 不需要强制转换

::RegisterClassEx(& wndclass); 加个条件判断,它成功的返回值非0

while(::GetMessage(&msg,hWnd,0,0)) 第二个参数0,要不然关掉窗口程序还在,CPU100

以上就是关于C++ 中如何使用API函数 生成一个窗体全部的内容,包括:C++ 中如何使用API函数 生成一个窗体、如何用纯Win32 API写模态窗口、windows API 窗口出不来啊 任务管理器里有这个进程了 是不是ShowWindow那里出错了 搞了一晚上 纠结 求指教等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/9780740.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-01
下一篇 2023-05-01

发表评论

登录后才能评论

评论列表(0条)

保存