winmain中一使用这个函数MyRegisterClass提示无法重载

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了winmain中一使用这个函数MyRegisterClass提示无法重载相关的知识,希望对你有一定的参考价值。

错误 1 error LNK2019: 无法解析的外部符号 "int __cdecl MyRegisterClass(struct HINSTANCE__ *)" (?MyRegisterClass@@YAHPAUHINSTANCE__@@@Z),该符号在函数 _WinMain@16 中被引用

参考技术A 先定义了一个WNDCLASSEX 结构,然后指定一些窗口特性,

调用 RegisterClassEx返回。

实际上,要生成一个窗口很简单,这里给你一个我写的CWin类,
在winmain中加入以下语句即可。
CWin win;
win.Create(hInstance, "程序示例");

以下是CWin基本框架:

win.h
// Win.h: interface for the CWin class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_WIN_H__4782C3BD_80D9_4A13_869D_D80A4A7CAC0D__INCLUDED_)
#define AFX_WIN_H__4782C3BD_80D9_4A13_869D_D80A4A7CAC0D__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CWin

public:
void SetBkGnd(LPCTSTR BkGndFile);
void OnGetminmaxinfo(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
virtual int RealWinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
void OnDraw(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
CWin(int nWidth, int nHeight);
static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
bool Create(HINSTANCE hInstance, TCHAR* WndName);
CWin();
virtual ~CWin();

protected:
HBITMAP m_hBkGnd;
int m_nBkColor;
int m_nHeight;
int m_nWidth;
HWND m_hWnd;
;

#endif // !defined(AFX_WIN_H__4782C3BD_80D9_4A13_869D_D80A4A7CAC0D__INCLUDED_)

win.cpp

// Win.cpp: implementation of the CWin class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Win.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CWin::CWin()

m_hWnd = NULL;
m_nWidth = 640;
m_nHeight = 480;
m_nBkColor = 0xffffff; // white
m_hBkGnd = NULL;


CWin::CWin(int nWidth, int nHeight)

m_nWidth = nWidth>0 ? nWidth:640;
m_nHeight = nHeight>0 ? nHeight:480;


CWin::~CWin()

if(m_hBkGnd)

DeleteObject(m_hBkGnd);
m_hBkGnd = NULL;



bool CWin::Create(HINSTANCE hInstance, TCHAR *WndName)

WNDCLASSEX wcex;
int x, y, w, h;

wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL, (LPCTSTR)IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, (LPCTSTR)IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = WndName;
wcex.hIconSm = LoadIcon(NULL, (LPCTSTR)IDI_WINLOGO);

RegisterClassEx(&wcex);

m_hWnd = CreateWindow(WndName, WndName, WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if(!m_hWnd)
return NULL;

::SetWindowLong(m_hWnd,GWL_USERDATA,(LONG)this);//将this指针传给窗口

w = m_nWidth + 2*GetSystemMetrics(SM_CXFRAME);
h = m_nHeight +2*GetSystemMetrics(SM_CYFRAME) + GetSystemMetrics(SM_CYCAPTION);
x = GetSystemMetrics(SM_CXSCREEN);
y = GetSystemMetrics(SM_CYSCREEN);

x = (x - w) / 2;
y = (y - h) / 2;

SetWindowPos(m_hWnd, HWND_NOTOPMOST, x, y, w, h, SWP_HIDEWINDOW);
ShowWindow(m_hWnd, SW_NORMAL);
UpdateWindow(m_hWnd);

return true;


LRESULT CALLBACK CWin::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

CWin* pWin = NULL;
if(message!=WM_NCCREATE && message !=WM_CREATE)
pWin = (CWin*)::GetWindowLong(hWnd,GWL_USERDATA);
if(pWin)
return pWin->RealWinProc(hWnd, message, wParam, lParam);
return DefWindowProc(hWnd, message, wParam, lParam);


void CWin::OnDraw(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

HDC hDC, hMemDC;
HBITMAP hBitmap = NULL;
HBRUSH hBrush = NULL;
RECT rt = 0, 0, m_nWidth, m_nHeight;
HFONT hFont;
TCHAR szWait[20]="等待中...";

ValidateRect(hWnd, NULL);

hDC = GetDC(hWnd);
hMemDC = CreateCompatibleDC(hDC);
hBitmap = CreateCompatibleBitmap(hDC, m_nWidth, m_nHeight);
hBrush = CreateSolidBrush(m_nBkColor);

SelectObject(hMemDC, hBitmap);
SelectObject(hMemDC, hBrush);
FillRect(hMemDC, &rt, hBrush);
hFont = CreateFont(30, // nHeight
0, // nWidth
0, // nEscapement
0, // nOrientation
FW_NORMAL, // nWeight
true, // bItalic
FALSE, // bUnderline
0, // cStrikeOut
ANSI_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
DEFAULT_QUALITY, // nQuality
DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
"Arial");
SelectObject(hMemDC, hFont);

if(m_hBkGnd)
SelectObject(hMemDC, m_hBkGnd);
///////////////////////////////////////////////////////////////
SetBkColor(hMemDC, m_nBkColor);
SetTextColor(hMemDC, 0x000000);
SetBkMode(hMemDC, TRANSPARENT);
TextOut(hMemDC, 0, 0, szWait, strlen(szWait));

///////////////////////////////////////////////////////////////
BitBlt(hDC, 0, 0, m_nWidth, m_nHeight, hMemDC, 0, 0, SRCCOPY);

DeleteObject(hFont);
DeleteObject(hBrush);
DeleteObject(hBitmap);
ReleaseDC(hWnd, hMemDC);
ReleaseDC(hWnd, hDC);


int CWin::RealWinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

switch(message)

case WM_GETMINMAXINFO:
OnGetminmaxinfo(hWnd, message, wParam, lParam);
break;
case WM_PAINT:
OnDraw(hWnd, message, wParam, lParam);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);

return 0;


void CWin::OnGetminmaxinfo(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

LPMINMAXINFO lpmmi;
int width = m_nWidth, height = m_nHeight;

lpmmi = (LPMINMAXINFO)lParam;
width = width + 2*GetSystemMetrics(SM_CXFRAME);
height = height +2*GetSystemMetrics(SM_CYFRAME) + GetSystemMetrics(SM_CYCAPTION);
lpmmi->ptMaxTrackSize.x = lpmmi->ptMinTrackSize.x = width;
lpmmi->ptMaxTrackSize.y = lpmmi->ptMinTrackSize.y = height;


void CWin::SetBkGnd(LPCTSTR BkGndFile)

if(m_hBkGnd)
DeleteObject(m_hBkGnd);
if(BkGndFile)
m_hBkGnd = (HBITMAP)LoadImage((HINSTANCE)GetWindowLong(m_hWnd, GWL_HINSTANCE),
BkGndFile,
IMAGE_BITMAP,
0,
0,
LR_CREATEDIBSECTION | LR_LOADFROMFILE);
else
m_hBkGnd = NULL;

InvalidateRect(m_hWnd, NULL, false);
UpdateWindow(m_hWnd);
本回答被提问者采纳

主函数WinMain

WinMain或者wwinMain

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow);
  • hInstance 称为“实例句柄”或“模块句柄”。操作系统使用此值在内存中加载可执行文件时标识可执行文件 (EXE) 。 某些Windows函数需要实例句柄,例如加载图标或位图。
  • hPrevInstance 没有意义。 它在 16 位Windows中使用,但现在始终为零。
  • pCmdLine 包含命令行参数作为 Unicode 字符串。
  • nCmdShow 是一个标志,指示主应用程序窗口是最小化、最大化还是正常显示。

以上是关于winmain中一使用这个函数MyRegisterClass提示无法重载的主要内容,如果未能解决你的问题,请参考以下文章

主函数WinMain

关于VC++的Winmain函数(WINAPI是啥?)

c++主函数如何调用"WinMain"函数

关于VC++的Winmain函数(WINAPI是啥?)

(转)InitInstance函数的理解

MFC6.0建立一个文档视图框架时,怎么找不到入口WinMain()函数