怎样用window api实现程序全屏显示

2025-04-17 03:46:00
推荐回答(3个)
回答1:

给你完整的代码吧。这应该是你想要的。

//请用alt+f4关闭该窗口,以下代码可运行于vc6至vc9(vs2008)版本
#include

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("HelloWin") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;

wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground= (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName= szAppName ;

if (!RegisterClass (&wndclass))
{
MessageBox ( NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}

hwnd = CreateWindow( szAppName, // window class name
TEXT ("Full Screen Display"), // window caption
// WS_OVERLAPPEDWINDOW, // window style
WS_POPUP,
// CW_USEDEFAULT,// initial x position
// CW_USEDEFAULT,// initial y position
// CW_USEDEFAULT,// initial x size
// CW_USEDEFAULT,// initial y size
0,
0,
::GetSystemMetrics(SM_CXSCREEN),
::GetSystemMetrics(SM_CYSCREEN),
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters

ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;

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

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;

switch (message)
{
case WM_CREATE:
// PlaySound (TEXT ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC) ;
return 0 ;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
DrawText (hdc, TEXT ("This window can cover the task item!"), -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}

回答2:

这种效果实际上应该是窗口处于非最大化状态


ShowWindow(hWnd, SW_SHOWNORMAL);

GetSystemMetrics(SM_CXSCREEN);
GetSystemMetrics(SM_CYSCREEN);
获取屏幕大小

然后调整窗口大小和位置

使GetClientRect(hWnd, lpRect);
客户区左上角位于屏幕(0,0)的位置
客户区宽度为GetSystemMetrics(SM_CXSCREEN)
客户区高度为GetSystemMetrics(SM_CYSCREEN)

通过 ClientToScreen(hWnd,lpPoint);
计算调整窗口大小和位置

就可实现楼主要求的效果

API代码:

int x = GetSystemMetrics(SM_CXSCREEN);
int y = GetSystemMetrics(SM_CYSCREEN);
int dx, dy;
ShowWindow(hwnd, SW_SHOWNORMAL);
RECT rect;
RECT wrect;
POINT point;
::GetClientRect(hwnd, &rect);
::GetWindowRect(hwnd, &wrect);

point.x = 0;
point.y = 0;
::ClientToScreen(hwnd, &point);
wrect.right += x - rect.right;
wrect.bottom += y - rect.bottom;
::MoveWindow(hwnd, wrect.left - point.x, wrect.top - point.y, wrect.right, wrect.bottom, true);

这样垂直方向已到最大值无法再大
只有再处理 WM_GETMINMAXINFO 消息了

回答3:

int cx = GetSystemMetrics( SM_CXSCREEN);
int cy = GetSystemMetrics( SM_CYSCREEN);
MoveWindow( 0,0,cx,cy);

不用MFC的话,CreateWindow的时候,相应修改窗口的属性。看看MSDN说明。
如果是对话框,SetWindowLong一样可以修改。