第七章 弹出式窗体与子窗体

来源:互联网 发布:化妆品推荐 知乎 编辑:程序博客网 时间:2024/05/16 17:50

  我们讨论三种窗体,它们分别是“重叠式窗体”(overlapped window)“弹出式窗体”(popup window) 与“子窗体”(child window)。

窗体风格

1. WS_OVERLAPPED
2. WS_OVERLAPPEDWINDOW
3. WS_POPUP
4. WS_POPUPWINDOW
5. WS_CHILD
6. WS_CHILDWINDOW
 利用带有WINDOW窗体风格建立的窗体,一般具有较为完备的窗体要素;
 主窗体一般采用层叠式(WS_OVERLAPPED)窗体风格;
 子窗体一般采用子窗体(WS_CHILD)风格或弹出式窗体(WS_POPUP)风格;
 利用子窗体(WS_CHILD)风格建立的子窗体只能在其父窗体的客户区内;
 利用弹出式窗体(WS_POPUP)风格或层叠式(WS_OVERLAPPED)窗体风格建立的子窗体可以子左面上任意移动。

相关函数

一.SetWindowText()函数
1.功能:
用于设置窗体标题条标题。
2.函数声明:
BOOL SetWindowText(HWND hWnd,
LPCTSTR lpString);
3.参数:
hWnd: 窗体句柄;
lpString:指向标题字符串指针。
4.返回值:
如果成功,返回TRUE,否则返回FALSE。
二.wsprintf()函数
1.功能:
用于内部文件写操作。
2.函数声明:
int wsprintf(LPTSTR lpOut, LPCTSTR lpFmt, ...);
3.参数:
lpOut:内部文件指针;
lpFmt:可是说明字符串;
...: 变量表。
4.返回值: 如果成功,返回内部文件长度,否则0。

弹出式窗体与子窗体实例

一.多窗体实例
#include <windows.h>
HWND hWnds[8];
int WINAPI WinMain(HINSTANCE, HINSTANCE,LPSTR,int);
LRESULT CALLBACK WndProc(HWND,UINT, WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
  WNDCLASSEX wndclass;
  wndclass.cbSize = sizeof(WNDCLASSEX);
  wndclass.style = CS_HREDRAW|CS_VREDRAW;
  wndclass.lpfnWndProc = (WNDPROC)WndProc;
  wndclass.cbClsExtra = 0;
  wndclass.cbWndExtra = 0;
  wndclass.hInstance = hInstance;
  wndclass.hIcon = LoadIcon(NULL,(LPCTSTR)IDI_APPLICATION);
  wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
  wndclass.hbrBackground = (HBRUSH)COLOR_WINDOW;
  wndclass.lpszMenuName = NULL;
  wndclass.lpszClassName = "WindowClass";
  wndclass.hIconSm = LoadIcon(NULL,(LPCTSTR)IDI_APPLICATION); 
  if(!RegisterClassEx(&wndclass)) return FALSE;
  for(int i=0;i<8;i++)
  {
    hWnds[i] = CreateWindowEx(NULL,
                 "WindowClass",
                 NULL,
                 WS_OVERLAPPEDWINDOW,
                 CW_USEDEFAULT,
                 CW_USEDEFAULT,
                 CW_USEDEFAULT,
                 CW_USEDEFAULT,
                 NULL,
                 NULL,
                 hInstance,
                 NULL);
    if(!hWnds[i]) return FALSE;
    char szBuffer[30];
    wsprintf(szBuffer,"Window Number %d",i);
    SetWindowText(hWnds[i],szBuffer);
    ShowWindow(hWnds[i], nCmdShow);
    UpdateWindow(hWnds[i]);
  }
  MSG msg;

  while(GetMessage(&msg, NULL, 0, 0))
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
  return msg.wParam;


LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
  switch(message) 
  {
    case WM_DESTROY:
    if(hWnd==hWnds[0]) PostQuitMessage(0);
    break;
    default:
    return DefWindowProc(hWnd,message,wParam,lParam);
  }
  return 0;
}
二.弹出式窗体实例
#include <windows.h>
int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int);
LRESULT CALLBACK PopupWndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
  WNDCLASSEX wndclass;
  wndclass.cbSize = sizeof(WNDCLASSEX); 
  wndclass.style = CS_HREDRAW|CS_VREDRAW;
  wndclass.lpfnWndProc = (WNDPROC)PopupWndProc;
  wndclass.cbClsExtra = 0;
  wndclass.cbWndExtra = 0;
  wndclass.hInstance = hInstance;
  wndclass.hIcon = LoadIcon(NULL,(LPCTSTR)IDI_APPLICATION);
  wndclass.hCursor = LoadCursor(NULL,IDC_CROSS);
  wndclass.hbrBackground = (HBRUSH)COLOR_WINDOW;
  wndclass.lpszMenuName = NULL;
  wndclass.lpszClassName = "PopupWindowClass";
  wndclass.hIconSm = LoadIcon(NULL,(LPCTSTR)IDI_APPLICATION);
  if(!RegisterClassEx(&wndclass)) return FALSE;
  HWND hWnd = CreateWindowEx( NULL,
                "PopupWindowClass",
                NULL,
                WS_POPUP,
                100,
                100,
                300,
                200,
                NULL,
                NULL,
                hInstance,
                NULL);
  if(!hWnd) return FALSE;
  ShowWindow(hWnd, nCmdShow);
  UpdateWindow(hWnd);
  MSG msg;

   while(GetMessage(&msg,NULL,0,0))
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
  return msg.wParam;
}

