C语言 Win32应用程序关闭后仍在后台运行



我是一个初级程序员。

在这段代码中,我想使用C语言在Win32 API中创建一个颜色的窗口。我正在使用Windows PowerShell和MinGW来编译它。它的工作,但每当我试图再次编译它关闭后,我得到这个错误:…/mingw32/bin/ld.exe:不能打开输出文件out.exe:权限被拒绝collect2.exe:错误:ld返回1退出状态

就我而言,这是一个链接问题,它发生是因为Windows不能与已经在执行的程序进行链接。我不希望一直在任务管理器中终止执行。

我不认为代码超出了微软的文档告诉我要做的,我只是把它包装在函数和结构体中,而不是在WinMain中编写所有内容,应用程序循环可以在那里找到。我在WM_CREATE中创建了一个RGB数组,并将其传递给WM_PAINT中的HBITMAP,以防你想知道我做了什么,这就是为什么重复的高度和宽度,我假装稍后改变。下面是代码:

#include<windows.h>
#include<stdlib.h>
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Window Procedure
HBITMAP bitmap_handle;
COLORREF *rgb_p=NULL;
LRESULT CALLBACK window_procedure_func(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_CREATE:
{
int width=500;
int height=500;
rgb_p=(COLORREF*)malloc(width*height*sizeof(COLORREF));
int  pixel_count=0;
for(int row=0; row<height; row++)
{
for(int col=0; col<width; col++)
{
rgb_p[pixel_count]=RGB(255, 0, 0);
pixel_count++;
}
}
}
break;
case WM_PAINT:
{
int width=500;
int height=500;
HBITMAP bitmap_handle=CreateBitmap(width, height, 1, 32, rgb_p);

PAINTSTRUCT     ps;
HDC             hdc;
BITMAP          bitmap;
HDC             hdcMem;
HGDIOBJ         oldBitmap;
hdc = BeginPaint(hWnd, &ps);
hdcMem = CreateCompatibleDC(hdc);
oldBitmap = SelectObject(hdcMem, bitmap_handle);
GetObject(bitmap_handle, sizeof(bitmap), &bitmap);
BitBlt(hdc, 0, 0, bitmap.bmWidth, bitmap.bmHeight, hdcMem, 0, 0, SRCCOPY);
SelectObject(hdcMem, oldBitmap);
DeleteDC(hdcMem);
EndPaint(hWnd, &ps);
}
break;
case WM_CLOSE:
{
DestroyWindow(hWnd);
}
break;
case WM_DESTROY:
{
free(rgb_p);
DeleteObject(bitmap_handle);
PostQuitMessage(0);
}
break;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Simple functions to initalize a window
struct window_wrapper
{
WNDCLASS window_class;
HWND window_handle;
};
WNDCLASS window_class_ini(char *window_name, HINSTANCE instance_handle, WNDPROC window_procedure)
{
WNDCLASS window_class={};
window_class.lpfnWndProc=window_procedure;
window_class.hInstance=instance_handle;
window_class.lpszClassName=window_name;
return window_class;
}
HWND window_handle_ini(char *window_name, HINSTANCE instance_handle, int width, int height)
{
return CreateWindow(
window_name,
window_name,
WS_OVERLAPPEDWINDOW,
250, 250, width, height,
NULL,  
NULL,
instance_handle,
NULL);
}
struct window_wrapper *malloc_window_wrapper(char *window_name, HINSTANCE instance_handle, WNDPROC window_procedure, int windows_status, int width, int height)
{
struct window_wrapper *window_wrapper_p=NULL;
window_wrapper_p=(struct window_wrapper*)malloc(sizeof(struct window_wrapper));
window_wrapper_p->window_class=window_class_ini(window_name, instance_handle, window_procedure);
RegisterClass(&window_wrapper_p->window_class);
window_wrapper_p->window_handle=window_handle_ini(window_name, instance_handle, width, height);
ShowWindow(window_wrapper_p->window_handle, windows_status);
return window_wrapper_p;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Windows Entry Point
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
LRESULT CALLBACK (*procedure_p)(HWND, UINT, WPARAM, LPARAM)=&window_procedure_func;
struct window_wrapper *wrapper=malloc_window_wrapper("window_name", hInstance, (WNDPROC)procedure_p, nCmdShow, 500, 500);
MSG msg={};
int running=1;
while(running)
{
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if(msg.message==WM_QUIT)
{
running=0;
}
}
free(wrapper);
return 0;
}

我也尝试使用exit()而不是在结束时返回0,但没有成功。感谢大家的回复。

您的消息循环结构不正确。只有当PeekMessage()返回false时,才到达if语句,这意味着msg的内容不确定。因此,if将永远不会看到有效的WM_QUIT消息(或任何有效消息)来打破循环。

去掉running变量,用这个循环代替:

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDPROC procedure_p = &window_procedure_func;
struct window_wrapper *wrapper = malloc_window_wrapper("window_name", hInstance, procedure_p, nCmdShow, 500, 500);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
free(wrapper);
return 0;
}

最新更新