请教怎样在控制台程序中创建窗口
请问怎样在控制台程序中创建窗口
我想在一个控制台程序中创建一个窗口,程序能编译通过但是运行时总是崩溃,全部代码如下:
#include <windows.h>
const TCHAR szWindowClass[] = L"MyWindowClass";
int main(int argc, char* argv[])
{
HINSTANCE hInstance = ::GetModuleHandle(NULL);
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = NULL;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = NULL;
wcex.hCursor = NULL;
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = NULL;
RegisterClassEx(&wcex);
CreateWindow(szWindowClass, szWindowClass, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, NULL, NULL);
return 0;
}
请问那位大侠有解决方案?谢谢!
------解决方案--------------------
入口点是WinMain......
消息循环呢?
消息处理函数呢?
LZ你这基础.....
------解决方案--------------------
大哥,窗口函数入口点不是main,改成winmain,还有你的对话框消息函数没有加
------解决方案--------------------
------解决方案--------------------
可以,要修改设置,project->settings 的c/c++选项,在code generation 中的 use run-time library选 multithread dll
------解决方案--------------------
lpfnWndProc 要指定一个函数来处理消息循环 不可为NULL
------解决方案--------------------
#include <windows.h>
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static char szAppName[] = TEXT ("myclass");
HWND hwnd;
MSG msg;
//WNDCLASSEX wndclassex = {0};
WNDCLASSEX wndclassex;
wndclassex.cbSize = sizeof(WNDCLASSEX);
wndclassex.cbClsExtra = 0;
wndclassex.cbWndExtra = 0;
wndclassex.hInstance = hInstance;
wndclassex.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wndclassex.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclassex.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wndclassex.lpfnWndProc = WndProc;
wndclassex.lpszMenuName = NULL;
wndclassex.lpszClassName = szAppName;
wndclassex.hIconSm = wndclassex.hIcon;
wndclassex.style = CS_HREDRAW | CS_VREDRAW;
if (!RegisterClassEx (&wndclassex)) {
MessageBox (NULL, TEXT ("RegisterClassEx failed!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindowEx (WS_EX_OVERLAPPEDWINDOW, szAppName, TEXT ("WindowTitle"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); 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;
switch (message)
{
我想在一个控制台程序中创建一个窗口,程序能编译通过但是运行时总是崩溃,全部代码如下:
#include <windows.h>
const TCHAR szWindowClass[] = L"MyWindowClass";
int main(int argc, char* argv[])
{
HINSTANCE hInstance = ::GetModuleHandle(NULL);
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = NULL;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = NULL;
wcex.hCursor = NULL;
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = NULL;
RegisterClassEx(&wcex);
CreateWindow(szWindowClass, szWindowClass, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, NULL, NULL);
return 0;
}
请问那位大侠有解决方案?谢谢!
------解决方案--------------------
入口点是WinMain......
消息循环呢?
消息处理函数呢?
LZ你这基础.....
------解决方案--------------------
大哥,窗口函数入口点不是main,改成winmain,还有你的对话框消息函数没有加
------解决方案--------------------
------解决方案--------------------
可以,要修改设置,project->settings 的c/c++选项,在code generation 中的 use run-time library选 multithread dll
------解决方案--------------------
lpfnWndProc 要指定一个函数来处理消息循环 不可为NULL
------解决方案--------------------
#include <windows.h>
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static char szAppName[] = TEXT ("myclass");
HWND hwnd;
MSG msg;
//WNDCLASSEX wndclassex = {0};
WNDCLASSEX wndclassex;
wndclassex.cbSize = sizeof(WNDCLASSEX);
wndclassex.cbClsExtra = 0;
wndclassex.cbWndExtra = 0;
wndclassex.hInstance = hInstance;
wndclassex.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wndclassex.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclassex.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wndclassex.lpfnWndProc = WndProc;
wndclassex.lpszMenuName = NULL;
wndclassex.lpszClassName = szAppName;
wndclassex.hIconSm = wndclassex.hIcon;
wndclassex.style = CS_HREDRAW | CS_VREDRAW;
if (!RegisterClassEx (&wndclassex)) {
MessageBox (NULL, TEXT ("RegisterClassEx failed!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindowEx (WS_EX_OVERLAPPEDWINDOW, szAppName, TEXT ("WindowTitle"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); 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;
switch (message)
{