LRESULT CALLBACK PopupWndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
  switch(message) 
  {
    case WM_DESTROY:
    PostQuitMessage(0);
    break;
    default:
    return DefWindowProc(hWnd, message, wParam, lParam);
  }
  return 0;
}
三.子窗体实例
#include <windows.h>
int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int);
LRESULT CALLBACK ParentWndProc(HWND,UINT,WPARAM,LPARAM);
LRESULT CALLBACK ChildWndProc(HWND,UINT,WPARAM,LPARAM);
LRESULT CALLBACK PopupWndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
  WNDCLASSEX wndclass;
  wndclass.cbSize = sizeof(WNDCLASSEX); 
  wndclass.style = CS_HREDRAW|CS_VREDRAW;
  wndclass.lpfnWndProc = (WNDPROC)ParentWndProc;
  wndclass.cbClsExtra = 0;
  wndclass.cbWndExtra = 0;
  wndclass.hInstance = hInstance;
  wndclass.hIcon = LoadIcon(NULL,(LPCTSTR)IDI_APPLICATION);
  wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
  wndclass.hbrBackground = (HBRUSH)COLOR_WINDOW;
  wndclass.lpszMenuName = NULL;
  wndclass.lpszClassName = "ParentWindowClass";
  wndclass.hIconSm = LoadIcon(wndclass.hInstance,(LPCTSTR)IDI_APPLICATION);
  if(!RegisterClassEx(&wndclass)) return FALSE;
  wndclass.cbSize = sizeof(WNDCLASSEX);
  wndclass.style = CS_HREDRAW|CS_VREDRAW;
  wndclass.lpfnWndProc = (WNDPROC)ChildWndProc;
  wndclass.cbClsExtra = 0;
  wndclass.cbWndExtra = 0;
  wndclass.hInstance = hInstance;
  wndclass.hIcon = NULL;
  wndclass.hCursor = LoadCursor(NULL,IDC_CROSS);
  wndclass.hbrBackground = (HBRUSH)COLOR_WINDOW;
  wndclass.lpszMenuName = NULL;
  wndclass.lpszClassName = "ChildWindowClass";
  wndclass.hIconSm = NULL;
  return FALSE;
  wndclass.cbSize = sizeof(WNDCLASSEX);
  wndclass.style = CS_HREDRAW|CS_VREDRAW;
  wndclass.lpfnWndProc = (WNDPROC)PopupWndProc;
  wndclass.cbClsExtra = 0;
  wndclass.cbWndExtra = 0;
  wndclass.hInstance = hInstance;
  wndclass.hIcon = NULL;
  wndclass.hCursor = LoadCursor(NULL,IDC_CROSS);
  wndclass.hbrBackground = (HBRUSH)COLOR_WINDOW;
  wndclass.lpszMenuName = NULL;
  wndclass.lpszClassName = "PopupWindowClass";
  wndclass.hIconSm = NULL;
  if(!RegisterClassEx(&wndclass)) return FALSE;
  HWND hWndParent = CreateWindowEx( NULL,
                   "ParentWindowClass",
                   "Parent Window",
                   WS_OVERLAPPEDWINDOW,
                   100,
                   100,
                   530,
                   300,
                   NULL,
                   NULL,
                   hInstance,
                   NULL);
  if(!hWndParent) return FALSE;
  ShowWindow(hWndParent, nCmdShow);
  UpdateWindow(hWndParent);
  HWND hWndChild = CreateWindowEx(NULL,
                  "ChildWindowClass",
                  "Child Window with Parent",
                  WS_CHILDWINDOW|WS_CAPTION,
                  110,
                  150,
                  250,
                  200,
                  hWndParent,
                  NULL,
                  hInstance,
                  NULL);
  if(!hWndChild) return FALSE;
  ShowWindow(hWndChild, nCmdShow);
  UpdateWindow(hWndChild);
  HWND hWndPopup = CreateWindowEx(NULL,
                  "PopupWindowClass",
                  "Popup Window without Parent", 
                  WS_POPUPWINDOW|WS_CAPTION,
                  370,
                  150,
                  250,
                  200,
                  NULL,
                  NULL,
                  hInstance,
                  NULL);
  if(!hWndPopup) return FALSE;
  ShowWindow(hWndPopup, nCmdShow);
  UpdateWindow(hWndPopup);
  MSG msg;

  while(GetMessage(&msg, NULL, 0, 0))
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
  return msg.wParam;


LRESULT CALLBACK ParentWndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
  switch(message) 
  {
    case WM_DESTROY:
    PostQuitMessage(0);
    break;
    default:
    return DefWindowProc(hWnd,message,wParam,lParam);
  }
  return 0;
}

LRESULT CALLBACK ChildWndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
  return DefWindowProc(hWnd,message,wParam,lParam);
}

LRESULT CALLBACK PopupWndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
  return DefWindowProc(hWnd,message,wParam,lParam);
}