萬盛學電腦網

 萬盛學電腦網 >> 電腦基本常識 >> Win32api設置窗口全屏的方法

Win32api設置窗口全屏的方法

   首先是考慮全屏處理的時機,是在創建窗口時還是顯示窗口時進行,若是前者,則可以:

  BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)

  {

  HWND hWnd;

  hInst = hInstance; // 將實例句柄存儲在全局變量中

  UINT width = GetSystemMetrics(SM_CXSCREEN);

  UINT height = GetSystemMetrics(SM_CYSCREEN);

  //創建窗口

  hWnd=CreateWindow(

  szWindowClass,

  szTitle,

  WS_POPUP,

  0,0,

  width,height,

  NULL,NULL,

  hInstance,

  NULL);

  if (!hWnd)

  {

  return FALSE;

  }

  ShowWindow(hWnd, nCmdShow);

  UpdateWindow(hWnd);

  return TRUE;

  }

  若是在顯示窗口時進行處理:

  BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)

  {

  HWND hWnd;

  hInst = hInstance; // 將實例句柄存儲在全局變量中

  hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,

  CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

  if (!hWnd)

  {

  return FALSE;

  }

  HWND hDesk;

  RECT rc;

  hDesk = GetDesktopWindow();

  GetWindowRect( hDesk, &rc );

  SetWindowLong( hWnd, GWL_STYLE, WS_BORDER );

  SetWindowPos( hWnd, HWND_TOPMOST, 0, 0, rc.right, rc.bottom, SWP_SHOWWINDOW);

  ShowWindow(hWnd, nCmdShow);

  UpdateWindow(hWnd);

  return TRUE;

  }

  也可以讓用戶控制全屏的時機,比如按下”ESC“鍵後進入全屏

  BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)

  {

  HWND hWnd;

  hInst = hInstance; // 將實例句柄存儲在全局變量中

  hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,

  CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

  if (!hWnd)

  {

  return FALSE;

  }

  ShowWindow(hWnd, nCmdShow);

  UpdateWindow(hWnd)

  return TRUE;

  }

  在窗口處理函數中對ESC鍵進行處理:

  switch (message)

  {

  case WM_KEYDOWN:

  switch(wParam)

  {

  case VK_ESCAPE:

  {

  HWND hDesk;

  RECT rc;

  hDesk = GetDesktopWindow();

  GetWindowRect( hDesk, &rc );

  SetWindowLong( hWnd, GWL_STYLE, WS_BORDER );

  SetWindowPos( hWnd, HWND_TOPMOST, 0, 0, rc.right, rc.bottom, SWP_SHOWWINDOW);

  }

  break;

  }

  return 0;

  }

copyright © 萬盛學電腦網 all rights reserved