鱼C论坛

 找回密码
 立即注册
查看: 1081|回复: 2

[已解决]关于C语言窗口隐藏的问题

[复制链接]
发表于 2024-7-4 23:13:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 --_--|| 于 2024-7-4 23:13 编辑

本人想做一个便携工具,目标是无参数运行时进入配置界面,有参数时进入运行参数命令。
想做到在配置界面时窗口正常显示,而带参运行时窗口隐藏
本来用#pragma comment(linker,"/subsystem:\"Windows\" /entry:\"mainCRTStartup\"")宏定义可以完美隐藏,但不知道怎么再把窗口调出来
而用ShowWindow()可以做到自由隐藏和显示,但开始会出现cmd窗口闪一下才隐藏
今天特地来问问各位鱼兄,有没有好办法可以实现这个效果,感谢!!!


  1. /***********************
  2. This Is An Example
  3. ************************/
  4. #include <iostream>
  5. #include <Windows.h>
  6. //#pragma comment(linker,"/subsystem:"Windows" /entry:"mainCRTStartup"")//宏定义隐藏窗口
  7. using namespace std;
  8. void HideWindow();//隐藏窗口
  9. void ShowWindow();//显示窗口
  10. int config();//配置界面
  11. int main(int argc,char *argv[])
  12. {
  13.     HideWindow();
  14.     if (argc==1)//无参数运行,进入配置界面
  15.     {
  16.         ShowWindow();
  17.         config();
  18.     }
  19.     system(argv[1]);//带参运行,运行参数
  20.     return 0;
  21. }

  22. void HideWindow()
  23. {
  24.     HWND hwnd = GetForegroundWindow();
  25.     ShowWindow(hwnd,SW_HIDE);
  26. }

  27. void ShowWindow()
  28. {
  29.     HWND hwnd = GetForegroundWindow();
  30.     ShowWindow(hwnd,SW_NORMAL);
  31. }

  32. int config()
  33. {
  34.     printf("This is Config\n");
  35.     return 0;
  36. }
复制代码

最佳答案
2024-7-5 00:14:47
  1. #include <windows.h>
  2. #include <stdlib.h>

  3. static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
  4.     HDC hdc;
  5.     PAINTSTRUCT ps;
  6.     RECT rect;
  7.     switch(message) {
  8.         case WM_PAINT:
  9.             hdc = BeginPaint(hwnd, &ps);
  10.             GetClientRect(hwnd, &rect);
  11.             DrawText(hdc, TEXT("大家好,这是我的第一个窗口程序!"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  12.             EndPaint(hwnd, &ps);
  13.             return 0;
  14.         case WM_DESTROY:
  15.             PostQuitMessage(0);
  16.             return 0;
  17.     }
  18.     return DefWindowProc(hwnd, message, wParam, lParam);
  19. }

  20. static int config(void) {
  21.     AllocConsole();
  22.     system(__argv[1]);
  23.     system("pause");
  24.     FreeConsole();
  25.     return 0;
  26. }

  27. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {


  28.     if(__argc != 1) return config();
  29.    
  30.    
  31.     static const TCHAR szAppName[] = TEXT("MyWindows");
  32.     HWND hwnd;
  33.     MSG msg;
  34.     WNDCLASS wndclass;

  35.     wndclass.style = CS_HREDRAW | CS_VREDRAW;
  36.     wndclass.lpfnWndProc = WndProc;
  37.     wndclass.cbClsExtra = 0;
  38.     wndclass.cbWndExtra = 0;
  39.     wndclass.hInstance = hInstance;
  40.     wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  41.     wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  42.     wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  43.     wndclass.lpszMenuName = NULL;
  44.     wndclass.lpszClassName = szAppName;

  45.     if(!RegisterClass(&wndclass)) {
  46.         MessageBox(NULL, TEXT("这个程序需要在 Windows NT 才能执行!"), szAppName, MB_ICONERROR);
  47.         return 0;
  48.     }

  49.     hwnd = CreateWindow(szAppName, TEXT("鱼C工作室"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
  50.         CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
  51.     ShowWindow(hwnd, iCmdShow);
  52.     UpdateWindow(hwnd);

  53.     while(GetMessage(&msg, NULL, 0, 0)) {
  54.         TranslateMessage(&msg);
  55.         DispatchMessage(&msg);
  56.     }
  57.     return msg.wParam;
  58. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-7-5 00:14:47 | 显示全部楼层    本楼为最佳答案   
  1. #include <windows.h>
  2. #include <stdlib.h>

  3. static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
  4.     HDC hdc;
  5.     PAINTSTRUCT ps;
  6.     RECT rect;
  7.     switch(message) {
  8.         case WM_PAINT:
  9.             hdc = BeginPaint(hwnd, &ps);
  10.             GetClientRect(hwnd, &rect);
  11.             DrawText(hdc, TEXT("大家好,这是我的第一个窗口程序!"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  12.             EndPaint(hwnd, &ps);
  13.             return 0;
  14.         case WM_DESTROY:
  15.             PostQuitMessage(0);
  16.             return 0;
  17.     }
  18.     return DefWindowProc(hwnd, message, wParam, lParam);
  19. }

  20. static int config(void) {
  21.     AllocConsole();
  22.     system(__argv[1]);
  23.     system("pause");
  24.     FreeConsole();
  25.     return 0;
  26. }

  27. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {


  28.     if(__argc != 1) return config();
  29.    
  30.    
  31.     static const TCHAR szAppName[] = TEXT("MyWindows");
  32.     HWND hwnd;
  33.     MSG msg;
  34.     WNDCLASS wndclass;

  35.     wndclass.style = CS_HREDRAW | CS_VREDRAW;
  36.     wndclass.lpfnWndProc = WndProc;
  37.     wndclass.cbClsExtra = 0;
  38.     wndclass.cbWndExtra = 0;
  39.     wndclass.hInstance = hInstance;
  40.     wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  41.     wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  42.     wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  43.     wndclass.lpszMenuName = NULL;
  44.     wndclass.lpszClassName = szAppName;

  45.     if(!RegisterClass(&wndclass)) {
  46.         MessageBox(NULL, TEXT("这个程序需要在 Windows NT 才能执行!"), szAppName, MB_ICONERROR);
  47.         return 0;
  48.     }

  49.     hwnd = CreateWindow(szAppName, TEXT("鱼C工作室"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
  50.         CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
  51.     ShowWindow(hwnd, iCmdShow);
  52.     UpdateWindow(hwnd);

  53.     while(GetMessage(&msg, NULL, 0, 0)) {
  54.         TranslateMessage(&msg);
  55.         DispatchMessage(&msg);
  56.     }
  57.     return msg.wParam;
  58. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-7-7 22:11:42 | 显示全部楼层
嗯,好的,谢谢大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-10 09:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表