萌新请教一个VC++的窗口程序问题
大大们,请问我这个程序为什么每次运行只有进程而没有窗口呀,RegisterWindow()里面的lpszClassName和Create()里面的lpszClassName是一样的,而且我也试过把nCmdShow设为SW_SHOW,都没用,求助,{:9_241:} 谢谢 么么哒#include<windows.h>
//=============================
//HINSTANCE hInst;
HINSTANCE hInstance;
char lpszClassName[] = "classclassdf";
char *ShowText;
MSG msg;
//=============================
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void On_LButtonDown(HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam);
void On_Paint(HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam);
void On_Destroy(HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam);
//=============================
class CFrameWnd{
public:
HWND hWnd;
public:
int RegisterWindow();
void Create(LPCTSTR lpClassName, LPCTSTR lpWindowName);
void ShowWindow(int nCmdShow);
void UpdateWindow();
};
int CFrameWnd::RegisterWindow(){
WNDCLASS wc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.style = 0;
wc.hInstance = hInstance;
wc.lpszClassName = lpszClassName;
wc.lpszMenuName = NULL;
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.lpfnWndProc = WndProc;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
return RegisterClass(&wc);
}
void CFrameWnd::Create(LPCTSTR lpClassName, LPCTSTR lpWindowName){
RegisterWindow();
// hInst = hInstance;
hWnd = CreateWindow(lpszClassName,
lpWindowName,
WS_OVERLAPPEDWINDOW,
0,0,800,600,
NULL,
NULL,
hInstance,
NULL);
}
void CFrameWnd::ShowWindow(int nCmdWnd){
::ShowWindow(hWnd,nCmdWnd);
}
void CFrameWnd::UpdateWindow(){
::UpdateWindow(hWnd);
}
//=============================================================
class CWinApp{
public:
CWinApp *m_pCurrentWinApp;
public:
CWinApp();
~CWinApp();
public:
CFrameWnd *m_pMainWnd;
public:
virtual BOOL InitInstance(int nCmdShow);
int Run();
};
CWinApp::CWinApp(){
m_pCurrentWinApp = this;
}
BOOL CWinApp::InitInstance(int nCmdShow){
m_pMainWnd = new CFrameWnd;
m_pMainWnd->Create(NULL,"封装的Windows程序");
m_pMainWnd->ShowWindow(nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}
int CWinApp::Run(){
while( GetMessage(&msg, NULL, NULL, NULL) ){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
CWinApp::~CWinApp(){
delete m_pMainWnd;
}
//==================================================
class CMyWnd:public CFrameWnd{
};
//==================================================
class CMyApp:public CWinApp{
public:
BOOL InitInstance(int nCmdShow);
};
//==================================================
BOOL CMyApp::InitInstance(int nCmdShow){
CMyWnd * pMainWnd;
pMainWnd = new CMyWnd;
pMainWnd->Create(NULL,"应用窗体的派生类的程序");
pMainWnd->ShowWindow(nCmdShow);
pMainWnd->UpdateWindow();
m_pMainWnd = pMainWnd;
return TRUE;
}
//==================================================
CWinApp MyApp;
//==================================================
CWinApp *AfxGetApp(){
return MyApp.m_pCurrentWinApp;
}
//==================================================
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
int ResultCode = -1;
CWinApp *pApp;
pApp = AfxGetApp();
pApp->InitInstance(SW_SHOW);
return ResultCode = pApp->Run();
}
//==================================================
LRESULT CALLBACK WndProc(HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
switch(message){
case WM_LBUTTONDOWN:
On_LButtonDown(hWnd,message,wParam,lParam);
break;
case WM_PAINT:
On_Paint(hWnd,message,wParam,lParam);
break;
case WM_DESTROY:
On_Destroy(hWnd,message,wParam,lParam);
default:
DefWindowProc(hWnd,message,wParam,lParam);
}
return 0;
}
void On_LButtonDown(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
ShowText = "Hello!";
InvalidateRect(hWnd,NULL,1);
}
void On_Paint(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
PAINTSTRUCT ps;
HDC hdc;
hdc = BeginPaint(hWnd, &ps);
TextOut(hdc, 50, 50, ShowText, 6);
EndPaint(hWnd,&ps);
}
void On_Destroy(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
PostQuitMessage(0);
} 虽然还没学到VC++写的窗口程序 ,但是也看了下小甲鱼老师的SDK视频,我大概猜想下 ,你能不能把 WINMAIN头放在程序前面,把处理程序过程放在后面,在运行下程序看行不,如果不行那就要好好检查程序内容了如果成功希望奖励下鱼币{:5_109:} 谦虚求学 发表于 2017-1-4 10:16
虽然还没学到VC++写的窗口程序 ,但是也看了下小甲鱼老师的SDK视频,我大概猜想下 ,你能不能把 WINMAIN头 ...
感谢回复,不过好像不是WINAPI ENTRY WINMAIN的位置问题呢。我再试试,谢谢啦 int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
hInstance//这个是系统传进去的,相当于系统分配的
//而你
wc.hInstance = hInstance;//和
hWnd = CreateWindow(lpszClassName,
lpWindowName,
WS_OVERLAPPEDWINDOW,
0,0,800,600,
NULL,
NULL,
hInstance,
NULL);
//都是你的全局变量,HINSTANCE hInstance;,并没有分配实际的句柄
msdn的例子
#include <windows.h>
// Global variable
HINSTANCE hinst;
// Function prototypes.
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
InitApplication(HINSTANCE);
InitInstance(HINSTANCE, int);
LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
// Application entry point.
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
if (!InitApplication(hinstance))
return FALSE;
if (!InitInstance(hinstance, nCmdShow))
return FALSE;
BOOL fGotMessage;
while ((fGotMessage = GetMessage(&msg, (HWND) NULL, 0, 0)) != 0 && fGotMessage != -1)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
UNREFERENCED_PARAMETER(lpCmdLine);
}
BOOL InitApplication(HINSTANCE hinstance)
{
WNDCLASSEX wcx;
// Fill in the window class structure with parameters
// that describe the main window.
wcx.cbSize = sizeof(wcx); // size of structure
wcx.style = CS_HREDRAW |
CS_VREDRAW; // redraw if size changes
wcx.lpfnWndProc = MainWndProc; // points to window procedure
wcx.cbClsExtra = 0; // no extra class memory
wcx.cbWndExtra = 0; // no extra window memory
wcx.hInstance = hinstance; // handle to instance
wcx.hIcon = LoadIcon(NULL,
IDI_APPLICATION); // predefined app. icon
wcx.hCursor = LoadCursor(NULL,
IDC_ARROW); // predefined arrow
wcx.hbrBackground = GetStockObject(
WHITE_BRUSH); // white background brush
wcx.lpszMenuName ="MainMenu"; // name of menu resource
wcx.lpszClassName = "MainWClass";// name of window class
wcx.hIconSm = LoadImage(hinstance, // small class icon
MAKEINTRESOURCE(5),
IMAGE_ICON,
GetSystemMetrics(SM_CXSMICON),
GetSystemMetrics(SM_CYSMICON),
LR_DEFAULTCOLOR);
// Register the window class.
return RegisterClassEx(&wcx);
}
BOOL InitInstance(HINSTANCE hinstance, int nCmdShow)
{
HWND hwnd;
// Save the application-instance handle.
hinst = hinstance;
// Create the main window.
hwnd = CreateWindow(
"MainWClass", // name of window class
"Sample", // title-bar string
WS_OVERLAPPEDWINDOW, // top-level window
CW_USEDEFAULT, // default horizontal position
CW_USEDEFAULT, // default vertical position
CW_USEDEFAULT, // default width
CW_USEDEFAULT, // default height
(HWND) NULL, // no owner window
(HMENU) NULL, // use class menu
hinstance, // handle to application instance
(LPVOID) NULL); // no window-creation data
if (!hwnd)
return FALSE;
// Show the window and send a WM_PAINT message to the window
// procedure.
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
return TRUE;
}
识海君 发表于 2017-1-9 12:34
感谢回复,不过好像不是WINAPI ENTRY WINMAIN的位置问题呢。我再试试,谢谢啦
{:9_237:}
页:
[1]